| 1 | # SPDX-License-Identifier: BSD-2-Clause |
| 2 | # |
| 3 | # Copyright (c) 2016, Matt Layman |
| 4 | |
| 5 | import os |
| 6 | import unittest |
| 7 | |
| 8 | from tap.adapter import Adapter |
| 9 | from tap.parser import Parser |
| 10 | from tap.rules import Rules |
| 11 | |
| 12 | |
| 13 | class Loader(object): |
| 14 | """Load TAP lines into unittest-able objects.""" |
| 15 | |
| 16 | ignored_lines = set(['diagnostic', 'unknown']) |
| 17 | |
| 18 | def __init__(self): |
| 19 | self._parser = Parser() |
| 20 | |
| 21 | def load(self, files): |
| 22 | """Load any files found into a suite. |
| 23 | |
| 24 | Any directories are walked and their files are added as TAP files. |
| 25 | |
| 26 | :returns: A ``unittest.TestSuite`` instance |
| 27 | """ |
| 28 | suite = unittest.TestSuite() |
| 29 | for filepath in files: |
| 30 | if os.path.isdir(filepath): |
| 31 | self._find_tests_in_directory(filepath, suite) |
| 32 | else: |
| 33 | suite.addTest(self.load_suite_from_file(filepath)) |
| 34 | return suite |
| 35 | |
| 36 | def load_suite_from_file(self, filename): |
| 37 | """Load a test suite with test lines from the provided TAP file. |
| 38 | |
| 39 | :returns: A ``unittest.TestSuite`` instance |
| 40 | """ |
| 41 | suite = unittest.TestSuite() |
| 42 | rules = Rules(filename, suite) |
| 43 | |
| 44 | if not os.path.exists(filename): |
| 45 | rules.handle_file_does_not_exist() |
| 46 | return suite |
| 47 | |
| 48 | line_generator = self._parser.parse_file(filename) |
| 49 | return self._load_lines(filename, line_generator, suite, rules) |
| 50 | |
| 51 | def load_suite_from_stdin(self): |
| 52 | """Load a test suite with test lines from the TAP stream on STDIN. |
| 53 | |
| 54 | :returns: A ``unittest.TestSuite`` instance |
| 55 | """ |
| 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) |
| 60 | |
| 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)) |
| 67 | |
| 68 | def _load_lines(self, filename, line_generator, suite, rules): |
| 69 | """Load a suite with lines produced by the line generator.""" |
| 70 | line_counter = 0 |
| 71 | for line in line_generator: |
| 72 | line_counter += 1 |
| 73 | |
| 74 | if line.category in self.ignored_lines: |
| 75 | continue |
| 76 | |
| 77 | if line.category == 'test': |
| 78 | suite.addTest(Adapter(filename, line)) |
| 79 | rules.saw_test() |
| 80 | elif line.category == 'plan': |
| 81 | if line.skip: |
| 82 | rules.handle_skipping_plan(line) |
| 83 | return suite |
| 84 | rules.saw_plan(line, line_counter) |
| 85 | elif line.category == 'bail': |
| 86 | rules.handle_bail(line) |
| 87 | return suite |
| 88 | elif line.category == 'version': |
| 89 | rules.saw_version_at(line_counter) |
| 90 | |
| 91 | rules.check(line_counter) |
| 92 | return suite |