From 79535f03fa0a2fb9eb3dde7a548a952f3854880a Mon Sep 17 00:00:00 2001 From: herr kaste Date: Fri, 7 Dec 2018 01:05:03 +0100 Subject: [PATCH 1/2] UX: Remember last current file and use it If a user switches from test file to implementation file, it is useful that 'Test Current File' remembers and executes the actual last test file run. Changes: - We actually match the filename against `settings['pattern']`. Not all files with a '.py' suffix are test files. - We store the last used test file in the `window.settings()` which is cheap to implement and probably the right thing. --- unittesting/mixin.py | 7 ------- unittesting/test_current.py | 19 ++++++++++++++++--- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/unittesting/mixin.py b/unittesting/mixin.py index f4b490c3..2f8b1888 100644 --- a/unittesting/mixin.py +++ b/unittesting/mixin.py @@ -62,13 +62,6 @@ def current_package_name(self): return None - @property - def current_test_file(self): - current_file = sublime.active_window().active_view().file_name() - if current_file and current_file.endswith(".py"): - current_file = os.path.basename(current_file) - return current_file - def input_parser(self, package): m = re.match(r'([^:]+):(.+)', package) if m: diff --git a/unittesting/test_current.py b/unittesting/test_current.py index 16a321ea..17303272 100644 --- a/unittesting/test_current.py +++ b/unittesting/test_current.py @@ -1,3 +1,5 @@ +from fnmatch import fnmatch +import os import sublime import sys from .test_package import UnitTestingCommand @@ -43,9 +45,20 @@ def run(self, **kwargs): sublime.message_dialog("Cannot determine package name.") return - test_file = self.current_test_file - if not test_file: - test_file = "" + window = sublime.active_window() + if not window: + return + + view = window.active_view() + + settings = self.load_unittesting_settings(project_name, kwargs) + current_file = (view and view.file_name()) or '' + file_name = os.path.basename(current_file) + if file_name and fnmatch(file_name, settings['pattern']): + test_file = file_name + window.settings().set('last_test_file', test_file) + else: + test_file = window.settings().get('last_test_file') or current_file sublime.set_timeout_async( lambda: super(UnitTestingCurrentFileCommand, self).run( From d3204274fa0bf2cd507d5e72b9de5c434c46ab66 Mon Sep 17 00:00:00 2001 From: herr kaste Date: Fri, 7 Dec 2018 20:45:28 +0100 Subject: [PATCH 2/2] Namespace the 'last_test_file' setting key --- .flake8 | 3 ++- unittesting/test_current.py | 7 +++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/.flake8 b/.flake8 index 16851d91..9efa3bb1 100644 --- a/.flake8 +++ b/.flake8 @@ -7,5 +7,6 @@ max-line-length = 120 # D103 Missing docstring in public function # D104 Missing docstring in public package # D107 Missing docstring in __init__ +# W503 line break before binary operator # W606 'async' and 'await' are reserved keywords starting with Python 3.7 -ignore = D100, D101, D102, D103, D104, D107, W606 +ignore = D100, D101, D102, D103, D104, D107, W503, W606 diff --git a/unittesting/test_current.py b/unittesting/test_current.py index 17303272..6a1e01b9 100644 --- a/unittesting/test_current.py +++ b/unittesting/test_current.py @@ -56,9 +56,12 @@ def run(self, **kwargs): file_name = os.path.basename(current_file) if file_name and fnmatch(file_name, settings['pattern']): test_file = file_name - window.settings().set('last_test_file', test_file) + window.settings().set('UnitTesting.last_test_file', test_file) else: - test_file = window.settings().get('last_test_file') or current_file + test_file = ( + window.settings().get('UnitTesting.last_test_file') + or current_file + ) sublime.set_timeout_async( lambda: super(UnitTestingCurrentFileCommand, self).run(