Skip to content

Commit a8ebc17

Browse files
committed
feat: introduce opossum files to CLI
* create interface with basic test * create empty function shell for conversion
1 parent bc5054a commit a8ebc17

File tree

4 files changed

+99
-15
lines changed

4 files changed

+99
-15
lines changed

src/opossum_lib/cli.py

+32-10
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
import click
1313

1414
from opossum_lib.opossum.file_generation import write_opossum_information_to_file
15+
from opossum_lib.opossum.opossum_file import OpossumInformation
16+
from opossum_lib.opossum.read_opossum_file import read_opossum_file
1517
from opossum_lib.spdx.convert_to_opossum import convert_spdx_to_opossum_information
1618

1719

@@ -27,6 +29,12 @@ def opossum_file() -> None:
2729
multiple=True,
2830
type=click.Path(exists=True),
2931
)
32+
@click.option(
33+
"--opossum",
34+
help="opossum files used as input.",
35+
multiple=True,
36+
type=click.Path(exists=True),
37+
)
3038
@click.option(
3139
"--outfile",
3240
"-o",
@@ -35,23 +43,16 @@ def opossum_file() -> None:
3543
help="The file path to write the generated opossum document to. "
3644
'If appropriate, the extension ".opossum" will be appended.',
3745
)
38-
def generate(spdx: list[str], outfile: str) -> None:
46+
def generate(spdx: list[str], opossum: list[str], outfile: str) -> None:
3947
"""
4048
Generate an Opossum file from various other file formats.
4149
4250
\b
4351
Currently supported input formats:
4452
- SPDX
4553
"""
46-
if len(spdx) == 0:
47-
logging.warning("No input provided. Exiting.")
48-
sys.exit(1)
49-
if len(spdx) > 1:
50-
logging.error("Merging of multiple SPDX files not yet supported!")
51-
sys.exit(1)
52-
53-
the_spdx_file = spdx[0]
54-
opossum_information = convert_spdx_to_opossum_information(the_spdx_file)
54+
validate_input_exit_on_error(spdx, opossum)
55+
opossum_information = convert_after_valid_input(spdx, opossum)
5556

5657
if not outfile.endswith(".opossum"):
5758
outfile += ".opossum"
@@ -62,5 +63,26 @@ def generate(spdx: list[str], outfile: str) -> None:
6263
write_opossum_information_to_file(opossum_information, Path(outfile))
6364

6465

66+
def validate_input_exit_on_error(spdx: list[str], opossum: list[str]) -> None:
67+
total_number_of_files = len(spdx) + len(opossum)
68+
if total_number_of_files == 0:
69+
logging.warning("No input provided. Exiting.")
70+
sys.exit(1)
71+
if total_number_of_files > 1:
72+
logging.error("Merging of multiple files not yet supported!")
73+
sys.exit(1)
74+
75+
76+
def convert_after_valid_input(
77+
spdx: list[str], opossum_files: list[str]
78+
) -> OpossumInformation:
79+
if len(spdx) == 1:
80+
the_spdx_file = spdx[0]
81+
return convert_spdx_to_opossum_information(the_spdx_file)
82+
else:
83+
opossum_file = opossum_files[0]
84+
return read_opossum_file(opossum_file)
85+
86+
6587
if __name__ == "__main__":
6688
opossum_file()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# SPDX-FileCopyrightText: TNG Technology Consulting GmbH <https://www.tngtech.com>
2+
#
3+
# SPDX-License-Identifier: Apache-2.0
4+
import logging
5+
from datetime import datetime
6+
7+
from opossum_lib.opossum.opossum_file import (
8+
Metadata,
9+
OpossumInformation,
10+
Resource,
11+
ResourceType,
12+
)
13+
14+
15+
def read_opossum_file(filename: str) -> OpossumInformation:
16+
logging.info(f"Converting opossum to opossum {filename}")
17+
dummy_metadata = Metadata(
18+
projectId="test id",
19+
fileCreationDate=datetime.now().isoformat(),
20+
projectTitle="test title",
21+
)
22+
return OpossumInformation(
23+
metadata=dummy_metadata,
24+
resources=Resource(type=ResourceType.FILE, children={}),
25+
externalAttributions={},
26+
resourcesToAttributions={},
27+
attributionBreakpoints=[],
28+
externalAttributionSources={},
29+
)

tests/data/opossum_input.opossum

34.7 KB
Binary file not shown.

tests/test_cli.py

+38-5
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,19 @@
1313
from opossum_lib.cli import generate
1414
from tests.test_spdx.helper_methods import _create_minimal_document
1515

16+
test_data_path = Path(__file__).resolve().parent / "data"
17+
18+
19+
def generate_valid_spdx_argument(filename: str = "SPDX.spdx") -> str:
20+
return "--spdx " + str(test_data_path / filename)
21+
22+
23+
def generate_valid_opossum_argument(filename: str = "opossum_input.opossum") -> str:
24+
return "--opossum " + str(test_data_path / filename)
25+
1626

1727
@pytest.mark.parametrize("options", ["--outfile", "-o"])
18-
def test_cli_with_system_exit_code_0(tmp_path: Path, options: str) -> None:
28+
def test_successful_conversion_of_spdx_file(tmp_path: Path, options: str) -> None:
1929
runner = CliRunner()
2030

2131
result = runner.invoke(
@@ -47,6 +57,22 @@ def test_cli_with_system_exit_code_0(tmp_path: Path, options: str) -> None:
4757
assert opossum_dict == expected_opossum_dict
4858

4959

60+
def test_successful_conversion_of_opossum_file(tmp_path: Path) -> None:
61+
runner = CliRunner()
62+
63+
result = runner.invoke(
64+
generate,
65+
[
66+
"--opossum",
67+
str(Path(__file__).resolve().parent / "data" / "opossum_input.opossum"),
68+
"-o",
69+
str(tmp_path / "output_opossum"),
70+
],
71+
)
72+
73+
assert result.exit_code == 0
74+
75+
5076
def test_cli_no_output_file_provided() -> None:
5177
runner = CliRunner()
5278

@@ -104,17 +130,24 @@ def test_cli_with_invalid_document(caplog: LogCaptureFixture) -> None:
104130
]
105131

106132

107-
def test_cli_with_multiple_documents(caplog: LogCaptureFixture) -> None:
133+
@pytest.mark.parametrize(
134+
"options",
135+
[
136+
generate_valid_spdx_argument() + " " + generate_valid_spdx_argument(),
137+
generate_valid_spdx_argument() + " " + generate_valid_opossum_argument(),
138+
generate_valid_opossum_argument() + " " + generate_valid_opossum_argument(),
139+
],
140+
)
141+
def test_cli_with_multiple_files(caplog: LogCaptureFixture, options: list[str]) -> None:
108142
runner = CliRunner()
109-
path_to_spdx = str(Path(__file__).resolve().parent / "data" / "SPDX.spdx")
110143

111144
result = runner.invoke(
112145
generate,
113-
["--spdx", path_to_spdx, "--spdx", path_to_spdx],
146+
options,
114147
)
115148
assert result.exit_code == 1
116149

117-
assert caplog.messages == ["Merging of multiple SPDX files not yet supported!"]
150+
assert caplog.messages == ["Merging of multiple files not yet supported!"]
118151

119152

120153
def test_cli_without_inputs(caplog: LogCaptureFixture) -> None:

0 commit comments

Comments
 (0)