Skip to content

Commit

Permalink
[issue-573] unify parse_from_file tests for all formats
Browse files Browse the repository at this point in the history
Signed-off-by: Armin Tänzer <armin.taenzer@tngtech.com>
  • Loading branch information
armintaenzertng committed Apr 12, 2023
1 parent bfcd5e2 commit 5feba30
Show file tree
Hide file tree
Showing 10 changed files with 2,089 additions and 110 deletions.
329 changes: 329 additions & 0 deletions tests/spdx/data/formats/SPDXTagExample-v2.2.spdx

Large diffs are not rendered by default.

443 changes: 443 additions & 0 deletions tests/spdx/data/formats/SPDXXMLExample-v2.2.spdx.xml

Large diffs are not rendered by default.

460 changes: 460 additions & 0 deletions tests/spdx/data/formats/SPDXXMLExample-v2.3.spdx.xml

Large diffs are not rendered by default.

390 changes: 390 additions & 0 deletions tests/spdx/data/formats/SPDXYAMLExample-v2.2.spdx.yaml

Large diffs are not rendered by default.

406 changes: 406 additions & 0 deletions tests/spdx/data/formats/SPDXYAMLExample-v2.3.spdx.yaml

Large diffs are not rendered by default.

File renamed without changes.
61 changes: 61 additions & 0 deletions tests/spdx/parser/all_formats/test_parse_from_file.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# SPDX-FileCopyrightText: 2022 spdx contributors
#
# SPDX-License-Identifier: Apache-2.0

import os

import pytest

from spdx.model.document import Document
from spdx.parser.json import json_parser
from spdx.parser.rdf import rdf_parser
from spdx.parser.tagvalue import tagvalue_parser
from spdx.parser.xml import xml_parser
from spdx.parser.yaml import yaml_parser


@pytest.mark.parametrize(
"parser, format_name, extension",
[
(json_parser, "JSON", ".json"),
(xml_parser, "XML", ".xml"),
(yaml_parser, "YAML", ".yaml"),
(rdf_parser, "Rdf", ".rdf.xml"),
(tagvalue_parser, "Tag", ""),
],
)
class TestParseFromFile:
def test_parse_from_file_not_found(self, parser, format_name, extension):
with pytest.raises(FileNotFoundError) as err:
wrong_file_path = os.path.join(os.path.dirname(__file__), f"hnjfkjsedhnflsiafg.spdx{extension}")
parser.parse_from_file(wrong_file_path)

assert err.value.args[1] == "No such file or directory"

def test_parse_from_file_with_2_3_example(self, parser, format_name, extension):
doc = parser.parse_from_file(
os.path.join(
os.path.dirname(__file__), f"../../data/formats/SPDX{format_name}Example-v2.3.spdx{extension}"
)
)
assert type(doc) == Document
assert len(doc.annotations) == 5
assert len(doc.files) == 5
assert len(doc.packages) == 4
assert len(doc.snippets) == 1
assert len(doc.relationships) == 13
assert len(doc.extracted_licensing_info) == 5

def test_parse_json_with_2_2_example(self, parser, format_name, extension):
doc = parser.parse_from_file(
os.path.join(
os.path.dirname(__file__), f"../../data/formats/SPDX{format_name}Example-v2.2.spdx{extension}"
)
)
assert type(doc) == Document
assert len(doc.annotations) == 5
assert len(doc.files) == 4
assert len(doc.packages) == 4
assert len(doc.snippets) == 1
assert len(doc.relationships) == 11
assert len(doc.extracted_licensing_info) == 5
44 changes: 0 additions & 44 deletions tests/spdx/parser/json/test_json_parser.py

This file was deleted.

48 changes: 0 additions & 48 deletions tests/spdx/parser/rdf/test_rdf_parser.py

This file was deleted.

18 changes: 0 additions & 18 deletions tests/spdx/parser/tagvalue/test_tag_value_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@
# SPDX-FileCopyrightText: 2023 spdx contributors
#
# SPDX-License-Identifier: Apache-2.0
import os

import pytest

from spdx.constants import DOCUMENT_SPDX_ID
from spdx.model.document import Document
from spdx.model.relationship import Relationship, RelationshipType
from spdx.parser.error import SPDXParsingError
from spdx.parser.tagvalue.parser import Parser
Expand All @@ -22,22 +20,6 @@ def test_parse_unknown_tag():
parser.parse(unknown_tag_str)


def test_tag_value_parser():
parser = Parser()
fn = os.path.join(os.path.dirname(__file__), "../../data/formats/SPDXTagExample-v2.3.spdx")

with open(fn) as f:
data = f.read()
doc = parser.parse(data)
assert type(doc) == Document
assert len(doc.annotations) == 5
assert len(doc.files) == 5
assert len(doc.packages) == 4
assert len(doc.snippets) == 1
assert len(doc.relationships) == 13
assert len(doc.extracted_licensing_info) == 5


def test_building_contains_relationship():
parser = Parser()
document_str = "\n".join(
Expand Down

0 comments on commit 5feba30

Please sign in to comment.