diff options
author | William Deegan <bill@baddogconsulting.com> | 2018-10-15 15:16:00 (GMT) |
---|---|---|
committer | William Deegan <bill@baddogconsulting.com> | 2018-10-15 15:16:00 (GMT) |
commit | 202ed97e33f4728534502472556089bb47505605 (patch) | |
tree | 590574b98ac4b6dcde73cb1889bb35658fe7e362 /testing/framework | |
parent | 1b6e5f48a60bd10b417d5718915080a2e1b53c41 (diff) | |
parent | 55c4cebac66372da7a27c7b1e00d435dc2abe27c (diff) | |
download | SCons-202ed97e33f4728534502472556089bb47505605.zip SCons-202ed97e33f4728534502472556089bb47505605.tar.gz SCons-202ed97e33f4728534502472556089bb47505605.tar.bz2 |
Merge remote-tracking branch 'upstream/master' into fix_java_tests_path_with_spaces
Diffstat (limited to 'testing/framework')
-rw-r--r-- | testing/framework/TestCommon.py | 105 | ||||
-rw-r--r-- | testing/framework/TestCommonTests.py | 56 |
2 files changed, 96 insertions, 65 deletions
diff --git a/testing/framework/TestCommon.py b/testing/framework/TestCommon.py index 012f134..ca4a147 100644 --- a/testing/framework/TestCommon.py +++ b/testing/framework/TestCommon.py @@ -206,6 +206,28 @@ def separate_files(flist): missing.append(f) return existing, missing +def contains(seq, subseq, find): + # Returns True or False. + if find is None: + return subseq in seq + else: + f = find(seq, subseq) + return f not in (None, -1) and f is not False + +def find_index(seq, subseq, find): + # Returns either an index of the subseq within the seq, or None. + # Accepts a function find(seq, subseq), which returns an integer on success + # and either: None, False, or -1, on failure. + if find is None: + try: + return seq.index(subseq) + except ValueError: + return None + else: + i = find(seq, subseq) + return None if (i in (None, -1) or i is False) else i + + if os.name == 'posix': def _failed(self, status = 0): if self.status is None or status is None: @@ -280,8 +302,8 @@ class TestCommon(TestCmd): reading the file; current implementation will convert. mode (string): file open mode. find (func): optional custom search routine. Must take the - form "find(output, line)" returning non-zero on success - and None on failure. + form "find(output, line)" non-negative integer on success + and None, False, or -1, on failure. Calling test exits FAILED if search result is false """ @@ -291,20 +313,14 @@ class TestCommon(TestCmd): # (str) type in that, so convert. required = to_bytes(required) file_contents = self.read(file, mode) - if find is None: - def find(o, l): - try: - return o.index(l) - except ValueError: - return None - contains = find(file_contents, required) - if not contains: + + if not contains(file_contents, required, find): print("File `%s' does not contain required string." % file) print(self.banner('Required string ')) print(required) print(self.banner('%s contents ' % file)) print(file_contents) - self.fail_test(not contains) + self.fail_test() def must_contain_all(self, output, input, title=None, find=None): """Ensures that the specified output string (first argument) @@ -314,20 +330,13 @@ class TestCommon(TestCmd): of output being searched, and only shows up in failure output. An optional fourth argument can be used to supply a different - function, of the form "find(line, output), to use when searching + function, of the form "find(output, line)", to use when searching for lines in the output. """ - if find is None: - def find(o, i): - try: - return o.index(i) - except ValueError: - return None - if is_List(output): output = os.newline.join(output) - if find(output, input) is None: + if not contains(output, input, find): if title is None: title = 'output' print('Missing expected input from {}:'.format(title)) @@ -344,21 +353,15 @@ class TestCommon(TestCmd): of output being searched, and only shows up in failure output. An optional fourth argument can be used to supply a different - function, of the form "find(line, output), to use when searching + function, of the form "find(output, line)", to use when searching for lines in the output. """ - if find is None: - def find(o, l): - try: - return o.index(l) - except ValueError: - return None missing = [] if is_List(output): output = '\n'.join(output) for line in lines: - if find(output, line) is None: + if not contains(output, line, find): missing.append(line) if missing: @@ -379,17 +382,11 @@ class TestCommon(TestCmd): of output being searched, and only shows up in failure output. An optional fourth argument can be used to supply a different - function, of the form "find(line, output), to use when searching + function, of the form "find(output, line)", to use when searching for lines in the output. """ - if find is None: - def find(o, l): - try: - return o.index(l) - except ValueError: - return None for line in lines: - if find(output, line) is not None: + if contains(output, line, find): return if title is None: @@ -410,7 +407,7 @@ class TestCommon(TestCmd): of output being searched, and only shows up in failure output. An optional fourth argument can be used to supply a different - function, of the form "find(line, output), to use when searching + function, of the form "find(output, line)", to use when searching for lines in the output. The function must return the index of the found line in the output, or None if the line is not found. """ @@ -422,19 +419,13 @@ class TestCommon(TestCmd): if sorted(out) == sorted(exp): # early out for exact match return - if find is None: - def find(o, l): - try: - return o.index(l) - except ValueError: - return None missing = [] for line in exp: - found = find(out, line) - if found is None: + i = find_index(out, line, find) + if i is None: missing.append(line) else: - out.pop(found) + out.pop(i) if not missing and not out: # all lines were matched @@ -513,20 +504,14 @@ class TestCommon(TestCmd): """Ensures that the specified file doesn't contain the banned text. """ file_contents = self.read(file, mode) - if find is None: - def find(o, l): - try: - return o.index(l) - except ValueError: - return None - contains = find(file_contents, banned) - if contains: + + if contains(file_contents, banned, find): print("File `%s' contains banned string." % file) print(self.banner('Banned string ')) print(banned) print(self.banner('%s contents ' % file)) print(file_contents) - self.fail_test(contains) + self.fail_test() def must_not_contain_any_line(self, output, lines, title=None, find=None): """Ensures that the specified output string (first argument) @@ -536,18 +521,12 @@ class TestCommon(TestCmd): of output being searched, and only shows up in failure output. An optional fourth argument can be used to supply a different - function, of the form "find(line, output), to use when searching + function, of the form "find(output, line)", to use when searching for lines in the output. """ - if find is None: - def find(o, l): - try: - return o.index(l) - except ValueError: - return None unexpected = [] for line in lines: - if find(output, line) is not None: + if contains(output, line, find): unexpected.append(line) if unexpected: diff --git a/testing/framework/TestCommonTests.py b/testing/framework/TestCommonTests.py index 7949cb8..c54f33f 100644 --- a/testing/framework/TestCommonTests.py +++ b/testing/framework/TestCommonTests.py @@ -306,6 +306,23 @@ class must_contain_TestCase(TestCommonTestCase): stderr = run_env.stderr() assert stderr == "PASSED\n", stderr + def test_success_index_0(self): + """Test must_contain(): success at index 0""" + run_env = self.run_env + + script = lstrip("""\ + from TestCommon import TestCommon + tc = TestCommon(workdir='') + tc.write('file1', "file1 contents\\n") + tc.must_contain('file1', "file1 c") + tc.pass_test() + """) + run_env.run(program=sys.executable, stdin=script) + stdout = run_env.stdout() + assert stdout == "", stdout + stderr = run_env.stderr() + assert stderr == "PASSED\n", stderr + def test_file_missing(self): """Test must_contain(): file missing""" run_env = self.run_env @@ -1145,6 +1162,9 @@ class must_match_TestCase(TestCommonTestCase): """) expect = lstrip("""\ + match_re: mismatch at line 0: + search re='^file1$' + line='file1 does not match' Unexpected contents of `file1' contents ======================================================================= 1c1 @@ -1324,6 +1344,31 @@ class must_not_contain_TestCase(TestCommonTestCase): stderr = run_env.stderr() assert stderr.find("FAILED") != -1, stderr + def test_failure_index_0(self): + """Test must_not_contain(): failure at index 0""" + run_env = self.run_env + + script = lstrip("""\ + from TestCommon import TestCommon + tc = TestCommon(workdir='') + tc.write('file1', "file1 does contain contents\\n") + tc.must_not_contain('file1', "file1 does") + tc.run() + """) + expect = lstrip("""\ + File `file1' contains banned string. + Banned string ================================================================== + file1 does + file1 contents ================================================================= + file1 does contain contents + + """) + run_env.run(program=sys.executable, stdin=script) + stdout = run_env.stdout() + assert stdout == expect, repr(stdout) + stderr = run_env.stderr() + assert stderr.find("FAILED") != -1, stderr + def test_mode(self): """Test must_not_contain(): mode""" run_env = self.run_env @@ -1812,6 +1857,7 @@ class run_TestCase(TestCommonTestCase): """) expect_stdout = lstrip("""\ + match_re: expected 1 lines, found 2 STDOUT ========================================================================= STDERR ========================================================================= @@ -2024,6 +2070,9 @@ class run_TestCase(TestCommonTestCase): """) expect_stdout = lstrip("""\ + match_re: mismatch at line 0: + search re='^Not found$' + line='%(pass_script)s: STDOUT: []' STDOUT ========================================================================= 1c1 < Not found @@ -2053,6 +2102,9 @@ class run_TestCase(TestCommonTestCase): """) expect_stdout = lstrip("""\ + match_re: mismatch at line 0: + search re='^Not found$' + line='%(stderr_script)s: STDERR: []' STDOUT ========================================================================= STDERR ========================================================================= @@ -2286,14 +2338,14 @@ class variables_TestCase(TestCommonTestCase): 'dll_suffix', ] - script = "from __future__ import print_function" + \ + script = "from __future__ import print_function\n" + \ "import TestCommon\n" + \ '\n'.join([ "print(TestCommon.%s)\n" % v for v in variables ]) run_env.run(program=sys.executable, stdin=script) stderr = run_env.stderr() assert stderr == "", stderr - script = "from __future__ import print_function" + \ + script = "from __future__ import print_function\n" + \ "from TestCommon import *\n" + \ '\n'.join([ "print(%s)" % v for v in variables ]) run_env.run(program=sys.executable, stdin=script) |