1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
|
import os
import re
import sys
import unittest
from unittest import mock
import pyglet
from pyglet.resource import get_script_home, get_settings_path
_executable = os.path.abspath(os.path.join('path', 'exec'))
_script_home = os.path.abspath('path')
def _mock_expand_user(p):
parts = re.split(r"[\\/]+", p)
parts[0] = 'pyglet'
return os.path.join(*parts)
class ResourcePathTest(unittest.TestCase):
"""
Test default paths returned for different platforms.
"""
@mock.patch.object(sys, 'frozen', 'console_exe', create=True)
@mock.patch.object(sys, 'executable', _executable)
def test_script_home_frozen_console_exe(self):
"""Py2exe console executable"""
self.assertEqual(get_script_home(), _script_home)
@mock.patch.object(sys, 'frozen', 'windows_exe', create=True)
@mock.patch.object(sys, 'executable', _executable)
def test_script_home_frozen_windows_exe(self):
"""Py2exe windows executable"""
self.assertEqual(get_script_home(), _script_home)
@mock.patch.object(sys, 'frozen', 'macosx_app', create=True)
@mock.patch.dict(os.environ, {'RESOURCEPATH': _script_home})
def test_script_home_frozen_macosx(self):
"""Py2App OSX bundle"""
self.assertEqual(get_script_home(), _script_home)
@mock.patch.object(sys.modules['__main__'], '__file__', _executable)
def test_script_home_normal_python(self):
"""Normal execution of a script in python."""
self.assertEqual(get_script_home(), _script_home)
@mock.patch.dict('sys.modules', {'__main__': None})
@mock.patch.object(sys, 'executable', _executable)
def test_script_home_cx_freeze(self):
"""Frozen binary created with cx_freeze"""
self.assertEqual(get_script_home(), _script_home)
@mock.patch('os.getcwd', return_value=_script_home)
@mock.patch.dict('sys.modules', {'__main__': None})
@mock.patch.object(sys, 'executable', 'python')
def test_script_home_interactive(self, *_):
"""Interactive prompt, eg idle or cpython"""
self.assertEqual(get_script_home(), _script_home)
@mock.patch.object(pyglet, 'compat_platform', 'cygwin')
@mock.patch.dict('os.environ', {'APPDATA': 'pyglet'})
def test_settings_path_cygwin_appdata(self):
"""Settings path on cygwin with APPDATA set."""
self.assertEqual(get_settings_path('myapp'),
os.path.join('pyglet', 'myapp'))
@mock.patch.object(pyglet, 'compat_platform', 'win32')
@mock.patch.dict('os.environ', {'APPDATA': 'pyglet'})
def test_settings_path_windows_appdata(self):
"""Settings path on cygwin with APPDATA set."""
self.assertEqual(get_settings_path('myapp'),
os.path.join('pyglet', 'myapp'))
@mock.patch.object(pyglet, 'compat_platform', 'cygwin')
@mock.patch.object(os, 'environ', {})
@mock.patch.object(os.path, 'expanduser', _mock_expand_user)
def test_settings_path_cygwin_no_appdata(self):
"""Settings path on cygwin without APPDATA set."""
self.assertEqual(get_settings_path('myapp'),
os.path.join('pyglet', 'myapp'))
@mock.patch.object(pyglet, 'compat_platform', 'win32')
@mock.patch.object(os, 'environ', {})
@mock.patch.object(os.path, 'expanduser', _mock_expand_user)
def test_settings_path_windows_no_appdata(self):
"""Settings path on cygwin without APPDATA set."""
self.assertEqual(get_settings_path('myapp'),
os.path.join('pyglet', 'myapp'))
@mock.patch.object(pyglet, 'compat_platform', 'darwin')
@mock.patch.object(os.path, 'expanduser', _mock_expand_user)
def test_settings_path_darwin(self):
"""Settings path on OSX."""
self.assertEqual(get_settings_path('myapp'),
os.path.join('pyglet', 'Library', 'Application Support', 'myapp'))
@mock.patch.object(pyglet, 'compat_platform', 'linux')
@mock.patch.dict('os.environ', {'XDG_CONFIG_HOME': 'pyglet'})
def test_settings_path_linux_xdg_config_home(self):
"""Settings path on Linux with XDG_CONFIG_HOME available."""
self.assertEqual(get_settings_path('myapp'),
os.path.join('pyglet', 'myapp'))
@mock.patch.object(pyglet, 'compat_platform', 'linux')
@mock.patch.object(os, 'environ', {})
@mock.patch.object(os.path, 'expanduser', _mock_expand_user)
def test_settings_path_linux_xdg_config_home(self):
"""Settings path on Linux without XDG_CONFIG_HOME."""
self.assertEqual(get_settings_path('myapp'),
os.path.join('pyglet', '.config', 'myapp'))
|