Coverage for tests/unit_tests/commands/test_apio_build.py: 100%

46 statements  

« prev     ^ index     » next       coverage.py v7.11.0, created at 2025-11-06 10:20 +0000

1"""Test for the "apio build" 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_build_without_apio_ini(apio_runner: ApioRunner): 

9 """Tests build command with no apio.ini.""" 

10 

11 with apio_runner.in_sandbox() as sb: 

12 

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 

17 

18 

19def test_build_with_apio_ini(apio_runner: ApioRunner): 

20 """Test build command with apio.ini.""" 

21 

22 with apio_runner.in_sandbox() as sb: 

23 

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 ) 

32 

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 ) 

48 

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 ) 

65 

66 

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

71 

72 with apio_runner.in_sandbox() as sb: 

73 

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 ) 

81 

82 

83def test_files_order(apio_runner: ApioRunner): 

84 """Tests that source files are sorted in apio build command.""" 

85 

86 # -- This is a slow test. Skip it if running with --fast-only flag. 

87 apio_runner.skip_test_if_fast_only() 

88 

89 with apio_runner.in_sandbox() as sb: 

90 

91 # -- Fetch a working example. 

92 result = sb.invoke_apio_cmd( 

93 apio, 

94 ["examples", "fetch", "alhambra-ii/ledon"], 

95 terminal_mode=False, 

96 ) 

97 

98 # -- Add dummy source files 

99 Path("aa").mkdir(parents=True) 

100 Path("bb").mkdir(parents=True) 

101 Path("aa/bb.v").touch() 

102 Path("aa/cc.v").touch() 

103 Path("bb/aa.v").touch() 

104 

105 # -- Add a fake source files in _build directory. It should not be 

106 # -- picked up. 

107 Path("_build").mkdir() 

108 Path("_build/zzz.v").touch() 

109 

110 # -- 'apio build' 

111 result = sb.invoke_apio_cmd(apio, ["build"]) 

112 sb.assert_result_ok(result) 

113 assert "SUCCESS" in result.output 

114 

115 # -- Check that the source file from the _build directory was not 

116 # -- picked up. 

117 assert "zzz.v" not in result.output 

118 

119 # -- Check that the files in the build command are sorted. 

120 # -- We adjust for the "/" vs "\" difference between Windows and Linux. 

121 expected_order = ["ledon.v", "aa/bb.v", "aa/cc.v", "bb/aa.v"] 

122 expected_text = " ".join([str(Path(f)) for f in expected_order]) 

123 assert expected_text in result.output