Coverage for tests/unit_tests/test_apio_context.py: 100%
49 statements
« prev ^ index » next coverage.py v7.16.1, created at 2026-09-23 03:53 +0000
« prev ^ index » next coverage.py v7.16.1, created at 2026-09-23 03:53 +0000
1"""
2Tests of apio_context.py
3"""
5import os
6import sys
7import subprocess
8from pathlib import Path
9from pytest import 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,
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 apio_runner.with_logger() as log:
67 with raises(SystemExit) as e:
68 ApioContext(
69 project_policy=ProjectPolicy.NO_PROJECT,
70 remote_config_policy=RemoteConfigPolicy.CACHED_OK,
71 packages_policy=PackagesPolicy.ENSURE_PACKAGES,
72 )
73 assert e.value.code == 1
74 # -- The space char is reported by its name, for visibility.
75 char_name = "space" if invalid_char == " " else invalid_char
76 assert f"Unsupported character [{char_name}]" in log.out
79def test_home_dir_with_a_space_in_subprocess(apio_runner: ApioRunner):
80 """Tests that a home dir with a space fails with a clean error message
81 also when running apio as a fresh process, whose console has not been
82 configured yet. This is a regression test for a crash with an unhandled
83 AssertionError that the in-process tests can't catch because the test
84 fixtures pre-configure the console."""
86 with apio_runner.in_sandbox() as sb:
88 # -- Make up a home dir path with a space.
89 invalid_home_dir = sb.sandbox_dir / "apio home"
91 # -- Run an apio command in a fresh process. It should exit with a
92 # -- clean error message, not an AssertionError crash.
93 result = subprocess.run(
94 [sys.executable, apio_main.__file__, "--version"],
95 env={**os.environ, "APIO_HOME": str(invalid_home_dir)},
96 capture_output=True,
97 text=True,
98 check=False,
99 )
101 assert result.returncode == 1
102 output = result.stdout + result.stderr
103 assert "Unsupported character [space]" in output, output
104 assert "AssertionError" not in output, output
107def test_home_dir_with_relative_path(apio_runner: ApioRunner):
108 """Apio context should fail if the apio home dir is a relative path"""
110 with apio_runner.in_sandbox():
112 # -- Make up a home dir path with the invalid char.
113 invalid_home_dir = Path("./aa/bb")
114 os.environ["APIO_HOME"] = str(invalid_home_dir)
116 # -- Initialize an apio context. It should exit with an error.
117 with apio_runner.with_logger() as log:
118 with raises(SystemExit) as e:
119 ApioContext(
120 project_policy=ProjectPolicy.NO_PROJECT,
121 remote_config_policy=RemoteConfigPolicy.CACHED_OK,
122 packages_policy=PackagesPolicy.ENSURE_PACKAGES,
123 )
124 assert e.value.code == 1
125 assert "Error: Apio home dir should be an absolute path" in log.out