Skip to content

Commit

Permalink
fixed tests, new e2e test
Browse files Browse the repository at this point in the history
  • Loading branch information
Jordan Heffernan committed Mar 26, 2024
1 parent 05917c8 commit 478519d
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 38 deletions.
15 changes: 5 additions & 10 deletions secureli/actions/action.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 = []
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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
)
Expand Down
10 changes: 6 additions & 4 deletions secureli/modules/language_analyzer/language_support.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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))
Expand Down
24 changes: 0 additions & 24 deletions tests/actions/test_action.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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,
Expand Down
22 changes: 22 additions & 0 deletions tests/end-to-end/testinstallnewhooks.bats
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 478519d

Please sign in to comment.