def test_xml_parser(self): parser = xmlparser.Parser(Builder(), StandardLogger()) test_file = utils_test.get_test_loc('formats/SPDXXmlExample.xml') with io.open(test_file, encoding='utf-8') as f: document, _ = parser.parse(f) expected_loc = utils_test.get_test_loc('doc_parse/expected.json') self.check_document(document, expected_loc)
def test_rdf_parser(self): parser = rdf.Parser(RDFBuilder(), StandardLogger()) test_file = utils_test.get_test_loc('../../data/SPDXRdfExample.rdf', test_data_dir=utils_test.test_data_dir) with io.open(test_file, 'rb') as f: document, _ = parser.parse(f) expected_loc = utils_test.get_test_loc('doc_parse/spdx-expected.json', test_data_dir=utils_test.test_data_dir) self.check_document(document, expected_loc)
def test_sbomyaml_parser(self): parser = yamlparser.Parser(Builder(), StandardLogger()) test_file = utils_test.get_test_loc('formats/SPDXSBOMExample.spdx.yml') with io.open(test_file, encoding='utf-8') as f: document, errors = parser.parse(f) assert not errors expected_loc = utils_test.get_test_loc('doc_parse/SBOMexpected.json') self.check_document(document, expected_loc)
def test_file_lic_conc(self): parser = Parser(Builder(), StandardLogger()) test_file = utils_test.get_test_loc('../../data/SPDXRdfExample.rdf', test_data_dir=utils_test.test_data_dir) with open(test_file, 'r') as f: document, _ = parser.parse(f) files = sorted(document.package.files) assert files[0].conc_lics.identifier.toPython() == 'LicenseRef-1' assert files[1].conc_lics.identifier == 'Apache-2.0'
def test_pkg_lic_conc(self): parser = Parser(Builder(), StandardLogger()) test_file = utils_test.get_test_loc('../../data/SPDXRdfExample.rdf', test_data_dir=utils_test.test_data_dir) with open(test_file, 'r') as f: document, _ = parser.parse(f) # It is needed to ckeck it as sorted lists because the order changes when parsing lic_expected = ['Apache-1.0', 'Apache-2.0', 'LicenseRef-1', 'LicenseRef-2', 'LicenseRef-3', 'LicenseRef-4', 'MPL-1.1'] lic_result = sorted(document.package.conc_lics.identifier.split(' AND ')) assert lic_result == lic_expected
def test_extracted_licenses(self): parser = Parser(Builder(), StandardLogger()) test_file = utils_test.get_test_loc('../../data/SPDXRdfExample.rdf', test_data_dir=utils_test.test_data_dir) with open(test_file, 'r') as f: document, _ = parser.parse(f) assert len(document.extracted_licenses) == 4 # It is needed to sort the list because the order changes when parsing licenses = sorted(document.extracted_licenses) assert licenses[0].identifier.toPython() == 'LicenseRef-1' assert licenses[1].identifier.toPython() == 'LicenseRef-2' assert licenses[2].identifier.toPython() == 'LicenseRef-3' assert licenses[3].identifier.toPython() == 'LicenseRef-4'
def test_write_document_rdf_mini(self): from spdx.writers.rdf import write_document doc = self._get_mini_doc() temp_dir = '' try: temp_dir = tempfile.mkdtemp(prefix='test_spdx') result_file = os.path.join(temp_dir, 'spdx-simple.rdf') with open(result_file, 'wb') as output: write_document(doc, output, validate=False) expected_file = utils_test.get_test_loc('doc_write/rdf-mini.json') utils_test.check_rdf_scan(expected_file, result_file, regen=False) finally: if temp_dir and os.path.exists(temp_dir): shutil.rmtree(temp_dir)
def test_write_document_tv_with_validate(self): from spdx.writers.tagvalue import write_document doc = self._get_lgpl_doc() temp_dir = '' try: temp_dir = tempfile.mkdtemp(prefix='test_spdx') result_file = os.path.join(temp_dir, 'spdx-simple.tv') with open(result_file, 'w') as output: write_document(doc, output, validate=True) expected_file = utils_test.get_test_loc( 'doc_write/tv-simple.tv', test_data_dir=utils_test.test_data_dir) utils_test.check_tv_scan(expected_file, result_file, regen=False) finally: if temp_dir and os.path.exists(temp_dir): shutil.rmtree(temp_dir)
def test_write_document_rdf_with_or_later_with_validate(self): from spdx.writers.rdf import write_document doc = self._get_lgpl_doc(or_later=True) temp_dir = '' try: temp_dir = tempfile.mkdtemp(prefix='test_spdx') result_file = os.path.join(temp_dir, 'spdx-simple-plus.rdf') # test proper! with open(result_file, 'wb') as output: write_document(doc, output, validate=True) expected_file = utils_test.get_test_loc( 'doc_write/rdf-simple-plus.json', test_data_dir=utils_test.test_data_dir) utils_test.check_rdf_scan(expected_file, result_file, regen=False) finally: if temp_dir and os.path.exists(temp_dir): shutil.rmtree(temp_dir)
def test_xml_xml(self): doc = self.parse_xml_file( utils_test.get_test_loc('formats/SPDXXmlExample.xml')) filename = get_temp_file('.xml') self.write_xml_file(doc, filename) self.parse_xml_file(filename)
def test_json_yaml(self): doc = self.parse_json_file( utils_test.get_test_loc('formats/SPDXJsonExample.json')) filename = get_temp_file('.yaml') self.write_yaml_file(doc, filename) self.parse_yaml_file(filename)
def test_rdf_yaml(self): doc = self.parse_rdf_file( utils_test.get_test_loc('formats/SPDXRdfExample.rdf')) filename = get_temp_file('.yaml') self.write_yaml_file(doc, filename) self.parse_yaml_file(filename)
def test_tagvalue_yaml(self): doc = self.parse_tagvalue_file( utils_test.get_test_loc('formats/SPDXTagExample.tag')) filename = get_temp_file('.yaml') self.write_yaml_file(doc, filename) self.parse_yaml_file(filename)
def test_json_tagvalue(self): doc = self.parse_json_file( utils_test.get_test_loc('formats/SPDXJsonExample.json')) filename = get_temp_file('.tag') self.write_tagvalue_file(doc, filename) self.parse_tagvalue_file(filename)
def test_rdf_tagvalue(self): doc = self.parse_rdf_file( utils_test.get_test_loc('formats/SPDXRdfExample.rdf')) filename = get_temp_file('.tag') self.write_tagvalue_file(doc, filename) self.parse_tagvalue_file(filename)