Rework a TestCommon test to look less ugly [skip appveyor[
[scons.git] / testing / framework / TestCommonTests.py
blob3beca8010238606ff4e0233799367ed5954660e6
1 #!/usr/bin/env python
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
12 # DAMAGE.
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
22 """
23 Unit tests for the TestCommon.py module.
24 """
26 import os
27 import re
28 import signal
29 import sys
30 import unittest
31 from textwrap import dedent
33 # Strip the current directory so we get the right TestCommon.py module.
34 sys.path = sys.path[1:]
36 import TestCmd
37 import TestCommon
40 # this used to be a custom function, now use the stdlib equivalent
41 def lstrip(s):
42 return dedent(s)
45 expected_newline = '\\n'
48 def assert_display(expect, result, error=None):
49 try:
50 expect = expect.pattern
51 except AttributeError:
52 pass
53 display = [
54 '\n',
55 f"{'EXPECTED: ':*<80}\n",
56 expect,
57 f"{'GOT: ':*<80}\n",
58 result,
59 '' if error is None else error,
60 f"{'':*<80}\n",
62 return ''.join(display)
65 class TestCommonTestCase(unittest.TestCase):
66 """Base class for TestCommon test cases, fixture and utility methods."""
68 create_run_env = True
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'
99 else:
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("""\
112 import os
113 import signal
114 os.kill(os.getpid(), signal.SIGTERM)
115 """)
117 run_env.write(self.signal_script, wrapper % signal_body)
119 stdin_body = lstrip(
120 """\
121 import sys
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)
149 try:
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)
154 else:
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)
164 script = lstrip("""\
165 from TestCommon import TestCommon
166 tc = TestCommon(workdir='')
167 import os
168 print(os.getcwd())
169 """)
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:
181 """Test banner()"""
182 tc = TestCommon.TestCommon(workdir='')
184 text = 'xyzzy '
185 b = tc.banner(text)
186 expect = f"{text}{tc.banner_char * (tc.banner_width - len(text))}"
187 assert b == expect, b
189 tc.banner_width = 10
190 b = tc.banner(text)
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
198 tc.banner_char = '-'
199 b = tc.banner(text)
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
209 script = lstrip("""\
210 from TestCommon import TestCommon
211 tc = TestCommon(workdir='')
212 tc.must_be_writable('file1')
213 tc.pass_test()
214 """)
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
225 script = lstrip("""\
226 import os
227 import stat
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')
235 tc.pass_test()
236 """)
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
247 script = lstrip("""\
248 import os
249 import stat
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')
257 tc.pass_test()
258 """)
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
269 script = lstrip("""\
270 import os
271 import stat
272 from TestCommon import TestCommon
273 tc = TestCommon(workdir='')
274 tc.subdir('sub')
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'])
280 tc.pass_test()
281 """)
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
294 script = lstrip("""\
295 from TestCommon import TestCommon
296 tc = TestCommon(workdir='')
297 tc.write('file1', "file1 contents\\n")
298 tc.must_contain('file1', "1 c")
299 tc.pass_test()
300 """)
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
311 script = lstrip("""\
312 from TestCommon import TestCommon
313 tc = TestCommon(workdir='')
314 tc.write('file1', "file1 contents\\n")
315 tc.must_contain('file1', "file1 c")
316 tc.pass_test()
317 """)
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
328 script = lstrip("""\
329 from TestCommon import TestCommon
330 tc = TestCommon(workdir='')
331 tc.must_contain('file1', "1 c\\n")
332 tc.pass_test()
333 """)
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
344 script = lstrip("""\
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")
349 tc.run()
350 """)
351 expect = lstrip("""\
352 File `file1' does not contain required string.
353 Required string ================================================================
354 b'1 c'
355 file1 contents =================================================================
356 b'file1 does not match\\n'
357 """)
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
368 script = lstrip("""\
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')
375 tc.pass_test()
376 """)
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
389 script = lstrip("""
390 import TestCommon
391 test = TestCommon.TestCommon(workdir='')
393 lines = [
394 'xxx\\n',
395 'yyy\\n',
398 output = '''\\
405 test.must_contain_all_lines(output, lines)
407 test.must_contain_all_lines(output, ['www\\n'])
409 test.pass_test()
410 """)
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
422 script = lstrip("""
423 import TestCommon
424 test = TestCommon.TestCommon(workdir='')
426 lines = [
427 'xxx\\n',
428 'yyy\\n',
431 output = '''\\
436 test.must_contain_all_lines(output, lines)
438 test.pass_test()
439 """)
441 expect = lstrip(
442 """\
443 Missing expected lines from output:
444 'xxx%(expected_newline)s'
445 'yyy%(expected_newline)s'
446 output =========================================================================
450 % globals()
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
463 script = lstrip("""
464 import re
465 import TestCommon
466 test = TestCommon.TestCommon(workdir='')
468 lines = [
469 'x.*',
470 '.*y',
473 output = '''\\
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)
485 test.pass_test()
486 """)
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
498 script = lstrip("""
499 import TestCommon
500 test = TestCommon.TestCommon(workdir='')
502 lines = [
503 'xxx\\n',
504 'yyy\\n',
507 output = '''\\
512 test.must_contain_all_lines(output, lines, title='STDERR')
514 test.pass_test()
515 """)
517 expect = lstrip(
518 """\
519 Missing expected lines from STDERR:
520 'xxx%(expected_newline)s'
521 'yyy%(expected_newline)s'
522 STDERR =========================================================================
526 % globals()
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
541 script = lstrip("""
542 import TestCommon
543 test = TestCommon.TestCommon(workdir='')
545 lines = [
546 'aaa\\n',
547 'yyy\\n',
550 output = '''\\
557 test.must_contain_any_line(output, lines)
559 test.must_contain_any_line(output, ['www\\n'])
561 test.pass_test()
562 """)
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
574 script = lstrip("""
575 import TestCommon
576 test = TestCommon.TestCommon(workdir='')
578 lines = [
579 'xxx\\n',
580 'yyy\\n',
583 output = '''\\
588 test.must_contain_any_line(output, lines)
590 test.pass_test()
591 """)
593 expect = lstrip(
594 """\
595 Missing any expected line from output:
596 'xxx%(expected_newline)s'
597 'yyy%(expected_newline)s'
598 output =========================================================================
602 % globals()
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
615 script = lstrip("""
616 import re
617 import TestCommon
618 test = TestCommon.TestCommon(workdir='')
620 lines = [
621 'aaa',
622 '.*y',
625 output = '''\\
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)
636 test.pass_test()
637 """)
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
649 script = lstrip("""
650 import TestCommon
651 test = TestCommon.TestCommon(workdir='')
653 lines = [
654 'xxx\\n',
655 'yyy\\n',
658 output = '''\\
663 test.must_contain_any_line(output, lines, title='STDOUT')
665 test.pass_test()
666 """)
668 expect = lstrip(
669 """\
670 Missing any expected line from STDOUT:
671 'xxx%(expected_newline)s'
672 'yyy%(expected_newline)s'
673 STDOUT =========================================================================
677 % globals()
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
692 script = lstrip("""
693 import TestCommon
694 test = TestCommon.TestCommon(workdir='')
696 lines = [
697 'yyy\\n',
698 'xxx\\n',
699 'zzz',
700 'www\\n',
703 output = '''\\
710 test.must_contain_exactly_lines(output, lines)
712 test.pass_test()
713 """)
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
725 script = lstrip("""
726 import TestCommon
727 test = TestCommon.TestCommon(workdir='')
729 lines = '''\\
736 output = '''\\
743 test.must_contain_exactly_lines(output, lines)
745 test.pass_test()
746 """)
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
758 script = lstrip("""
759 import TestCommon
760 test = TestCommon.TestCommon(workdir='')
762 lines = [
763 'xxx\\n',
764 'yyy\\n',
767 output = '''\\
772 test.must_contain_exactly_lines(output, lines)
774 test.pass_test()
775 """)
777 expect = lstrip("""\
778 Missing expected lines from output:
779 'xxx'
780 'yyy'
781 Missing output =================================================================
782 Extra unexpected lines from output:
783 'www'
784 'zzz'
785 Extra output ===================================================================
786 """)
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
798 script = lstrip("""
799 import re
800 import TestCommon
801 test = TestCommon.TestCommon(workdir='')
803 lines = [
804 'zzz',
805 '.*y',
806 'xxx',
807 'www',
810 output = '''\\\
817 def re_search(output, line):
818 pattern = re.compile(line, re.S)
819 index = 0
820 for o in output:
821 if pattern.search(o):
822 return index
823 index +=1
824 return None
825 test.must_contain_exactly_lines(output, lines, find=re_search)
827 test.pass_test()
828 """)
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
844 script = lstrip("""
845 import TestCommon
846 test = TestCommon.TestCommon(workdir='')
848 lines = [
849 'xxx\\n',
850 'yyy\\n',
853 output = '''\\
858 test.must_contain_exactly_lines(output, lines, title='STDOUT')
860 test.pass_test()
861 """)
863 expect = lstrip("""\
864 Missing expected lines from STDOUT:
865 'xxx'
866 'yyy'
867 Missing STDOUT =================================================================
868 Extra unexpected lines from STDOUT:
869 'www'
870 'zzz'
871 Extra STDOUT ===================================================================
872 """)
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
886 script = lstrip("""
887 import TestCommon
888 test = TestCommon.TestCommon(workdir='')
890 lines = [
891 'xxx\\n',
892 'yyy\\n',
895 output = '''\\
902 test.must_contain_lines(lines, output)
904 test.pass_test()
905 """)
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
917 script = lstrip("""
918 import TestCommon
919 test = TestCommon.TestCommon(workdir='')
921 lines = [
922 'xxx\\n',
923 'yyy\\n',
926 output = '''\\
931 test.must_contain_lines(lines, output)
933 test.pass_test()
934 """)
936 expect = lstrip(
937 """\
938 Missing expected lines from output:
939 'xxx%(expected_newline)s'
940 'yyy%(expected_newline)s'
941 output =========================================================================
945 % globals()
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
960 script = lstrip("""\
961 from TestCommon import TestCommon
962 tc = TestCommon(workdir='')
963 tc.write('file1', "file1\\n")
964 tc.must_exist('file1')
965 tc.pass_test()
966 """)
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
977 script = lstrip("""\
978 from TestCommon import TestCommon
979 tc = TestCommon(workdir='')
980 tc.must_exist('file1')
981 tc.pass_test()
982 """)
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
993 script = lstrip("""\
994 from TestCommon import TestCommon
995 tc = TestCommon(workdir='')
996 tc.must_exist('file1', message="Extra Info")
997 tc.pass_test()
998 """)
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='')
1012 tc.subdir('sub')
1013 tc.write(['sub', 'file1'], "sub/file1\\n")
1014 tc.must_exist(['sub', 'file1'])
1015 tc.pass_test()
1016 """)
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')
1033 tc.pass_test()
1034 """)
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'])
1052 tc.pass_test()
1053 """)
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'])
1068 tc.pass_test()
1069 """)
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")
1084 tc.pass_test()
1085 """)
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'])
1101 tc.pass_test()
1102 """)
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?'])
1118 tc.pass_test()
1119 """)
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='')
1133 tc.subdir('sub')
1134 tc.write(['sub', 'file1'], "sub/file1\\n")
1135 tc.must_exist_one_of(['file2', ['sub', 'file1']])
1136 tc.pass_test()
1137 """)
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='')
1151 tc.subdir('sub')
1152 tc.write(['sub', 'file1'], "sub/file1\\n")
1153 tc.must_exist_one_of(['file2', ('sub', 'file1')])
1154 tc.pass_test()
1155 """)
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")
1173 tc.pass_test()
1174 """)
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")
1189 tc.pass_test()
1190 """)
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")
1206 tc.run()
1207 """)
1209 expect = lstrip("""\
1210 match_re: mismatch at line 0:
1211 search re='^file1$'
1212 line='file1 does not match'
1213 Unexpected contents of `file1'
1214 contents =======================================================================
1216 < file1
1218 > file1 does not match
1219 """)
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')
1237 tc.pass_test()
1238 """)
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')
1255 tc.pass_test()
1256 """)
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("""\
1268 import os
1269 import stat
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')
1277 tc.pass_test()
1278 """)
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("""\
1290 import os
1291 import stat
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')
1299 tc.pass_test()
1300 """)
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("""\
1312 import os
1313 import stat
1314 from TestCommon import TestCommon
1315 tc = TestCommon(workdir='')
1316 tc.subdir('sub')
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'])
1322 tc.pass_test()
1323 """)
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")
1341 tc.pass_test()
1342 """)
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")
1357 tc.pass_test()
1358 """)
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")
1374 tc.run()
1375 """)
1376 expect = lstrip("""\
1377 File `file1' contains banned string.
1378 Banned string ==================================================================
1379 b'1 does contain c'
1380 file1 contents =================================================================
1381 b'file1 does contain contents\\n'
1382 """)
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")
1399 tc.run()
1400 """)
1401 expect = lstrip("""\
1402 File `file1' contains banned string.
1403 Banned string ==================================================================
1404 b'file1 does'
1405 file1 contents =================================================================
1406 b'file1 does contain contents\\n'
1407 """)
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')
1426 tc.pass_test()
1427 """)
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
1440 script = lstrip("""
1441 import TestCommon
1442 test = TestCommon.TestCommon(workdir='')
1444 lines = [
1445 'xxx\\n',
1446 'yyy\\n',
1447 'www\\n',
1450 output = '''\\
1457 test.must_not_contain_any_line(output, lines)
1459 test.pass_test()
1460 """)
1462 expect = lstrip(
1463 """\
1464 Unexpected lines in output:
1465 'xxx%(expected_newline)s'
1466 'yyy%(expected_newline)s'
1467 'www%(expected_newline)s'
1468 output =========================================================================
1474 % globals()
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
1487 script = lstrip("""
1488 import re
1489 import TestCommon
1490 test = TestCommon.TestCommon(workdir='')
1492 lines = [
1493 'x.*'
1494 '.*y'
1497 output = '''\\
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)
1506 test.pass_test()
1507 """)
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
1519 script = lstrip("""
1520 import TestCommon
1521 test = TestCommon.TestCommon(workdir='')
1523 lines = [
1524 'xxx\\n'
1525 'yyy\\n'
1528 output = '''\\
1533 test.must_not_contain_any_line(output, lines)
1535 test.pass_test()
1536 """)
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
1548 script = lstrip("""
1549 import TestCommon
1550 test = TestCommon.TestCommon(workdir='')
1552 lines = [
1553 'xxx\\n',
1554 'yyy\\n',
1557 output = '''\\
1564 test.must_not_contain_any_line(output, lines, title='XYZZY')
1566 test.pass_test()
1567 """)
1569 expect = lstrip(
1570 """\
1571 Unexpected lines in XYZZY:
1572 'xxx%(expected_newline)s'
1573 'yyy%(expected_newline)s'
1574 XYZZY ==========================================================================
1580 % globals()
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
1595 script = lstrip("""
1596 import TestCommon
1597 test = TestCommon.TestCommon(workdir='')
1599 lines = [
1600 'xxx\\n',
1601 'yyy\\n',
1604 output = '''\\
1611 test.must_not_contain_lines(lines, output)
1613 test.pass_test()
1614 """)
1616 expect = lstrip(
1617 """\
1618 Unexpected lines in output:
1619 'xxx%(expected_newline)s'
1620 'yyy%(expected_newline)s'
1621 output =========================================================================
1627 % globals()
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
1640 script = lstrip("""
1641 import TestCommon
1642 test = TestCommon.TestCommon(workdir='')
1644 lines = [
1645 'xxx\\n'
1646 'yyy\\n'
1649 output = '''\\
1654 test.must_not_contain_lines(lines, output)
1656 test.pass_test()
1657 """)
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')
1676 tc.pass_test()
1677 """)
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')
1692 tc.pass_test()
1693 """)
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='')
1707 tc.subdir('sub')
1708 tc.must_not_exist(['sub', 'file1'])
1709 tc.pass_test()
1710 """)
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')
1727 tc.pass_test()
1728 """)
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'])
1745 tc.pass_test()
1746 """)
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'])
1762 tc.pass_test()
1763 """)
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'])
1778 tc.pass_test()
1779 """)
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?'])
1795 tc.pass_test()
1796 """)
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='')
1810 tc.subdir('sub')
1811 tc.write(['sub', 'file1'], "sub/file1\\n")
1812 tc.must_not_exist_any_of(['file2',
1813 ['sub', 'files*']])
1814 tc.pass_test()
1815 """)
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='')
1829 tc.subdir('sub')
1830 tc.write(['sub', 'file1'], "sub/file1\\n")
1831 tc.must_not_exist_any_of(['file2',
1832 ('sub', 'files?')])
1833 tc.pass_test()
1834 """)
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')
1852 tc.pass_test()
1853 """)
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')
1869 tc.pass_test()
1870 """)
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')
1885 tc.pass_test()
1886 """)
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',
1902 workdir="",
1903 match=match_exact)
1904 tc.run(arguments = "arg1 arg2 arg3",
1905 stdout = r"%(pass_script)s: STDOUT: ['arg1', 'arg2', 'arg3']" + "\\n")
1906 """)
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',
1917 workdir='')
1918 tc.run()
1919 """)
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',
1930 workdir='')
1931 tc.run()
1932 """)
1934 expect_stdout = lstrip("""\
1935 %(fail_script)s returned 1
1936 STDOUT =========================================================================
1937 %(fail_script)s: STDOUT: []
1939 STDERR =========================================================================
1941 """)
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>\\))?
1948 """)
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',
1959 workdir='')
1960 tc.run()
1961 """)
1963 expect_stdout = lstrip("""\
1964 match_re: expected 1 lines, found 2
1965 STDOUT =========================================================================
1967 STDERR =========================================================================
1969 > %(stderr_script)s: STDERR: []
1970 """)
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>
1977 """)
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("""\
1985 import TestCmd
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',
1992 workdir='')
1993 tc.run()
1994 """)
1996 expect_stdout = lstrip("""\
1997 STDOUT =========================================================================
1998 None
1999 STDERR =========================================================================
2000 None
2001 """)
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
2010 p = self\.start\(
2011 (?:\s*\^*\s)? File \"[^\"]+TestCommon\.py\", line \d+, in start
2012 raise e
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
2030 p = self\.start\(
2031 program=program,
2032 \.\.\.<4 lines>\.\.\.
2033 stdin=stdin,
2035 (?:\s*[~\^]*\s*)?File \"[^\"]+TestCommon\.py\", line \d+, in start
2036 raise e
2037 File "[^"]+TestCommon\.py", line \d+, in start
2038 return super\(\)\.start\(
2039 (?:\s*[~\^]*\s*)?program, interpreter, arguments, universal_newlines, \*\*kw
2040 (?:\s*[~\^]*\s*)?\)
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)
2047 else:
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',
2058 workdir='')
2059 tc.run(stderr = None)
2060 """)
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',
2072 workdir="",
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)
2077 """)
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',
2089 workdir="",
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)
2094 """)
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',
2105 workdir='')
2106 tc.run(status = 1)
2107 """)
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',
2118 workdir="",
2119 match=match_exact)
2120 tc.run(stdout = r"%(pass_script)s: STDOUT: []" + "\\n")
2121 """)
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',
2132 workdir="",
2133 match=match_exact)
2134 tc.run(stderr = r"%(stderr_script)s: STDERR: []" + "\\n")
2135 """)
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',
2146 workdir='')
2147 tc.run(status = 1)
2148 """)
2150 expect_stdout = lstrip("""\
2151 %(pass_script)s returned 0 (expected 1)
2152 STDOUT =========================================================================
2153 %(pass_script)s: STDOUT: []
2155 STDERR =========================================================================
2157 """)
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>\\))?
2164 """)
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',
2176 workdir='')
2177 tc.run(status = 2)
2178 """)
2180 expect_stdout = lstrip("""\
2181 %(fail_script)s returned 1 (expected 2)
2182 STDOUT =========================================================================
2183 %(fail_script)s: STDOUT: []
2185 STDERR =========================================================================
2187 """)
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>\\))?
2194 """)
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',
2206 workdir='')
2207 tc.run(stdout = "Not found\\n")
2208 """)
2210 expect_stdout = lstrip("""\
2211 match_re: mismatch at line 0:
2212 search re='^Not found$'
2213 line='%(pass_script)s: STDOUT: []'
2214 STDOUT =========================================================================
2216 < Not found
2218 > %(pass_script)s: STDOUT: []
2219 """)
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>\\))?
2226 """)
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',
2238 workdir='')
2239 tc.run(stderr = "Not found\\n")
2240 """)
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 =========================================================================
2250 < Not found
2252 > %(stderr_script)s: STDERR: []
2253 """)
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>\\))?
2260 """)
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',
2272 workdir="",
2273 match=match_exact)
2274 tc.run(options = "opt1 opt2 opt3",
2275 stdout = r"%(pass_script)s: STDOUT: ['opt1', 'opt2', 'opt3']" + "\\n")
2276 """)
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',
2287 workdir="",
2288 match=match_exact)
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")
2292 """)
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
2301 in place anyway.
2303 try:
2304 _ = os.kill
2305 except AttributeError:
2306 sys.stderr.write('can not test, no os.kill ... ')
2307 return
2309 script = lstrip("""\
2310 from TestCommon import TestCommon
2311 tc = TestCommon(program=r'%(signal_script)s',
2312 interpreter=r'%(python)s',
2313 workdir='')
2314 tc.run()
2315 """)
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 =========================================================================
2326 """)
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>
2333 """)
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',
2345 workdir='',
2346 match=match_exact)
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)
2350 """)
2352 expect_stdout = lstrip("""\
2353 %(pass_script)s returned 0 (expected 1)
2354 STDOUT =========================================================================
2355 %(pass_script)s: STDOUT: []
2357 STDERR =========================================================================
2359 """)
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',
2372 workdir="",
2373 match=match_exact)
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)
2377 """)
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',
2388 workdir="",
2389 match=match_exact)
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)
2394 """)
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("""\
2405 import TestCommon
2406 test = TestCommon.TestCommon(workdir='')
2407 test.skip_test()
2408 """)
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()
2413 expect = [
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("""\
2420 import TestCommon
2421 test = TestCommon.TestCommon(workdir='')
2422 test.skip_test("skipping test because I said so\\n")
2423 """)
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()
2428 expect = [
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)
2434 import os
2436 os.environ['TESTCOMMON_PASS_SKIPS'] = '1'
2438 try:
2439 script = lstrip("""\
2440 import TestCommon
2441 test = TestCommon.TestCommon(workdir='')
2442 test.skip_test()
2443 """)
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
2450 finally:
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
2459 variables = [
2460 'fail_test',
2461 'no_result',
2462 'pass_test',
2463 'match_exact',
2464 'match_re',
2465 'match_re_dotall',
2466 'python',
2467 '_python_',
2468 'TestCmd',
2469 'TestCommon',
2470 'exe_suffix',
2471 'obj_suffix',
2472 'shobj_prefix',
2473 'shobj_suffix',
2474 'lib_prefix',
2475 'lib_suffix',
2476 'dll_prefix',
2477 'dll_suffix',
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__":
2496 unittest.main()
2499 # Local Variables:
2500 # tab-width:4
2501 # indent-tabs-mode:nil
2502 # End:
2503 # vim: set expandtab tabstop=4 shiftwidth=4: