A license detection test is used to verify that license detection works correctly It consists of two files with the same base name: a .yml file with test data and a test file with any other extension that needs to be tested for detection The following data are loaded from
| 29 | |
| 30 | @attr.attrs(slots=True) |
| 31 | class LicenseTest(object): |
| 32 | """ |
| 33 | A license detection test is used to verify that license detection works |
| 34 | correctly |
| 35 | |
| 36 | It consists of two files with the same base name: a .yml file with test data |
| 37 | and a test file with any other extension that needs to be tested for |
| 38 | detection |
| 39 | |
| 40 | The following data are loaded from the .yml file: |
| 41 | - a test file to scan for licenses, |
| 42 | - a list of expected licenses expressions to detect |
| 43 | - optional notes. |
| 44 | - a boolean flag expected_failure set to True if a test is expected to fail |
| 45 | for now. |
| 46 | |
| 47 | If the list of license expressions is empty, then this test should not |
| 48 | detect any license in the test file. |
| 49 | """ |
| 50 | data_file = attr.attrib(default=None) |
| 51 | test_file = attr.attrib(default=None) |
| 52 | test_file_name = attr.attrib(default=None) |
| 53 | license_expressions = attr.attrib(default=attr.Factory(list)) |
| 54 | notes = attr.attrib(default=None) |
| 55 | expected_failure = attr.attrib(default=False) |
| 56 | language = attr.attrib(default='en') |
| 57 | |
| 58 | licensing = Licensing() |
| 59 | |
| 60 | def __attrs_post_init__(self, *args, **kwargs): |
| 61 | if self.test_file: |
| 62 | _, _, self.test_file_name = self.test_file.partition( |
| 63 | os.path.join('licensedcode', 'data') + os.sep) |
| 64 | |
| 65 | data = {} |
| 66 | if self.data_file: |
| 67 | try: |
| 68 | with io.open(self.data_file, encoding='utf-8') as df: |
| 69 | data = saneyaml.load(df.read()) or {} |
| 70 | except Exception as e: |
| 71 | raise Exception(f'Failed to read: file://{self.data_file}', e) |
| 72 | |
| 73 | self.license_expressions = data.pop('license_expressions', []) |
| 74 | self.language = data.pop('language', 'en') |
| 75 | self.notes = data.pop('notes', None) |
| 76 | # True if the test is expected to fail |
| 77 | self.expected_failure = data.pop('expected_failure', False) |
| 78 | |
| 79 | if data: |
| 80 | raise Exception( |
| 81 | 'Unknown data elements: ' + repr(data) + |
| 82 | ' for: file://' + self.data_file) |
| 83 | |
| 84 | if self.license_expressions: |
| 85 | for i, exp in enumerate(self.license_expressions[:]): |
| 86 | try: |
| 87 | expression = self.licensing.parse(exp) |
| 88 | except: |