From 478519d5e6add51c12545b655639f869c2a746cb Mon Sep 17 00:00:00 2001 From: Jordan Heffernan Date: Mon, 25 Mar 2024 23:38:47 -0600 Subject: [PATCH] fixed tests, new e2e test --- secureli/actions/action.py | 15 ++++-------- .../language_analyzer/language_support.py | 10 ++++---- tests/actions/test_action.py | 24 ------------------- tests/end-to-end/testinstallnewhooks.bats | 22 +++++++++++++++++ 4 files changed, 33 insertions(+), 38 deletions(-) create mode 100644 tests/end-to-end/testinstallnewhooks.bats diff --git a/secureli/actions/action.py b/secureli/actions/action.py index c2b5cb91..8e1b3a23 100644 --- a/secureli/actions/action.py +++ b/secureli/actions/action.py @@ -85,13 +85,13 @@ def verify_install( preferred_config_path = self.action_deps.hooks_scanner.pre_commit.get_preferred_pre_commit_config_path( folder_path ) - - if ( + pre_commit_to_preserve = ( self.action_deps.hooks_scanner.pre_commit.pre_commit_config_exists( folder_path ) and not pre_commit_config_location_is_correct - ): + ) + if pre_commit_to_preserve: update_result: VerifyResult = ( self._update_secureli_pre_commit_config_location( folder_path, always_yes @@ -105,11 +105,6 @@ def verify_install( return update_result else: preferred_config_path = update_result.file_path - elif not preferred_config_path.exists(): - self.action_deps.echo.warning( - "No pre-commit-config was found, creating new config." - ) - open(preferred_config_path, "w") config = self.get_secureli_config(reset=reset) languages = [] @@ -145,7 +140,7 @@ def verify_install( languages, newly_detected_languages, always_yes, - preferred_config_path, + preferred_config_path if pre_commit_to_preserve else None, ) else: self.action_deps.echo.print( @@ -178,7 +173,7 @@ def _install_secureli( # pre-install new_install = len(detected_languages) == len(install_languages) - + self.action_deps.echo.print(f"BINGOOOOOOO, {new_install}") should_install = self._prompt_to_install( install_languages, always_yes, new_install ) diff --git a/secureli/modules/language_analyzer/language_support.py b/secureli/modules/language_analyzer/language_support.py index 2a67afd4..c1828a89 100644 --- a/secureli/modules/language_analyzer/language_support.py +++ b/secureli/modules/language_analyzer/language_support.py @@ -48,8 +48,10 @@ def apply_support( as well as a secret-detection hook ID, if present. """ - path_to_pre_commit_file: Path = self.pre_commit_hook.get_pre_commit_config_path( - SecureliConfig.FOLDER_PATH + path_to_pre_commit_file: Path = ( + self.pre_commit_hook.get_preferred_pre_commit_config_path( + SecureliConfig.FOLDER_PATH + ) ) linter_config_write_result = self._write_pre_commit_configs( @@ -156,7 +158,7 @@ def build_pre_commit_config( try: data = yaml.safe_load(stream) existing_data = data or {} - config_repos += data.get("repos") + config_repos += data["repos"] if data else [] except yaml.YAMLError: self.echo.error( @@ -188,7 +190,7 @@ def build_pre_commit_config( else None ) data = yaml.safe_load(result.config_data) - config_repos += data.get("repos") or [] + config_repos += data["repos"] or [] config = {**existing_data, "repos": config_repos} version = hash_config(yaml.dump(config)) diff --git a/tests/actions/test_action.py b/tests/actions/test_action.py index 302d0809..83d575de 100644 --- a/tests/actions/test_action.py +++ b/tests/actions/test_action.py @@ -422,7 +422,6 @@ def test_that_verify_install_returns_failure_result_without_pre_commit_config_fi mock_hooks_scanner: MagicMock, mock_echo: MagicMock, ): - with (patch.object(Path, "exists", return_value=False),): mock_hooks_scanner.pre_commit.get_preferred_pre_commit_config_path.return_value = ( test_folder_path / ".secureli" / ".pre-commit-config.yaml" @@ -461,29 +460,6 @@ def test_that_verify_install_continues_after_pre_commit_config_file_moved( assert verify_result.outcome == VerifyOutcome.INSTALL_SUCCEEDED -def test_pre_commit_config_created_if_not_exists( - action: Action, - mock_hooks_scanner: MagicMock, - mock_echo: MagicMock, -): - with ( - patch.object(Path, "exists", return_value=False), - patch("builtins.open", mock_open()) as mo, - ): - mock_hooks_scanner.pre_commit.get_preferred_pre_commit_config_path.return_value = ( - test_folder_path / ".secureli" / ".pre-commit-config.yaml" - ) - mock_hooks_scanner.pre_commit.pre_commit_config_exists.return_value = False - mock_hooks_scanner.pre_commit.get_pre_commit_config_path_is_correct.return_value = ( - False - ) - verify_result = action.verify_install( - test_folder_path, reset=False, always_yes=True, files=None - ) - mo.assert_called_once() - assert verify_result.outcome == VerifyOutcome.INSTALL_SUCCEEDED - - def test_that_update_secureli_pre_commit_config_location_moves_file( action: Action, mock_hooks_scanner: MagicMock, diff --git a/tests/end-to-end/testinstallnewhooks.bats b/tests/end-to-end/testinstallnewhooks.bats new file mode 100644 index 00000000..12bf185b --- /dev/null +++ b/tests/end-to-end/testinstallnewhooks.bats @@ -0,0 +1,22 @@ +# Test to ensure pre-exisiting hooks within the .pre-commit-config.yaml files +# are persisted when installing secureli + +MOCK_REPO='tests/test-data/mock-repo' + +setup() { + load "${BATS_LIBS_ROOT}/bats-support/load" + load "${BATS_LIBS_ROOT}/bats-assert/load" + mkdir -p $MOCK_REPO + echo 'print("hello world!")' > $MOCK_REPO/hw.py + run git init $MOCK_REPO +} + +@test "can preserve pre-existing hooks" { + run python secureli/main.py init -y --directory $MOCK_REPO + run grep 'https://github.com/psf/black' $MOCK_REPO/.secureli/.pre-commit-config.yaml + assert_output --partial 'https://github.com/psf/black' +} + +teardown() { + rm -rf $MOCK_REPO +}