Skip to content

Commit

Permalink
refactor: cleanup
Browse files Browse the repository at this point in the history
* Cleanup tests
* Introduce a few core constants
  • Loading branch information
Hellgartner committed Jan 14, 2025
1 parent 339336c commit 73f691a
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 57 deletions.
2 changes: 2 additions & 0 deletions src/opossum_lib/opossum/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
#
# SPDX-License-Identifier: Apache-2.0
COMPRESSION_LEVEL = 5
INPUT_JSON_NAME = "input.json"
OUTPUT_JSON_NAME = "output.json"
4 changes: 2 additions & 2 deletions src/opossum_lib/opossum/file_generation.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from pydantic import TypeAdapter

from opossum_lib.opossum.constants import COMPRESSION_LEVEL
from opossum_lib.opossum.constants import COMPRESSION_LEVEL, INPUT_JSON_NAME
from opossum_lib.opossum.opossum_file import (
OpossumInformation,
)
Expand All @@ -19,7 +19,7 @@ def write_opossum_information_to_file(
file_path, "w", compression=ZIP_DEFLATED, compresslevel=COMPRESSION_LEVEL
) as z:
z.writestr(
"input.json",
INPUT_JSON_NAME,
TypeAdapter(OpossumInformation).dump_json(
opossum_information, indent=4, exclude_none=True
),
Expand Down
11 changes: 6 additions & 5 deletions src/opossum_lib/opossum/read_opossum_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

from pydantic import TypeAdapter

from opossum_lib.opossum.constants import INPUT_JSON_NAME, OUTPUT_JSON_NAME
from opossum_lib.opossum.opossum_file import (
OpossumInformation,
)
Expand All @@ -21,7 +22,7 @@ def read_opossum_file(filename: str) -> OpossumInformation:
ZipFile(filename, "r") as input_zip_file,
):
validate_zip_file_contents(input_zip_file)
with input_zip_file.open("input.json") as input_json_file:
with input_zip_file.open(INPUT_JSON_NAME) as input_json_file:
input_json = json.load(input_json_file)
return TypeAdapter(OpossumInformation).validate_python(input_json)
except Exception as e:
Expand All @@ -31,15 +32,15 @@ def read_opossum_file(filename: str) -> OpossumInformation:


def validate_zip_file_contents(input_zip_file: ZipFile) -> None:
if "input.json" not in input_zip_file.namelist():
if INPUT_JSON_NAME not in input_zip_file.namelist():
logging.error(
f"Opossum file {input_zip_file.filename} is corrupt"
f" and does not contain 'input.json'"
f" and does not contain '{INPUT_JSON_NAME}'"
)
sys.exit(1)
if "output.json" in input_zip_file.namelist():
if OUTPUT_JSON_NAME in input_zip_file.namelist():
logging.error(
f"Opossum file {input_zip_file.filename} also contains"
f" 'output.json' which cannot be processed"
f" '{OUTPUT_JSON_NAME}' which cannot be processed"
)
sys.exit(1)
90 changes: 41 additions & 49 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@
# SPDX-License-Identifier: Apache-2.0
import json
from pathlib import Path
from typing import Any
from zipfile import ZipFile

import pytest
from _pytest.logging import LogCaptureFixture
from click.testing import CliRunner
from click.testing import CliRunner, Result
from spdx_tools.spdx.writer.write_anything import write_file

from opossum_lib.cli import generate
from opossum_lib.opossum.constants import INPUT_JSON_NAME
from tests.test_spdx.helper_methods import _create_minimal_document

test_data_path = Path(__file__).resolve().parent / "data"
Expand All @@ -24,31 +26,28 @@ def generate_valid_opossum_argument(filename: str = "opossum_input.opossum") ->
return "--opossum " + str(test_data_path / filename)


@pytest.mark.parametrize("options", ["--outfile", "-o"])
def test_successful_conversion_of_spdx_file(tmp_path: Path, options: str) -> None:
def run_with_command_line_arguments(cmd_line_arguments: list[str]) -> Result:
runner = CliRunner()
result = runner.invoke(generate, cmd_line_arguments)
return result

result = runner.invoke(
generate,

@pytest.mark.parametrize("options", ["--outfile", "-o"])
def test_successful_conversion_of_spdx_file(tmp_path: Path, options: str) -> None:
result = run_with_command_line_arguments(
[
"--spdx",
str(Path(__file__).resolve().parent / "data" / "SPDX.spdx"),
str(test_data_path / "SPDX.spdx"),
options,
str(tmp_path / "output"),
],
]
)
with open(
Path(__file__).resolve().parent / "data" / "expected_opossum.json"
) as file:
expected_opossum_dict = json.load(file)

assert result.exit_code == 0

with (
ZipFile(str(tmp_path / "output.opossum"), "r") as z,
z.open("input.json") as file,
):
opossum_dict = json.load(file)
opossum_dict = read_input_json_from_opossum(str(tmp_path / "output.opossum"))
expected_opossum_dict = read_json_from_file("expected_opossum.json")

