def test_load_distributed(testdata_dir, tmp_trestle_dir): """Test massive distributed load, that includes recursive load and list.""" # prepare trestle project dir with the file test_utils.ensure_trestle_config_dir(tmp_trestle_dir) test_data_source = testdata_dir / 'split_merge/step4_split_groups_array/catalogs' catalogs_dir = tmp_trestle_dir / 'catalogs' mycatalog_dir = catalogs_dir / 'mycatalog' catalog_file = mycatalog_dir / 'catalog.json' # Copy files from test/data/split_merge/step4 shutil.rmtree(catalogs_dir) shutil.copytree(test_data_source, catalogs_dir) actual_model_type, actual_model_alias, actual_model_instance = ModelUtils.load_distributed( catalog_file, tmp_trestle_dir) expected_model_instance = Catalog.oscal_read( testdata_dir / 'split_merge/load_distributed/catalog.json') assert actual_model_type == Catalog assert actual_model_alias == 'catalog' assert test_utils.models_are_equivalent(expected_model_instance, actual_model_instance) # confirm it fails attempting to load collection type that is not a list with pytest.raises(TrestleError): actual_model_type, actual_model_alias, actual_model_instance = ModelUtils.load_distributed( catalog_file, tmp_trestle_dir, Dict)
def test_get_trestle_project_root(tmp_dir, rand_str): """Test get_trestle_project_root method.""" project_path: pathlib.Path = pathlib.Path.joinpath(tmp_dir, rand_str) sub_path: pathlib.Path = project_path.joinpath('samples2') fs.ensure_directory(sub_path) assert sub_path.exists() and sub_path.is_dir() # create a file sub_path.joinpath('readme.md').touch() # create a data-dir and a file sub_data_dir = pathlib.Path.joinpath(sub_path, 'data') fs.ensure_directory(sub_data_dir) sub_data_dir.joinpath('readme.md').touch() assert fs.get_trestle_project_root(sub_data_dir) is None test_utils.ensure_trestle_config_dir(project_path) assert fs.get_trestle_project_root(sub_data_dir) == project_path assert fs.get_trestle_project_root( sub_data_dir.joinpath('readme.md')) == project_path assert fs.get_trestle_project_root( sub_path.joinpath('readme.md')) == project_path assert fs.get_trestle_project_root(sub_path) == project_path assert fs.get_trestle_project_root(project_path.parent) is None
def test_load_list(testdata_dir, tmp_trestle_dir): """Test loading a list recursively.""" # prepare trestle project dir with the file test_utils.ensure_trestle_config_dir(tmp_trestle_dir) test_data_source = testdata_dir / 'split_merge/step4_split_groups_array/catalogs' catalogs_dir = Path('catalogs/') mycatalog_dir = catalogs_dir / 'mycatalog' catalog_dir = mycatalog_dir / 'catalog' # Copy files from test/data/split_merge/step4 shutil.rmtree(catalogs_dir) shutil.copytree(test_data_source, catalogs_dir) actual_model_type, actual_model_alias, actual_roles = _load_list( catalog_dir / 'metadata' / 'roles') expected_roles = [ Role.oscal_read(catalog_dir / 'metadata/roles/00000__role.json'), Role.oscal_read(catalog_dir / 'metadata/roles/00001__role.json') ] expected_model_type, _ = fs.get_stripped_contextual_model( (catalog_dir / 'metadata/roles').absolute()) assert actual_model_type.__signature__ == expected_model_type.__signature__ assert actual_model_alias == 'catalog.metadata.roles' assert test_utils.list_unordered_equal(actual_roles, expected_roles)
def test_load_distributed(testdata_dir, tmp_trestle_dir): """Test massive distributed load, that includes recusive load, list and dict.""" # prepare trestle project dir with the file test_utils.ensure_trestle_config_dir(tmp_trestle_dir) test_data_source = testdata_dir / 'split_merge/step4_split_groups_array/catalogs' catalogs_dir = Path('catalogs/') mycatalog_dir = catalogs_dir / 'mycatalog' catalog_file = mycatalog_dir / 'catalog.json' # Copy files from test/data/split_merge/step4 shutil.rmtree(catalogs_dir) shutil.copytree(test_data_source, catalogs_dir) actual_model_type, actual_model_alias, actual_model_instance = load_distributed( catalog_file) expected_model_type, _ = fs.get_contextual_model_type( catalog_file.absolute()) expected_model_instance = Catalog.oscal_read( testdata_dir / 'split_merge/load_distributed/catalog.json') assert actual_model_type == expected_model_type assert actual_model_alias == 'catalog' assert len( list(dictdiffer.diff(expected_model_instance, actual_model_instance))) == 0
def test_has_trestle_project_in_path(tmp_dir, rand_str): """Test has_trestle_project_in_path method.""" project_path: pathlib.Path = pathlib.Path.joinpath(tmp_dir, rand_str) sub_path: pathlib.Path = project_path.joinpath('samples2') fs.ensure_directory(sub_path) assert sub_path.exists() and sub_path.is_dir() # create a file sub_path.joinpath('readme.md').touch() # create a data-dir and a file sub_data_dir = pathlib.Path.joinpath(sub_path, 'data') fs.ensure_directory(sub_data_dir) # create a file sub_data_dir.joinpath('readme.md').touch() assert fs.has_trestle_project_in_path(pathlib.Path('/')) is False assert fs.has_trestle_project_in_path(sub_data_dir) is False test_utils.ensure_trestle_config_dir(project_path) assert fs.has_trestle_project_in_path(sub_data_dir) is True assert fs.has_trestle_project_in_path( sub_data_dir.joinpath('readme.md')) is True assert fs.has_trestle_project_in_path( sub_path.joinpath('readme.md')) is True assert fs.has_trestle_project_in_path(sub_path) is True assert fs.has_trestle_project_in_path(project_path.parent) is False
def test_merge_everything_into_catalog_with_hidden_files_in_folders( testdata_dir, tmp_trestle_dir): """Test trestle merge -e 'catalog.*' when metadata and catalog are split and hidden files are present.""" # Assume we are running a command like below # trestle merge -e catalog.* content_type = FileContentType.JSON fext = FileContentType.to_file_extension(content_type) # prepare trestle project dir with the file test_utils.ensure_trestle_config_dir(tmp_trestle_dir) test_data_source = testdata_dir / 'split_merge/step4_split_groups_array/catalogs' catalogs_dir = Path('catalogs/') mycatalog_dir = catalogs_dir / 'mycatalog' # Copy files from test/data/split_merge/step4 shutil.rmtree(catalogs_dir) shutil.copytree(test_data_source, catalogs_dir) # Change directory to mycatalog_dir os.chdir(mycatalog_dir) catalog_file = Path(f'catalog{fext}').resolve() assert catalog_file.exists() # Read files # Create hand-crafter merge plan expected_plan: Plan = Plan() reset_destination_action = CreatePathAction(catalog_file, clear_content=True) expected_plan.add_action(reset_destination_action) _, _, merged_catalog_instance = ModelUtils.load_distributed( catalog_file, tmp_trestle_dir) element = Element(merged_catalog_instance) write_destination_action = WriteFileAction(catalog_file, element, content_type=content_type) expected_plan.add_action(write_destination_action) delete_element_action = RemovePathAction(Path('catalog').resolve()) expected_plan.add_action(delete_element_action) test_utils.make_hidden_file(tmp_trestle_dir / 'catalogs/mycatalog/.DS_Store') test_utils.make_hidden_file(tmp_trestle_dir / 'catalogs/mycatalog/catalog/.DS_Store') test_utils.make_hidden_file( tmp_trestle_dir / 'catalogs/mycatalog/catalog/metadata/.DS_Store') test_utils.make_hidden_file(tmp_trestle_dir / 'catalogs/mycatalog/catalog/groups/.DS_Store') # Call merge() generated_plan = MergeCmd.merge(Path.cwd(), ElementPath('catalog.*'), tmp_trestle_dir) # Assert the generated plan matches the expected plan' assert generated_plan == expected_plan
def create_sample_catalog_project(trestle_base_dir: pathlib.Path): """Create directory structure for a sample catalog named mycatalog.""" test_utils.ensure_trestle_config_dir(trestle_base_dir) mycatalog_dir = trestle_base_dir / 'catalogs' / 'mycatalog' directories = [ mycatalog_dir / 'catalog' / 'metadata' / 'roles', mycatalog_dir / 'catalog' / 'metadata' / 'responsible-parties', mycatalog_dir / 'catalog' / 'metadata' / 'properties', mycatalog_dir / 'catalog' / 'groups' / f'00000{IDX_SEP}group' / 'controls' ] for directory in directories: directory.mkdir(parents=True, exist_ok=True) files = [ mycatalog_dir / 'catalog.json', mycatalog_dir / 'catalog' / 'back-matter.json', mycatalog_dir / 'catalog' / 'metadata.json', mycatalog_dir / 'catalog' / 'metadata' / 'roles' / f'00000{IDX_SEP}role.json', mycatalog_dir / 'catalog' / 'metadata' / 'responsible-parties' / f'creator{IDX_SEP}responsible-party.json', mycatalog_dir / 'catalog' / 'metadata' / 'properties' / f'00000{IDX_SEP}prop.json', mycatalog_dir / 'catalog' / 'groups' / f'00000{IDX_SEP}group.json', mycatalog_dir / 'catalog' / 'groups' / f'00000{IDX_SEP}group' / 'controls' / f'00000{IDX_SEP}control.json', ] for file in files: file.touch()
def test_trash_store_dir(tmp_path: pathlib.Path) -> None: """Test moving whole directory to trash.""" test_utils.ensure_trestle_config_dir(tmp_path) # trash whole directory data_dir: pathlib.Path = tmp_path / 'data' data_dir.mkdir(exist_ok=True, parents=True) readme_file: pathlib.Path = data_dir / 'readme.md' readme_file.touch() # trash with deleting original assert not trash.to_trash_dir_path(data_dir).exists() assert not trash.to_trash_file_path(readme_file).exists() trash.store_dir(data_dir, True) assert data_dir.exists() is False assert readme_file.exists() is False assert trash.to_trash_dir_path(data_dir).exists() assert trash.to_trash_file_path(readme_file).exists() # trash without deleting original data_dir.mkdir(exist_ok=True, parents=True) readme_file.touch() trash.store_dir(data_dir, False) assert data_dir.exists() assert readme_file.exists() assert trash.to_trash_dir_path(data_dir).exists() assert trash.to_trash_file_path(readme_file).exists()
def test_to_origin_dir_path(tmp_path: pathlib.Path) -> None: """Test to origin dir path function.""" # invalid trestle project would error with pytest.raises(AssertionError): trash.to_origin_dir_path(tmp_path) test_utils.ensure_trestle_config_dir(tmp_path) trash_dir_path = trash.to_trash_dir_path(tmp_path) # missing trash path would error with pytest.raises(AssertionError): trash.to_origin_dir_path(trash_dir_path) (tmp_path / trash.TRESTLE_TRASH_DIR).mkdir(exist_ok=True, parents=True) origin_dir = trash.to_origin_dir_path(trash_dir_path) assert tmp_path.resolve() == origin_dir.resolve() data_dir = tmp_path / 'data' trash_dir_path = trash.to_trash_dir_path(data_dir) origin_dir = trash.to_origin_dir_path(trash_dir_path) assert data_dir.resolve() == origin_dir.resolve() # invalid trash path should error with pytest.raises(AssertionError): trash.to_origin_dir_path(data_dir) # trash file path should error tmp_file = tmp_path / 'temp_file.md' trash_file_path = trash.to_trash_file_path(tmp_file) with pytest.raises(AssertionError): trash.to_origin_dir_path(trash_file_path)
def test_target_dups(tmp_dir): """Test model validation.""" content_type = FileContentType.YAML models_dir_name = test_utils.TARGET_DEFS_DIR model_ref = ostarget.TargetDefinition test_utils.ensure_trestle_config_dir(tmp_dir) file_ext = FileContentType.to_file_extension(content_type) models_full_path = tmp_dir / models_dir_name / 'my_test_model' model_alias = utils.classname_to_alias(model_ref.__name__, 'json') model_def_file = models_full_path / f'{model_alias}{file_ext}' fs.ensure_directory(models_full_path) shutil.copyfile('tests/data/yaml/good_target.yaml', model_def_file) testcmd = f'trestle validate -f {model_def_file} -m duplicates -i uuid' with patch.object(sys, 'argv', testcmd.split()): with pytest.raises(SystemExit) as pytest_wrapped_e: cli.run() assert pytest_wrapped_e.type == SystemExit assert pytest_wrapped_e.value.code is None shutil.copyfile('tests/data/yaml/bad_target_dup_uuid.yaml', model_def_file) testcmd = f'trestle validate -f {model_def_file} -m duplicates -i uuid' with patch.object(sys, 'argv', testcmd.split()): with pytest.raises(TrestleValidationError) as pytest_wrapped_e: cli.run() assert pytest_wrapped_e.type == TrestleValidationError
def test_to_origin_dir_path(tmp_dir: pathlib.Path): """Test to origin dir path function.""" # invalid trestle project would error with pytest.raises(AssertionError): trash.to_origin_dir_path(tmp_dir) test_utils.ensure_trestle_config_dir(tmp_dir) trash_dir_path = trash.to_trash_dir_path(tmp_dir) # missing trash path would error with pytest.raises(AssertionError): trash.to_origin_dir_path(trash_dir_path) fs.ensure_directory(tmp_dir / trash.TRESTLE_TRASH_DIR) origin_dir = trash.to_origin_dir_path(trash_dir_path) assert tmp_dir.absolute() == origin_dir.absolute() data_dir = tmp_dir / 'data' trash_dir_path = trash.to_trash_dir_path(data_dir) origin_dir = trash.to_origin_dir_path(trash_dir_path) assert data_dir.absolute() == origin_dir.absolute() # invalid trash path should error with pytest.raises(AssertionError): trash.to_origin_dir_path(data_dir) # trash file path should error tmp_file = tmp_dir / 'temp_file.md' trash_file_path = trash.to_trash_file_path(tmp_file) with pytest.raises(AssertionError): trash.to_origin_dir_path(trash_file_path)
def test_get_trestle_project_root(tmp_path: pathlib.Path, rand_str: str) -> None: """Test get_trestle_project_root method.""" project_path: pathlib.Path = pathlib.Path.joinpath(tmp_path, rand_str) sub_path: pathlib.Path = project_path.joinpath('samples2') sub_path.mkdir(exist_ok=True, parents=True) assert sub_path.exists() and sub_path.is_dir() # create a file sub_path.joinpath('readme.md').touch() # create a data-dir and a file sub_data_dir = pathlib.Path.joinpath(sub_path, 'data') sub_data_dir.mkdir(exist_ok=True, parents=True) sub_data_dir.joinpath('readme.md').touch() assert file_utils.extract_trestle_project_root(sub_data_dir) is None test_utils.ensure_trestle_config_dir(project_path) assert file_utils.extract_trestle_project_root( sub_data_dir) == project_path assert file_utils.extract_trestle_project_root( sub_data_dir.joinpath('readme.md')) == project_path assert file_utils.extract_trestle_project_root( sub_path.joinpath('readme.md')) == project_path assert file_utils.extract_trestle_project_root(sub_path) == project_path assert file_utils.extract_trestle_project_root(project_path.parent) is None
def test_create_path_magic_methods(tmp_dir): """Test create path magic methods.""" tmp_data_dir = tmp_dir.joinpath('data') test_utils.ensure_trestle_config_dir(tmp_dir) cpa = CreatePathAction(tmp_data_dir) action_desc = cpa.to_string() assert action_desc == f'{cpa.get_type()} {tmp_data_dir}'
def test_is_valid_project_root(tmp_dir): """Test is_valid_project_root method.""" assert fs.is_valid_project_root(None) is False assert fs.is_valid_project_root('') is False assert fs.is_valid_project_root(tmp_dir) is False test_utils.ensure_trestle_config_dir(tmp_dir) assert fs.is_valid_project_root(tmp_dir) is True
def test_remove_path_magic_methods(tmp_path): """Test remove path magic methods.""" tmp_data_dir = tmp_path.joinpath('data') test_utils.ensure_trestle_config_dir(tmp_path) rpa = RemovePathAction(tmp_data_dir) action_desc = rpa.to_string() assert action_desc == f'{rpa.get_type()} {tmp_data_dir}'
def test_remove_path_file(tmp_path: pathlib.Path) -> None: """Test remove path with content clear option.""" tmp_data_dir = tmp_path.joinpath('data') tmp_data_file = tmp_data_dir.joinpath('readme.md') tmp_data_dir.mkdir(exist_ok=True, parents=True) # not a valid trestle project should error in constructor with pytest.raises(TrestleError): rpa = RemovePathAction(tmp_data_file) # create trestle project test_utils.ensure_trestle_config_dir(tmp_path) rpa = RemovePathAction(tmp_data_file) # missing file should error # with pytest.raises(TrestleError): # noqa: E800 remove path is not working properly # rpa.execute() # noqa: E800 # write some content in the file file_pos = 0 dummy_data = 'DUMMY DATA' with open(tmp_data_file, 'a+', encoding=const.FILE_ENCODING) as fp: fp.write(dummy_data) file_pos = fp.tell() assert file_pos >= 0 # remove file tmp_data_file_trash = trash.to_trash_file_path(tmp_data_file) assert tmp_data_file_trash.exists() is False rpa.execute() tmp_data_file_trash.exists() assert tmp_data_file.exists() is False # rollback file rpa.rollback() tmp_data_file_trash.exists() tmp_data_file.exists() with open(tmp_data_file, 'a+', encoding=const.FILE_ENCODING) as fp: assert file_pos == fp.tell() # remove dir rpa = RemovePathAction(tmp_data_dir) tmp_data_trash = trash.to_trash_dir_path(tmp_data_dir) tmp_data_file_trash = trash.to_trash_file_path(tmp_data_file) if tmp_data_trash.exists(): tmp_data_trash.rmdir() rpa.execute() assert tmp_data_trash.exists() assert tmp_data_file_trash.exists() assert tmp_data_file.exists() is False assert tmp_data_dir.exists() is False # rollback dir rpa.rollback() assert tmp_data_trash.exists() is False assert tmp_data_file_trash.exists() is False assert tmp_data_file.exists() assert tmp_data_dir.exists()
def test_remove_path_file(tmp_dir: pathlib.Path): """Test remove path with content clear option.""" tmp_data_dir = tmp_dir.joinpath('data') tmp_data_file = tmp_data_dir.joinpath('readme.md') fs.ensure_directory(tmp_data_dir) # not a valid trestle project should error in constructor with pytest.raises(TrestleError): rpa = RemovePathAction(tmp_data_file) # create trestle project test_utils.ensure_trestle_config_dir(tmp_dir) rpa = RemovePathAction(tmp_data_file) # missing file should error with pytest.raises(FileNotFoundError): rpa.execute() # write some content in the file file_pos = 0 dummy_data = 'DUMMY DATA' with open(tmp_data_file, 'a+') as fp: fp.write(dummy_data) file_pos = fp.tell() assert file_pos >= 0 # remove file tmp_data_file_trash = trash.to_trash_file_path(tmp_data_file) assert tmp_data_file_trash.exists() is False rpa.execute() tmp_data_file_trash.exists() assert tmp_data_file.exists() is False # rollback file rpa.rollback() tmp_data_file_trash.exists() tmp_data_file.exists() with open(tmp_data_file, 'a+') as fp: assert file_pos == fp.tell() # remove dir rpa = RemovePathAction(tmp_data_dir) tmp_data_trash = trash.to_trash_dir_path(tmp_data_dir) tmp_data_file_trash = trash.to_trash_file_path(tmp_data_file) if tmp_data_trash.exists(): tmp_data_trash.rmdir() rpa.execute() assert tmp_data_trash.exists() assert tmp_data_file_trash.exists() assert tmp_data_file.exists() is False assert tmp_data_dir.exists() is False # rollback dir rpa.rollback() assert tmp_data_trash.exists() is False assert tmp_data_file_trash.exists() is False assert tmp_data_file.exists() assert tmp_data_dir.exists()
def test_create_path_with_content_clear_option(tmp_dir: pathlib.Path): """Test create path with content clear option.""" # create trestle project test_utils.ensure_trestle_config_dir(tmp_dir) # create directories and a file tmp_data_dir = tmp_dir.joinpath('data') tmp_data_dir_file = tmp_data_dir.joinpath('readme.md') cpa = CreatePathAction(tmp_data_dir_file) cpa.execute() assert len(cpa.get_created_paths()) == 2 assert tmp_data_dir.exists() assert tmp_data_dir_file.exists() # write some content in the file file_pos = 0 dummy_data = '' with open(tmp_data_dir_file, 'a+') as fp: fp.write(dummy_data) file_pos = fp.tell() assert file_pos >= 0 # create action to create a file without clearing content cpa = CreatePathAction(tmp_data_dir_file, clear_content=False) cpa.execute() assert len(cpa.get_created_paths()) == 0 assert tmp_data_dir_file.exists() with open(tmp_data_dir_file, 'a+') as fp: assert file_pos == fp.tell() # create action to create a file with clearing content cpa = CreatePathAction(tmp_data_dir_file, clear_content=True) cpa.execute() assert len(cpa.get_created_paths()) == 0 assert tmp_data_dir_file.exists() with open(tmp_data_dir_file, 'a+') as fp: assert 0 == fp.tell() data = fp.readline() assert data == '' # rollback should bring back the cleared content cpa.rollback() assert tmp_data_dir.exists() assert tmp_data_dir_file.exists() with open(tmp_data_dir_file, 'a+') as fp: assert file_pos == fp.tell() fp.readline() # clearing content on direction should have no effect of the flag tmp_data_dir2 = tmp_dir / 'data2' tmp_data_dir2.mkdir() cpa = CreatePathAction(tmp_data_dir2, clear_content=True) cpa.execute() assert len(cpa.get_created_paths()) == 0 assert tmp_data_dir2.exists() cpa.rollback() assert tmp_data_dir2.exists()
def test_split_merge(testdata_dir: pathlib.Path, tmp_trestle_dir: pathlib.Path) -> None: """Test merging data that has been split using the split command- to ensure symmetry.""" # trestle split -f catalog.json -e catalog.groups.*.controls.* # prepare trestle project dir with the file test_utils.ensure_trestle_config_dir(tmp_trestle_dir) test_data_source = testdata_dir / 'split_merge/step0-merged_catalog/catalogs' catalogs_dir = Path('catalogs/') mycatalog_dir = catalogs_dir / 'mycatalog' # Copy files from test/data/split_merge/step4 shutil.rmtree(catalogs_dir) shutil.copytree(test_data_source, catalogs_dir) os.chdir(mycatalog_dir) catalog_file = Path('catalog.json') # Read and store the catalog before split stripped_catalog_type, _ = ModelUtils.get_stripped_model_type( catalog_file.resolve(), tmp_trestle_dir) pre_split_catalog = stripped_catalog_type.oscal_read(catalog_file) assert 'groups' in pre_split_catalog.__fields__.keys() # Split the catalog args = argparse.Namespace(name='split', file='catalog.json', verbose=1, element='catalog.groups.*.controls.*', trestle_root=tmp_trestle_dir) split = SplitCmd()._run(args) assert split == 0 interim_catalog_type, _ = ModelUtils.get_stripped_model_type( catalog_file.resolve(), tmp_trestle_dir) interim_catalog = interim_catalog_type.oscal_read(catalog_file.resolve()) assert 'groups' not in interim_catalog.__fields__.keys() # Merge everything back into the catalog # Equivalent to trestle merge -e catalog.* args = argparse.Namespace(name='merge', element='catalog.*', verbose=2, trestle_root=tmp_trestle_dir) rc = MergeCmd()._run(args) assert rc == 0 # Check both the catalogs are the same. post_catalog_type, _ = ModelUtils.get_stripped_model_type( catalog_file.resolve(), tmp_trestle_dir) post_merge_catalog = post_catalog_type.oscal_read(catalog_file) assert post_merge_catalog == pre_split_catalog
def test_split_run_failure( tmp_path: pathlib.Path, sample_target_def: ostarget.TargetDefinition) -> None: """Test split run failure.""" # prepare trestle project dir with the file target_def_dir: pathlib.Path = tmp_path / 'target-definitions' / 'mytarget' target_def_file: pathlib.Path = target_def_dir / 'target-definition.yaml' target_def_dir.mkdir(exist_ok=True, parents=True) sample_target_def.oscal_write(target_def_file) invalid_file = target_def_dir / 'invalid.file' invalid_file.touch() cwd = os.getcwd() os.chdir(target_def_dir) # not a trestle project testargs = [ 'trestle', 'split', '-f', 'target-definition.yaml', '-e', 'target-definition.metadata, target-definition.targets.*' ] with patch.object(sys, 'argv', testargs): with pytest.raises(TrestleError): Trestle().run() # create trestle project test_utils.ensure_trestle_config_dir(tmp_path) # no file specified testargs = [ 'trestle', 'split', '-e', 'target-definition.metadata, target-definition.targets.*' ] with patch.object(sys, 'argv', testargs): rc = Trestle().run() assert rc > 0 # check with missing file testargs = [ 'trestle', 'split', '-f', 'missing.yaml', '-e', 'target-definition.metadata, target-definition.targets.*' ] with patch.object(sys, 'argv', testargs): rc = Trestle().run() assert rc > 0 # check with incorrect file type testargs = [ 'trestle', 'split', '-f', invalid_file.name, '-e', 'target-definition.metadata, target-definition.targets.*' ] with patch.object(sys, 'argv', testargs): with pytest.raises(TrestleError): Trestle().run() os.chdir(cwd)
def test_to_trash_path(tmp_dir: pathlib.Path): """Test to trash path function.""" data_dir: pathlib.Path = tmp_dir / 'data' fs.ensure_directory(data_dir) readme_file: pathlib.Path = data_dir / 'readme.md' readme_file.touch() test_utils.ensure_trestle_config_dir(tmp_dir) assert trash.to_trash_file_path(readme_file) == trash.to_trash_path(readme_file) assert trash.to_trash_dir_path(readme_file.parent) == trash.to_trash_path(readme_file.parent)
def test_to_origin_file_path(tmp_path: pathlib.Path) -> None: """Test to origin file path function.""" test_utils.ensure_trestle_config_dir(tmp_path) (tmp_path / trash.TRESTLE_TRASH_DIR).mkdir(exist_ok=True, parents=True) tmp_file = tmp_path / 'temp_file.md' trash_file_path = trash.to_trash_file_path(tmp_file) origin_file_path = trash.to_origin_file_path(trash_file_path) assert tmp_file.resolve() == origin_file_path.resolve() with pytest.raises(AssertionError): trash.to_origin_file_path(tmp_file)
def test_to_origin_file_path(tmp_dir: pathlib.Path): """Test to origin file path function.""" test_utils.ensure_trestle_config_dir(tmp_dir) fs.ensure_directory(tmp_dir / trash.TRESTLE_TRASH_DIR) tmp_file = tmp_dir / 'temp_file.md' trash_file_path = trash.to_trash_file_path(tmp_file) origin_file_path = trash.to_origin_file_path(trash_file_path) assert tmp_file.absolute() == origin_file_path.absolute() with pytest.raises(AssertionError): trash.to_origin_file_path(tmp_file)
def test_split_run_failures( keep_cwd: pathlib.Path, tmp_path: pathlib.Path, sample_nist_component_def: component.ComponentDefinition, monkeypatch: MonkeyPatch) -> None: """Test split run failure.""" # prepare trestle project dir with the file component_def_dir: pathlib.Path = tmp_path / 'component-definitions' / 'mytarget' component_def_file: pathlib.Path = component_def_dir / 'component-definition.yaml' component_def_dir.mkdir(exist_ok=True, parents=True) sample_nist_component_def.oscal_write(component_def_file) invalid_file = component_def_dir / 'invalid.file' invalid_file.touch() os.chdir(component_def_dir) # not a trestle project testargs = [ 'trestle', 'split', '-tr', str(tmp_path), '-f', 'component-definition.yaml', '-e', 'component-definition.metadata, component-definition.components.*' ] monkeypatch.setattr(sys, 'argv', testargs) with pytest.raises(SystemExit) as wrapped_error: trestle.cli.run() assert wrapped_error.value.code == 1 # create trestle project test_utils.ensure_trestle_config_dir(tmp_path) # no file specified and garbage element testargs = ['trestle', 'split', '-e', 'foo.bar'] monkeypatch.setattr(sys, 'argv', testargs) rc = Trestle().run() assert rc > 0 # check with missing file testargs = [ 'trestle', 'split', '-f', 'missing.yaml', '-e', 'component-definition.metadata, component-definition.components.*' ] monkeypatch.setattr(sys, 'argv', testargs) rc = Trestle().run() assert rc > 0 # check with incorrect file type testargs = [ 'trestle', 'split', '-f', invalid_file.name, '-e', 'component-definition.metadata, component-definition.components.*' ] monkeypatch.setattr(sys, 'argv', testargs) rc = Trestle().run() assert rc == 1
def test_to_trash_path(tmp_path: pathlib.Path) -> None: """Test to trash path function.""" data_dir = tmp_path / 'data' data_dir.mkdir(exist_ok=True, parents=True) readme_file = data_dir / 'readme.md' readme_file.touch() test_utils.ensure_trestle_config_dir(tmp_path) assert trash.to_trash_file_path(readme_file) == trash.to_trash_path( readme_file) assert trash.to_trash_dir_path(readme_file.parent) == trash.to_trash_path( readme_file.parent)
def test_to_origin_path(tmp_dir: pathlib.Path): """Test to origin path function.""" test_utils.ensure_trestle_config_dir(tmp_dir) fs.ensure_directory(tmp_dir / trash.TRESTLE_TRASH_DIR) tmp_file = tmp_dir / 'temp_file.md' trash_file_path = trash.to_trash_file_path(tmp_file) origin_file_path = trash.to_origin_path(trash_file_path) assert tmp_file.absolute() == origin_file_path.absolute() data_dir = tmp_dir / 'data' trash_dir_path = trash.to_trash_dir_path(data_dir) origin_dir = trash.to_origin_path(trash_dir_path) assert data_dir.absolute() == origin_dir.absolute()
def test_get_trash_root(tmp_dir: pathlib.Path): """Test get trash root function.""" assert trash.get_trash_root(pathlib.Path('')) is None readme_file: pathlib.Path = tmp_dir / 'data/readme.md' assert trash.get_trash_root(readme_file) is None test_utils.ensure_trestle_config_dir(tmp_dir) trash_root = tmp_dir / trash.TRESTLE_TRASH_DIR fs.ensure_directory(trash_root) trash_file_path = trash.to_trash_file_path(readme_file) found_root = trash.get_trash_root(trash_file_path) assert trash_root.absolute() == found_root.absolute()
def test_to_origin_path(tmp_path: pathlib.Path) -> None: """Test to origin path function.""" test_utils.ensure_trestle_config_dir(tmp_path) (tmp_path / trash.TRESTLE_TRASH_DIR).mkdir(exist_ok=True, parents=True) tmp_file = tmp_path / 'temp_file.md' trash_file_path = trash.to_trash_file_path(tmp_file) origin_file_path = trash.to_origin_path(trash_file_path) assert tmp_file.resolve() == origin_file_path.resolve() data_dir = tmp_path / 'data' trash_dir_path = trash.to_trash_dir_path(data_dir) origin_dir = trash.to_origin_path(trash_dir_path) assert data_dir.resolve() == origin_dir.resolve()
def test_get_trash_root(tmp_path: pathlib.Path) -> None: """Test get trash root function.""" assert trash.get_trash_root(pathlib.Path('')) is None readme_file: pathlib.Path = tmp_path / 'data/readme.md' assert trash.get_trash_root(readme_file) is None test_utils.ensure_trestle_config_dir(tmp_path) trash_root = tmp_path / trash.TRESTLE_TRASH_DIR trash_root.mkdir(exist_ok=True, parents=True) trash_file_path = trash.to_trash_file_path(readme_file) found_root = trash.get_trash_root(trash_file_path) assert trash_root.resolve() == found_root.resolve()
def test_trash_recover_file(tmp_path) -> None: """Test recover trashed file.""" test_utils.ensure_trestle_config_dir(tmp_path) data_dir: pathlib.Path = tmp_path / 'data' data_dir.mkdir(exist_ok=True, parents=True) readme_file: pathlib.Path = data_dir / 'readme.md' readme_file.touch() trash.store_file(readme_file, True) assert data_dir.exists() assert readme_file.exists() is False trash.recover_file(readme_file) assert data_dir.exists() assert readme_file.exists()