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

76 statements  

« prev     ^ index     » next       coverage.py v7.4.4, created at 2024-04-20 20:51 +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://github.com/tsfpga/tsfpga 

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

8 

9# Standard libraries 

10import os 

11from pathlib import Path 

12 

13# Third party libraries 

14import pytest 

15from git import Actor, Repo 

16 

17# First party libraries 

18from tsfpga import REPO_ROOT, TSFPGA_DOC 

19from tsfpga.git_utils import find_git_files, get_git_commit, git_commands_are_available 

20from tsfpga.system_utils import create_directory, create_file, system_is_windows 

21 

22THIS_FILE = Path(__file__) 

23THIS_DIR = THIS_FILE.parent 

24 

25 

26def test_this_file_is_listed_by_find_git_files(): 

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

28 assert THIS_FILE in git_files 

29 # Test with string as well as tuple 

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

31 assert THIS_FILE in git_files 

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

33 assert THIS_FILE in git_files 

34 

35 

36def test_this_file_is_not_listed_by_find_git_files_with_bad_argument(): 

37 git_files = find_git_files(directory=TSFPGA_DOC) 

38 assert THIS_FILE not in git_files 

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

40 assert THIS_FILE not in git_files 

41 

42 

43def test_this_file_is_not_listed_by_find_git_files_with_file_endings_avoid(): 

44 # Test with string as well as tuple 

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

46 assert THIS_FILE not in git_files 

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

48 assert THIS_FILE not in git_files 

49 

50 

51def test_this_file_is_not_listed_by_find_git_files_with_exclude_directory(): 

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

53 assert THIS_FILE not in git_files 

54 

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

56 assert THIS_FILE not in git_files 

57 

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

59 assert THIS_FILE not in git_files 

60 

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

62 assert THIS_FILE not in git_files 

63 

64 

65def test_this_file_is_listed_by_find_git_files_with_bad_exclude_directory(): 

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

67 assert THIS_FILE in git_files 

68 

69 

70def test_git_commands_are_available_should_pass(): 

71 assert git_commands_are_available(directory=THIS_DIR) 

72 assert git_commands_are_available(directory=REPO_ROOT) 

73 

74 

75def test_git_commands_are_available_with_invalid_directory_should_fail(): 

76 if system_is_windows(): 

77 path_outside_of_repo = "c:/" 

78 else: 

79 path_outside_of_repo = "/" 

80 

81 assert not git_commands_are_available(directory=path_outside_of_repo) 

82 

83 

84@pytest.fixture 

85def git_commit_with_local_changes_test(tmp_path): 

86 class TestGitCommitWithLocalChanges: 

87 def __init__(self): 

88 self.repo_path = tmp_path / "my_repo" 

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

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

91 

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

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

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

95 

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

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

98 

99 return TestGitCommitWithLocalChanges() 

100 

101 

102# False positive for pytest fixtures 

103# pylint: disable=redefined-outer-name 

104 

105 

106def test_get_git_commit_with_local_changes(git_commit_with_local_changes_test): 

107 assert get_git_commit(directory=git_commit_with_local_changes_test.repo_path).endswith( 

108 " (local changes present)" 

109 ) 

110 

111 

112def test_get_git_commit_with_env_variable_and_local_changes(git_commit_with_local_changes_test): 

113 if "GIT_COMMIT" in os.environ: 

114 old_env = os.environ["GIT_COMMIT"] 

115 else: 

116 old_env = None 

117 

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

119 assert ( 

120 get_git_commit(directory=git_commit_with_local_changes_test.repo_path) 

121 == f"{os.environ['GIT_COMMIT'][0:16]} (local changes present)" 

122 ) 

123 

124 if old_env is None: 

125 del os.environ["GIT_COMMIT"] 

126 else: 

127 os.environ["GIT_COMMIT"] = old_env 

128 

129 

130def test_get_git_commit_without_local_changes(git_commit_with_local_changes_test): 

131 git_commit_with_local_changes_test.repo.index.commit( 

132 "Trash commit", 

133 author=git_commit_with_local_changes_test.actor, 

134 committer=git_commit_with_local_changes_test.actor, 

135 ) 

136 assert not get_git_commit(directory=git_commit_with_local_changes_test.repo_path).endswith( 

137 " (local changes present)" 

138 ) 

139 

140 

141def test_get_git_commit_from_child_directory(git_commit_with_local_changes_test): 

142 child_directory = create_directory( 

143 git_commit_with_local_changes_test.repo_path / "child_directory" 

144 ) 

145 assert get_git_commit(directory=child_directory).endswith(" (local changes present)")