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

1"""Test for the "apio sim" command""" 

2 

3from pathlib import Path 

4from tests.conftest import ApioRunner 

5from apio.commands.apio import apio_top_cli as apio 

6 

7 

8def test_sim(apio_runner: ApioRunner): 

9 """Test apio sim without apio.ini. Should fail.""" 

10 

11 with apio_runner.in_sandbox() as sb: 

12 

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 

17 

18 

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.""" 

23 

24 with apio_runner.in_sandbox() as sb: 

25 

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 ) 

33 

34 

35def test_sim_with_no_user_gtkw_file(apio_runner: ApioRunner): 

36 """Verify that 'apio sim' creates a default .gtkw file.""" 

37 

38 with apio_runner.in_sandbox() as sb: 

39 

40 # -- Get example project. 

41 result = sb.invoke_apio_cmd( 

42 apio, ["examples", "fetch", "alhambra-ii/ledon"] 

43 ) 

44 sb.assert_result_ok(result) 

45 

46 # -- Verify it has a testbench gtkw file. 

47 gtkw_path = Path("ledon_tb.gtkw") 

48 assert gtkw_path.exists(), gtkw_path 

49 

50 # -- Delete teh gtkw file. 

51 gtkw_path.unlink() 

52 assert not gtkw_path.exists(), gtkw_path 

53 

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 

58 

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 

65 

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 

70 

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 

76 

77 

78def test_sim_with_user_gtkw_file(apio_runner: ApioRunner): 

79 """Verify that 'apio sim' doesn't overwrite user saved .gtkw file.""" 

80 

81 with apio_runner.in_sandbox() as sb: 

82 

83 # -- Get example project. 

84 result = sb.invoke_apio_cmd( 

85 apio, ["examples", "fetch", "alhambra-ii/ledon"] 

86 ) 

87 sb.assert_result_ok(result) 

88 

89 # -- Verify it has a testbench gtkw file. 

90 gtkw_path = Path("ledon_tb.gtkw") 

91 assert gtkw_path.exists(), gtkw_path 

92 

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 

97 

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 

102 

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