Debian Codegraph @

tests/unit/test_resource_path.py

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