Skip to content

Commit

Permalink
Fix project environment initialization crash during first copy (#58)
Browse files Browse the repository at this point in the history
  • Loading branch information
lkubb authored Sep 27, 2024
1 parent a871bec commit ebbc475
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
1 change: 1 addition & 0 deletions changelog/+initcrash.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed project environment initialization crash during first copy. This crash did not result in any issues other than a warning after copy.
11 changes: 9 additions & 2 deletions project/tools/helpers/pre_commit.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,13 @@ def _run_pre_commit_loop(retries_left):
prompt.warn(f"✗ Failing hook ({i + 1}): {failing_hook}", failing[failing_hook])
finally:
if new_files:
# Undo git add --intent-to-add to allow RenovateBot to detect new files correctly
git("restore", "--staged", *new_files)
try:
# Check if there is at least one commit in the repo,
# otherwise git restore --staged fails.
git("rev-parse", "HEAD")
except ProcessExecutionError:
pass
else:
# Undo git add --intent-to-add to allow RenovateBot to detect new files correctly
git("restore", "--staged", *new_files)
return False
12 changes: 9 additions & 3 deletions tests/test_basics.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,12 @@ def test_copy_works_with_salt_minor_version(copie, answers):


@pytest.mark.parametrize("skip_init_migrate", (False,), indirect=True)
def test_project_init_works(copie, answers):
def test_project_init_works(copie, answers, capfd):
res = copie.copy(extra_answers=answers)
_assert_worked(res)
# ensure the environment initialization did not fail
# (it does not cause an exit code > 0 since it's optional)
assert "Failed initializing environment" not in capfd.readouterr().err
proj = res.project_dir
# ensure git init worked and the default branch is main
assert (proj / ".git").is_dir()
Expand All @@ -66,7 +69,7 @@ def test_project_init_works(copie, answers):

@pytest.mark.parametrize("skip_init_migrate", (False,), indirect=True)
@pytest.mark.parametrize("project", ("0.2.0",), indirect=True)
def test_project_migration_works(copie, project, project_venv, request):
def test_project_migration_works(copie, project, project_venv, request, capfd):
def _check_version(expected):
curr = project_venv.run_module("pre_commit", "--version").stdout.split()[-1]
assert (curr == "2.13.0") is expected
Expand All @@ -90,7 +93,10 @@ def _check_version(expected):
request.getfixturevalue("project_committed")
res = copie.update(project)
_assert_worked(res)
# ensure upgrade worked
# ensure the environment migration did not fail
# (it does not cause an exit code > 0 since it's optional)
assert "Failed migrating environment" not in capfd.readouterr().err
# ensure the upgrade worked
assert new_file.exists()
# ensure boilerplate was not recreated
for bpl in boilerplate:
Expand Down

0 comments on commit ebbc475

Please sign in to comment.