Coverage for tests/unit_tests/test_apio_context.py: 100%

35 statements  

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

1""" 

2Tests of apio_context.py 

3""" 

4 

5import os 

6from pathlib import Path 

7from pytest import LogCaptureFixture, raises 

8from tests.conftest import ApioRunner 

9from apio.apio_context import ( 

10 ApioContext, 

11 PackagesPolicy, 

12 ProjectPolicy, 

13 RemoteConfigPolicy, 

14) 

15from apio.common.common_util import PROJECT_BUILD_PATH 

16 

17 

18def test_init(apio_runner: ApioRunner): 

19 """Tests the initialization of the apio context.""" 

20 

21 with apio_runner.in_sandbox() as sb: 

22 

23 # -- Create an apio.ini file. 

24 sb.write_default_apio_ini() 

25 

26 # -- Default init. 

27 apio_ctx = ApioContext( 

28 project_policy=ProjectPolicy.PROJECT_REQUIRED, 

29 remote_config_policy=RemoteConfigPolicy.CACHED_OK, 

30 packages_policy=PackagesPolicy.ENSURE_PACKAGES, 

31 ) 

32 

33 assert apio_ctx.has_project 

34 

35 # -- Verify context's project dir. 

36 assert str(apio_ctx.project_dir) == "." 

37 assert apio_ctx.project_dir.samefile(Path.cwd()) 

38 assert apio_ctx.project_dir.samefile(sb.proj_dir) 

39 

40 # -- Verify context's home and packages dirs. 

41 assert apio_ctx.apio_home_dir == sb.home_dir 

42 assert apio_ctx.apio_packages_dir == sb.packages_dir 

43 

44 # -- Verify build dir 

45 assert PROJECT_BUILD_PATH == Path("_build") 

46 assert apio_ctx.env_build_path == Path("_build/default") 

47 

48 

49def test_home_dir_with_a_bad_character( 

50 apio_runner: ApioRunner, capsys: LogCaptureFixture 

51): 

52 """Tests the initialization of the apio context with home dirs that 

53 contain invalid chars.""" 

54 

55 for invalid_char in ["ó", "ñ", " ", "😼"]: 

56 with apio_runner.in_sandbox() as sb: 

57 

58 # -- Make up a home dir path with the invalid char. 

59 invalid_home_dir = sb.sandbox_dir / f"apio-{invalid_char}-home" 

60 os.environ["APIO_HOME"] = str(invalid_home_dir) 

61 

62 # -- Initialize an apio context. It should exit with an error. 

63 with raises(SystemExit) as e: 

64 ApioContext( 

65 project_policy=ProjectPolicy.NO_PROJECT, 

66 remote_config_policy=RemoteConfigPolicy.CACHED_OK, 

67 packages_policy=PackagesPolicy.ENSURE_PACKAGES, 

68 ) 

69 assert e.value.code == 1 

70 assert ( 

71 f"Unsupported character [{invalid_char}]" 

72 in capsys.readouterr().out 

73 ) 

74 

75 

76def test_home_dir_with_relative_path( 

77 apio_runner: ApioRunner, capsys: LogCaptureFixture 

78): 

79 """Apio context should fail if the apio home dir is a relative path""" 

80 

81 with apio_runner.in_sandbox(): 

82 

83 # -- Make up a home dir path with the invalid char. 

84 invalid_home_dir = Path("./aa/bb") 

85 os.environ["APIO_HOME"] = str(invalid_home_dir) 

86 

87 # -- Initialize an apio context. It should exit with an error. 

88 with raises(SystemExit) as e: 

89 ApioContext( 

90 project_policy=ProjectPolicy.NO_PROJECT, 

91 remote_config_policy=RemoteConfigPolicy.CACHED_OK, 

92 packages_policy=PackagesPolicy.ENSURE_PACKAGES, 

93 ) 

94 assert e.value.code == 1 

95 assert ( 

96 "Error: Apio home dir should be an absolute path" 

97 in capsys.readouterr().out 

98 )