d758690f5f7c651286077a24f9839d0d3dfa5296
1 # Copyright (c) 2016, Matt Layman
6 from tap
.adapter
import Adapter
7 from tap
.parser
import Parser
8 from tap
.rules
import Rules
12 """Load TAP lines into unittest-able objects."""
14 ignored_lines
= set(['diagnostic', 'unknown'])
17 self
._parser
= Parser()
19 def load(self
, files
):
20 """Load any files found into a suite.
22 Any directories are walked and their files are added as TAP files.
24 :returns: A ``unittest.TestSuite`` instance
26 suite
= unittest
.TestSuite()
27 for filepath
in files
:
28 if os
.path
.isdir(filepath
):
29 self
._find
_tests
_in
_directory
(filepath
, suite
)
31 suite
.addTest(self
.load_suite_from_file(filepath
))
34 def load_suite_from_file(self
, filename
):
35 """Load a test suite with test lines from the provided TAP file.
37 :returns: A ``unittest.TestSuite`` instance
39 suite
= unittest
.TestSuite()
40 rules
= Rules(filename
, suite
)
42 if not os
.path
.exists(filename
):
43 rules
.handle_file_does_not_exist()
46 line_generator
= self
._parser
.parse_file(filename
)
47 return self
._load
_lines
(filename
, line_generator
, suite
, rules
)
49 def load_suite_from_stdin(self
):
50 """Load a test suite with test lines from the TAP stream on STDIN.
52 :returns: A ``unittest.TestSuite`` instance
54 suite
= unittest
.TestSuite()
55 rules
= Rules('stream', suite
)
56 line_generator
= self
._parser
.parse_stdin()
57 return self
._load
_lines
('stream', line_generator
, suite
, rules
)
59 def _find_tests_in_directory(self
, directory
, suite
):
60 """Find test files in the directory and add them to the suite."""
61 for dirpath
, dirnames
, filenames
in os
.walk(directory
):
62 for filename
in filenames
:
63 filepath
= os
.path
.join(dirpath
, filename
)
64 suite
.addTest(self
.load_suite_from_file(filepath
))
66 def _load_lines(self
, filename
, line_generator
, suite
, rules
):
67 """Load a suite with lines produced by the line generator."""
69 for line
in line_generator
:
72 if line
.category
in self
.ignored_lines
:
75 if line
.category
== 'test':
76 suite
.addTest(Adapter(filename
, line
))
78 elif line
.category
== 'plan':
80 rules
.handle_skipping_plan(line
)
82 rules
.saw_plan(line
, line_counter
)
83 elif line
.category
== 'bail':
84 rules
.handle_bail(line
)
86 elif line
.category
== 'version':
87 rules
.saw_version_at(line_counter
)
89 rules
.check(line_counter
)
This page took 0.030951 seconds and 3 git commands to generate.