3 # Copyright 2000-2010 Steven Knight
5 # This module is free software, and you may redistribute it and/or modify
6 # it under the same terms as Python itself, so long as this copyright message
7 # and disclaimer are retained in their original form.
9 # IN NO EVENT SHALL THE AUTHOR BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,
10 # SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OF
11 # THIS CODE, EVEN IF THE AUTHOR HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
14 # THE AUTHOR SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT
15 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
16 # PARTICULAR PURPOSE. THE CODE PROVIDED HEREUNDER IS ON AN "AS IS" BASIS,
17 # AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE,
18 # SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
20 # Python License: https://docs.python.org/3/license.html#psf-license
23 Unit tests for the TestCommon.py module.
31 from textwrap
import dedent
33 # Strip the current directory so we get the right TestCommon.py module.
34 sys
.path
= sys
.path
[1:]
40 # this used to be a custom function, now use the stdlib equivalent
45 expected_newline
= '\\n'
48 def assert_display(expect
, result
, error
=None):
50 expect
= expect
.pattern
51 except AttributeError:
55 f
"{'EXPECTED: ':*<80}\n",
59 '' if error
is None else error
,
62 return ''.join(display
)
65 class TestCommonTestCase(unittest
.TestCase
):
66 """Base class for TestCommon test cases, fixture and utility methods."""
70 def setUp(self
) -> None:
71 self
.orig_cwd
= os
.getcwd()
72 if self
.create_run_env
:
73 self
.run_env
= TestCmd
.TestCmd(workdir
='')
75 def tearDown(self
) -> None:
76 os
.chdir(self
.orig_cwd
)
78 def set_up_execution_scripts(self
) -> None:
79 run_env
= self
.run_env
81 run_env
.subdir('sub dir')
83 self
.python
= sys
.executable
85 self
.pass_script
= run_env
.workpath('sub dir', 'pass')
86 self
.fail_script
= run_env
.workpath('sub dir', 'fail')
87 self
.stdout_script
= run_env
.workpath('sub dir', 'stdout')
88 self
.stderr_script
= run_env
.workpath('sub dir', 'stderr')
89 self
.signal_script
= run_env
.workpath('sub dir', 'signal')
90 self
.stdin_script
= run_env
.workpath('sub dir', 'stdin')
92 preamble
= "import sys"
93 stdout
= "; sys.stdout.write(r'%s: STDOUT: ' + repr(sys.argv[1:]) + '\\n')"
94 stderr
= "; sys.stderr.write(r'%s: STDERR: ' + repr(sys.argv[1:]) + '\\n')"
95 exit0
= "; sys.exit(0)"
96 exit1
= "; sys.exit(1)"
97 if sys
.platform
== 'win32':
98 wrapper
= '@python -c "%s" %%1 %%2 %%3 %%4 %%5 %%6 %%7 %%8 %%9\n'
100 wrapper
= '#! /usr/bin/env python\n%s\n'
101 wrapper
= '#! /usr/bin/env python\n%s\n'
103 pass_body
= preamble
+ stdout
% self
.pass_script
+ exit0
104 fail_body
= preamble
+ stdout
% self
.fail_script
+ exit1
105 stderr_body
= preamble
+ stderr
% self
.stderr_script
+ exit0
107 run_env
.write(self
.pass_script
, wrapper
% pass_body
)
108 run_env
.write(self
.fail_script
, wrapper
% fail_body
)
109 run_env
.write(self
.stderr_script
, wrapper
% stderr_body
)
111 signal_body
= lstrip("""\
114 os.kill(os.getpid(), signal.SIGTERM)
117 run_env
.write(self
.signal_script
, wrapper
% signal_body
)
122 input = sys.stdin.read()[:-1]
123 sys.stdout.write(r'%s: STDOUT: ' + repr(input) + '\\n')
124 sys.stderr.write(r'%s: STDERR: ' + repr(input) + '\\n')
126 % (self
.stdin_script
, self
.stdin_script
)
129 run_env
.write(self
.stdin_script
, wrapper
% stdin_body
)
131 def run_execution_test(self
, script
, expect_stdout
, expect_stderr
) -> None:
132 self
.set_up_execution_scripts()
134 run_env
= self
.run_env
136 os
.chdir(run_env
.workpath('sub dir'))
138 # Everything before this prepared our "source directory."
139 # Now do the real test.
140 script
= script
% self
.__dict
__
141 run_env
.run(program
=sys
.executable
, stdin
=script
)
143 stdout
= run_env
.stdout()
144 stderr
= run_env
.stderr()
146 expect_stdout
= expect_stdout
% self
.__dict
__
147 assert stdout
== expect_stdout
, assert_display(expect_stdout
, stdout
, stderr
)
150 match
= expect_stderr
.match
151 except AttributeError:
152 expect_stderr
= expect_stderr
% self
.__dict
__
153 assert stderr
== expect_stderr
, assert_display(expect_stderr
, stderr
)
155 assert expect_stderr
.match(stderr
), assert_display(expect_stderr
, stderr
)
158 class __init__TestCase(TestCommonTestCase
):
159 def test___init__(self
) -> None:
160 """Test initialization"""
161 run_env
= self
.run_env
163 os
.chdir(run_env
.workdir
)
165 from TestCommon import TestCommon
166 tc = TestCommon(workdir='')
170 run_env
.run(program
=sys
.executable
, stdin
=script
)
171 stdout
= run_env
.stdout()[:-1]
172 assert stdout
!= run_env
.workdir
, stdout
173 stderr
= run_env
.stderr()
174 assert stderr
== "", stderr
177 class banner_TestCase(TestCommonTestCase
):
178 create_run_env
= False
180 def test_banner(self
) -> None:
182 tc
= TestCommon
.TestCommon(workdir
='')
186 expect
= f
"{text}{tc.banner_char * (tc.banner_width - len(text))}"
187 assert b
== expect
, b
191 expect
= f
"{text}{tc.banner_char * (tc.banner_width - len(text))}"
192 assert b
== expect
, b
194 b
= tc
.banner(text
, 20)
195 expect
= f
"{text}{tc.banner_char * (20 - len(text))}"
196 assert b
== expect
, b
200 expect
= f
"{text}{tc.banner_char * (tc.banner_width - len(text))}"
201 assert b
== expect
, b
204 class must_be_writable_TestCase(TestCommonTestCase
):
205 def test_file_does_not_exists(self
) -> None:
206 """Test must_be_writable(): file does not exist"""
207 run_env
= self
.run_env
210 from TestCommon import TestCommon
211 tc = TestCommon(workdir='')
212 tc.must_be_writable('file1')
215 run_env
.run(program
=sys
.executable
, stdin
=script
)
216 stdout
= run_env
.stdout()
217 assert stdout
== "Missing files: `file1'\n", stdout
218 stderr
= run_env
.stderr()
219 assert stderr
.find("FAILED") != -1, stderr
221 def test_writable_file_exists(self
) -> None:
222 """Test must_be_writable(): writable file exists"""
223 run_env
= self
.run_env
228 from TestCommon import TestCommon
229 tc = TestCommon(workdir='')
230 tc.write('file1', "file1\\n")
231 f1 = tc.workpath('file1')
232 mode = os.stat(f1).st_mode
233 os.chmod(f1, mode | stat.S_IWUSR)
234 tc.must_be_writable('file1')
237 run_env
.run(program
=sys
.executable
, stdin
=script
)
238 stdout
= run_env
.stdout()
239 assert stdout
== "", stdout
240 stderr
= run_env
.stderr()
241 assert stderr
== "PASSED\n", stderr
243 def test_non_writable_file_exists(self
) -> None:
244 """Test must_be_writable(): non-writable file exists"""
245 run_env
= self
.run_env
250 from TestCommon import TestCommon
251 tc = TestCommon(workdir='')
252 tc.write('file1', "file1\\n")
253 f1 = tc.workpath('file1')
254 mode = os.stat(f1).st_mode
255 os.chmod(f1, mode & ~stat.S_IWUSR)
256 tc.must_be_writable('file1')
259 run_env
.run(program
=sys
.executable
, stdin
=script
)
260 stdout
= run_env
.stdout()
261 assert stdout
== "Unwritable files: `file1'\n", stdout
262 stderr
= run_env
.stderr()
263 assert stderr
.find("FAILED") != -1, stderr
265 def test_file_specified_as_list(self
) -> None:
266 """Test must_be_writable(): file specified as list"""
267 run_env
= self
.run_env
272 from TestCommon import TestCommon
273 tc = TestCommon(workdir='')
275 tc.write(['sub', 'file1'], "sub/file1\\n")
276 f1 = tc.workpath('sub', 'file1')
277 mode = os.stat(f1).st_mode
278 os.chmod(f1, mode | stat.S_IWUSR)
279 tc.must_be_writable(['sub', 'file1'])
282 run_env
.run(program
=sys
.executable
, stdin
=script
)
283 stdout
= run_env
.stdout()
284 assert stdout
== "", stdout
285 stderr
= run_env
.stderr()
286 assert stderr
== "PASSED\n", stderr
289 class must_contain_TestCase(TestCommonTestCase
):
290 def test_success(self
) -> None:
291 """Test must_contain(): success"""
292 run_env
= self
.run_env
295 from TestCommon import TestCommon
296 tc = TestCommon(workdir='')
297 tc.write('file1', "file1 contents\\n")
298 tc.must_contain('file1', "1 c")
301 run_env
.run(program
=sys
.executable
, stdin
=script
)
302 stdout
= run_env
.stdout()
303 assert stdout
== "", stdout
304 stderr
= run_env
.stderr()
305 assert stderr
== "PASSED\n", stderr
307 def test_success_index_0(self
) -> None:
308 """Test must_contain(): success at index 0"""
309 run_env
= self
.run_env
312 from TestCommon import TestCommon
313 tc = TestCommon(workdir='')
314 tc.write('file1', "file1 contents\\n")
315 tc.must_contain('file1', "file1 c")
318 run_env
.run(program
=sys
.executable
, stdin
=script
)
319 stdout
= run_env
.stdout()
320 assert stdout
== "", stdout
321 stderr
= run_env
.stderr()
322 assert stderr
== "PASSED\n", stderr
324 def test_file_missing(self
) -> None:
325 """Test must_contain(): file missing"""
326 run_env
= self
.run_env
329 from TestCommon import TestCommon
330 tc = TestCommon(workdir='')
331 tc.must_contain('file1', "1 c\\n")
334 run_env
.run(program
=sys
.executable
, stdin
=script
)
335 stdout
= run_env
.stdout()
336 assert stdout
== "", stdout
337 stderr
= run_env
.stderr()
338 assert stderr
.find("No such file or directory:") != -1, stderr
340 def test_failure(self
) -> None:
341 """Test must_contain(): failure"""
342 run_env
= self
.run_env
345 from TestCommon import TestCommon
346 tc = TestCommon(workdir='')
347 tc.write('file1', "file1 does not match\\n")
348 tc.must_contain('file1', "1 c")
352 File `file1' does not contain required string.
353 Required string ================================================================
355 file1 contents =================================================================
356 b'file1 does not match\\n'
358 run_env
.run(program
=sys
.executable
, stdin
=script
)
359 stdout
= run_env
.stdout()
360 assert stdout
== expect
, f
"got:\n{stdout}\nexpected:\n{expect}"
361 stderr
= run_env
.stderr()
362 assert stderr
.find("FAILED") != -1, stderr
364 def test_mode(self
) -> None:
365 """Test must_contain(): mode"""
366 run_env
= self
.run_env
369 from TestCommon import TestCommon
370 tc = TestCommon(workdir='')
371 tc.write('file1', "file1 contents\\n", mode='w')
372 tc.must_contain('file1', "1 c", mode='r')
373 tc.write('file2', "file2 contents\\n", mode='wb')
374 tc.must_contain('file2', "2 c", mode='rb')
377 run_env
.run(program
=sys
.executable
, stdin
=script
)
378 stdout
= run_env
.stdout()
379 assert stdout
== "", stdout
380 stderr
= run_env
.stderr()
381 assert stderr
== "PASSED\n", stderr
384 class must_contain_all_lines_TestCase(TestCommonTestCase
):
385 def test_success(self
) -> None:
386 """Test must_contain_all_lines(): success"""
387 run_env
= self
.run_env
391 test = TestCommon.TestCommon(workdir='')
405 test.must_contain_all_lines(output, lines)
407 test.must_contain_all_lines(output, ['www\\n'])
412 run_env
.run(program
=sys
.executable
, stdin
=script
)
413 stdout
= run_env
.stdout()
414 assert stdout
== "", stdout
415 stderr
= run_env
.stderr()
416 assert stderr
== "PASSED\n", stderr
418 def test_failure(self
) -> None:
419 """Test must_contain_all_lines(): failure"""
420 run_env
= self
.run_env
424 test = TestCommon.TestCommon(workdir='')
436 test.must_contain_all_lines(output, lines)
443 Missing expected lines from output:
444 'xxx%(expected_newline)s'
445 'yyy%(expected_newline)s'
446 output =========================================================================
453 run_env
.run(program
=sys
.executable
, stdin
=script
)
454 stdout
= run_env
.stdout()
455 stderr
= run_env
.stderr()
456 assert stdout
== expect
, assert_display(expect
, stdout
, stderr
)
457 assert stderr
.find("FAILED") != -1, stderr
459 def test_find(self
) -> None:
460 """Test must_contain_all_lines(): find"""
461 run_env
= self
.run_env
466 test = TestCommon.TestCommon(workdir='')
480 def re_search(output, line):
481 return re.compile(line, re.S).search(output)
483 test.must_contain_all_lines(output, lines, find=re_search)
488 run_env
.run(program
=sys
.executable
, stdin
=script
)
489 stdout
= run_env
.stdout()
490 assert stdout
== "", stdout
491 stderr
= run_env
.stderr()
492 assert stderr
== "PASSED\n", stderr
494 def test_title(self
) -> None:
495 """Test must_contain_all_lines(): title"""
496 run_env
= self
.run_env
500 test = TestCommon.TestCommon(workdir='')
512 test.must_contain_all_lines(output, lines, title='STDERR')
519 Missing expected lines from STDERR:
520 'xxx%(expected_newline)s'
521 'yyy%(expected_newline)s'
522 STDERR =========================================================================
529 run_env
.run(program
=sys
.executable
, stdin
=script
)
530 stdout
= run_env
.stdout()
531 stderr
= run_env
.stderr()
532 assert stdout
== expect
, assert_display(expect
, stdout
, stderr
)
533 assert stderr
.find("FAILED") != -1, stderr
536 class must_contain_any_line_TestCase(TestCommonTestCase
):
537 def test_success(self
) -> None:
538 """Test must_contain_any_line(): success"""
539 run_env
= self
.run_env
543 test = TestCommon.TestCommon(workdir='')
557 test.must_contain_any_line(output, lines)
559 test.must_contain_any_line(output, ['www\\n'])
564 run_env
.run(program
=sys
.executable
, stdin
=script
)
565 stdout
= run_env
.stdout()
566 assert stdout
== "", stdout
567 stderr
= run_env
.stderr()
568 assert stderr
== "PASSED\n", stderr
570 def test_failure(self
) -> None:
571 """Test must_contain_any_line(): failure"""
572 run_env
= self
.run_env
576 test = TestCommon.TestCommon(workdir='')
588 test.must_contain_any_line(output, lines)
595 Missing any expected line from output:
596 'xxx%(expected_newline)s'
597 'yyy%(expected_newline)s'
598 output =========================================================================
605 run_env
.run(program
=sys
.executable
, stdin
=script
)
606 stdout
= run_env
.stdout()
607 stderr
= run_env
.stderr()
608 assert stdout
== expect
, assert_display(expect
, stdout
, stderr
)
609 assert stderr
.find("FAILED") != -1, stderr
611 def test_find(self
) -> None:
612 """Test must_contain_any_line(): find"""
613 run_env
= self
.run_env
618 test = TestCommon.TestCommon(workdir='')
632 def re_search(output, line):
633 return re.compile(line, re.S).search(output)
634 test.must_contain_any_line(output, lines, find=re_search)
639 run_env
.run(program
=sys
.executable
, stdin
=script
)
640 stdout
= run_env
.stdout()
641 assert stdout
== "", stdout
642 stderr
= run_env
.stderr()
643 assert stderr
== "PASSED\n", stderr
645 def test_title(self
) -> None:
646 """Test must_contain_any_line(): title"""
647 run_env
= self
.run_env
651 test = TestCommon.TestCommon(workdir='')
663 test.must_contain_any_line(output, lines, title='STDOUT')
670 Missing any expected line from STDOUT:
671 'xxx%(expected_newline)s'
672 'yyy%(expected_newline)s'
673 STDOUT =========================================================================
680 run_env
.run(program
=sys
.executable
, stdin
=script
)
681 stdout
= run_env
.stdout()
682 stderr
= run_env
.stderr()
683 assert stdout
== expect
, assert_display(expect
, stdout
, stderr
)
684 assert stderr
.find("FAILED") != -1, stderr
687 class must_contain_exactly_lines_TestCase(TestCommonTestCase
):
688 def test_success_list(self
) -> None:
689 """Test must_contain_exactly_lines(): success (input list)"""
690 run_env
= self
.run_env
694 test = TestCommon.TestCommon(workdir='')
710 test.must_contain_exactly_lines(output, lines)
715 run_env
.run(program
=sys
.executable
, stdin
=script
)
716 stdout
= run_env
.stdout()
717 assert stdout
== "", stdout
718 stderr
= run_env
.stderr()
719 assert stderr
== "PASSED\n", stderr
721 def test_success_string(self
) -> None:
722 """Test must_contain_exactly_lines(): success (input string)"""
723 run_env
= self
.run_env
727 test = TestCommon.TestCommon(workdir='')
743 test.must_contain_exactly_lines(output, lines)
748 run_env
.run(program
=sys
.executable
, stdin
=script
)
749 stdout
= run_env
.stdout()
750 assert stdout
== "", stdout
751 stderr
= run_env
.stderr()
752 assert stderr
== "PASSED\n", stderr
754 def test_failure(self
) -> None:
755 """Test must_contain_exactly_lines(): failure"""
756 run_env
= self
.run_env
760 test = TestCommon.TestCommon(workdir='')
772 test.must_contain_exactly_lines(output, lines)
778 Missing expected lines from output:
781 Missing output =================================================================
782 Extra unexpected lines from output:
785 Extra output ===================================================================
788 run_env
.run(program
=sys
.executable
, stdin
=script
)
789 stdout
= run_env
.stdout()
790 stderr
= run_env
.stderr()
791 assert stdout
== expect
, assert_display(expect
, stdout
, stderr
)
792 assert stderr
.find("FAILED") != -1, stderr
794 def test_find(self
) -> None:
795 """Test must_contain_exactly_lines(): find"""
796 run_env
= self
.run_env
801 test = TestCommon.TestCommon(workdir='')
817 def re_search(output, line):
818 pattern = re.compile(line, re.S)
821 if pattern.search(o):
825 test.must_contain_exactly_lines(output, lines, find=re_search)
830 run_env
.run(program
=sys
.executable
, stdin
=script
)
831 stdout
= run_env
.stdout()
832 assert stdout
== "", stdout
833 stderr
= run_env
.stderr()
834 # Somehow, this fails on Py 3.12+ with:
835 # AssertionError: <stdin>:13: SyntaxWarning: invalid escape sequence '\ '
836 # assert stderr == "PASSED\n", stderr
837 # So, just look for a substring:
838 self
.assertIn("PASSED", stderr
)
840 def test_title(self
) -> None:
841 """Test must_contain_exactly_lines(): title"""
842 run_env
= self
.run_env
846 test = TestCommon.TestCommon(workdir='')
858 test.must_contain_exactly_lines(output, lines, title='STDOUT')
864 Missing expected lines from STDOUT:
867 Missing STDOUT =================================================================
868 Extra unexpected lines from STDOUT:
871 Extra STDOUT ===================================================================
874 run_env
.run(program
=sys
.executable
, stdin
=script
)
875 stdout
= run_env
.stdout()
876 stderr
= run_env
.stderr()
877 assert stdout
== expect
, assert_display(expect
, stdout
, stderr
)
878 assert stderr
.find("FAILED") != -1, stderr
881 class must_contain_lines_TestCase(TestCommonTestCase
):
882 def test_success(self
) -> None:
883 """Test must_contain_lines(): success"""
884 run_env
= self
.run_env
888 test = TestCommon.TestCommon(workdir='')
902 test.must_contain_lines(lines, output)
907 run_env
.run(program
=sys
.executable
, stdin
=script
)
908 stdout
= run_env
.stdout()
909 assert stdout
== "", stdout
910 stderr
= run_env
.stderr()
911 assert stderr
== "PASSED\n", stderr
913 def test_failure(self
) -> None:
914 """Test must_contain_lines(): failure"""
915 run_env
= self
.run_env
919 test = TestCommon.TestCommon(workdir='')
931 test.must_contain_lines(lines, output)
938 Missing expected lines from output:
939 'xxx%(expected_newline)s'
940 'yyy%(expected_newline)s'
941 output =========================================================================
948 run_env
.run(program
=sys
.executable
, stdin
=script
)
949 stdout
= run_env
.stdout()
950 stderr
= run_env
.stderr()
951 assert stdout
== expect
, assert_display(expect
, stdout
, stderr
)
952 assert stderr
.find("FAILED") != -1, stderr
955 class must_exist_TestCase(TestCommonTestCase
):
956 def test_success(self
) -> None:
957 """Test must_exist(): success"""
958 run_env
= self
.run_env
961 from TestCommon import TestCommon
962 tc = TestCommon(workdir='')
963 tc.write('file1', "file1\\n")
964 tc.must_exist('file1')
967 run_env
.run(program
=sys
.executable
, stdin
=script
)
968 stdout
= run_env
.stdout()
969 assert stdout
== "", stdout
970 stderr
= run_env
.stderr()
971 assert stderr
== "PASSED\n", stderr
973 def test_failure(self
) -> None:
974 """Test must_exist(): failure"""
975 run_env
= self
.run_env
978 from TestCommon import TestCommon
979 tc = TestCommon(workdir='')
980 tc.must_exist('file1')
983 run_env
.run(program
=sys
.executable
, stdin
=script
)
984 stdout
= run_env
.stdout()
985 assert stdout
== "Missing files: `file1'\n", stdout
986 stderr
= run_env
.stderr()
987 assert stderr
.find("FAILED") != -1, stderr
989 def test_failure_message(self
) -> None:
990 """Test must_exist(): failure with extra message"""
991 run_env
= self
.run_env
994 from TestCommon import TestCommon
995 tc = TestCommon(workdir='')
996 tc.must_exist('file1', message="Extra Info")
999 run_env
.run(program
=sys
.executable
, stdin
=script
)
1000 stdout
= run_env
.stdout()
1001 assert stdout
== "Missing files: `file1'\n", stdout
1002 stderr
= run_env
.stderr()
1003 assert stderr
.find("Extra Info") != -1, stderr
1005 def test_file_specified_as_list(self
) -> None:
1006 """Test must_exist(): file specified as list"""
1007 run_env
= self
.run_env
1009 script
= lstrip("""\
1010 from TestCommon import TestCommon
1011 tc = TestCommon(workdir='')
1013 tc.write(['sub', 'file1'], "sub/file1\\n")
1014 tc.must_exist(['sub', 'file1'])
1017 run_env
.run(program
=sys
.executable
, stdin
=script
)
1018 stdout
= run_env
.stdout()
1019 assert stdout
== "", stdout
1020 stderr
= run_env
.stderr()
1021 assert stderr
== "PASSED\n", stderr
1023 @unittest.skipIf(sys
.platform
== 'win32', "Skip symlink test on win32")
1024 def test_broken_link(self
) -> None:
1025 """Test must_exist(): exists but it is a broken link"""
1026 run_env
= self
.run_env
1028 script
= lstrip("""\
1029 from TestCommon import TestCommon
1030 tc = TestCommon(workdir='')
1031 tc.symlink('badtarget', "brokenlink")
1032 tc.must_exist('brokenlink')
1035 run_env
.run(program
=sys
.executable
, stdin
=script
)
1036 stdout
= run_env
.stdout()
1037 assert stdout
== "", stdout
1038 stderr
= run_env
.stderr()
1039 assert stderr
== "PASSED\n", stderr
1042 class must_exist_one_of_TestCase(TestCommonTestCase
):
1043 def test_success(self
) -> None:
1044 """Test must_exist_one_of(): success"""
1045 run_env
= self
.run_env
1047 script
= lstrip("""\
1048 from TestCommon import TestCommon
1049 tc = TestCommon(workdir='')
1050 tc.write('file1', "file1\\n")
1051 tc.must_exist_one_of(['file1'])
1054 run_env
.run(program
=sys
.executable
, stdin
=script
)
1055 stdout
= run_env
.stdout()
1056 assert stdout
== "", stdout
1057 stderr
= run_env
.stderr()
1058 assert stderr
== "PASSED\n", stderr
1060 def test_failure(self
) -> None:
1061 """Test must_exist_one_of(): failure"""
1062 run_env
= self
.run_env
1064 script
= lstrip("""\
1065 from TestCommon import TestCommon
1066 tc = TestCommon(workdir='')
1067 tc.must_exist_one_of(['file1'])
1070 run_env
.run(program
=sys
.executable
, stdin
=script
)
1071 stdout
= run_env
.stdout()
1072 assert stdout
== "Missing one of: `file1'\n", stdout
1073 stderr
= run_env
.stderr()
1074 assert stderr
.find("FAILED") != -1, stderr
1076 def test_failure_message(self
) -> None:
1077 """Test must_exist_one_of(): failure with extra message"""
1078 run_env
= self
.run_env
1080 script
= lstrip("""\
1081 from TestCommon import TestCommon
1082 tc = TestCommon(workdir='')
1083 tc.must_exist_one_of(['file1'], message="Extra Info")
1086 run_env
.run(program
=sys
.executable
, stdin
=script
)
1087 stdout
= run_env
.stdout()
1088 assert stdout
== "Missing one of: `file1'\n", stdout
1089 stderr
= run_env
.stderr()
1090 assert stderr
.find("Extra Info") != -1, stderr
1092 def test_files_specified_as_list(self
) -> None:
1093 """Test must_exist_one_of(): files specified as list"""
1094 run_env
= self
.run_env
1096 script
= lstrip("""\
1097 from TestCommon import TestCommon
1098 tc = TestCommon(workdir='')
1099 tc.write('file1', "file1\\n")
1100 tc.must_exist_one_of(['file2', 'file1'])
1103 run_env
.run(program
=sys
.executable
, stdin
=script
)
1104 stdout
= run_env
.stdout()
1105 assert stdout
== "", stdout
1106 stderr
= run_env
.stderr()
1107 assert stderr
== "PASSED\n", stderr
1109 def test_files_specified_with_wildcards(self
) -> None:
1110 """Test must_exist_one_of(): files specified with wildcards"""
1111 run_env
= self
.run_env
1113 script
= lstrip("""\
1114 from TestCommon import TestCommon
1115 tc = TestCommon(workdir='')
1116 tc.write('file7', "file7\\n")
1117 tc.must_exist_one_of(['file?'])
1120 run_env
.run(program
=sys
.executable
, stdin
=script
)
1121 stdout
= run_env
.stdout()
1122 assert stdout
== "", stdout
1123 stderr
= run_env
.stderr()
1124 assert stderr
== "PASSED\n", stderr
1126 def test_file_given_as_list(self
) -> None:
1127 """Test must_exist_one_of(): file given as list"""
1128 run_env
= self
.run_env
1130 script
= lstrip("""\
1131 from TestCommon import TestCommon
1132 tc = TestCommon(workdir='')
1134 tc.write(['sub', 'file1'], "sub/file1\\n")
1135 tc.must_exist_one_of(['file2', ['sub', 'file1']])
1138 run_env
.run(program
=sys
.executable
, stdin
=script
)
1139 stdout
= run_env
.stdout()
1140 assert stdout
== "", stdout
1141 stderr
= run_env
.stderr()
1142 assert stderr
== "PASSED\n", stderr
1144 def test_file_given_as_sequence(self
) -> None:
1145 """Test must_exist_one_of(): file given as sequence"""
1146 run_env
= self
.run_env
1148 script
= lstrip("""\
1149 from TestCommon import TestCommon
1150 tc = TestCommon(workdir='')
1152 tc.write(['sub', 'file1'], "sub/file1\\n")
1153 tc.must_exist_one_of(['file2', ('sub', 'file1')])
1156 run_env
.run(program
=sys
.executable
, stdin
=script
)
1157 stdout
= run_env
.stdout()
1158 assert stdout
== "", stdout
1159 stderr
= run_env
.stderr()
1160 assert stderr
== "PASSED\n", stderr
1163 class must_match_TestCase(TestCommonTestCase
):
1164 def test_success(self
) -> None:
1165 """Test must_match(): success"""
1166 run_env
= self
.run_env
1168 script
= lstrip("""\
1169 from TestCommon import TestCommon
1170 tc = TestCommon(workdir='')
1171 tc.write('file1', "file1\\n")
1172 tc.must_match('file1', "file1\\n")
1175 run_env
.run(program
=sys
.executable
, stdin
=script
)
1176 stdout
= run_env
.stdout()
1177 assert stdout
== "", stdout
1178 stderr
= run_env
.stderr()
1179 assert stderr
== "PASSED\n", stderr
1181 def test_file_does_not_exists(self
) -> None:
1182 """Test must_match(): file does not exist"""
1183 run_env
= self
.run_env
1185 script
= lstrip("""\
1186 from TestCommon import TestCommon
1187 tc = TestCommon(workdir='')
1188 tc.must_match('file1', "file1\\n")
1191 run_env
.run(program
=sys
.executable
, stdin
=script
)
1192 stdout
= run_env
.stdout()
1193 assert stdout
== "", stdout
1194 stderr
= run_env
.stderr()
1195 assert stderr
.find("No such file or directory:") != -1, stderr
1197 def test_failure(self
) -> None:
1198 """Test must_match(): failure"""
1199 run_env
= self
.run_env
1201 script
= lstrip("""\
1202 from TestCommon import TestCommon
1203 tc = TestCommon(workdir='')
1204 tc.write('file1', "file1 does not match\\n")
1205 tc.must_match('file1', "file1\\n")
1209 expect
= lstrip("""\
1210 match_re: mismatch at line 0:
1212 line='file1 does not match'
1213 Unexpected contents of `file1'
1214 contents =======================================================================
1218 > file1 does not match
1220 run_env
.run(program
=sys
.executable
, stdin
=script
)
1221 stdout
= run_env
.stdout()
1222 assert stdout
== expect
, stdout
1223 stderr
= run_env
.stderr()
1224 assert stderr
.find("FAILED") != -1, stderr
1226 def test_mode(self
) -> None:
1227 """Test must_match(): mode"""
1228 run_env
= self
.run_env
1230 script
= lstrip("""\
1231 from TestCommon import TestCommon
1232 tc = TestCommon(workdir='')
1233 tc.write('file1', "file1\\n", mode='w')
1234 tc.must_match('file1', "file1\\n", mode='r')
1235 tc.write('file2', "file2\\n", mode='wb')
1236 tc.must_match('file2', "file2\\n", mode='rb')
1239 run_env
.run(program
=sys
.executable
, stdin
=script
)
1240 stdout
= run_env
.stdout()
1241 assert stdout
== "", stdout
1242 stderr
= run_env
.stderr()
1243 assert stderr
== "PASSED\n", stderr
1246 class must_not_be_writable_TestCase(TestCommonTestCase
):
1247 def test_file_does_not_exists(self
) -> None:
1248 """Test must_not_be_writable(): file does not exist"""
1249 run_env
= self
.run_env
1251 script
= lstrip("""\
1252 from TestCommon import TestCommon
1253 tc = TestCommon(workdir='')
1254 tc.must_not_be_writable('file1')
1257 run_env
.run(program
=sys
.executable
, stdin
=script
)
1258 stdout
= run_env
.stdout()
1259 assert stdout
== "Missing files: `file1'\n", stdout
1260 stderr
= run_env
.stderr()
1261 assert stderr
.find("FAILED") != -1, stderr
1263 def test_writable_file_exists(self
) -> None:
1264 """Test must_not_be_writable(): writable file exists"""
1265 run_env
= self
.run_env
1267 script
= lstrip("""\
1270 from TestCommon import TestCommon
1271 tc = TestCommon(workdir='')
1272 tc.write('file1', "file1\\n")
1273 f1 = tc.workpath('file1')
1274 mode = os.stat(f1).st_mode
1275 os.chmod(f1, mode | stat.S_IWUSR)
1276 tc.must_not_be_writable('file1')
1279 run_env
.run(program
=sys
.executable
, stdin
=script
)
1280 stdout
= run_env
.stdout()
1281 assert stdout
== "Writable files: `file1'\n", stdout
1282 stderr
= run_env
.stderr()
1283 assert stderr
.find("FAILED") != -1, stderr
1285 def test_non_writable_file_exists(self
) -> None:
1286 """Test must_not_be_writable(): non-writable file exists"""
1287 run_env
= self
.run_env
1289 script
= lstrip("""\
1292 from TestCommon import TestCommon
1293 tc = TestCommon(workdir='')
1294 tc.write('file1', "file1\\n")
1295 f1 = tc.workpath('file1')
1296 mode = os.stat(f1).st_mode
1297 os.chmod(f1, mode & ~stat.S_IWUSR)
1298 tc.must_not_be_writable('file1')
1301 run_env
.run(program
=sys
.executable
, stdin
=script
)
1302 stdout
= run_env
.stdout()
1303 assert stdout
== "", stdout
1304 stderr
= run_env
.stderr()
1305 assert stderr
== "PASSED\n", stderr
1307 def test_file_specified_as_list(self
) -> None:
1308 """Test must_not_be_writable(): file specified as list"""
1309 run_env
= self
.run_env
1311 script
= lstrip("""\
1314 from TestCommon import TestCommon
1315 tc = TestCommon(workdir='')
1317 tc.write(['sub', 'file1'], "sub/file1\\n")
1318 f1 = tc.workpath('sub', 'file1')
1319 mode = os.stat(f1).st_mode
1320 os.chmod(f1, mode & ~stat.S_IWUSR)
1321 tc.must_not_be_writable(['sub', 'file1'])
1324 run_env
.run(program
=sys
.executable
, stdin
=script
)
1325 stdout
= run_env
.stdout()
1326 assert stdout
== "", stdout
1327 stderr
= run_env
.stderr()
1328 assert stderr
== "PASSED\n", stderr
1331 class must_not_contain_TestCase(TestCommonTestCase
):
1332 def test_success(self
) -> None:
1333 """Test must_not_contain(): success"""
1334 run_env
= self
.run_env
1336 script
= lstrip("""\
1337 from TestCommon import TestCommon
1338 tc = TestCommon(workdir='')
1339 tc.write('file1', "file1 contents\\n")
1340 tc.must_not_contain('file1', b"1 does not contain c")
1343 run_env
.run(program
=sys
.executable
, stdin
=script
)
1344 stdout
= run_env
.stdout()
1345 assert stdout
== "", stdout
1346 stderr
= run_env
.stderr()
1347 assert stderr
== "PASSED\n", stderr
1349 def test_file_does_not_exist(self
) -> None:
1350 """Test must_not_contain(): file does not exist"""
1351 run_env
= self
.run_env
1353 script
= lstrip("""\
1354 from TestCommon import TestCommon
1355 tc = TestCommon(workdir='')
1356 tc.must_not_contain('file1', "1 c\\n")
1359 run_env
.run(program
=sys
.executable
, stdin
=script
)
1360 stdout
= run_env
.stdout()
1361 assert stdout
== "", stdout
1362 stderr
= run_env
.stderr()
1363 assert stderr
.find("No such file or directory:") != -1, stderr
1365 def test_failure(self
) -> None:
1366 """Test must_not_contain(): failure"""
1367 run_env
= self
.run_env
1369 script
= lstrip("""\
1370 from TestCommon import TestCommon
1371 tc = TestCommon(workdir='')
1372 tc.write('file1', "file1 does contain contents\\n")
1373 tc.must_not_contain('file1', b"1 does contain c")
1376 expect
= lstrip("""\
1377 File `file1' contains banned string.
1378 Banned string ==================================================================
1380 file1 contents =================================================================
1381 b'file1 does contain contents\\n'
1383 run_env
.run(program
=sys
.executable
, stdin
=script
)
1384 stdout
= run_env
.stdout()
1385 assert stdout
== expect
, f
"\ngot:\n{stdout}\nexpected:\n{expect}"
1387 stderr
= run_env
.stderr()
1388 assert stderr
.find("FAILED") != -1, stderr
1390 def test_failure_index_0(self
) -> None:
1391 """Test must_not_contain(): failure at index 0"""
1392 run_env
= self
.run_env
1394 script
= lstrip("""\
1395 from TestCommon import TestCommon
1396 tc = TestCommon(workdir='')
1397 tc.write('file1', "file1 does contain contents\\n")
1398 tc.must_not_contain('file1', b"file1 does")
1401 expect
= lstrip("""\
1402 File `file1' contains banned string.
1403 Banned string ==================================================================
1405 file1 contents =================================================================
1406 b'file1 does contain contents\\n'
1408 run_env
.run(program
=sys
.executable
, stdin
=script
)
1409 stdout
= run_env
.stdout()
1410 assert stdout
== expect
, f
"\ngot:\n{stdout}\nexpected:\n{expect}"
1412 stderr
= run_env
.stderr()
1413 assert stderr
.find("FAILED") != -1, stderr
1415 def test_mode(self
) -> None:
1416 """Test must_not_contain(): mode"""
1417 run_env
= self
.run_env
1419 script
= lstrip("""\
1420 from TestCommon import TestCommon
1421 tc = TestCommon(workdir='')
1422 tc.write('file1', "file1 contents\\n", mode='w')
1423 tc.must_not_contain('file1', "1 does not contain c", mode='r')
1424 tc.write('file2', "file2 contents\\n", mode='wb')
1425 tc.must_not_contain('file2', b"2 does not contain c", mode='rb')
1428 run_env
.run(program
=sys
.executable
, stdin
=script
)
1429 stdout
= run_env
.stdout()
1430 assert stdout
== "", stdout
1431 stderr
= run_env
.stderr()
1432 assert stderr
== "PASSED\n", stderr
1435 class must_not_contain_any_line_TestCase(TestCommonTestCase
):
1436 def test_failure(self
) -> None:
1437 """Test must_not_contain_any_line(): failure"""
1438 run_env
= self
.run_env
1442 test = TestCommon.TestCommon(workdir='')
1457 test.must_not_contain_any_line(output, lines)
1464 Unexpected lines in output:
1465 'xxx%(expected_newline)s'
1466 'yyy%(expected_newline)s'
1467 'www%(expected_newline)s'
1468 output =========================================================================
1477 run_env
.run(program
=sys
.executable
, stdin
=script
)
1478 stdout
= run_env
.stdout()
1479 stderr
= run_env
.stderr()
1480 assert stdout
== expect
, assert_display(expect
, stdout
, stderr
)
1481 assert stderr
.find("FAILED") != -1, stderr
1483 def test_find(self
) -> None:
1484 """Test must_not_contain_any_line(): find"""
1485 run_env
= self
.run_env
1490 test = TestCommon.TestCommon(workdir='')
1502 def re_search(output, line):
1503 return re.compile(line, re.S).search(output)
1504 test.must_not_contain_any_line(output, lines, find=re_search)
1509 run_env
.run(program
=sys
.executable
, stdin
=script
)
1510 stdout
= run_env
.stdout()
1511 assert stdout
== "", stdout
1512 stderr
= run_env
.stderr()
1513 assert stderr
== "PASSED\n", stderr
1515 def test_success(self
) -> None:
1516 """Test must_not_contain_any_line(): success"""
1517 run_env
= self
.run_env
1521 test = TestCommon.TestCommon(workdir='')
1533 test.must_not_contain_any_line(output, lines)
1538 run_env
.run(program
=sys
.executable
, stdin
=script
)
1539 stdout
= run_env
.stdout()
1540 assert stdout
== "", stdout
1541 stderr
= run_env
.stderr()
1542 assert stderr
== "PASSED\n", stderr
1544 def test_title(self
) -> None:
1545 """Test must_not_contain_any_line(): title"""
1546 run_env
= self
.run_env
1550 test = TestCommon.TestCommon(workdir='')
1564 test.must_not_contain_any_line(output, lines, title='XYZZY')
1571 Unexpected lines in XYZZY:
1572 'xxx%(expected_newline)s'
1573 'yyy%(expected_newline)s'
1574 XYZZY ==========================================================================
1583 run_env
.run(program
=sys
.executable
, stdin
=script
)
1584 stdout
= run_env
.stdout()
1585 stderr
= run_env
.stderr()
1586 assert stdout
== expect
, assert_display(expect
, stdout
, stderr
)
1587 assert stderr
.find("FAILED") != -1, stderr
1590 class must_not_contain_lines_TestCase(TestCommonTestCase
):
1591 def test_failure(self
) -> None:
1592 """Test must_not_contain_lines(): failure"""
1593 run_env
= self
.run_env
1597 test = TestCommon.TestCommon(workdir='')
1611 test.must_not_contain_lines(lines, output)
1618 Unexpected lines in output:
1619 'xxx%(expected_newline)s'
1620 'yyy%(expected_newline)s'
1621 output =========================================================================
1630 run_env
.run(program
=sys
.executable
, stdin
=script
)
1631 stdout
= run_env
.stdout()
1632 stderr
= run_env
.stderr()
1633 assert stdout
== expect
, assert_display(expect
, stdout
, stderr
)
1634 assert stderr
.find("FAILED") != -1, stderr
1636 def test_success(self
) -> None:
1637 """Test must_not_contain_lines(): success"""
1638 run_env
= self
.run_env
1642 test = TestCommon.TestCommon(workdir='')
1654 test.must_not_contain_lines(lines, output)
1659 run_env
.run(program
=sys
.executable
, stdin
=script
)
1660 stdout
= run_env
.stdout()
1661 assert stdout
== "", stdout
1662 stderr
= run_env
.stderr()
1663 assert stderr
== "PASSED\n", stderr
1666 class must_not_exist_TestCase(TestCommonTestCase
):
1667 def test_failure(self
) -> None:
1668 """Test must_not_exist(): failure"""
1669 run_env
= self
.run_env
1671 script
= lstrip("""\
1672 from TestCommon import TestCommon
1673 tc = TestCommon(workdir='')
1674 tc.write('file1', "file1\\n")
1675 tc.must_not_exist('file1')
1678 run_env
.run(program
=sys
.executable
, stdin
=script
)
1679 stdout
= run_env
.stdout()
1680 assert stdout
== "Unexpected files exist: `file1'\n", stdout
1681 stderr
= run_env
.stderr()
1682 assert stderr
.find("FAILED") != -1, stderr
1684 def test_success(self
) -> None:
1685 """Test must_not_exist(): success"""
1686 run_env
= self
.run_env
1688 script
= lstrip("""\
1689 from TestCommon import TestCommon
1690 tc = TestCommon(workdir='')
1691 tc.must_not_exist('file1')
1694 run_env
.run(program
=sys
.executable
, stdin
=script
)
1695 stdout
= run_env
.stdout()
1696 assert stdout
== "", stdout
1697 stderr
= run_env
.stderr()
1698 assert stderr
== "PASSED\n", stderr
1700 def test_file_specified_as_list(self
) -> None:
1701 """Test must_not_exist(): file specified as list"""
1702 run_env
= self
.run_env
1704 script
= lstrip("""\
1705 from TestCommon import TestCommon
1706 tc = TestCommon(workdir='')
1708 tc.must_not_exist(['sub', 'file1'])
1711 run_env
.run(program
=sys
.executable
, stdin
=script
)
1712 stdout
= run_env
.stdout()
1713 assert stdout
== "", stdout
1714 stderr
= run_env
.stderr()
1715 assert stderr
== "PASSED\n", stderr
1717 @unittest.skipIf(sys
.platform
== 'win32', "Skip symlink test on win32")
1718 def test_existing_broken_link(self
) -> None:
1719 """Test must_not_exist(): exists but it is a broken link"""
1720 run_env
= self
.run_env
1722 script
= lstrip("""\
1723 from TestCommon import TestCommon
1724 tc = TestCommon(workdir='')
1725 tc.symlink('badtarget', 'brokenlink')
1726 tc.must_not_exist('brokenlink')
1729 run_env
.run(program
=sys
.executable
, stdin
=script
)
1730 stdout
= run_env
.stdout()
1731 assert stdout
== "Unexpected files exist: `brokenlink'\n", stdout
1732 stderr
= run_env
.stderr()
1733 assert stderr
.find("FAILED") != -1, stderr
1736 class must_not_exist_any_of_TestCase(TestCommonTestCase
):
1737 def test_success(self
) -> None:
1738 """Test must_not_exist_any_of(): success"""
1739 run_env
= self
.run_env
1741 script
= lstrip("""\
1742 from TestCommon import TestCommon
1743 tc = TestCommon(workdir='')
1744 tc.must_not_exist_any_of(['file1'])
1747 run_env
.run(program
=sys
.executable
, stdin
=script
)
1748 stdout
= run_env
.stdout()
1749 assert stdout
== "", stdout
1750 stderr
= run_env
.stderr()
1751 assert stderr
== "PASSED\n", stderr
1753 def test_failure(self
) -> None:
1754 """Test must_not_exist_any_of(): failure"""
1755 run_env
= self
.run_env
1757 script
= lstrip("""\
1758 from TestCommon import TestCommon
1759 tc = TestCommon(workdir='')
1760 tc.write('file1', "file1\\n")
1761 tc.must_not_exist_any_of(['file1'])
1764 run_env
.run(program
=sys
.executable
, stdin
=script
)
1765 stdout
= run_env
.stdout()
1766 assert stdout
== "Unexpected files exist: `file1'\n", stdout
1767 stderr
= run_env
.stderr()
1768 assert stderr
.find("FAILED") != -1, stderr
1770 def test_files_specified_as_list(self
) -> None:
1771 """Test must_not_exist_any_of(): files specified as list"""
1772 run_env
= self
.run_env
1774 script
= lstrip("""\
1775 from TestCommon import TestCommon
1776 tc = TestCommon(workdir='')
1777 tc.must_not_exist_any_of(['file2', 'file1'])
1780 run_env
.run(program
=sys
.executable
, stdin
=script
)
1781 stdout
= run_env
.stdout()
1782 assert stdout
== "", stdout
1783 stderr
= run_env
.stderr()
1784 assert stderr
== "PASSED\n", stderr
1786 def test_files_specified_with_wildcards(self
) -> None:
1787 """Test must_not_exist_any_of(): files specified with wildcards"""
1788 run_env
= self
.run_env
1790 script
= lstrip("""\
1791 from TestCommon import TestCommon
1792 tc = TestCommon(workdir='')
1793 tc.write('file7', "file7\\n")
1794 tc.must_not_exist_any_of(['files?'])
1797 run_env
.run(program
=sys
.executable
, stdin
=script
)
1798 stdout
= run_env
.stdout()
1799 assert stdout
== "", stdout
1800 stderr
= run_env
.stderr()
1801 assert stderr
== "PASSED\n", stderr
1803 def test_file_given_as_list(self
) -> None:
1804 """Test must_not_exist_any_of(): file given as list"""
1805 run_env
= self
.run_env
1807 script
= lstrip("""\
1808 from TestCommon import TestCommon
1809 tc = TestCommon(workdir='')
1811 tc.write(['sub', 'file1'], "sub/file1\\n")
1812 tc.must_not_exist_any_of(['file2',
1816 run_env
.run(program
=sys
.executable
, stdin
=script
)
1817 stdout
= run_env
.stdout()
1818 assert stdout
== "", stdout
1819 stderr
= run_env
.stderr()
1820 assert stderr
== "PASSED\n", stderr
1822 def test_file_given_as_sequence(self
) -> None:
1823 """Test must_not_exist_any_of(): file given as sequence"""
1824 run_env
= self
.run_env
1826 script
= lstrip("""\
1827 from TestCommon import TestCommon
1828 tc = TestCommon(workdir='')
1830 tc.write(['sub', 'file1'], "sub/file1\\n")
1831 tc.must_not_exist_any_of(['file2',
1835 run_env
.run(program
=sys
.executable
, stdin
=script
)
1836 stdout
= run_env
.stdout()
1837 assert stdout
== "", stdout
1838 stderr
= run_env
.stderr()
1839 assert stderr
== "PASSED\n", stderr
1842 class must_not_be_empty_TestCase(TestCommonTestCase
):
1843 def test_failure(self
) -> None:
1844 """Test must_not_be_empty(): failure"""
1845 run_env
= self
.run_env
1847 script
= lstrip("""\
1848 from TestCommon import TestCommon
1849 tc = TestCommon(workdir='')
1850 tc.write('file1', "")
1851 tc.must_not_be_empty('file1')
1854 run_env
.run(program
=sys
.executable
, stdin
=script
)
1855 stdout
= run_env
.stdout()
1856 assert stdout
== "File is empty: `file1'\n", stdout
1857 stderr
= run_env
.stderr()
1858 assert stderr
.find("FAILED") != -1, stderr
1860 def test_success(self
) -> None:
1861 """Test must_not_be_empty(): success"""
1862 run_env
= self
.run_env
1864 script
= lstrip("""\
1865 from TestCommon import TestCommon
1866 tc = TestCommon(workdir='')
1867 tc.write('file1', "file1\\n")
1868 tc.must_not_be_empty('file1')
1871 run_env
.run(program
=sys
.executable
, stdin
=script
)
1872 stdout
= run_env
.stdout()
1873 assert stdout
== "", stdout
1874 stderr
= run_env
.stderr()
1875 assert stderr
== "PASSED\n", stderr
1877 def test_file_doesnt_exist(self
) -> None:
1878 """Test must_not_be_empty(): failure"""
1879 run_env
= self
.run_env
1881 script
= lstrip("""\
1882 from TestCommon import TestCommon
1883 tc = TestCommon(workdir='')
1884 tc.must_not_be_empty('file1')
1887 run_env
.run(program
=sys
.executable
, stdin
=script
)
1888 stdout
= run_env
.stdout()
1889 assert stdout
== "File doesn't exist: `file1'\n", stdout
1890 stderr
= run_env
.stderr()
1891 assert stderr
.find("FAILED") != -1, stderr
1894 class run_TestCase(TestCommonTestCase
):
1895 def test_argument_handling(self
) -> None:
1896 """Test run(): argument handling"""
1898 script
= lstrip("""\
1899 from TestCommon import TestCommon, match_exact
1900 tc = TestCommon(program=r'%(pass_script)s',
1901 interpreter=r'%(python)s',
1904 tc.run(arguments = "arg1 arg2 arg3",
1905 stdout = r"%(pass_script)s: STDOUT: ['arg1', 'arg2', 'arg3']" + "\\n")
1908 self
.run_execution_test(script
, "", "")
1910 def test_default_pass(self
) -> None:
1911 """Test run(): default arguments, script passes"""
1913 script
= lstrip("""\
1914 from TestCommon import TestCommon
1915 tc = TestCommon(program=r'%(pass_script)s',
1916 interpreter=r'%(python)s',
1921 self
.run_execution_test(script
, "", "")
1923 def test_default_fail(self
) -> None:
1924 """Test run(): default arguments, script fails"""
1926 script
= lstrip("""\
1927 from TestCommon import TestCommon
1928 tc = TestCommon(program=r'%(fail_script)s',
1929 interpreter=r'%(python)s',
1934 expect_stdout
= lstrip("""\
1935 %(fail_script)s returned 1
1936 STDOUT =========================================================================
1937 %(fail_script)s: STDOUT: []
1939 STDERR =========================================================================
1943 expect_stderr
= lstrip("""\
1944 FAILED test of .*fail
1945 \\tat line \\d+ of .*TestCommon\\.py \\(_complete\\)
1946 \\tfrom line \\d+ of .*TestCommon\\.py \\(run\\)
1947 \\tfrom line \\d+ of <stdin>( \\(<module>\\))?
1949 expect_stderr
= re
.compile(expect_stderr
, re
.M
)
1951 self
.run_execution_test(script
, expect_stdout
, expect_stderr
)
1953 def test_default_stderr(self
) -> None:
1954 """Test run(): default arguments, error output"""
1955 script
= lstrip("""\
1956 from TestCommon import TestCommon
1957 tc = TestCommon(program=r'%(stderr_script)s',
1958 interpreter=r'%(python)s',
1963 expect_stdout
= lstrip("""\
1964 match_re: expected 1 lines, found 2
1965 STDOUT =========================================================================
1967 STDERR =========================================================================
1969 > %(stderr_script)s: STDERR: []
1972 expect_stderr
= lstrip("""\
1973 FAILED test of .*stderr
1974 \\tat line \\d+ of .*TestCommon\\.py \\(_complete\\)
1975 \\tfrom line \\d+ of .*TestCommon\\.py \\(run\\)
1976 \\tfrom line \\d+ of <stdin>
1978 expect_stderr
= re
.compile(expect_stderr
, re
.M
)
1980 self
.run_execution_test(script
, expect_stdout
, expect_stderr
)
1982 def test_exception_handling(self
) -> None:
1983 """Test run(): exception handling"""
1984 script
= lstrip("""\
1986 from TestCommon import TestCommon
1987 def raise_exception(*args, **kw):
1988 raise TypeError("forced TypeError")
1989 TestCmd.TestCmd.start = raise_exception
1990 tc = TestCommon(program=r'%(pass_script)s',
1991 interpreter=r'%(python)s',
1996 expect_stdout
= lstrip("""\
1997 STDOUT =========================================================================
1999 STDERR =========================================================================
2003 expect_stderr
= lstrip(
2004 rf
"""Exception trying to execute: \[{re.escape(repr(sys.executable))}, '[^']*pass'\]
2005 Traceback \(most recent call last\):
2006 File "<stdin>", line \d+, in (\?|<module>)
2007 File "[^"]+TestCommon\.py", line \d+, in run
2008 super\(\)\.run\(\*\*kw\)
2009 File "[^"]+TestCmd\.py", line \d+, in run
2011 (?:\s*\^*\s)? File \"[^\"]+TestCommon\.py\", line \d+, in start
2013 File "[^"]+TestCommon\.py", line \d+, in start
2014 return super\(\)\.start\(
2015 (?:\s*\^*\s)? File \"<stdin>\", line \d+, in raise_exception
2016 TypeError: forced TypeError
2020 # Python 3.13+ expanded error msgs again, not in a way we can easily
2021 # accomodate with the other regex.
2022 # TODO: broken again after reformat work
2023 expect_enhanced_stderr
= lstrip(
2024 rf
"""Exception trying to execute: \[{re.escape(repr(sys.executable))}, '[^']*pass'\]
2025 Traceback \(most recent call last\):
2026 File "<stdin>", line \d+, in (\?|<module>)
2027 File "[^"]+TestCommon\.py", line \d+, in run
2028 super\(\)\.run\(\*\*kw\)
2029 (?:\s*[~\^]*\s*)?File "[^"]+TestCmd\.py", line \d+, in run
2032 \.\.\.<4 lines>\.\.\.
2035 (?:\s*[~\^]*\s*)?File \"[^\"]+TestCommon\.py\", line \d+, in start
2037 File "[^"]+TestCommon\.py", line \d+, in start
2038 return super\(\)\.start\(
2039 (?:\s*[~\^]*\s*)?program, interpreter, arguments, universal_newlines, \*\*kw
2041 (?:\s*[~\^]*\s*)?File \"<stdin>\", line \d+, in raise_exception
2042 TypeError: forced TypeError
2045 if sys
.version_info
[:2] > (3, 12):
2046 expect_stderr
= re
.compile(expect_enhanced_stderr
, re
.M
)
2048 expect_stderr
= re
.compile(expect_stderr
, re
.M
)
2049 self
.run_execution_test(script
, expect_stdout
, expect_stderr
)
2051 def test_ignore_stderr(self
) -> None:
2052 """Test run(): ignore stderr"""
2054 script
= lstrip("""\
2055 from TestCommon import TestCommon
2056 tc = TestCommon(program=r'%(stderr_script)s',
2057 interpreter=r'%(python)s',
2059 tc.run(stderr = None)
2062 self
.run_execution_test(script
, "", "")
2064 def test_match_function_stdout(self
) -> None:
2065 """Test run(): explicit match function, stdout"""
2067 script
= lstrip("""\
2068 def my_match_exact(actual, expect): return actual == expect
2069 from TestCommon import TestCommon, match_re_dotall
2070 tc = TestCommon(program=r'%(pass_script)s',
2071 interpreter=r'%(python)s',
2073 match=match_re_dotall)
2074 tc.run(arguments = "arg1 arg2 arg3",
2075 stdout = r"%(pass_script)s: STDOUT: ['arg1', 'arg2', 'arg3']" + "\\n",
2076 match = my_match_exact)
2079 self
.run_execution_test(script
, "", "")
2081 def test_match_function_stderr(self
) -> None:
2082 """Test run(): explicit match function, stderr"""
2084 script
= lstrip("""\
2085 def my_match_exact(actual, expect): return actual == expect
2086 from TestCommon import TestCommon, match_re_dotall
2087 tc = TestCommon(program=r'%(stderr_script)s',
2088 interpreter=r'%(python)s',
2090 match=match_re_dotall)
2091 tc.run(arguments = "arg1 arg2 arg3",
2092 stderr = r"%(stderr_script)s: STDERR: ['arg1', 'arg2', 'arg3']" + "\\n",
2093 match = my_match_exact)
2096 self
.run_execution_test(script
, "", "")
2098 def test_matched_status_fails(self
) -> None:
2099 """Test run(): matched status, script fails"""
2101 script
= lstrip("""\
2102 from TestCommon import TestCommon
2103 tc = TestCommon(program=r'%(fail_script)s',
2104 interpreter=r'%(python)s',
2109 self
.run_execution_test(script
, "", "")
2111 def test_matched_stdout(self
) -> None:
2112 """Test run(): matched stdout"""
2114 script
= lstrip("""\
2115 from TestCommon import TestCommon, match_exact
2116 tc = TestCommon(program=r'%(pass_script)s',
2117 interpreter=r'%(python)s',
2120 tc.run(stdout = r"%(pass_script)s: STDOUT: []" + "\\n")
2123 self
.run_execution_test(script
, "", "")
2125 def test_matched_stderr(self
) -> None:
2126 """Test run(): matched stderr"""
2128 script
= lstrip("""\
2129 from TestCommon import TestCommon, match_exact
2130 tc = TestCommon(program=r'%(stderr_script)s',
2131 interpreter=r'%(python)s',
2134 tc.run(stderr = r"%(stderr_script)s: STDERR: []" + "\\n")
2137 self
.run_execution_test(script
, "", "")
2139 def test_mismatched_status_pass(self
) -> None:
2140 """Test run(): mismatched status, script passes"""
2142 script
= lstrip("""\
2143 from TestCommon import TestCommon
2144 tc = TestCommon(program=r'%(pass_script)s',
2145 interpreter=r'%(python)s',
2150 expect_stdout
= lstrip("""\
2151 %(pass_script)s returned 0 (expected 1)
2152 STDOUT =========================================================================
2153 %(pass_script)s: STDOUT: []
2155 STDERR =========================================================================
2159 expect_stderr
= lstrip("""\
2160 FAILED test of .*pass
2161 \\tat line \\d+ of .*TestCommon\\.py \\(_complete\\)
2162 \\tfrom line \\d+ of .*TestCommon\\.py \\(run\\)
2163 \\tfrom line \\d+ of <stdin>( \\(<module>\\))?
2165 expect_stderr
= re
.compile(expect_stderr
, re
.M
)
2167 self
.run_execution_test(script
, expect_stdout
, expect_stderr
)
2169 def test_mismatched_status_fail(self
) -> None:
2170 """Test run(): mismatched status, script fails"""
2172 script
= lstrip("""\
2173 from TestCommon import TestCommon
2174 tc = TestCommon(program=r'%(fail_script)s',
2175 interpreter=r'%(python)s',
2180 expect_stdout
= lstrip("""\
2181 %(fail_script)s returned 1 (expected 2)
2182 STDOUT =========================================================================
2183 %(fail_script)s: STDOUT: []
2185 STDERR =========================================================================
2189 expect_stderr
= lstrip("""\
2190 FAILED test of .*fail
2191 \\tat line \\d+ of .*TestCommon\\.py \\(_complete\\)
2192 \\tfrom line \\d+ of .*TestCommon\\.py \\(run\\)
2193 \\tfrom line \\d+ of <stdin>( \\(<module>\\))?
2195 expect_stderr
= re
.compile(expect_stderr
, re
.M
)
2197 self
.run_execution_test(script
, expect_stdout
, expect_stderr
)
2199 def test_mismatched_stdout(self
) -> None:
2200 """Test run(): mismatched stdout"""
2202 script
= lstrip("""\
2203 from TestCommon import TestCommon
2204 tc = TestCommon(program=r'%(pass_script)s',
2205 interpreter=r'%(python)s',
2207 tc.run(stdout = "Not found\\n")
2210 expect_stdout
= lstrip("""\
2211 match_re: mismatch at line 0:
2212 search re='^Not found$'
2213 line='%(pass_script)s: STDOUT: []'
2214 STDOUT =========================================================================
2218 > %(pass_script)s: STDOUT: []
2221 expect_stderr
= lstrip("""\
2222 FAILED test of .*pass
2223 \\tat line \\d+ of .*TestCommon\\.py \\(_complete\\)
2224 \\tfrom line \\d+ of .*TestCommon\\.py \\(run\\)
2225 \\tfrom line \\d+ of <stdin>( \\(<module>\\))?
2227 expect_stderr
= re
.compile(expect_stderr
, re
.M
)
2229 self
.run_execution_test(script
, expect_stdout
, expect_stderr
)
2231 def test_mismatched_stderr(self
) -> None:
2232 """Test run(): mismatched stderr"""
2234 script
= lstrip("""\
2235 from TestCommon import TestCommon
2236 tc = TestCommon(program=r'%(stderr_script)s',
2237 interpreter=r'%(python)s',
2239 tc.run(stderr = "Not found\\n")
2242 expect_stdout
= lstrip("""\
2243 match_re: mismatch at line 0:
2244 search re='^Not found$'
2245 line='%(stderr_script)s: STDERR: []'
2246 STDOUT =========================================================================
2248 STDERR =========================================================================
2252 > %(stderr_script)s: STDERR: []
2255 expect_stderr
= lstrip("""\
2256 FAILED test of .*stderr
2257 \\tat line \\d+ of .*TestCommon\\.py \\(_complete\\)
2258 \\tfrom line \\d+ of .*TestCommon\\.py \\(run\\)
2259 \\tfrom line \\d+ of <stdin>( \\(<module>\\))?
2261 expect_stderr
= re
.compile(expect_stderr
, re
.M
)
2263 self
.run_execution_test(script
, expect_stdout
, expect_stderr
)
2265 def test_option_handling(self
) -> None:
2266 """Test run(): option handling"""
2268 script
= lstrip("""\
2269 from TestCommon import TestCommon, match_exact
2270 tc = TestCommon(program=r'%(pass_script)s',
2271 interpreter=r'%(python)s',
2274 tc.run(options = "opt1 opt2 opt3",
2275 stdout = r"%(pass_script)s: STDOUT: ['opt1', 'opt2', 'opt3']" + "\\n")
2278 self
.run_execution_test(script
, "", "")
2280 def test_options_plus_arguments(self
) -> None:
2281 """Test run(): option handling with arguments"""
2283 script
= lstrip("""\
2284 from TestCommon import TestCommon, match_exact
2285 tc = TestCommon(program=r'%(pass_script)s',
2286 interpreter=r'%(python)s',
2289 tc.run(options = "opt1 opt2 opt3",
2290 arguments = "arg1 arg2 arg3",
2291 stdout = r"%(pass_script)s: STDOUT: ['opt1', 'opt2', 'opt3', 'arg1', 'arg2', 'arg3']" + "\\n")
2294 self
.run_execution_test(script
, "", "")
2296 def test_signal_handling(self
) -> None:
2297 """Test run(): signal handling.
2299 Only strange platforms unlikely to support SCons like the
2300 webassembly ones don't support kill(), but keep the test
2305 except AttributeError:
2306 sys
.stderr
.write('can not test, no os.kill ... ')
2309 script
= lstrip("""\
2310 from TestCommon import TestCommon
2311 tc = TestCommon(program=r'%(signal_script)s',
2312 interpreter=r'%(python)s',
2317 self
.SIGTERM
= f
"{'' if sys.platform == 'win32' else '-'}{signal.SIGTERM}"
2319 # Script returns the signal value as a negative number.
2320 expect_stdout
= lstrip("""\
2321 %(signal_script)s returned %(SIGTERM)s
2322 STDOUT =========================================================================
2324 STDERR =========================================================================
2328 expect_stderr
= lstrip("""\
2329 FAILED test of .*signal
2330 \\tat line \\d+ of .*TestCommon\\.py \\(_complete\\)
2331 \\tfrom line \\d+ of .*TestCommon\\.py \\(run\\)
2332 \\tfrom line \\d+ of <stdin>
2334 expect_stderr
= re
.compile(expect_stderr
, re
.M
)
2336 self
.run_execution_test(script
, expect_stdout
, expect_stderr
)
2338 def test_stdin(self
) -> None:
2339 """Test run(): stdin handling"""
2341 script
= lstrip("""\
2342 from TestCommon import TestCommon, match_exact
2343 tc = TestCommon(program=r'%(stdin_script)s',
2344 interpreter=r'%(python)s',
2347 expect_stdout = r"%(stdin_script)s: STDOUT: 'input'" + "\\n"
2348 expect_stderr = r"%(stdin_script)s: STDERR: 'input'" + "\\n"
2349 tc.run(stdin="input\\n", stdout = expect_stdout, stderr = expect_stderr)
2352 expect_stdout
= lstrip("""\
2353 %(pass_script)s returned 0 (expected 1)
2354 STDOUT =========================================================================
2355 %(pass_script)s: STDOUT: []
2357 STDERR =========================================================================
2361 self
.run_execution_test(script
, "", "")
2364 class start_TestCase(TestCommonTestCase
):
2365 def test_option_handling(self
) -> None:
2366 """Test start(): option handling"""
2368 script
= lstrip("""\
2369 from TestCommon import TestCommon, match_exact
2370 tc = TestCommon(program=r'%(pass_script)s',
2371 interpreter=r'%(python)s',
2374 p = tc.start(options = "opt1 opt2 opt3")
2375 expect = r"%(pass_script)s: STDOUT: ['opt1', 'opt2', 'opt3']" + "\\n"
2376 tc.finish(p, stdout = expect)
2379 self
.run_execution_test(script
, "", "")
2381 def test_options_plus_arguments(self
) -> None:
2382 """Test start(): option handling with arguments"""
2384 script
= lstrip("""\
2385 from TestCommon import TestCommon, match_exact
2386 tc = TestCommon(program=r'%(pass_script)s',
2387 interpreter=r'%(python)s',
2390 p = tc.start(options = "opt1 opt2 opt3",
2391 arguments = "arg1 arg2 arg3")
2392 expect = r"%(pass_script)s: STDOUT: ['opt1', 'opt2', 'opt3', 'arg1', 'arg2', 'arg3']" + "\\n"
2393 tc.finish(p, stdout = expect)
2396 self
.run_execution_test(script
, "", "")
2399 class skip_test_TestCase(TestCommonTestCase
):
2400 def test_skip_test(self
) -> None:
2401 """Test skip_test()"""
2402 run_env
= self
.run_env
2404 script
= lstrip("""\
2406 test = TestCommon.TestCommon(workdir='')
2409 run_env
.run(program
=sys
.executable
, stdin
=script
)
2410 stdout
= run_env
.stdout()
2411 assert stdout
== "Skipping test.\n", stdout
2412 stderr
= run_env
.stderr()
2414 "NO RESULT for test at line 3 of <stdin>\n",
2415 "NO RESULT for test at line 3 of <stdin> (<module>)\n",
2417 assert stderr
in expect
, repr(stderr
)
2419 script
= lstrip("""\
2421 test = TestCommon.TestCommon(workdir='')
2422 test.skip_test("skipping test because I said so\\n")
2424 run_env
.run(program
=sys
.executable
, stdin
=script
)
2425 stdout
= run_env
.stdout()
2426 assert stdout
== "skipping test because I said so\n", stdout
2427 stderr
= run_env
.stderr()
2429 "NO RESULT for test at line 3 of <stdin>\n",
2430 "NO RESULT for test at line 3 of <stdin> (<module>)\n",
2432 assert stderr
in expect
, repr(stderr
)
2436 os
.environ
['TESTCOMMON_PASS_SKIPS'] = '1'
2439 script
= lstrip("""\
2441 test = TestCommon.TestCommon(workdir='')
2444 run_env
.run(program
=sys
.executable
, stdin
=script
)
2445 stdout
= run_env
.stdout()
2446 assert stdout
== "Skipping test.\n", stdout
2447 stderr
= run_env
.stderr()
2448 assert stderr
== "PASSED\n", stderr
2451 del os
.environ
['TESTCOMMON_PASS_SKIPS']
2454 class variables_TestCase(TestCommonTestCase
):
2455 def test_variables(self
) -> None:
2456 """Test global variables"""
2457 run_env
= self
.run_env
2480 script
= "import TestCommon\n" + '\n'.join(
2481 [f
"print(TestCommon.{v})\n" for v
in variables
]
2483 run_env
.run(program
=sys
.executable
, stdin
=script
)
2484 stderr
= run_env
.stderr()
2485 assert stderr
== "", stderr
2487 script
= "from TestCommon import *\n" + '\n'.join(
2488 [f
"print({v})" for v
in variables
]
2490 run_env
.run(program
=sys
.executable
, stdin
=script
)
2491 stderr
= run_env
.stderr()
2492 assert stderr
== "", stderr
2495 if __name__
== "__main__":
2501 # indent-tabs-mode:nil
2503 # vim: set expandtab tabstop=4 shiftwidth=4: