Coverage for tsfpga/test/unit/test_git_utils.py: 96%
77 statements
« prev ^ index » next coverage.py v7.2.1, created at 2023-09-27 20:00 +0000
« prev ^ index » next coverage.py v7.2.1, created at 2023-09-27 20:00 +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# --------------------------------------------------------------------------------------------------
9# Standard libraries
10import os
11import unittest
12from pathlib import Path
14# Third party libraries
15import pytest
16from git import Actor, Repo
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
23THIS_FILE = Path(__file__)
24THIS_DIR = THIS_FILE.parent
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
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
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
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
56 git_files = find_git_files(directory=REPO_ROOT, exclude_directories=[THIS_DIR.parent])
57 assert THIS_FILE not in git_files
59 git_files = find_git_files(directory=REPO_ROOT, exclude_directories=[THIS_DIR.parent, THIS_DIR])
60 assert THIS_FILE not in git_files
62 git_files = find_git_files(directory=REPO_ROOT, exclude_directories=[THIS_FILE])
63 assert THIS_FILE not in git_files
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
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)
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 = "/"
82 assert not git_commands_are_available(directory=path_outside_of_repo)
85@pytest.mark.usefixtures("fixture_tmp_path")
86class TestGitCommitWithLocalChanges(unittest.TestCase):
87 tmp_path = None
89 _local_changes_present = " (local changes present)"
91 def setUp(self):
92 self.repo_path = self.tmp_path / "my_repo"
93 self.repo = Repo.init(self.repo_path)
94 self.actor = Actor("A name", "an@email.com")
96 initial_file = create_file(self.repo_path / "initial_commit_file.txt")
97 self.repo.index.add(str(initial_file))
98 self.repo.index.commit("Initial commit", author=self.actor, committer=self.actor)
100 trash_file = create_file(self.repo_path / "local_file_for_git_test.apa")
101 self.repo.index.add(str(trash_file))
103 def test_get_git_commit_with_local_changes(self):
104 assert get_git_commit(directory=self.repo_path).endswith(self._local_changes_present)
106 def test_get_git_commit_with_env_variable_and_local_changes(self):
107 if "GIT_COMMIT" in os.environ:
108 old_env = os.environ["GIT_COMMIT"]
109 else:
110 old_env = None
112 os.environ["GIT_COMMIT"] = "54849b5a8152b07e0809b8f90fc24d54262cb4d6"
113 assert (
114 get_git_commit(directory=self.repo_path)
115 == f"{os.environ['GIT_COMMIT'][0:16]}{self._local_changes_present}"
116 )
118 if old_env is None:
119 del os.environ["GIT_COMMIT"]
120 else:
121 os.environ["GIT_COMMIT"] = old_env
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)
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)