assert "metadata" in opossum_dict
# we are using randomly generated UUIDs for the project-id, therefore
# we need to exclude the "metadata" section from the comparison
Expand All @@ -58,44 +57,46 @@ def test_successful_conversion_of_spdx_file(tmp_path: Path, options: str) -> Non


def test_successful_conversion_of_opossum_file(tmp_path: Path) -> None:
runner = CliRunner()

output_file = str(tmp_path / "output_opossum.opossum")
result = runner.invoke(
generate,
result = run_with_command_line_arguments(
[
"--opossum",
str(Path(__file__).resolve().parent / "data" / "opossum_input.opossum"),
str(test_data_path / "opossum_input.opossum"),
"-o",
output_file,
],
)

with open(Path(__file__).resolve().parent / "data" / "opossum_input.json") as file:
expected_opossum_dict = json.load(file)
assert result.exit_code == 0
expected_opossum_dict = read_json_from_file("opossum_input.json")
opossum_dict = read_input_json_from_opossum(output_file)

# Doing individual asserts as otherwise the diff viewer does no longer work
# in case of errors
assert result.exit_code == 0
assert_expected_opossum_equals_generated_opossum(
expected_opossum_dict, opossum_dict
)


def read_input_json_from_opossum(output_file_path: str) -> Any:
with (
ZipFile(output_file, "r") as z,
z.open("input.json") as file,
ZipFile(output_file_path, "r") as z,
z.open(INPUT_JSON_NAME) as file,
):
opossum_dict = json.load(file)
return opossum_dict

## goal
# metadata
# resources
# externalAttributions
# resourcesToAttributions
# frequentLicenses
# attributionBreakpoints
# filesWithChildren
# baseUrlsForSources
# externalAttributionSources

# Doing individual asserts as otherwise the diff viewer does no longer work
# in case of errors
assert result.exit_code == 0
def read_json_from_file(filename: str) -> Any:
with open(test_data_path / filename) as file:
expected_opossum_dict = json.load(file)
return expected_opossum_dict


def assert_expected_opossum_equals_generated_opossum(
expected_opossum_dict: Any, opossum_dict: Any
) -> None:
assert opossum_dict["metadata"] == expected_opossum_dict["metadata"]
assert opossum_dict["resources"] == expected_opossum_dict["resources"]
assert (
Expand Down Expand Up @@ -136,7 +137,6 @@ def test_cli_no_output_file_provided() -> None:
)

assert result.exit_code == 0

assert Path.is_file(Path("output.opossum"))


Expand Down Expand Up @@ -190,22 +190,14 @@ def test_cli_with_invalid_document(caplog: LogCaptureFixture) -> None:
],
)
def test_cli_with_multiple_files(caplog: LogCaptureFixture, options: list[str]) -> None:
runner = CliRunner()

result = runner.invoke(
generate,
options,
)
result = run_with_command_line_arguments(options)
assert result.exit_code == 1

assert caplog.messages == ["Merging of multiple files not yet supported!"]


def test_cli_without_inputs(caplog: LogCaptureFixture) -> None:
runner = CliRunner()

result = runner.invoke(
generate,
result = run_with_command_line_arguments(
[
"-o",
"output.opossum",
Expand Down
50 changes: 49 additions & 1 deletion tests/test_opossum/test_opossum_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@
# SPDX-License-Identifier: Apache-2.0
import pytest

from opossum_lib.opossum.opossum_file import Resource, ResourceType
from opossum_lib.opossum.opossum_file import (
Resource,
ResourceInFile,
ResourceType,
convert_resource_in_file_to_resource,
)


def test_resource_to_dict_with_file_as_leaf() -> None:
Expand Down Expand Up @@ -190,3 +195,46 @@ def test_resource_drop_element() -> None:
assert resource_without_element.get_paths_of_all_leaf_nodes_with_types() == [
[("A", ResourceType.FOLDER), ("B", ResourceType.FILE)]
]


def test_convert_resource_in_file_to_resource() -> None:
resource_in_file: ResourceInFile = {
"baseFolder": {
"subFolder": {},
"anotherSubFolder": {
"file1": 1,
"file2": 1,
"subSubFolder": {
"file3": 1,
},
},
"file4": 1,
}
}

resource = convert_resource_in_file_to_resource(resource_in_file)

expected_resource = Resource(
type=ResourceType.TOP_LEVEL,
children={
"baseFolder": Resource(
type=ResourceType.FOLDER,
children={
"subFolder": Resource(type=ResourceType.FOLDER),
"anotherSubFolder": Resource(
type=ResourceType.FOLDER,
children={
"file1": Resource(type=ResourceType.FILE),
"file2": Resource(type=ResourceType.FILE),
"subSubFolder": Resource(
type=ResourceType.FOLDER,
children={"file3": Resource(type=ResourceType.FILE)},
),
},
),
"file4": Resource(type=ResourceType.FILE),
},
)
},
)
assert resource == expected_resource

0 comments on commit 73f691a

Please sign in to comment.