Coverage for tsfpga/test/unit/test_git_utils.py: 96%
77 statements
« prev ^ index » next coverage.py v6.4, created at 2022-05-28 04:01 +0000
« prev ^ index » next coverage.py v6.4, created at 2022-05-28 04:01 +0000
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# --------------------------------------------------------------------------------------------------
9from pathlib import Path
10import os
11import unittest
13import pytest
14from git import Actor, Repo
16from tsfpga import REPO_ROOT, TSFPGA_DOC
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
25THIS_FILE = Path(__file__)
26THIS_DIR = THIS_FILE.parent
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
39def test_this_file_is_not_listed_by_find_git_files_with_bad_argument():
40 git_files = find_git_files(directory=TSFPGA_DOC)
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
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
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
58 git_files = find_git_files(directory=REPO_ROOT, exclude_directories=[THIS_DIR.parent])
59 assert THIS_FILE not in git_files
61 git_files = find_git_files(directory=REPO_ROOT, exclude_directories=[THIS_DIR.parent, THIS_DIR])
62 assert THIS_FILE not in git_files
64 git_files = find_git_files(directory=REPO_ROOT, exclude_directories=[THIS_FILE])
65 assert THIS_FILE not in git_files
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
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)
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 = "/"
84 assert not git_commands_are_available(directory=path_outside_of_repo)
87@pytest.mark.usefixtures("fixture_tmp_path")
88class TestGitCommitWithLocalChanges(unittest.TestCase):
90 tmp_path = None
92 _local_changes_present = " (local changes present)"
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")
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)
103 trash_file = create_file(self.repo_path / "local_file_for_git_test.apa")
104 self.repo.index.add(str(trash_file))
106 def test_get_git_commit_with_local_changes(self):
107 assert get_git_commit(directory=self.repo_path).endswith(self._local_changes_present)
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
115 os.environ["GIT_COMMIT"] = "54849b5a8152b07e0809b8f90fc24d54262cb4d6"
116 assert (
117 get_git_commit(directory=self.repo_path)
118 == f"{os.environ['GIT_COMMIT'][0:16]}{self._local_changes_present}"
119 )
121 if old_env is None:
122 del os.environ["GIT_COMMIT"]
123 else:
124 os.environ["GIT_COMMIT"] = old_env
126 def test_get_git_commit_without_local_changes(self):
127 self.repo.index.commit("Trash commit", author=self.actor, committer=self.actor)
128 assert not get_git_commit(directory=self.repo_path).endswith(self._local_changes_present)
130 def test_get_git_commit_from_child_directory(self):
131 child_directory = create_directory(self.repo_path / "child_directory")
132 assert get_git_commit(directory=child_directory).endswith(self._local_changes_present)