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_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 = self.repo_path / "initial_commit_file.txt" 

100 create_file(initial_file) 

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

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

103 

104 self.trash_file = self.repo_path / "local_file_for_git_test.apa" 

105 create_file(self.trash_file) 

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

107 

108 def test_get_git_commit_with_local_changes(self): 

109 assert get_git_commit(directory=self.repo_path).endswith(" (local changes present)") 

110 

111 def test_get_git_commit_with_env_variable_and_local_changes(self): 

112 if "GIT_COMMIT" in os.environ: 

113 old_env = os.environ["GIT_COMMIT"] 

114 else: 

115 old_env = None 

116 

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

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

119 

120 if old_env is None: 

121 del os.environ["GIT_COMMIT"] 

122 else: 

123 os.environ["GIT_COMMIT"] = old_env 

124 

125 def test_get_git_commit_without_local_changes(self): 

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

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