Coverage for tests/unit_tests/test_apio_context.py: 100%
47 statements
« prev ^ index » next coverage.py v7.15.4, created at 2026-08-09 01:55 +0000
« prev ^ index » next coverage.py v7.15.4, created at 2026-08-09 01:55 +0000
1"""
2Tests of apio_context.py
3"""
5import os
6import sys
7import subprocess
8from pathlib import Path
9from pytest import LogCaptureFixture, raises
10from tests.conftest import ApioRunner
11from apio import __main__ as apio_main
12from apio.apio_context import (
13 ApioContext,
14 PackagesPolicy,
15 ProjectPolicy,
16 RemoteConfigPolicy,
17)
18from apio.common.common_util import PROJECT_BUILD_PATH
21def test_init(apio_runner: ApioRunner):
22 """Tests the initialization of the apio context."""
24 with apio_runner.in_sandbox() as sb:
26 # -- Create an apio.ini file.
27 sb.write_default_apio_ini()
29 # -- Default init.
30 apio_ctx = ApioContext(
31 project_policy=ProjectPolicy.PROJECT_REQUIRED,
32 remote_config_policy=RemoteConfigPolicy.CACHED_OK,
33 packages_policy=PackagesPolicy.ENSURE_PACKAGES,
34 )
36 assert apio_ctx.has_project
38 # -- Verify context's project dir.
39 assert str(apio_ctx.project_dir) == "."
40 assert apio_ctx.project_dir.samefile(Path.cwd())
41 assert apio_ctx.project_dir.samefile(sb.proj_dir)
43 # -- Verify context's home and packages dirs.
44 assert apio_ctx.apio_home_dir == sb.home_dir
45 assert apio_ctx.apio_packages_dir == sb.packages_dir
47 # -- Verify build dir
48 assert PROJECT_BUILD_PATH == Path("_build")
49 assert apio_ctx.env_build_path == Path("_build/default")
52def test_home_dir_with_a_bad_character(
53 apio_runner: ApioRunner, capsys: LogCaptureFixture
54):
55 """Tests the initialization of the apio context with home dirs that
56 contain invalid chars."""
58 for invalid_char in ["ó", "ñ", " ", "😼"]:
59 with apio_runner.in_sandbox() as sb:
61 # -- Make up a home dir path with the invalid char.
62 invalid_home_dir = sb.sandbox_dir / f"apio-{invalid_char}-home"
63 os.environ["APIO_HOME"] = str(invalid_home_dir)
65 # -- Initialize an apio context. It should exit with an error.
66 with raises(SystemExit) as e:
67 ApioContext(
68 project_policy=ProjectPolicy.NO_PROJECT,
69 remote_config_policy=RemoteConfigPolicy.CACHED_OK,
70 packages_policy=PackagesPolicy.ENSURE_PACKAGES,
71 )
72 assert e.value.code == 1
73 # -- The space char is reported by its name, for visibility.
74 char_name = "space" if invalid_char == " " else invalid_char
75 assert (
76 f"Unsupported character [{char_name}]"
77 in capsys.readouterr().out
78 )
81def test_home_dir_with_a_space_in_subprocess(apio_runner: ApioRunner):
82 """Tests that a home dir with a space fails with a clean error message
83 also when running apio as a fresh process, whose console has not been
84 configured yet. This is a regression test for a crash with an unhandled
85 AssertionError that the in-process tests can't catch because the test
86 fixtures pre-configure the console."""
88 with apio_runner.in_sandbox() as sb:
90 # -- Make up a home dir path with a space.
91 invalid_home_dir = sb.sandbox_dir / "apio home"
93 # -- Run an apio command in a fresh process. It should exit with a
94 # -- clean error message, not an AssertionError crash.
95 result = subprocess.run(
96 [sys.executable, apio_main.__file__, "--version"],
97 env={**os.environ, "APIO_HOME": str(invalid_home_dir)},
98 capture_output=True,
99 text=True,
100 check=False,
101 )
103 assert result.returncode == 1
104 output = result.stdout + result.stderr
105 assert "Unsupported character [space]" in output, output
106 assert "AssertionError" not in output, output
109def test_home_dir_with_relative_path(
110 apio_runner: ApioRunner, capsys: LogCaptureFixture
111):
112 """Apio context should fail if the apio home dir is a relative path"""
114 with apio_runner.in_sandbox():
116 # -- Make up a home dir path with the invalid char.
117 invalid_home_dir = Path("./aa/bb")
118 os.environ["APIO_HOME"] = str(invalid_home_dir)
120 # -- Initialize an apio context. It should exit with an error.
121 with raises(SystemExit) as e:
122 ApioContext(
123 project_policy=ProjectPolicy.NO_PROJECT,
124 remote_config_policy=RemoteConfigPolicy.CACHED_OK,
125 packages_policy=PackagesPolicy.ENSURE_PACKAGES,
126 )
127 assert e.value.code == 1
128 assert (
129 "Error: Apio home dir should be an absolute path"
130 in capsys.readouterr().out
131 )