Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(app) fix work dir in managed runs #74

Merged
merged 6 commits into from
Sep 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions craft_application/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,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 @@ -146,7 +150,8 @@ def _configure_services(self, build_for: str | None) -> 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()
# Current working directory contains the project file
project_file = pathlib.Path(f"{self.app.name}.yaml").resolve()
craft_cli.emit.debug(f"Loading project file '{project_file!s}'")
return self.app.ProjectClass.from_yaml_file(project_file)

Expand Down
46 changes: 46 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 @@ -332,3 +333,48 @@ def test_run_error_debug(monkeypatch, mock_dispatcher, app, fake_project, error)
def test_filter_plan(mocker, plan, build_for, host_arch, result):
mocker.patch("craft_application.util.get_host_architecture", return_value=host_arch)
assert application._filter_plan(plan, build_for) == result


@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


@pytest.mark.usefixtures("fake_project_file")
def test_work_dir_project_non_managed(monkeypatch, 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"


@pytest.mark.usefixtures("fake_project_file")
def test_work_dir_project_managed(monkeypatch, 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"