Coverage for tests/test_cli.py: 100%

52 statements  

« prev     ^ index     » next       coverage.py v6.4.2, created at 2022-08-01 13:01 +0200

1import os 

2import sys 

3 

4from pathlib import Path 

5import pytest 

6 

7from iso_freeze.cli import determine_default_file, validate_pip_version, parse_args 

8 

9 

10def test_determine_default() -> None: 

11 """Test whether correct default file is picked if none is specified.""" 

12 # If only requirements.in found, return that 

13 os.chdir(Path(Path(__file__).parent.resolve(), "test_directories", "requirements")) 

14 assert determine_default_file() == Path("requirements.in") 

15 # If only pyproject.toml found, return that 

16 os.chdir(Path(Path(__file__).parent.resolve(), "test_directories", "pyproject")) 

17 assert determine_default_file() == Path("pyproject.toml") 

18 # If neither requirements.in or pyproject.toml, return None 

19 os.chdir(Path(Path(__file__).parent.resolve(), "test_directories", "neither")) 

20 assert determine_default_file() is None 

21 # If both requirements.in or pyproject.toml, return requirements.in 

22 os.chdir(Path(Path(__file__).parent.resolve(), "test_directories", "both")) 

23 assert determine_default_file() == Path("requirements.in") 

24 

25 

26def test_validate_pip_version() -> None: 

27 """Test whether pip version is validated correctly.""" 

28 mocked_pip_version_output_1 = "pip 22.2 from /funny/path/pip (python 3.9)" 

29 assert validate_pip_version(pip_version_output=mocked_pip_version_output_1) is True 

30 mocked_pip_version_output_2 = "pip 22.1 from /funny/path/pip (python 3.9)" 

31 assert validate_pip_version(pip_version_output=mocked_pip_version_output_2) is False 

32 mocked_pip_version_output_3 = "pip 23.1 from /funny/path/pip (python 3.10)" 

33 assert validate_pip_version(pip_version_output=mocked_pip_version_output_3) is True 

34 mocked_pip_version_output_4 = "pip 20.1.3 from /funny/path/pip (python 3.8)" 

35 assert validate_pip_version(pip_version_output=mocked_pip_version_output_4) is False 

36 mocked_pip_version_output_5 = "pip 34.2.9 from /funny/path/pip (python 3.15)" 

37 assert validate_pip_version(pip_version_output=mocked_pip_version_output_5) is True 

38 

39 

40def test_parse_args() -> None: 

41 """Test of args are correctly parsed.""" 

42 sys.argv[1:] = ["pyproject.toml", "-d", "dev"] 

43 test_args1 = parse_args() 

44 assert test_args1.python == Path("python3") 

45 assert test_args1.dependency == "dev" 

46 assert test_args1.file == Path("pyproject.toml") 

47 assert test_args1.output == Path("requirements.txt") 

48 sys.argv[1:] = ["requirements.in", "--pip-args", "--upgrade-strategy eager"] 

49 test_args2 = parse_args() 

50 assert test_args2.pip_args == ["--upgrade-strategy", "eager"] 

51 assert test_args2.sync is False 

52 assert test_args2.hashes is False 

53 

54 

55def test_input_file_doesnt_exist() -> None: 

56 """Test if providing non-existing file causes sys.exit().""" 

57 sys.argv[1:] = ["some file that should not exist!!!111"] 

58 with pytest.raises(SystemExit) as e: 

59 parse_args() 

60 assert e.type == SystemExit 

61 

62 

63def test_no_default_file_found() -> None: 

64 """Test if sys.exit() raised when no file provided and no default can be found.""" 

65 os.chdir(Path(Path(__file__).parent.resolve(), "test_directories", "neither")) 

66 with pytest.raises(SystemExit) as e: 

67 parse_args() 

68 assert e.type == SystemExit 

69 

70 

71def test_combine_requirements_dependency() -> None: 

72 """Test if sys.exit() when optional dependency specified for requirements file.""" 

73 sys.argv[1:] = ["requirements.in", "-d", "dev"] 

74 with pytest.raises(SystemExit) as e: 

75 parse_args() 

76 assert e.type == SystemExit