Skip to content

Commit

Permalink
refactor testing code
Browse files Browse the repository at this point in the history
  • Loading branch information
guillermooo committed Apr 19, 2015
1 parent dcfffb7 commit 356d61f
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 34 deletions.
15 changes: 10 additions & 5 deletions dev_cmds.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,24 @@ def find_project_path(path):


class RunTestsForActiveViewCommand(sublime_plugin.WindowCommand):
'''Runs tests:
'''
Runs tests:
- From a file with the name 'test_<active_file_basename>' if it exists.
- Else, from the active file.
- from a file with the name 'test_<active_file_basename>' if it exists,
- from a file with the .cmd-test[-solo] extension,
- else, from the active file.
'''

def run(self):
v = self.window.active_view()
if v is None:
return

proj_path = find_project_path(v.file_name())
if not proj_path or not v.file_name().endswith('.py'):
print('Vintageous (Dev): Not a project or python file.')
if not proj_path or not v.file_name().endswith(('.py', '.cmd-test', '.cmd-test-solo')):
print(
'Vintageous (Dev): Not a project, cmd-test or python file: '
+ v.file_name())
return

# If it's a test_* file, run it.
Expand Down
78 changes: 49 additions & 29 deletions test_runner.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import sublime
import sublime_plugin

from threading import Thread
import contextlib
import os
import unittest
import contextlib

from threading import Thread
import sublime
import sublime_plugin


class __vi_tests_write_buffer(sublime_plugin.TextCommand):
Expand All @@ -25,11 +24,16 @@ def run(self, edit):


class OutputPanel(object):
def __init__(self, name, file_regex='', line_regex='', base_dir=None,
word_wrap=False, line_numbers=False, gutter=False,
scroll_past_end=False,
syntax='Packages/Text/Plain text.tmLanguage',
):
def __init__(self, name,
file_regex='',
line_regex='',
base_dir=None,
word_wrap=False,
line_numbers=False,
gutter=False,
scroll_past_end=False,
syntax='',
):

self.name = name
self.window = sublime.active_window()
Expand Down Expand Up @@ -75,55 +79,71 @@ def close(self):


class RunVintageousTests(sublime_plugin.WindowCommand):
'''Runs tests and displays the result.
'''
Runs tests and displays the results.
- Do not use ST while tests are running.
- Do not use Sublime Text while tests are running.
@working_dir
Required. Should be the parent of the top-level directory for `tests`.
@loader_pattern
Optional. Only run tests matching this glob.
@tests_dir
Name of the directory containing tests.
@active_file_only
Optional. Only run tests in the active file in ST. Shadows
@loader_pattern.
To use this runner conveniently, open the command palette and select one
of the `Build: Vintageous - Test *` commands.
'''
@contextlib.contextmanager
def chdir(self, path=None):
old_path = os.getcwd()
if path:
assert os.path.exists(path), "'path' is invalid {}".format(path)
os.chdir(path)
yield
if path is not None:
os.chdir(old_path)

def run(self, **kwargs):
with self.chdir(kwargs.get('working_dir')):
p = os.path.join(os.getcwd(), 'tests')
patt = kwargs.get('loader_pattern', 'test*.py',)
def run(self, working_dir,
loader_pattern="test*.py",
tests_dir="tests",
**kwargs):
assert os.path.exists(working_dir), 'working_dir must exist'

with self.chdir(working_dir):
p = os.path.join(os.getcwd(), tests_dir)

patt = loader_pattern
# TODO(guillermooo): I can't get $file to expand in the build
# system. It should be possible to make the following code simpler
# with it.
if kwargs.get('active_file_only') is True:
patt = os.path.basename(self.window.active_view().file_name())
# run text-based tests
if patt.endswith('.cmd-test'):
patt = 'test_all_cmds.py'
suite = unittest.TestLoader().discover(p, pattern=patt)

file_regex = r'^\s*File\s*"([^.].*?)",\s*line\s*(\d+),.*$'
display = OutputPanel('vintageous.tests', file_regex=file_regex,
word_wrap=True)
display = OutputPanel('vintageous.tests',
file_regex=file_regex,
word_wrap=True
)

display.show()
runner = unittest.TextTestRunner(stream=display, verbosity=1)

def run_and_display():
runner.run(suite)
display.show()
# If we don't do this, custom mappings won't be available
# XXX: If we don't do this, custom mappings won't be available
# after running the test suite.
self.window.run_command('reset_vintageous')

Thread(target=run_and_display).start()

@contextlib.contextmanager
def chdir(self, path=None):
old_path = os.getcwd()
if path:
assert os.path.exists(path), "'path' is invalid {}".format(path)
os.chdir(path)
yield
if path is not None:
os.chdir(old_path)

0 comments on commit 356d61f

Please sign in to comment.