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

52 statements  

« 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 

8 

9"""Test for the "apio packages" command.""" 

10 

11from os import listdir, rename 

12from tests.conftest import ApioRunner 

13from apio.commands.apio import apio_top_cli as apio 

14from apio.common.apio_console import cunstyle 

15 

16 

17def test_packages(apio_runner: ApioRunner): 

18 """Test "apio packages" with different parameters""" 

19 

20 with apio_runner.in_sandbox() as sb: 

21 

22 # -- Execute "apio packages" 

23 result = sb.invoke_apio_cmd(apio, ["packages"]) 

24 sb.assert_result_ok(result) 

25 assert "Subcommands:" in result.output 

26 assert "apio packages install" in cunstyle(result.output) 

27 assert "apio packages list" in cunstyle(result.output) 

28 assert result.output != cunstyle(result.output) # Colored. 

29 

30 

31def test_packages_list(apio_runner: ApioRunner): 

32 """Test "apio packages" with different parameters""" 

33 

34 with apio_runner.in_sandbox() as sb: 

35 

36 # -- Execute "apio packages list" 

37 result = sb.invoke_apio_cmd(apio, ["packages", "list"]) 

38 sb.assert_result_ok(result) 

39 

40 

41def test_packages_install(apio_runner: ApioRunner): 

42 """Tests listing, installation and uninstallation of packages.""" 

43 

44 with apio_runner.in_sandbox() as sb: 

45 

46 # -- Run 'apio packages list' 

47 result = sb.invoke_apio_cmd(apio, ["packages", "list"]) 

48 sb.assert_result_ok(result) 

49 assert "definitions" in result.output 

50 assert "oss-cad-suite" in result.output 

51 

52 # -- Run 'apio packages install'. 

53 # -- Both 'definitions' and 'oss-cad-suite' should exist, and 

54 # -- possibly others, depending on the platform. 

55 result = sb.invoke_apio_cmd(apio, ["packages", "install"]) 

56 sb.assert_result_ok(result) 

57 assert "All Apio packages are installed OK" in result.output 

58 assert listdir(sb.packages_dir / "definitions/examples/alhambra-ii") 

59 assert listdir(sb.packages_dir / "oss-cad-suite/bin") 

60 

61 # -- Delete a file from the definitions package, we will use it as an 

62 # -- indicator for the reinstallation of the package. 

63 marker_file = ( 

64 sb.packages_dir / "definitions/examples/alhambra-ii/ledon/main.v" 

65 ) 

66 assert marker_file.is_file() 

67 marker_file.unlink() 

68 assert not marker_file.exists() 

69 

70 # -- Run 'apio packages install'. 

71 # -- This should not do anything since it's considered to be installed. 

72 result = sb.invoke_apio_cmd(apio, ["packages", "install"]) 

73 sb.assert_result_ok(result) 

74 assert "Package 'definitions' installed" not in result.output 

75 assert not marker_file.exists() 

76 

77 # -- Run 'apio packages install --force' 

78 # -- This should recover the file. 

79 result = sb.invoke_apio_cmd(apio, ["packages", "install", "--force"]) 

80 sb.assert_result_ok(result) 

81 assert "Package 'definitions' installed" in result.output 

82 assert marker_file.is_file() 

83 

84 # -- Break the definitions package by renaming it. This also creates an 

85 # -- orphan dir. 

86 definitions_package_dir = sb.packages_dir / "definitions" 

87 bad_package_dir = sb.packages_dir / "unknown-package" 

88 rename(definitions_package_dir, bad_package_dir) 

89 assert not definitions_package_dir.exists() 

90 assert bad_package_dir.is_dir() 

91 

92 # -- Run 'apio packages install'. This should fix everything. 

93 result = sb.invoke_apio_cmd(apio, ["packages", "install"]) 

94 sb.assert_result_ok(result) 

95 assert "Uninstalling broken package 'definitions'" in result.output 

96 assert ( 

97 "Deleting unknown package dir 'unknown-package'" in result.output 

98 ) 

99 assert "Package 'definitions' installed successfully" in result.output 

100 assert definitions_package_dir.is_dir() 

101 assert marker_file.exists() 

102 assert not bad_package_dir.exists()