1 # SPDX-License-Identifier: BSD-2-Clause
3 # Copyright (c) 2016, Matt Layman
8 from tap
.adapter
import Adapter
9 from tap
.parser
import Parser
10 from tap
.rules
import Rules
14 """Load TAP lines into unittest-able objects."""
16 ignored_lines
= set(['diagnostic', 'unknown'])
19 self
._parser
= Parser()
21 def load(self
, files
):
22 """Load any files found into a suite.
24 Any directories are walked and their files are added as TAP files.
26 :returns: A ``unittest.TestSuite`` instance
28 suite
= unittest
.TestSuite()
29 for filepath
in files
:
30 if os
.path
.isdir(filepath
):
31 self
._find
_tests
_in
_directory
(filepath
, suite
)
33 suite
.addTest(self
.load_suite_from_file(filepath
))
36 def load_suite_from_file(self
, filename
):
37 """Load a test suite with test lines from the provided TAP file.
39 :returns: A ``unittest.TestSuite`` instance
41 suite
= unittest
.TestSuite()
42 rules
= Rules(filename
, suite
)
44 if not os
.path
.exists(filename
):
45 rules
.handle_file_does_not_exist()
48 line_generator
= self
._parser
.parse_file(filename
)
49 return self
._load
_lines
(filename
, line_generator
, suite
, rules
)
51 def load_suite_from_stdin(self
):
52 """Load a test suite with test lines from the TAP stream on STDIN.
54 :returns: A ``unittest.TestSuite`` instance
56 suite
= unittest
.TestSuite()
57 rules
= Rules('stream', suite
)
58 line_generator
= self
._parser
.parse_stdin()
59 return self
._load
_lines
('stream', line_generator
, suite
, rules
)
61 def _find_tests_in_directory(self
, directory
, suite
):
62 """Find test files in the directory and add them to the suite."""
63 for dirpath
, dirnames
, filenames
in os
.walk(directory
):
64 for filename
in filenames
:
65 filepath
= os
.path
.join(dirpath
, filename
)
66 suite
.addTest(self
.load_suite_from_file(filepath
))
68 def _load_lines(self
, filename
, line_generator
, suite
, rules
):
69 """Load a suite with lines produced by the line generator."""
71 for line
in line_generator
:
74 if line
.category
in self
.ignored_lines
:
77 if line
.category
== 'test':
78 suite
.addTest(Adapter(filename
, line
))
80 elif line
.category
== 'plan':
82 rules
.handle_skipping_plan(line
)
84 rules
.saw_plan(line
, line_counter
)
85 elif line
.category
== 'bail':
86 rules
.handle_bail(line
)
88 elif line
.category
== 'version':
89 rules
.saw_version_at(line_counter
)
91 rules
.check(line_counter
)
This page took 0.034258 seconds and 5 git commands to generate.