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

18 statements  

« prev     ^ index     » next       coverage.py v7.16.1, created at 2026-09-23 03:53 +0000

1""" 

2Tests of apio's __main__.py entry point. 

3""" 

4 

5import os 

6import sys 

7import subprocess 

8from subprocess import CompletedProcess 

9from tests.conftest import ApioRunner 

10from apio import __main__ as apio_main 

11 

12 

13def _run_apio_version(extra_env: dict) -> CompletedProcess: 

14 """Runs 'apio --version' in a fresh process with the given extra env 

15 vars.""" 

16 return subprocess.run( 

17 [sys.executable, apio_main.__file__, "--version"], 

18 env={**os.environ, **extra_env}, 

19 capture_output=True, 

20 encoding="utf-8", 

21 text=True, 

22 check=False, 

23 ) 

24 

25 

26def test_apio_debug_env_var_values(apio_runner: ApioRunner): 

27 """Tests that malformed APIO_DEBUG values don't crash the apio entry 

28 point. It used to crash with an unhandled ValueError on every command.""" 

29 

30 with apio_runner.in_sandbox(): 

31 

32 # -- Malformed values should be tolerated (debug off). 

33 for value in ["x", "", "1.5", '"x"', '"']: 

34 result = _run_apio_version({"APIO_DEBUG": value}) 

35 assert result.returncode != 0, result.stderr 

36 assert ( 

37 "Error: env value APIO_DEBUG" in result.stdout 

38 ), result.stdout 

39 

40 # -- Valid values, including windows style quoted values, should 

41 # -- work as before. 

42 for value in ["0", "1", '"2"']: 

43 result = _run_apio_version({"APIO_DEBUG": value}) 

44 assert result.returncode == 0, result.stderr 

45 assert "Apio CLI version" in result.stdout, result.stdout