Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move unit tests to the unit tests directory #152

Merged
merged 2 commits into from
Apr 11, 2023
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
41 changes: 2 additions & 39 deletions tests/integration_tests/test_cli.py
Original file line number Diff line number Diff line change
@@ -1,61 +1,24 @@
# Copyright 2022, Red Hat, Inc.
# SPDX-License-Identifier: LGPL-2.1-or-later

import argparse
import subprocess
import tempfile
from io import BytesIO
from pathlib import Path
from unittest import mock

import pytest

from openscap_report.cli import CommandLineAPI
from openscap_report.scap_results_parser import (ARF_SCHEMAS_PATH,
SCAPResultsParser)
from openscap_report.scap_results_parser import SCAPResultsParser

from ..constants import PATH_TO_ARF, PATH_TO_EMPTY_FILE
from ..test_utils import get_fake_args

PATH_TO_RESULT_FILE = Path(tempfile.gettempdir()) / "oscap-report-tests_result.html"
OSCAP_REPORT_COMMAND = "oscap-report"
CAT_ARF_FILE = ["cat", str(PATH_TO_ARF)]


def get_fake_args():
# pylint: disable=bad-option-value,R1732
input_file = open(PATH_TO_ARF, "r", encoding="utf-8")
output_file = open(PATH_TO_RESULT_FILE, "wb")
return argparse.Namespace(
FILE=input_file, output=output_file,
log_file=None, log_level="WARNING", format="HTML",
debug=[""],
)


@pytest.mark.unit_test
@mock.patch('argparse.ArgumentParser.parse_args',
return_value=get_fake_args())
def test_load_file(mock_args): # pylint: disable=W0613
api = CommandLineAPI()
xml_report = api.load_file()
parser = SCAPResultsParser(xml_report)
assert parser.validate(ARF_SCHEMAS_PATH)
api.close_files()


@pytest.mark.unit_test
@pytest.mark.usefixtures("remove_generated_file")
@mock.patch('argparse.ArgumentParser.parse_args',
return_value=get_fake_args())
def test_store_file(mock_args): # pylint: disable=W0613
api = CommandLineAPI()
data = BytesIO(b'<html><h1>TEST DATA</h1></html>')
api.store_file(data)
api.close_files()
with open(PATH_TO_RESULT_FILE, "r", encoding="utf-8") as result_file:
assert result_file.read() == data.getvalue().decode("utf-8")


@pytest.mark.integration_test
@mock.patch('argparse.ArgumentParser.parse_args',
return_value=get_fake_args())
Expand Down
16 changes: 16 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
# Copyright 2022, Red Hat, Inc.
# SPDX-License-Identifier: LGPL-2.1-or-later

import argparse
import tempfile
from dataclasses import replace
from pathlib import Path

try:
from functools import cache
Expand All @@ -22,6 +25,8 @@
PATH_TO_ARF_REPRODUCING_DANGLING_REFERENCE_TO)
from .unit_tests.test_oval_tree_eval import OVAL_TREE_TRUE

PATH_TO_RESULT_FILE = Path(tempfile.gettempdir()) / "oscap-report-tests_result.html"


@cache
def get_xml_data(file_path):
Expand Down Expand Up @@ -104,3 +109,14 @@ def get_dummy_cpe_oval_definition():
"oval:ssg-installed_env_has_zipl_package:def:1": dummy_oval_definition,
"oval:ssg-system_boot_mode_is_uefi:def:1": dummy_oval_definition,
}


def get_fake_args():
# pylint: disable=bad-option-value,R1732
input_file = open(PATH_TO_ARF, "r", encoding="utf-8")

Check warning

Code scanning / CodeQL

File is not always closed

File is opened but is not closed.
output_file = open(PATH_TO_RESULT_FILE, "wb")

Check warning

Code scanning / CodeQL

File is not always closed

File is opened but is not closed.
return argparse.Namespace(
FILE=input_file, output=output_file,
log_file=None, log_level="WARNING", format="HTML",
debug=[""],
)
41 changes: 41 additions & 0 deletions tests/unit_tests/test_cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Copyright 2022, Red Hat, Inc.
# SPDX-License-Identifier: LGPL-2.1-or-later

import tempfile
from io import BytesIO
from pathlib import Path
from unittest import mock

import pytest

from openscap_report.cli import CommandLineAPI
from openscap_report.scap_results_parser import (ARF_SCHEMAS_PATH,
SCAPResultsParser)

from ..test_utils import get_fake_args

PATH_TO_RESULT_FILE = Path(tempfile.gettempdir()) / "oscap-report-tests_result.html"


@pytest.mark.unit_test
@mock.patch('argparse.ArgumentParser.parse_args',
return_value=get_fake_args())
def test_load_file(mock_args): # pylint: disable=W0613
api = CommandLineAPI()
xml_report = api.load_file()
parser = SCAPResultsParser(xml_report)
assert parser.validate(ARF_SCHEMAS_PATH)
api.close_files()


@pytest.mark.unit_test
@pytest.mark.usefixtures("remove_generated_file")
@mock.patch('argparse.ArgumentParser.parse_args',
return_value=get_fake_args())
def test_store_file(mock_args): # pylint: disable=W0613
api = CommandLineAPI()
data = BytesIO(b'<html><h1>TEST DATA</h1></html>')
api.store_file(data)
api.close_files()
with open(PATH_TO_RESULT_FILE, "r", encoding="utf-8") as result_file:
assert result_file.read() == data.getvalue().decode("utf-8")