Skip to content

Commit

Permalink
Enable unit tests to run in PyCharm
Browse files Browse the repository at this point in the history
PyCharm has trouble keeping the working directory consistent, so make
the tests look for files relative to the test file instead of the CWD
  • Loading branch information
d10n committed Nov 7, 2024
1 parent 63e21ab commit 5ddc4c9
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 18 deletions.
4 changes: 3 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,9 @@ def tmp_empty_cwd(tmp_path: pathlib.Path) -> Iterator[pathlib.Path]:
@pytest.fixture(scope='function')
def testdata_dir() -> pathlib.Path:
"""Return absolute path to test data directory."""
test_data_source = pathlib.Path('tests/data')
test_dir = pathlib.Path(__file__).parent.resolve()
data_path = test_dir / 'data'
test_data_source = pathlib.Path(data_path)
return test_data_source.resolve()


Expand Down
17 changes: 9 additions & 8 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,26 +53,27 @@

logger = logging.getLogger(__name__)

BASE_TMP_DIR = pathlib.Path('tests/__tmp_path').resolve()
YAML_TEST_DATA_PATH = pathlib.Path('tests/data/yaml/').resolve()
JSON_TEST_DATA_PATH = pathlib.Path('tests/data/json/').resolve()
ENV_TEST_DATA_PATH = pathlib.Path('tests/data/env/').resolve()
JSON_NIST_DATA_PATH = pathlib.Path('nist-content/nist.gov/SP800-53/rev5/json/').resolve()
TEST_DIR = pathlib.Path(__file__).parent.resolve()
BASE_TMP_DIR = pathlib.Path(TEST_DIR / '__tmp_path').resolve()
YAML_TEST_DATA_PATH = pathlib.Path(TEST_DIR / 'data/yaml/').resolve()
JSON_TEST_DATA_PATH = pathlib.Path(TEST_DIR / 'data/json/').resolve()
ENV_TEST_DATA_PATH = pathlib.Path(TEST_DIR / 'data/env/').resolve()
JSON_NIST_DATA_PATH = pathlib.Path(TEST_DIR / '../nist-content/nist.gov/SP800-53/rev5/json/').resolve()
JSON_NIST_CATALOG_NAME = 'NIST_SP-800-53_rev5_catalog.json'
JSON_NIST_PROFILE_NAME = 'NIST_SP-800-53_rev5_MODERATE-baseline_profile.json'
JSON_NIST_REV_4_DATA_PATH = pathlib.Path('nist-content/nist.gov/SP800-53/rev4/json/').resolve()
JSON_NIST_REV_4_DATA_PATH = pathlib.Path(TEST_DIR / '../nist-content/nist.gov/SP800-53/rev4/json/').resolve()
JSON_NIST_REV_4_CATALOG_NAME = 'NIST_SP-800-53_rev4_catalog.json'
JSON_NIST_REV_5_CATALOG_NAME = 'nist-rev5-catalog-full.json'
JSON_NIST_REV_4_PROFILE_NAME = 'NIST_SP-800-53_rev4_MODERATE-baseline_profile.json'
SIMPLIFIED_NIST_CATALOG_NAME = 'simplified_nist_catalog.json'
SIMPLIFIED_NIST_PROFILE_NAME = 'simplified_nist_profile.json'
TASK_XLSX_OUTPUT_PATH = pathlib.Path('tests/data/tasks/xlsx/output').resolve()
TASK_XLSX_OUTPUT_PATH = pathlib.Path(TEST_DIR / 'data/tasks/xlsx/output').resolve()

CATALOGS_DIR = 'catalogs'
PROFILES_DIR = 'profiles'
COMPONENT_DEF_DIR = 'component-definitions'

NIST_EXAMPLES = pathlib.Path('nist-content/examples')
NIST_EXAMPLES = pathlib.Path(TEST_DIR / '../nist-content/examples')
NIST_SAMPLE_CD_JSON = NIST_EXAMPLES / 'component-definition' / 'json' / 'example-component.json'

NEW_MODEL_AGE_SECONDS = 100
Expand Down
18 changes: 12 additions & 6 deletions tests/trestle/core/commands/validate_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
from trestle.oscal.common import ResponsibleParty, Role
from trestle.oscal.component import ComponentDefinition, ControlImplementation

test_data_dir = pathlib.Path('tests/data').resolve()

md_path = 'md_comp'

