Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

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

3# 

4# This file is part of the tsfpga project. 

5# https://tsfpga.com 

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

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

8 

9from pathlib import Path 

10import os 

11import unittest 

12 

13import pytest 

14from git import Actor, Repo 

15 

16from tsfpga import REPO_ROOT, TSFPGA_MODULES 

17from tsfpga.git_utils import ( 

18 git_commands_are_available, 

19 find_git_files, 

20 get_git_commit, 

21) 

22from tsfpga.system_utils import create_directory, create_file, system_is_windows 

23 

24 

25THIS_FILE = Path(__file__) 

26THIS_DIR = THIS_FILE.parent 

27 

28 

29def test_this_file_is_listed_by_find_git_files(): 

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

31 assert THIS_FILE in git_files 

32 # Test with string as well as tuple 

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

34 assert THIS_FILE in git_files 

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

36 assert THIS_FILE in git_files 

37 

38 

39def test_this_file_is_not_listed_by_find_git_files_with_bad_argument(): 

40 git_files = find_git_files(directory=TSFPGA_MODULES) 

41 assert THIS_FILE not in git_files 

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

43 assert THIS_FILE not in git_files 

44 

45 

46def test_this_file_is_not_listed_by_find_git_files_with_file_endings_avoid(): 

47 # Test with string as well as tuple 

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

49 assert THIS_FILE not in git_files 

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

51 assert THIS_FILE not in git_files 

52 

53 

54def test_this_file_is_not_listed_by_find_git_files_with_exclude_directory(): 

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

56 assert THIS_FILE not in git_files 

57 

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

59 assert THIS_FILE not in git_files 

60 

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

62 assert THIS_FILE not in git_files 

63 

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

65 assert THIS_FILE not in git_files 

66 

67 

68def test_this_file_is_listed_by_find_git_files_with_bad_exclude_directory(): 

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

70 assert THIS_FILE in git_files 

71 

72 

73def test_git_commands_are_available_should_pass(): 

74 assert git_commands_are_available(directory=THIS_DIR) 

75 assert git_commands_are_available(directory=REPO_ROOT) 

76 

77 

78def test_git_commands_are_available_with_invalid_directory_should_fail(): 

79 if system_is_windows(): 

80 path_outside_of_repo = "c:/" 

81 else: 

82 path_outside_of_repo = "/" 

83 

84 assert not git_commands_are_available(directory=path_outside_of_repo) 

85 

86 

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

88class TestGitCommitWithLocalChanges(unittest.TestCase): 

89 

90 tmp_path = None 

91 

92 _local_changes_present = " (local changes present)" 

93 

94 def setUp(self): 

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

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

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

98 

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

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

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

102 

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

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

105 

106 def test_get_git_commit_with_local_changes(self): 

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

108 

109 def test_get_git_commit_with_env_variable_and_local_changes(self): 

110 if "GIT_COMMIT" in os.environ: 

111 old_env = os.environ["GIT_COMMIT"] 

112 else: 

113 old_env = None 

114 

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

116 assert get_git_commit(directory=self.repo_path) == os.environ["GIT_COMMIT"][0:16] 

117 

118 if old_env is None: 

119 del os.environ["GIT_COMMIT"] 

120 else: 

121 os.environ["GIT_COMMIT"] = old_env 

122 

123 def test_get_git_commit_without_local_changes(self): 

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

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

126 

127 def test_get_git_commit_from_child_directory(self): 

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

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