This repository has been archived by the owner on Aug 2, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
dc00f36
commit 9a61a4e
Showing
2 changed files
with
78 additions
and
20 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,42 @@ | ||
|
||
import os | ||
import os.path | ||
from unittest.main import main | ||
import sys | ||
import unittest | ||
|
||
|
||
TEST_ROOT = os.path.dirname(__file__) | ||
PROJECT_ROOT = os.path.dirname(TEST_ROOT) | ||
|
||
|
||
executable = sys.executable + ' -m unittest' | ||
if all(arg.startswith('-') for arg in sys.argv[1:]): | ||
argv = [executable, | ||
'discover', | ||
'--start-directory', PROJECT_ROOT, | ||
'--top-level-directory', PROJECT_ROOT, | ||
] + sys.argv[1:] | ||
else: | ||
argv = [executable] + sys.argv[1:] | ||
for i, arg in enumerate(argv[1:], 1): | ||
if arg.startswith('-'): | ||
continue | ||
mod, _, test = arg.partition(':') | ||
mod = mod.rstrip(os.sep) | ||
mod = mod.rstrip('.py') | ||
mod = mod.replace(os.sep, '.') | ||
argv[i] = mod if not test else mod + '.' + test | ||
def convert_argv(argv): | ||
args = [] | ||
modules = set() | ||
for arg in argv: | ||
# Unittest's main has only flags and positional args. | ||
# So we don't worry about options with values. | ||
if not arg.startswith('-'): | ||
# It must be the name of a test, case, module, or file. | ||
# We convert filenames to module names. For filenames | ||
# we support specifying a test name by appending it to | ||
# the filename with a ":" in between. | ||
mod, _, test = arg.partition(':') | ||
if mod.endswith(os.sep): | ||
mod = mod.rsplit(os.sep, 1)[0] | ||
mod = mod.rsplit('.py', 1)[0] | ||
mod = mod.replace(os.sep, '.') | ||
arg = mod if not test else mod + '.' + test | ||
modules.add(mod) | ||
args.append(arg) | ||
|
||
if not modules: | ||
# Do discovery. | ||
args = ['discover', | ||
'--start-directory', PROJECT_ROOT, | ||
] + args | ||
return [sys.executable + ' -m unittest'] + args | ||
#return [sys.executable, '-m', 'unittest'] + args | ||
|
||
|
||
main(module=None, argv=argv) | ||
if __name__ == '__main__': | ||
argv = convert_argv(sys.argv[1:]) | ||
unittest.main(module=None, argv=argv) |
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 |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import os | ||
import os.path | ||
import unittest | ||
import sys | ||
|
||
from .__main__ import convert_argv | ||
|
||
|
||
PROJECT_ROOT = os.path.dirname(os.path.dirname(__file__)) | ||
|
||
|
||
class ConvertArgsTests(unittest.TestCase): | ||
|
||
def test_discovery(self): | ||
argv = convert_argv(['-v', '--failfast']) | ||
|
||
self.assertEqual(argv, [ | ||
sys.executable + ' -m unittest', | ||
'discover', | ||
'--start-directory', PROJECT_ROOT, | ||
'-v', '--failfast', | ||
]) | ||
|
||
def test_modules(self): | ||
argv = convert_argv(['-v', '--failfast', | ||
'w', | ||
'x/y.py:Spam.test_spam'.replace('/', os.sep), | ||
'z:Eggs', | ||
]) | ||
|
||
self.assertEqual(argv, [ | ||
sys.executable + ' -m unittest', | ||
'-v', '--failfast', | ||
'w', | ||
'x.y.Spam.test_spam', | ||
'z.Eggs', | ||
]) | ||
|
||
def test_no_args(self): | ||
argv = convert_argv([]) | ||
|
||
self.assertEqual(argv, [ | ||
sys.executable + ' -m unittest', | ||
'discover', | ||
'--start-directory', PROJECT_ROOT, | ||
]) |