Skip to content

Commit

Permalink
fix(app) fix work dir in managed runs
Browse files Browse the repository at this point in the history
When running in managed mode, the work dir is _not_ the cwd: the
work dir is "/root/" and contains the lifecycle directories, while the
cwd is typically the directory containing the project file, mounted as
"/root/project/" inside the instance.

Fixes #53
  • Loading branch information
tigarmo committed Sep 18, 2023
1 parent d93caa8 commit 6753329
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 2 deletions.
8 changes: 6 additions & 2 deletions craft_application/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,11 @@ def __init__(
self.services = services
self._command_groups: list[craft_cli.CommandGroup] = []
self._global_arguments: list[craft_cli.GlobalArgument] = [GLOBAL_VERSION]
self._work_dir = pathlib.Path.cwd()

if self.services.ProviderClass.is_managed():
self._work_dir = pathlib.Path("/root")
else:
self._work_dir = pathlib.Path.cwd()

@property
def command_groups(self) -> list[craft_cli.CommandGroup]:
Expand Down Expand Up @@ -134,7 +138,7 @@ def _configure_services(self) -> None:
@functools.cached_property
def project(self) -> models.Project:
"""Get this application's Project metadata."""
project_file = (self._work_dir / f"{self.app.name}.yaml").resolve()
project_file = (pathlib.Path.cwd() / f"{self.app.name}.yaml").resolve()
return self.app.ProjectClass.from_yaml_file(project_file)

def run_managed(self) -> None:
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ ignore = [
"tests/**.py" = [ # Some things we want for the moin project are unnecessary in tests.
"D", # Ignore docstring rules in tests
"ANN", # Ignore type annotations in tests
"ARG001", # Ignore unused function arguments in tests (typically fixtures)
"S101", # Allow assertions in tests
"S103", # Allow `os.chmod` setting a permissive mask `0o555` on file or directory
"S108", # Allow Probable insecure usage of temporary file or directory
Expand Down
48 changes: 48 additions & 0 deletions tests/unit/test_application.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import re
import subprocess
import sys
from textwrap import dedent
from unittest import mock

import craft_application
Expand Down Expand Up @@ -247,3 +248,50 @@ def test_run_error_debug(monkeypatch, mock_dispatcher, app, fake_project, error)

with pytest.raises(error.__class__):
app.run()


@pytest.fixture()
def fake_project_file(monkeypatch, tmp_path):
project_dir = tmp_path / "project"
project_dir.mkdir()
project_path = project_dir / "testcraft.yaml"
project_path.write_text(
dedent(
"""
name: myproject
version: 1.0
parts:
mypart:
plugin: nil
"""
)
)
monkeypatch.chdir(project_dir)

return project_path


def test_work_dir_project_non_managed(
monkeypatch, fake_project_file, app_metadata, fake_services
):
monkeypatch.setenv(fake_services.ProviderClass.managed_mode_env_var, "0")

app = application.Application(app_metadata, fake_services)
assert app._work_dir == pathlib.Path.cwd()

# Make sure the project is loaded correctly (from the cwd)
assert app.project.name == "myproject"
assert app.project.version == "1.0"


def test_work_dir_project_managed(
monkeypatch, fake_project_file, app_metadata, fake_services
):
monkeypatch.setenv(fake_services.ProviderClass.managed_mode_env_var, "1")

app = application.Application(app_metadata, fake_services)
assert app._work_dir == pathlib.PosixPath("/root")

# Make sure the project is loaded correctly (from the cwd)
assert app.project.name == "myproject"
assert app.project.version == "1.0"

0 comments on commit 6753329

Please sign in to comment.