-
-
Notifications
You must be signed in to change notification settings - Fork 817
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor unittests for compatibility with pytest
Resolves #1318 This commit prepares unittests to run on official python tools like pytest by: 1. moving mocking modules to package_control/tests/mock and load them whenever `tests` package is imported outside of sublime text 2. moving internal test runner code to package_control_tests_command.py Note: Not sure whether this is still needed. 3. renaming all test modules to `test_...` so pytest can easily find them. 4. add pytest config to tox.ini
- Loading branch information
Showing
16 changed files
with
97 additions
and
157 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 56 additions & 13 deletions
69
package_control/commands/package_control_tests_command.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,65 @@ | ||
import io | ||
import os.path | ||
import threading | ||
import unittest | ||
|
||
import sublime | ||
import sublime_plugin | ||
|
||
from ..settings import pc_settings_filename | ||
|
||
try: | ||
from ..tests import TestRunner, TEST_CLASSES | ||
except ImportError: | ||
class PackageControlTestsCommand: | ||
|
||
class OutputPanel(io.TextIOWrapper): | ||
""" | ||
A TextIO wrapper to output test results in an output panel. | ||
""" | ||
|
||
def __init__(self, window): | ||
self.panel = window.get_output_panel("package_control_tests") | ||
self.panel.settings().set("word_wrap", True) | ||
self.panel.settings().set("scroll_past_end", False) | ||
window.run_command("show_panel", {"panel": "output.package_control_tests"}) | ||
|
||
def write(self, data): | ||
self.panel.run_command("package_control_insert", {"string": data}) | ||
|
||
def get(self): | ||
pass | ||
|
||
def flush(self): | ||
pass | ||
|
||
else: | ||
class PackageControlTestsCommand(sublime_plugin.ApplicationCommand): | ||
""" | ||
A command to run the tests for Package Control | ||
""" | ||
|
||
def run(self): | ||
TestRunner(args=(sublime.active_window(), TEST_CLASSES)).start() | ||
class PackageControlTestsCommand(sublime_plugin.ApplicationCommand): | ||
""" | ||
A command to run the tests for Package Control | ||
""" | ||
|
||
HAVE_TESTS = None | ||
|
||
def run(self): | ||
def worker(): | ||
package_root = os.path.join(sublime.packages_path(), "Package Control") | ||
|
||
# tests are excluded from production builds | ||
# so it's ok to rely on filesystem traversal | ||
suite = unittest.TestLoader().discover( | ||
pattern="test_*.py", | ||
start_dir=os.path.join(package_root, "package_control", "tests"), | ||
top_level_dir=package_root, | ||
) | ||
|
||
output = OutputPanel(sublime.active_window()) | ||
output.write("Running Package Control Tests\n\n") | ||
|
||
unittest.TextTestRunner(stream=output, verbosity=1).run(suite) | ||
|
||
threading.Thread(target=worker).start() | ||
|
||
def is_visible(self): | ||
if self.HAVE_TESTS is None: | ||
self.HAVE_TESTS = os.path.exists( | ||
os.path.join(sublime.packages_path(), "Package Control", "package_control", "tests") | ||
) | ||
|
||
def is_visible(self): | ||
return sublime.load_settings(pc_settings_filename()).get("enable_tests", False) | ||
return self.HAVE_TESTS and sublime.load_settings(pc_settings_filename()).get("enable_tests", False) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,90 +1,19 @@ | ||
import threading | ||
import unittest | ||
from sys import modules | ||
|
||
from . import ( | ||
clients, | ||
distinfo, | ||
downloaders, | ||
package_version, | ||
pep440_specifier, | ||
pep440_version, | ||
pep508_marker, | ||
providers, | ||
selectors, | ||
) | ||
if "sublime" not in modules: | ||
import importlib.machinery | ||
import os | ||
|
||
PACKAGE_ROOT = os.path.dirname(__file__) | ||
|
||
TEST_CLASSES = [ | ||
clients.BitBucketClientTests, | ||
clients.GitHubClientTests, | ||
clients.GitLabClientTests, | ||
distinfo.DistinfoTests, | ||
downloaders.CurlDownloaderTests, | ||
downloaders.OscryptoDownloaderTests, | ||
downloaders.ResolveUrlTests, | ||
downloaders.UrlLibDownloaderTests, | ||
downloaders.WgetDownloaderTests, | ||
downloaders.WinINetDownloaderTests, | ||
package_version.PackageVersionTests, | ||
pep440_specifier.PEP440VersionSpecifierTests, | ||
pep440_version.PEP440VersionTests, | ||
pep508_marker.PEP508MarkerTests, | ||
providers.BitBucketRepositoryProviderTests, | ||
providers.ChannelProviderTests, | ||
providers.GitHubRepositoryProviderTests, | ||
providers.GitHubUserProviderTests, | ||
providers.GitLabRepositoryProviderTests, | ||
providers.GitLabUserProviderTests, | ||
providers.JsonRepositoryProviderTests, | ||
selectors.PlatformSelectorTests, | ||
selectors.VersionSelectorTests, | ||
] | ||
# Mock the sublime module for CLI usage | ||
sublime = importlib.machinery.SourceFileLoader( | ||
"sublime", | ||
os.path.join(PACKAGE_ROOT, "mock_sublime.py") | ||
).load_module() | ||
|
||
|
||
class OutputPanel: | ||
def __init__(self, window): | ||
self.panel = window.get_output_panel("package_control_tests") | ||
self.panel.settings().set("word_wrap", True) | ||
self.panel.settings().set("scroll_past_end", False) | ||
window.run_command("show_panel", {"panel": "output.package_control_tests"}) | ||
|
||
def write(self, data): | ||
self.panel.run_command("package_control_insert", {"string": data}) | ||
|
||
def get(self): | ||
pass | ||
|
||
def flush(self): | ||
pass | ||
|
||
|
||
class TestRunner(threading.Thread): | ||
""" | ||
Runs tests in a thread and outputs the results to an output panel | ||
:example: | ||
TestRunner(args=(window, test_classes)).start() | ||
:param window: | ||
A sublime.Window object to use to display the results | ||
:param test_classes: | ||
A unittest.TestCase class, or list of classes | ||
""" | ||
|
||
def run(self): | ||
window, test_classes = self._args | ||
|
||
output = OutputPanel(window) | ||
output.write("Running Package Control Tests\n\n") | ||
|
||
if not isinstance(test_classes, list) and not isinstance(test_classes, tuple): | ||
test_classes = [test_classes] | ||
|
||
suite = unittest.TestSuite() | ||
|
||
loader = unittest.TestLoader() | ||
for test_class in test_classes: | ||
suite.addTest(loader.loadTestsFromTestCase(test_class)) | ||
|
||
unittest.TextTestRunner(stream=output, verbosity=1).run(suite) | ||
# Mock the sublime_plugin module for CLI usage | ||
sublime_plugin = importlib.machinery.SourceFileLoader( | ||
"sublime_plugin", | ||
os.path.join(PACKAGE_ROOT, "mock_sublime_plugin.py") | ||
).load_module() |
File renamed without changes.
3 changes: 1 addition & 2 deletions
3
dev/sublime_plugin.py → package_control/tests/mock_sublime_plugin.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,2 @@ | ||
|
||
class ZipImporter: | ||
pass | ||
pass |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters