Skip to content

Commit 3f9858a

Browse files
authored
Merge pull request #51 from common-workflow-language/short-names
Add short names to cwltest and junit-xml
2 parents a7f6330 + 05dcdbd commit 3f9858a

File tree

6 files changed

+97
-18
lines changed

6 files changed

+97
-18
lines changed

cwltest/__init__.py

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
import threading
1515
import time
1616

17-
import junit_xml
1817
import ruamel.yaml as yaml
1918
import ruamel.yaml.scanner as yamlscanner
2019
import schema_salad.ref_resolver
@@ -23,7 +22,8 @@
2322
from six.moves import zip
2423
from typing import Any, Dict, List
2524

26-
from cwltest.utils import compare, CompareFail, TestResult, REQUIRED
25+
import junit_xml
26+
from cwltest.utils import compare, CompareFail, TestResult, REQUIRED, get_test_number_by_key
2727

2828
_logger = logging.getLogger("cwltest")
2929
_logger.addHandler(logging.StreamHandler())
@@ -86,7 +86,10 @@ def run_test(args, i, tests, timeout):
8686
try:
8787
test_command = prepare_test_command(args, i, tests)
8888

89-
sys.stderr.write("%sTest [%i/%i] %s\n" % (prefix, i + 1, len(tests), suffix))
89+
if t.get("short_name"):
90+
sys.stderr.write("%sTest [%i/%i] %s: %s%s\n" % (prefix, i + 1, len(tests), t.get("short_name"), t.get("doc"), suffix))
91+
else:
92+
sys.stderr.write("%sTest [%i/%i] %s%s\n" % (prefix, i + 1, len(tests), t.get("doc"), suffix))
9093
sys.stderr.flush()
9194

9295
start_time = time.time()
@@ -154,7 +157,8 @@ def arg_parser(): # type: () -> argparse.ArgumentParser
154157
parser.add_argument("--test", type=str, help="YAML file describing test cases", required=True)
155158
parser.add_argument("--basedir", type=str, help="Basedir to use for tests", default=".")
156159
parser.add_argument("-l", action="store_true", help="List tests then exit")
157-
parser.add_argument("-n", type=str, default=None, help="Run a specific tests, format is 1,3-6,9")
160+
parser.add_argument("-n", type=str, default=None, help="Run specific tests, format is 1,3-6,9")
161+
parser.add_argument("-s", type=str, default=None, help="Run specific tests using their short names separated by comma")
158162
parser.add_argument("--tool", type=str, default="cwl-runner",
159163
help="CWL runner executable to use (default 'cwl-runner'")
160164
parser.add_argument("--only-tools", action="store_true", help="Only test CommandLineTools")
@@ -210,17 +214,30 @@ def main(): # type: () -> int
210214

211215
if args.l:
212216
for i, t in enumerate(tests):
213-
print(u"[%i] %s" % (i + 1, t["doc"].strip()))
217+
if t.get("short_name"):
218+
print(u"[%i] %s: %s" % (i + 1, t["short_name"], t["doc"].strip()))
219+
else:
220+
print(u"[%i] %s" % (i + 1, t["doc"].strip()))
221+
214222
return 0
215223

216-
if args.n is not None:
224+
if args.n is not None or args.s is not None:
217225
ntest = []
218-
for s in args.n.split(","):
219-
sp = s.split("-")
220-
if len(sp) == 2:
221-
ntest.extend(list(range(int(sp[0]) - 1, int(sp[1]))))
222-
else:
223-
ntest.append(int(s) - 1)
226+
if args.n is not None:
227+
for s in args.n.split(","):
228+
sp = s.split("-")
229+
if len(sp) == 2:
230+
ntest.extend(list(range(int(sp[0]) - 1, int(sp[1]))))
231+
else:
232+
ntest.append(int(s) - 1)
233+
if args.s is not None:
234+
for s in args.s.split(","):
235+
test_number = get_test_number_by_key(tests, "short_name", s)
236+
if test_number:
237+
ntest.append(test_number)
238+
else:
239+
_logger.error('Test with short name "%s" not found ' % s)
240+
return 1
224241
else:
225242
ntest = list(range(0, len(tests)))
226243

cwltest/utils.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import json
22

3-
import junit_xml
4-
from typing import Any, Dict, Set, Text
5-
63
from six.moves import range
4+
from typing import Any, Dict, Set, Text, List, Optional
75

6+
import junit_xml
87
REQUIRED = "required"
98

109

@@ -28,8 +27,9 @@ def create_test_case(self, test):
2827
category = ", ".join(test['tags'])
2928
else:
3029
category = REQUIRED
30+
short_name = test.get(u'short_name')
3131
case = junit_xml.TestCase(
32-
doc, elapsed_sec=self.duration, classname=self.classname,
32+
doc, elapsed_sec=self.duration, file=short_name,
3333
category=category, stdout=self.standard_output, stderr=self.error_output,
3434
)
3535
if self.return_code > 0:
@@ -167,3 +167,11 @@ def compare(expected, actual): # type: (Any, Any) -> None
167167

168168
except Exception as e:
169169
raise CompareFail(str(e))
170+
171+
172+
def get_test_number_by_key(tests, key, value):
173+
# type: (List[Dict[str, str]], str, str) -> Optional[int]
174+
for i, test in enumerate(tests):
175+
if key in test and test[key] == value:
176+
return i
177+
return None

tests/test-data/short-names.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
- job: v1.0/cat-job.json
2+
output: {}
3+
tool: return-0.cwl
4+
doc: Test with a short name
5+
short_name: opt-error
6+
tags: [ js, init_work_dir ]
7+
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
- job: v1.0/cat1-job.json
2+
output: {}
3+
tool: return-0.cwl
4+
doc: Test without a short name
5+
- job: v1.0/cat2-job.json
6+
output: {}
7+
tool: return-0.cwl
8+
doc: Test with a short name
9+
short_name: opt-error

tests/test_categories.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,15 @@ def test_unsupported_with_required_tests(self):
1212
args = ["--test", get_data("tests/test-data/required-unsupported.yml")]
1313
error_code, stdout, stderr = run_with_mock_cwl_runner(args)
1414
self.assertEquals(error_code, 1)
15-
self.assertEquals("Test [1/2] \n\nTest [2/2] \n\n"
15+
self.assertEquals("Test [1/2] Required test that is unsupported (without tags)\n\n"
16+
"Test [2/2] Required test that is unsupported (with tags)\n\n"
1617
"0 tests passed, 2 failures, 0 unsupported features\n", stderr)
1718

1819
def test_unsupported_with_optional_tests(self):
1920
args = ["--test", get_data("tests/test-data/optional-unsupported.yml")]
2021
error_code, stdout, stderr = run_with_mock_cwl_runner(args)
2122
self.assertEquals(error_code, 0)
22-
self.assertEquals("Test [1/1] \n\n"
23+
self.assertEquals("Test [1/1] Optional test that is unsupported\n\n"
2324
"0 tests passed, 1 unsupported features\n", stderr)
2425

2526
def test_error_with_optional_tests(self):

tests/test_short_names.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import unittest
2+
3+
import os
4+
5+
from .util import run_with_mock_cwl_runner, get_data
6+
import xml.etree.ElementTree as ET
7+
8+
9+
class TestShortNames(unittest.TestCase):
10+
11+
def test_stderr_output(self):
12+
args = ["--test", get_data("tests/test-data/short-names.yml")]
13+
error_code, stdout, stderr = run_with_mock_cwl_runner(args)
14+
self.assertIn("Test [1/1] opt-error: Test with a short name\n", stderr)
15+
16+
def test_run_by_short_name(self):
17+
short_name = "opt-error"
18+
args = ["--test", get_data("tests/test-data/with-and-without-short-names.yml"), "-s", short_name]
19+
error_code, stdout, stderr = run_with_mock_cwl_runner(args)
20+
self.assertIn("Test [2/2] opt-error: Test with a short name", stderr)
21+
self.assertNotIn("Test [1/2]", stderr)
22+
23+
def test_list_tests(self):
24+
args = ["--test", get_data("tests/test-data/with-and-without-short-names.yml"), "-l"]
25+
error_code, stdout, stderr = run_with_mock_cwl_runner(args)
26+
self.assertEquals("[1] Test without a short name\n"
27+
"[2] opt-error: Test with a short name\n", stdout)
28+
29+
def test_short_name_in_junit_xml(self):
30+
junit_xml_report = get_data("tests/test-data/junit-report.xml")
31+
args = ["--test", get_data("tests/test-data/short-names.yml"), "--junit-xml", junit_xml_report]
32+
run_with_mock_cwl_runner(args)
33+
tree = ET.parse(junit_xml_report)
34+
root = tree.getroot()
35+
category = root.find("testsuite").find("testcase").attrib['file']
36+
self.assertEquals(category, "opt-error")
37+
os.remove(junit_xml_report)

0 commit comments

Comments
 (0)