Skip to content

Commit

Permalink
fix: revert pyproject.toml changes on error
Browse files Browse the repository at this point in the history
  • Loading branch information
Mousa Zeid Baker committed Dec 19, 2022
1 parent 118bb75 commit 0e6c388
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 7 deletions.
20 changes: 13 additions & 7 deletions src/poetry_plugin_up/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ def handle(self) -> int:

selector = VersionSelector(self.poetry.pool)
pyproject_content = self.poetry.file.read()
original_pyproject_content = self.poetry.file.read()

for group in self.get_groups():
for dependency in group.dependencies:
Expand All @@ -78,14 +79,19 @@ def handle(self) -> int:

# write new content to pyproject.toml
self.poetry.file.write(pyproject_content)
self.reset_poetry()

if no_install:
# update lock file
self.call(name="lock", args="--no-update")
return 0

# update dependencies
self.call(name="update")
try:
if no_install:
# update lock file
self.call(name="lock", args="--no-update")
else:
# update dependencies
self.call(name="update")
except Exception as e:
self.line("\nReverting <comment>pyproject.toml</>")
self.poetry.file.write(original_pyproject_content)
raise e
return 0

def get_groups(self) -> Iterable[DependencyGroup]:
Expand Down
42 changes: 42 additions & 0 deletions tests/e2e/test_e2e.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ def test_command(
"poetry.version.version_selector.VersionSelector.find_best_candidate",
side_effect=packages,
)
mocker.patch(
"poetry.console.commands.installer_command.InstallerCommand.reset_poetry", # noqa: E501
return_value=None,
)

path = project_path / "expected_pyproject.toml"
expected = PyProjectTOML(path).file.read()
Expand All @@ -46,6 +50,10 @@ def test_command_with_latest(
"poetry.version.version_selector.VersionSelector.find_best_candidate",
side_effect=packages,
)
mocker.patch(
"poetry.console.commands.installer_command.InstallerCommand.reset_poetry", # noqa: E501
return_value=None,
)

path = project_path / "expected_pyproject_with_latest.toml"
expected = PyProjectTOML(path).file.read()
Expand All @@ -69,6 +77,10 @@ def test_command_with_dry_run(
"poetry.version.version_selector.VersionSelector.find_best_candidate",
side_effect=packages,
)
mocker.patch(
"poetry.console.commands.installer_command.InstallerCommand.reset_poetry", # noqa: E501
return_value=None,
)

expected = PyProjectTOML(tmp_pyproject_path).file.read()

Expand All @@ -93,10 +105,40 @@ def test_command_with_no_install(
"poetry.version.version_selector.VersionSelector.find_best_candidate",
side_effect=packages,
)
mocker.patch(
"poetry.console.commands.installer_command.InstallerCommand.reset_poetry", # noqa: E501
return_value=None,
)

path = project_path / "expected_pyproject.toml"
expected = PyProjectTOML(path).file.read()

assert app_tester.execute("up --no-install") == 0
assert PyProjectTOML(tmp_pyproject_path).file.read() == expected
command_call.assert_called_once_with(name="lock", args="--no-update")


def test_command_reverts_pyproject_on_error(
app_tester: ApplicationTester,
packages: List[Package],
mocker: MockerFixture,
tmp_pyproject_path: Path,
) -> None:
command_call = mocker.patch(
"poetry.console.commands.command.Command.call",
side_effect=Exception,
)
mocker.patch(
"poetry.version.version_selector.VersionSelector.find_best_candidate",
side_effect=packages,
)
mocker.patch(
"poetry.console.commands.installer_command.InstallerCommand.reset_poetry", # noqa: E501
return_value=None,
)

expected = PyProjectTOML(tmp_pyproject_path).file.read()

assert app_tester.execute("up") == 1
assert PyProjectTOML(tmp_pyproject_path).file.read() == expected
command_call.assert_called_once_with(name="update")

0 comments on commit 0e6c388

Please sign in to comment.