Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/install_and_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ jobs:
- name: Run tests
run: pytest
- name: Run CLI
run: pyspdxtools -i ./tests/spdx/data/formats/SPDXJSONExample-v2.3.spdx.json
run: pyspdxtools -i ./tests/spdx/data/SPDXJSONExample-v2.3.spdx.json

- name: Install optional dependencies
run: python -m pip install networkx
Expand Down
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,32 +64,32 @@ instead of `bin`.
* Use `pyspdxtools -i <filename>` where `<filename>` is the location of the file. The input format is inferred automatically from the file ending.

* If you are using a source distribution, try running:
`pyspdxtools -i tests/data/formats/SPDXJSONExample-v2.3.spdx.json`
`pyspdxtools -i tests/data/SPDXJSONExample-v2.3.spdx.json`

2. **CONVERTING** (for converting one format to another):

* Use `pyspdxtools -i <input_file> -o <output_file>` where `<input_file>` is the location of the file to be converted
and `<output_file>` is the location of the output file. The input and output formats are inferred automatically from the file endings.

* If you are using a source distribution, try running:
`pyspdxtools -i tests/data/formats/SPDXJSONExample-v2.3.spdx.json -o output.tag`
`pyspdxtools -i tests/data/SPDXJSONExample-v2.3.spdx.json -o output.tag`

* If you want to skip the validation process, provide the `--novalidation` flag, like so:
`pyspdxtools -i tests/data/formats/SPDXJSONExample-v2.3.spdx.json -o output.tag --novalidation`
`pyspdxtools -i tests/data/SPDXJSONExample-v2.3.spdx.json -o output.tag --novalidation`
(use this with caution: note that undetected invalid documents may lead to unexpected behavior of the tool)

* For help use `pyspdxtools --help`

3. **GRAPH GENERATION** (optional feature)

* This feature generates a graph representing all elements in the SPDX document and their connections based on the provided
relationships. The graph can be rendered to a picture. Below is an example for the file `tests/data/formats/SPDXJSONExample-v2.3.spdx.json`:
relationships. The graph can be rendered to a picture. Below is an example for the file `tests/data/SPDXJSONExample-v2.3.spdx.json`:
![SPDXJSONExample-v2.3.spdx.png](assets/SPDXJSONExample-v2.3.spdx.png)
* Make sure you install the optional dependencies `networkx` and `pygraphviz`. To do so run `pip install ".[graph_generation]"`.
* Use `pyspdxtools -i <input_file> --graph -o <output_file>` where `<output_file>` is an output file name with valid format for `pygraphviz` (check
the documentation [here](https://pygraphviz.github.io/documentation/stable/reference/agraph.html#pygraphviz.AGraph.draw)).
* If you are using a source distribution, try running
`pyspdxtools -i tests/data/formats/SPDXJSONExample-v2.3.spdx.json --graph -o SPDXJSONExample-v2.3.spdx.png` to generate
`pyspdxtools -i tests/data/SPDXJSONExample-v2.3.spdx.json --graph -o SPDXJSONExample-v2.3.spdx.png` to generate
a png with an overview of the structure of the example file.

## Library usage
Expand Down
329 changes: 329 additions & 0 deletions tests/spdx/data/SPDXTagExample-v2.2.spdx

Large diffs are not rendered by default.

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

Large diffs are not rendered by default.

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

Large diffs are not rendered by default.

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

Large diffs are not rendered by default.

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

Large diffs are not rendered by default.

57 changes: 57 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,57 @@
# 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/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/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.

2 changes: 1 addition & 1 deletion tests/spdx/parser/rdf/test_license_expression_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def test_license_expression_parser():

def test_license_expression_parser_with_coupled_licenses():
doc = rdf_parser.parse_from_file(
os.path.join(os.path.dirname(__file__), "../../data/formats/SPDXRdfExample-v2.3.spdx.rdf.xml")
os.path.join(os.path.dirname(__file__), "../../data/SPDXRdfExample-v2.3.spdx.rdf.xml")
)

packages_by_spdx_id = {package.spdx_id: package for package in doc.packages}
Expand Down
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
2 changes: 1 addition & 1 deletion tests/spdx/test_graph_generation.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def test_generate_graph_from_spdx(
edges_count: int,
relationship_node_keys: List[str],
) -> None:
document = parse_file(str(Path(__file__).resolve().parent.parent / "spdx" / "data" / "formats" / file_name))
document = parse_file(str(Path(__file__).resolve().parent.parent / "spdx" / "data" / file_name))
graph = generate_relationship_graph_from_spdx(document)

assert document.creation_info.spdx_id in graph.nodes()
Expand Down