Skip to content

Commit b9d7ae6

Browse files
authored
Simplify package structure (#263)
* Tweak python 3.3 plugin main module creation This commit moves code snippet for python 3.3 plugin.py into plugin_loaded() to keep it as local as possible. It doesn't need to live forever globally. * Drop utils package This commit... 1. moves StdioSplitter and OutputPanel to unittesting.base module 2. moves reloader module one level up 3. drops obsolete/empty utils package to simplify overall package structure.
1 parent f99cf2d commit b9d7ae6

7 files changed

Lines changed: 104 additions & 108 deletions

File tree

plugin.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
from .unittesting.syntax import UnitTestingSyntaxCompatibilityCommand
2121
from .unittesting.unit import UnitTestingCommand
2222

23-
23+
# publish plugin interface
2424
__all__ = [
2525
"UnitTestingCommand",
2626
"UnitTestingSyntaxCommand",
@@ -33,19 +33,22 @@
3333
# publish unittesting module
3434
sys.modules["unittesting"] = sys.modules["UnitTesting"].unittesting
3535

36-
UT33_CODE = """
37-
from UnitTesting import plugin as ut38
38-
39-
40-
class UnitTesting33Command(ut38.UnitTestingCommand):
41-
\"\"\"Execute unit tests for python 3.3 plugins.\"\"\"
42-
pass
43-
"""
44-
4536

4637
def plugin_loaded():
4738
if sys.version_info >= (3, 8):
4839
import json
40+
from textwrap import dedent
41+
42+
UT33_CODE = dedent(
43+
"""
44+
from UnitTesting import plugin as ut38
45+
46+
47+
class UnitTesting33Command(ut38.UnitTestingCommand):
48+
\"\"\"Execute unit tests for python 3.3 plugins.\"\"\"
49+
pass
50+
"""
51+
).lstrip()
4952

5053
UT33 = os.path.join(sublime.packages_path(), "UnitTesting33")
5154
os.makedirs(UT33, exist_ok=True)

unittesting/base.py

Lines changed: 89 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@
22
import re
33
import sublime
44
import sublime_plugin
5+
import threading
56

6-
from collections import ChainMap
7+
from collections import ChainMap, deque
78
from fnmatch import fnmatch
89
from glob import glob
910

10-
from .utils import OutputPanel
11-
1211
DEFAULT_SETTINGS = {
1312
# input
1413
"tests_dir": "tests",
@@ -50,6 +49,93 @@ def relative_to_spp(path):
5049
return None
5150

5251

52+
class OutputPanel:
53+
def __init__(
54+
self,
55+
window,
56+
name,
57+
file_regex="",
58+
line_regex="",
59+
base_dir=None,
60+
word_wrap=False,
61+
line_numbers=False,
62+
gutter=False,
63+
scroll_past_end=False,
64+
):
65+
self.name = name
66+
self.window = window
67+
self.output_view = window.create_output_panel(name)
68+
69+
# default to the current file directory
70+
if not base_dir:
71+
view = window.active_view()
72+
if view:
73+
file_name = view.file_name()
74+
if file_name:
75+
base_dir = os.path.dirname(file_name)
76+
77+
settings = self.output_view.settings()
78+
settings.set("result_file_regex", file_regex)
79+
settings.set("result_line_regex", line_regex)
80+
settings.set("result_base_dir", base_dir)
81+
settings.set("word_wrap", word_wrap)
82+
settings.set("line_numbers", line_numbers)
83+
settings.set("gutter", gutter)
84+
settings.set("scroll_past_end", scroll_past_end)
85+
86+
# make sure to apply settings
87+
self.output_view = window.create_output_panel(name)
88+
self.output_view.assign_syntax("unit-testing-test-result.sublime-syntax")
89+
self.output_view.set_read_only(True)
90+
self.closed = False
91+
92+
self.text_queue_lock = threading.Lock()
93+
self.text_queue = deque()
94+
95+
def write(self, s):
96+
with self.text_queue_lock:
97+
self.text_queue.append(s)
98+
99+
def writeln(self, s):
100+
self.write(s + "\n")
101+
102+
def _write(self):
103+
text = ""
104+
with self.text_queue_lock:
105+
while self.text_queue:
106+
text += self.text_queue.popleft()
107+
108+
self.output_view.run_command("append", {"characters": text, "force": True})
109+
self.output_view.show(self.output_view.size())
110+
111+
def flush(self):
112+
self._write()
113+
114+
def show(self):
115+
self.window.run_command("show_panel", {"panel": "output." + self.name})
116+
117+
def close(self):
118+
self.flush()
119+
self.closed = True
120+
121+
122+
class StdioSplitter:
123+
def __init__(self, io, stream):
124+
self.io = io
125+
self.stream = stream
126+
127+
def write(self, data):
128+
self.io.write(data)
129+
self.stream.write(data)
130+
131+
def writeln(self, s):
132+
self.write(s + "\n")
133+
134+
def flush(self):
135+
self.io.flush()
136+
self.stream.flush()
137+
138+
53139
class BaseUnittestingCommand(sublime_plugin.WindowCommand):
54140
def current_package_name(self):
55141
view = self.window.active_view()

unittesting/unit.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@
1010

1111
from .base import BaseUnittestingCommand
1212
from .base import DONE_MESSAGE
13+
from .base import StdioSplitter
1314
from .core import DeferrableTestCase
1415
from .core import DeferrableTestLoader
1516
from .core import DeferringTextTestRunner
16-
from .utils import reload_package
17-
from .utils import StdioSplitter
17+
from .reloader import reload_package
1818

1919
try:
2020
import coverage

unittesting/utils/__init__.py

Lines changed: 0 additions & 5 deletions
This file was deleted.

unittesting/utils/output_panel.py

Lines changed: 0 additions & 73 deletions
This file was deleted.

unittesting/utils/stdio_splitter.py

Lines changed: 0 additions & 15 deletions
This file was deleted.

0 commit comments

Comments
 (0)