Skip to content

Commit d4ab3fc

Browse files
author
deathaxe
authored
Refactor OverridePreferencesTestCase (#256)
* Fix test case module name * Refactor OverridePreferencesTestCase
1 parent 6a5dbc5 commit d4ab3fc

File tree

3 files changed

+80
-23
lines changed

3 files changed

+80
-23
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import os
2+
import sublime
3+
4+
from unittesting import OverridePreferencesTestCase
5+
6+
7+
class PreferencesTestCase(OverridePreferencesTestCase):
8+
SETTINGS_FILE = "_UnitTestingTest.sublime-settings"
9+
SETTINGS_PATH = os.path.join(sublime.packages_path(), "User", SETTINGS_FILE)
10+
11+
override_preferences = {
12+
SETTINGS_FILE: {
13+
"enable": True,
14+
"list": ["one", "two", "three"]
15+
}
16+
}
17+
18+
@classmethod
19+
def setUpClass(cls):
20+
settings = sublime.load_settings(cls.SETTINGS_FILE)
21+
settings.set("enable", False)
22+
settings.set("list", ["foo"])
23+
sublime.save_settings(cls.SETTINGS_FILE)
24+
yield 500 # give ST some time to load initial settings file
25+
yield from super().setUpClass()
26+
27+
@classmethod
28+
def tearDownClass(cls):
29+
yield from super().tearDownClass()
30+
os.remove(cls.SETTINGS_PATH)
31+
32+
def test_backup_exists(self):
33+
self.assertTrue(os.path.isfile(self.SETTINGS_PATH + ".bak"))
34+
35+
def test_override_exists(self):
36+
self.assertTrue(os.path.isfile(self.SETTINGS_PATH))
37+
38+
def test_enabled(self):
39+
settings = sublime.load_settings(self.SETTINGS_FILE)
40+
self.assertTrue(settings.get("enable"))
41+
42+
def test_list_setting(self):
43+
settings = sublime.load_settings(self.SETTINGS_FILE)
44+
self.assertEqual(
45+
self.override_preferences[self.SETTINGS_FILE]["list"],
46+
settings.get("list")
47+
)

unittesting/helpers/__init__.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# noqa: F401
2-
from .override_preferences_test_cast import OverridePreferencesTestCase
2+
from .override_preferences_test_case import OverridePreferencesTestCase
33
from .temp_directory_test_case import TempDirectoryTestCase
44
from .view_test_case import DeferrableViewTestCase
55
from .view_test_case import ViewTestCase
6+
7+
__all__ = [
8+
"DeferrableViewTestCase",
9+
"OverridePreferencesTestCase",
10+
"TempDirectoryTestCase",
11+
"ViewTestCase",
12+
]

unittesting/helpers/override_preferences_test_cast.py unittesting/helpers/override_preferences_test_case.py

+25-22
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
import os
2-
import json
3-
import shutil
42
import sublime
53

64
from ..core import DeferrableMethod
@@ -17,38 +15,43 @@ def setUpClass(cls):
1715
new_settings_path = settings_path + ".bak"
1816

1917
s = sublime.load_settings(settings_file_name)
20-
on_change_key = settings_file_name + "catch_on_change"
18+
on_change_key = settings_file_name + ".catch_on_change"
2119
caught = [False]
2220
s.add_on_change(on_change_key, lambda: caught.__setitem__(0, True))
23-
24-
if os.path.exists(settings_path):
25-
shutil.move(settings_path, new_settings_path)
21+
try:
22+
try:
23+
os.remove(new_settings_path)
24+
except FileNotFoundError:
25+
pass
26+
try:
27+
os.rename(settings_path, new_settings_path)
28+
except FileNotFoundError:
29+
pass
2630
yield lambda: caught[0]
27-
yield 500
28-
29-
caught = [False]
30-
with open(settings_path, "w") as f:
31-
f.write(json.dumps(settings))
32-
yield lambda: caught[0]
33-
yield 500
34-
s.clear_on_change(on_change_key)
31+
caught = [False]
32+
with open(settings_path, "w", encoding="utf-8") as f:
33+
f.write(sublime.encode_value(settings, True))
34+
yield lambda: caught[0]
35+
finally:
36+
s.clear_on_change(on_change_key)
3537

3638
deferred = super().setUpClass()
3739
if isinstance(deferred, DeferrableMethod):
3840
yield from deferred
3941

4042
@classmethod
4143
def tearDownClass(cls):
42-
for settings_file_name, settings in cls.override_preferences.items():
44+
for settings_file_name in cls.override_preferences:
4345
settings_path = os.path.join(sublime.packages_path(), "User", settings_file_name)
4446
new_settings_path = settings_path + ".bak"
45-
if os.path.exists(new_settings_path):
46-
shutil.move(new_settings_path, settings_path)
47-
else:
48-
try:
49-
os.unlink(settings_path)
50-
except Exception:
51-
pass
47+
try:
48+
os.remove(settings_path)
49+
except FileNotFoundError:
50+
pass
51+
try:
52+
os.rename(new_settings_path, settings_path)
53+
except FileNotFoundError:
54+
pass
5255

5356
deferred = super().tearDownClass()
5457
if isinstance(deferred, DeferrableMethod):

0 commit comments

Comments
 (0)