Coverage for tests / unit_tests / commands / test_apio_sim.py: 100%
53 statements
« prev ^ index » next coverage.py v7.13.3, created at 2026-02-08 02:47 +0000
« prev ^ index » next coverage.py v7.13.3, created at 2026-02-08 02:47 +0000
1"""Test for the "apio sim" command"""
3from pathlib import Path
4from tests.conftest import ApioRunner
5from apio.commands.apio import apio_top_cli as apio
8def test_sim(apio_runner: ApioRunner):
9 """Test apio sim without apio.ini. Should fail."""
11 with apio_runner.in_sandbox() as sb:
13 # -- apio sim
14 result = sb.invoke_apio_cmd(apio, ["sim"])
15 assert result.exit_code != 0, result.output
16 assert "Missing project file apio.ini" in result.output
19def test_sim_with_env_arg_error(apio_runner: ApioRunner):
20 """Tests the command with an invalid --env value. This error message
21 confirms that the --env arg was propagated to the apio.ini loading
22 logic."""
24 with apio_runner.in_sandbox() as sb:
26 # -- Run "apio sim --env no-such-env"
27 sb.write_apio_ini({"[env:default]": {"top-module": "main"}})
28 result = sb.invoke_apio_cmd(apio, ["sim", "--env", "no-such-env"])
29 assert result.exit_code == 1, result.output
30 assert (
31 "Error: Env 'no-such-env' not found in apio.ini" in result.output
32 )
35def test_sim_with_no_user_gtkw_file(apio_runner: ApioRunner):
36 """Verify that 'apio sim' creates a default .gtkw file."""
38 with apio_runner.in_sandbox() as sb:
40 # -- Get example project.
41 result = sb.invoke_apio_cmd(
42 apio, ["examples", "fetch", "alhambra-ii/ledon"]
43 )
44 sb.assert_result_ok(result)
46 # -- Verify it has a testbench gtkw file.
47 gtkw_path = Path("ledon_tb.gtkw")
48 assert gtkw_path.exists(), gtkw_path
50 # -- Delete teh gtkw file.
51 gtkw_path.unlink()
52 assert not gtkw_path.exists(), gtkw_path
54 # -- Run headless sim, it should create the gtkw file.
55 result = sb.invoke_apio_cmd(apio, ["sim", "--no-gtkwave"])
56 sb.assert_result_ok(result)
57 assert gtkw_path.exists(), gtkw_path
59 # -- Verify the file.
60 content1 = gtkw_path.read_text(encoding="utf-8")
61 timestamp1 = gtkw_path.stat().st_mtime
62 assert "THIS FILE WAS GENERATED AUTOMATICALLY BY APIO" in content1
63 assert "ledon_tb.DURATION" in content1
64 assert "ledon_tb.led0" in content1
66 # -- Run headless sim again, it should overwrite the gtkw file.
67 result = sb.invoke_apio_cmd(apio, ["sim", "--no-gtkwave"])
68 sb.assert_result_ok(result)
69 assert gtkw_path.exists(), gtkw_path
71 # -- Verify the file.
72 content2 = gtkw_path.read_text(encoding="utf-8")
73 timestamp2 = gtkw_path.stat().st_mtime
74 assert content1 == content2
75 assert timestamp1 != timestamp2
78def test_sim_with_user_gtkw_file(apio_runner: ApioRunner):
79 """Verify that 'apio sim' doesn't overwrite user saved .gtkw file."""
81 with apio_runner.in_sandbox() as sb:
83 # -- Get example project.
84 result = sb.invoke_apio_cmd(
85 apio, ["examples", "fetch", "alhambra-ii/ledon"]
86 )
87 sb.assert_result_ok(result)
89 # -- Verify it has a testbench gtkw file.
90 gtkw_path = Path("ledon_tb.gtkw")
91 assert gtkw_path.exists(), gtkw_path
93 # -- Verify the file.
94 content1 = gtkw_path.read_text(encoding="utf-8")
95 timestamp1 = gtkw_path.stat().st_mtime
96 assert "THIS FILE WAS GENERATED AUTOMATICALLY BY APIO" not in content1
98 # -- Run headless sim, it should create the gtkw file.
99 result = sb.invoke_apio_cmd(apio, ["sim", "--no-gtkwave"])
100 sb.assert_result_ok(result)
101 assert gtkw_path.exists(), gtkw_path
103 # -- Verify that the file was not changed file.
104 content2 = gtkw_path.read_text(encoding="utf-8")
105 timestamp2 = gtkw_path.stat().st_mtime
106 assert content1 == content2
107 assert timestamp1 == timestamp2