Coverage for tsfpga/test/unit/test_git_utils.py: 96%

77 statements  

« prev     ^ index     » next       coverage.py v6.5.0, created at 2022-11-29 20:01 +0000

1# -------------------------------------------------------------------------------------------------- 

2# Copyright (c) Lukas Vik. All rights reserved. 

3# 

4# This file is part of the tsfpga project, a project platform for modern FPGA development. 

5# https://tsfpga.com 

6# https://gitlab.com/tsfpga/tsfpga 

7# -------------------------------------------------------------------------------------------------- 

8 

9# Standard libraries 

10import os 

11import unittest 

12from pathlib import Path 

13 

14# Third party libraries 

15import pytest 

16from git import Actor, Repo 

17 

18# First party libraries 

19from tsfpga import REPO_ROOT, TSFPGA_DOC 

20from tsfpga.git_utils import find_git_files, get_git_commit, git_commands_are_available 

21from tsfpga.system_utils import create_directory, create_file, system_is_windows 

22 

23THIS_FILE = Path(__file__) 

24THIS_DIR = THIS_FILE.parent 

25 

26 

27def test_this_file_is_listed_by_find_git_files(): 

28 git_files = find_git_files(directory=THIS_DIR, file_endings_include="py") 

29 assert THIS_FILE in git_files 

30 # Test with string as well as tuple 

31 git_files = find_git_files(directory=REPO_ROOT, file_endings_include="py") 

32 assert THIS_FILE in git_files 

33 git_files = find_git_files(directory=REPO_ROOT, file_endings_include=("py",)) 

34 assert THIS_FILE in git_files 

35 

36 

37def test_this_file_is_not_listed_by_find_git_files_with_bad_argument(): 

38 git_files = find_git_files(directory=TSFPGA_DOC) 

39 assert THIS_FILE not in git_files 

40 git_files = find_git_files(directory=REPO_ROOT, file_endings_include="vhd") 

41 assert THIS_FILE not in git_files 

42 

43 

44def test_this_file_is_not_listed_by_find_git_files_with_file_endings_avoid(): 

45 # Test with string as well as tuple 

46 git_files = find_git_files(directory=REPO_ROOT, file_endings_avoid="py") 

47 assert THIS_FILE not in git_files 

48 git_files = find_git_files(directory=REPO_ROOT, file_endings_avoid=("py",)) 

49 assert THIS_FILE not in git_files 

50 

51 

52def test_this_file_is_not_listed_by_find_git_files_with_exclude_directory(): 

53 git_files = find_git_files(directory=REPO_ROOT, exclude_directories=[THIS_DIR]) 

54 assert THIS_FILE not in git_files 

55 

56 git_files = find_git_files(directory=REPO_ROOT, exclude_directories=[THIS_DIR.parent]) 

57 assert THIS_FILE not in git_files 

58 

59 git_files = find_git_files(directory=REPO_ROOT, exclude_directories=[THIS_DIR.parent, THIS_DIR]) 

60 assert THIS_FILE not in git_files 

61 

62 git_files = find_git_files(directory=REPO_ROOT, exclude_directories=[THIS_FILE]) 

63 assert THIS_FILE not in git_files 

64 

65 

66def test_this_file_is_listed_by_find_git_files_with_bad_exclude_directory(): 

67 git_files = find_git_files(directory=REPO_ROOT, exclude_directories=[THIS_DIR.parent / "apa"]) 

68 assert THIS_FILE in git_files 

69 

70 

71def test_git_commands_are_available_should_pass(): 

72 assert git_commands_are_available(directory=THIS_DIR) 

73 assert git_commands_are_available(directory=REPO_ROOT) 

74 

75 

76def test_git_commands_are_available_with_invalid_directory_should_fail(): 

77 if system_is_windows(): 

78 path_outside_of_repo = "c:/" 

79 else: 

80 path_outside_of_repo = "/" 

81 

82 assert not git_commands_are_available(directory=path_outside_of_repo) 

83 

84 

85@pytest.mark.usefixtures("fixture_tmp_path") 

86class TestGitCommitWithLocalChanges(unittest.TestCase): 

87 

88 tmp_path = None 

89 

90 _local_changes_present = " (local changes present)" 

91 

92 def setUp(self): 

93 self.repo_path = self.tmp_path / "my_repo" 

94 self.repo = Repo.init(self.repo_path) 

95 self.actor = Actor("A name", "an@email.com") 

96 

97 initial_file = create_file(self.repo_path / "initial_commit_file.txt") 

98 self.repo.index.add(str(initial_file)) 

99 self.repo.index.commit("Initial commit", author=self.actor, committer=self.actor) 

100 

101 trash_file = create_file(self.repo_path / "local_file_for_git_test.apa") 

102 self.repo.index.add(str(trash_file)) 

103 

104 def test_get_git_commit_with_local_changes(self): 

105 assert get_git_commit(directory=self.repo_path).endswith(self._local_changes_present) 

106 

107 def test_get_git_commit_with_env_variable_and_local_changes(self): 

108 if "GIT_COMMIT" in os.environ: 

109 old_env = os.environ["GIT_COMMIT"] 

110 else: 

111 old_env = None 

112 

113 os.environ["GIT_COMMIT"] = "54849b5a8152b07e0809b8f90fc24d54262cb4d6" 

114 assert ( 

115 get_git_commit(directory=self.repo_path) 

116 == f"{os.environ['GIT_COMMIT'][0:16]}{self._local_changes_present}" 

117 ) 

118 

119 if old_env is None: 

120 del os.environ["GIT_COMMIT"] 

121 else: 

122 os.environ["GIT_COMMIT"] = old_env 

123 

124 def test_get_git_commit_without_local_changes(self): 

125 self.repo.index.commit("Trash commit", author=self.actor, committer=self.actor) 

126 assert not get_git_commit(directory=self.repo_path).endswith(self._local_changes_present) 

127 

128 def test_get_git_commit_from_child_directory(self): 

129 child_directory = create_directory(self.repo_path / "child_directory") 

130 assert get_git_commit(directory=child_directory).endswith(self._local_changes_present)