Expand All @@ -58,7 +57,8 @@
('my_test_model', '-t', False), ('my_test_model', '-a', False), ('my_test_model', '-x', False)
]
)
def test_validation_happy(name, mode, parent, tmp_trestle_dir: pathlib.Path, monkeypatch: MonkeyPatch) -> None:
def test_validation_happy(name, mode, parent, tmp_trestle_dir: pathlib.Path, testdata_dir: pathlib.Path, monkeypatch: MonkeyPatch) -> None:
test_data_dir = testdata_dir
"""Test successful validation runs."""
(tmp_trestle_dir / test_utils.CATALOGS_DIR / 'my_test_model').mkdir(exist_ok=True, parents=True)
(tmp_trestle_dir / test_utils.CATALOGS_DIR / 'my_test_model2').mkdir(exist_ok=True, parents=True)
Expand Down Expand Up @@ -101,8 +101,9 @@ def test_validation_happy(name, mode, parent, tmp_trestle_dir: pathlib.Path, mon
]
)
def test_validation_unhappy(
name, mode, parent, status, tmp_trestle_dir: pathlib.Path, monkeypatch: MonkeyPatch
name, mode, parent, status, tmp_trestle_dir: pathlib.Path, testdata_dir: pathlib.Path, monkeypatch: MonkeyPatch
) -> None:
test_data_dir = testdata_dir
"""Test failure modes of validation."""
(tmp_trestle_dir / test_utils.CATALOGS_DIR / 'my_test_model').mkdir(exist_ok=True, parents=True)
(tmp_trestle_dir / test_utils.CATALOGS_DIR / 'my_test_model2').mkdir(exist_ok=True, parents=True)
Expand Down Expand Up @@ -421,8 +422,10 @@ def test_period(tmp_trestle_dir: pathlib.Path, monkeypatch: MonkeyPatch) -> None
pass


def test_validate_component_definition(tmp_trestle_dir: pathlib.Path, monkeypatch: MonkeyPatch) -> None:
def test_validate_component_definition(tmp_trestle_dir: pathlib.Path, testdata_dir: pathlib.Path, monkeypatch: MonkeyPatch) -> None:
"""Test validation of Component Definition."""
test_data_dir = testdata_dir

jfile = 'component-definition.json'

sdir = test_data_dir / 'validate' / 'component-definitions' / 'x1'
Expand All @@ -438,8 +441,9 @@ def test_validate_component_definition(tmp_trestle_dir: pathlib.Path, monkeypatc
test_utils.execute_command_and_assert(validate_command, 0, monkeypatch)


def test_validate_component_definition_ports(tmp_trestle_dir: pathlib.Path, monkeypatch: MonkeyPatch) -> None:
def test_validate_component_definition_ports(tmp_trestle_dir: pathlib.Path, testdata_dir: pathlib.Path, monkeypatch: MonkeyPatch) -> None:
"""Test validation of ports in Component Definition."""
test_data_dir = testdata_dir
jfile = 'component-definition.json'

sdir = test_data_dir / 'validate' / 'component-definitions' / 'x2'
Expand All @@ -455,8 +459,10 @@ def test_validate_component_definition_ports(tmp_trestle_dir: pathlib.Path, monk
test_utils.execute_command_and_assert(validate_command, 0, monkeypatch)


def test_validate_component_definition_ports_invalid(tmp_trestle_dir: pathlib.Path, monkeypatch: MonkeyPatch) -> None:
def test_validate_component_definition_ports_invalid(tmp_trestle_dir: pathlib.Path, testdata_dir: pathlib.Path, monkeypatch: MonkeyPatch) -> None:
"""Test validation of ports in Component Definition."""
test_data_dir = testdata_dir

jfile = 'component-definition.json'

sdir = test_data_dir / 'validate' / 'component-definitions' / 'x3'
Expand Down
7 changes: 4 additions & 3 deletions tests/trestle/core/control_io_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
from trestle.common.model_utils import ModelUtils
from trestle.core.catalog.catalog_interface import CatalogInterface
from trestle.core.control_context import ContextPurpose, ControlContext
from trestle.core.control_interface import ControlInterface, ParameterRep
from trestle.core.control_interface import ControlInterface, ParameterRep, ComponentImpInfo
from trestle.core.control_reader import ControlReader
from trestle.core.control_writer import ControlWriter
from trestle.core.markdown.control_markdown_node import ControlMarkdownNode, tree_context
Expand All @@ -39,6 +39,7 @@
from trestle.core.models.file_content_type import FileContentType
from trestle.core.profile_resolver import ProfileResolver
from trestle.oscal import common
from trestle.oscal.common import ImplementationStatus

case_1 = 'indent_normal'
case_2 = 'indent jump back 2'
Expand Down Expand Up @@ -338,10 +339,10 @@ def test_get_control_param_dict(tmp_trestle_dir: pathlib.Path) -> None:


@pytest.mark.parametrize('overwrite_header_values', [True, False])
def test_write_control_header_params(overwrite_header_values, tmp_path: pathlib.Path) -> None:
def test_write_control_header_params(overwrite_header_values, tmp_path: pathlib.Path, testdata_dir: pathlib.Path) -> None:
"""Test write/read of control header params."""
# orig file just has one param ac-1_prm_3
src_control_path = pathlib.Path('tests/data/author/controls/control_with_components_and_params.md')
src_control_path = pathlib.Path(testdata_dir / 'author/controls/control_with_components_and_params.md')
# header has two params - 3 and 4
header = {
const.SET_PARAMS_TAG: {
Expand Down

0 comments on commit 5ddc4c9

Please sign in to comment.