Coverage for tests/unit_tests/utils/test_env_options.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""" 

10Tests of env_options.py 

11""" 

12 

13import os 

14import pytest 

15from tests.conftest import ApioRunner 

16from apio.utils import env_options 

17from apio.utils.env_options import ( 

18 APIO_HOME, 

19 APIO_PLATFORM, 

20 APIO_DEBUG, 

21) 

22 

23 

24def test_get_all(): 

25 """Tests the list of supported env vars.""" 

26 # -- Make an independent copy so the test doesn't rely on get_all() 

27 # -- returning a copy. 

28 all_vars = env_options.get_all().copy() 

29 

30 # -- Spot check a few vars. 

31 assert APIO_HOME in all_vars 

32 assert APIO_DEBUG in all_vars 

33 

34 # -- All vars have the apio prefix. 

35 for var in all_vars: 

36 assert var.startswith("APIO_"), var 

37 

38 # -- There are no duplicates. 

39 assert len(set(all_vars)) == len(all_vars) 

40 

41 # -- The returned list is a copy, mutating it has no effect. 

42 env_options.get_all().clear() 

43 assert env_options.get_all() == all_vars 

44 

45 

46def test_get(apio_runner: ApioRunner): 

47 """Tests the get() function.""" 

48 

49 # -- The sandbox restores the system env on exit. 

50 with apio_runner.in_sandbox(): 

51 

52 # -- Undefined var returns the default. 

53 os.environ.pop(APIO_HOME, None) 

54 assert env_options.get(APIO_HOME) is None 

55 assert env_options.get(APIO_HOME, "my-default") == "my-default" 

56 

57 # -- Defined var returns its value, ignoring the default. 

58 os.environ[APIO_HOME] = "/my/home" 

59 assert env_options.get(APIO_HOME) == "/my/home" 

60 assert env_options.get(APIO_HOME, "my-default") == "/my/home" 

61 

62 # -- Surrounding quotes are removed (for the benefit of Windows). 

63 os.environ[APIO_HOME] = '"/my/home"' 

64 assert env_options.get(APIO_HOME) == "/my/home" 

65 

66 

67def test_get_unbalanced_quotes(apio_runner: ApioRunner): 

68 """Tests that a value with a leading double quote and no trailing one, 

69 or a trailing double quote and no leading one, is an error.""" 

70 

71 # -- The sandbox restores the system env on exit. 

72 with apio_runner.in_sandbox(): 

73 for value in ['"', '"aa', '"aa"bb', 'aa"', 'bb"aa"']: 

74 os.environ[APIO_HOME] = value 

75 with apio_runner.with_logger() as log: 

76 with pytest.raises(SystemExit) as e: 

77 env_options.get(APIO_HOME) 

78 assert e.value.code == 1, value 

79 assert ( 

80 "Env var APIO_HOME has unbalanced double quotes" in log.out 

81 ), value 

82 

83 

84def test_get_unknown_var(): 

85 """Tests that get() rejects vars that apio doesn't know about.""" 

86 with pytest.raises(AssertionError, match="Unknown apio env var"): 

87 env_options.get("APIO_NO_SUCH_VAR") 

88 

89 

90def test_is_defined(apio_runner: ApioRunner): 

91 """Tests the is_defined() function.""" 

92 

93 # -- The sandbox restores the system env on exit. 

94 with apio_runner.in_sandbox(): 

95 

96 # -- Undefined var. 

97 os.environ.pop(APIO_PLATFORM, None) 

98 assert not env_options.is_defined(APIO_PLATFORM) 

99 

100 # -- Defined var. 

101 os.environ[APIO_PLATFORM] = "linux-x86-64" 

102 assert env_options.is_defined(APIO_PLATFORM) 

103 

104 # -- An empty value still counts as defined. 

105 os.environ[APIO_PLATFORM] = "" 

106 assert env_options.is_defined(APIO_PLATFORM) 

107 

108 

109def test_get_defined(apio_runner: ApioRunner): 

110 """Tests the get_defined() function.""" 

111 

112 # -- The sandbox restores the system env on exit. 

113 with apio_runner.in_sandbox(): 

114 

115 # -- Start with all the apio vars undefined. The sandbox defines 

116 # -- some of them. 

117 for var in env_options.get_all(): 

118 os.environ.pop(var, None) 

119 assert not env_options.get_defined() 

120 

121 # -- Define two vars. They are returned in the order of get_all(). 

122 os.environ[APIO_DEBUG] = "1" 

123 os.environ[APIO_HOME] = "/my/home" 

124 assert env_options.get_defined() == [APIO_HOME, APIO_DEBUG]