Coverage for tests / unit_tests / commands / test_apio_build.py: 100%
45 statements
« prev ^ index » next coverage.py v7.13.0, created at 2025-12-24 01:53 +0000
« prev ^ index » next coverage.py v7.13.0, created at 2025-12-24 01:53 +0000
1"""Test for the "apio build" command."""
3from pathlib import Path
4from tests.conftest import ApioRunner
5from apio.commands.apio import apio_top_cli as apio
8def test_build_without_apio_ini(apio_runner: ApioRunner):
9 """Tests build command with no apio.ini."""
11 with apio_runner.in_sandbox() as sb:
13 # -- Run "apio build" without apio.ini
14 result = sb.invoke_apio_cmd(apio, ["build"])
15 assert result.exit_code != 0, result.output
16 assert "Error: Missing project file apio.ini" in result.output
19def test_build_with_apio_ini(apio_runner: ApioRunner):
20 """Test build command with apio.ini."""
22 with apio_runner.in_sandbox() as sb:
24 # -- Run "apio build" with a missing board var.
25 sb.write_apio_ini({"[env:default]": {"top-module": "main"}})
26 result = sb.invoke_apio_cmd(apio, ["build"])
27 assert result.exit_code == 1, result.output
28 assert (
29 "Error: Missing required option 'board' for env 'default'"
30 in result.output
31 )
33 # -- Run "apio build" with an invalid board
34 sb.write_apio_ini(
35 {
36 "[env:default]": {
37 "board": "no-such-board",
38 "top-module": "main",
39 }
40 }
41 )
42 result = sb.invoke_apio_cmd(apio, ["build"])
43 assert result.exit_code == 1, result.output
44 assert (
45 "Error: Unknown board id 'no-such-board' in apio.ini"
46 in result.output
47 )
49 # -- Run "apio build" with an unknown option.
50 sb.write_apio_ini(
51 {
52 "[env:default]": {
53 "board": "alhambra-ii",
54 "top-module": "main",
55 "unknown": "xyz",
56 }
57 }
58 )
59 result = sb.invoke_apio_cmd(apio, ["build"])
60 assert result.exit_code == 1, result.output
61 assert (
62 "Error: Unknown option 'unknown' in [env:default] section "
63 "of apio.ini" in result.output
64 )
67def test_build_with_env_arg_error(apio_runner: ApioRunner):
68 """Tests the command with an invalid --env value. This error message
69 confirms that the --env arg was propagated to the apio.ini loading
70 logic."""
72 with apio_runner.in_sandbox() as sb:
74 # -- Run "apio build --env no-such-env"
75 sb.write_apio_ini({"[env:default]": {"top-module": "main"}})
76 result = sb.invoke_apio_cmd(apio, ["build", "--env", "no-such-env"])
77 assert result.exit_code == 1, result.output
78 assert (
79 "Error: Env 'no-such-env' not found in apio.ini" in result.output
80 )
83def test_files_order(apio_runner: ApioRunner):
84 """Tests that source files are sorted in apio build command."""
86 with apio_runner.in_sandbox() as sb:
88 # -- Fetch a working example.
89 result = sb.invoke_apio_cmd(
90 apio,
91 ["examples", "fetch", "alhambra-ii/ledon"],
92 terminal_mode=False,
93 )
95 # -- Add dummy source files
96 Path("aa").mkdir(parents=True)
97 Path("bb").mkdir(parents=True)
98 Path("aa/bb.v").touch()
99 Path("aa/cc.v").touch()
100 Path("bb/aa.v").touch()
102 # -- Add a fake source files in _build directory. It should not be
103 # -- picked up.
104 Path("_build").mkdir()
105 Path("_build/zzz.v").touch()
107 # -- 'apio build'
108 result = sb.invoke_apio_cmd(apio, ["build"])
109 sb.assert_result_ok(result)
110 assert "SUCCESS" in result.output
112 # -- Check that the source file from the _build directory was not
113 # -- picked up.
114 assert "zzz.v" not in result.output
116 # -- Check that the files in the build command are sorted.
117 # -- We adjust for the "/" vs "\" difference between Windows and Linux.
118 expected_order = ["ledon.v", "aa/bb.v", "aa/cc.v", "bb/aa.v"]
119 expected_text = " ".join([str(Path(f)) for f in expected_order])
120 assert expected_text in result.output