Coverage for tests/unit_tests/test_apio_context.py: 100%
49 statements
« prev ^ index » next coverage.py v7.17.2, created at 2026-11-07 05:08 +0000
« prev ^ index » next coverage.py v7.17.2, created at 2026-11-07 05:08 +0000
1# This file is part of the Apio project
2# (C) 2016-present FPGAwars
3# Project authors:
4# * Jesús Arroyo (Jesus89) (2016-2019)
5# * Juan Gonzalez (obijuan) (2019-2024)
6# * The Apio Team (2024-present)
7# License: GPLv2
9"""
10Tests of apio_context.py
11"""
13import os
14import sys
15import subprocess
16from pathlib import Path
17from pytest import raises
18from tests.conftest import ApioRunner
19from apio import __main__ as apio_main
20from apio.apio_context import (
21 ApioContext,
22 PackagesPolicy,
23 ProjectPolicy,
24 RemoteConfigPolicy,
25)
26from apio.common.common_util import PROJECT_BUILD_PATH
29def test_init(apio_runner: ApioRunner):
30 """Tests the initialization of the apio context."""
32 with apio_runner.in_sandbox() as sb:
34 # -- Create an apio.ini file.
35 sb.write_default_apio_ini()
37 # -- Default init.
38 apio_ctx = ApioContext(
39 project_policy=ProjectPolicy.PROJECT_REQUIRED,
40 remote_config_policy=RemoteConfigPolicy.CACHED_OK,
41 packages_policy=PackagesPolicy.ENSURE_PACKAGES,
42 )
44 assert apio_ctx.has_project
46 # -- Verify context's project dir.
47 assert str(apio_ctx.project_dir) == "."
48 assert apio_ctx.project_dir.samefile(Path.cwd())
49 assert apio_ctx.project_dir.samefile(sb.proj_dir)
51 # -- Verify context's home and packages dirs.
52 assert apio_ctx.apio_home_dir == sb.home_dir
53 assert apio_ctx.apio_packages_dir == sb.packages_dir
55 # -- Verify build dir
56 assert PROJECT_BUILD_PATH == Path("_build")
57 assert apio_ctx.env_build_path == Path("_build/default")
60def test_home_dir_with_a_bad_character(
61 apio_runner: ApioRunner,
62):
63 """Tests the initialization of the apio context with home dirs that
64 contain invalid chars."""
66 for invalid_char in ["ó", "ñ", " ", "😼"]:
67 with apio_runner.in_sandbox() as sb:
69 # -- Make up a home dir path with the invalid char.
70 invalid_home_dir = sb.sandbox_dir / f"apio-{invalid_char}-home"
71 os.environ["APIO_HOME"] = str(invalid_home_dir)
73 # -- Initialize an apio context. It should exit with an error.
74 with apio_runner.with_logger() as log:
75 with raises(SystemExit) as e:
76 ApioContext(
77 project_policy=ProjectPolicy.NO_PROJECT,
78 remote_config_policy=RemoteConfigPolicy.CACHED_OK,
79 packages_policy=PackagesPolicy.ENSURE_PACKAGES,
80 )
81 assert e.value.code == 1
82 # -- The space char is reported by its name, for visibility.
83 char_name = "space" if invalid_char == " " else invalid_char
84 assert f"Unsupported character [{char_name}]" in log.out
87def test_home_dir_with_a_space_in_subprocess(apio_runner: ApioRunner):
88 """Tests that a home dir with a space fails with a clean error message
89 also when running apio as a fresh process, whose console has not been
90 configured yet. This is a regression test for a crash with an unhandled
91 AssertionError that the in-process tests can't catch because the test
92 fixtures pre-configure the console."""
94 with apio_runner.in_sandbox() as sb:
96 # -- Make up a home dir path with a space.
97 invalid_home_dir = sb.sandbox_dir / "apio home"
99 # -- Run an apio command in a fresh process. It should exit with a
100 # -- clean error message, not an AssertionError crash.
101 result = subprocess.run(
102 [sys.executable, apio_main.__file__, "--version"],
103 env={**os.environ, "APIO_HOME": str(invalid_home_dir)},
104 capture_output=True,
105 text=True,
106 check=False,
107 )
109 assert result.returncode == 1
110 output = result.stdout + result.stderr
111 assert "Unsupported character [space]" in output, output
112 assert "AssertionError" not in output, output
115def test_home_dir_with_relative_path(apio_runner: ApioRunner):
116 """Apio context should fail if the apio home dir is a relative path"""
118 with apio_runner.in_sandbox():
120 # -- Make up a home dir path with the invalid char.
121 invalid_home_dir = Path("./aa/bb")
122 os.environ["APIO_HOME"] = str(invalid_home_dir)
124 # -- Initialize an apio context. It should exit with an error.
125 with apio_runner.with_logger() as log:
126 with raises(SystemExit) as e:
127 ApioContext(
128 project_policy=ProjectPolicy.NO_PROJECT,
129 remote_config_policy=RemoteConfigPolicy.CACHED_OK,
130 packages_policy=PackagesPolicy.ENSURE_PACKAGES,
131 )
132 assert e.value.code == 1
133 assert "Error: Apio home dir should be an absolute path" in log.out