Coverage for tsfpga/test/test_git_utils.py: 96%
77 statements
« prev ^ index » next coverage.py v7.6.10, created at 2025-01-04 20:51 +0000
« prev ^ index » next coverage.py v7.6.10, created at 2025-01-04 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# --------------------------------------------------------------------------------------------------
9# Standard libraries
10import os
11from pathlib import Path
13# Third party libraries
14import pytest
15from git import Actor, Repo
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
22THIS_FILE = Path(__file__)
23THIS_DIR = THIS_FILE.parent
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
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
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
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
55 git_files = find_git_files(directory=REPO_ROOT, exclude_directories=[THIS_DIR.parent])
56 assert THIS_FILE not in git_files
58 git_files = find_git_files(directory=REPO_ROOT, exclude_directories=[THIS_DIR.parent, THIS_DIR])
59 assert THIS_FILE not in git_files
61 git_files = find_git_files(directory=REPO_ROOT, exclude_directories=[THIS_FILE])
62 assert THIS_FILE not in git_files
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
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)
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 = "/"
81 assert not git_commands_are_available(directory=path_outside_of_repo)
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")
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)
96 trash_file = create_file(self.repo_path / "local_file_for_git_test.apa")
97 self.repo.index.add(str(trash_file))
99 return TestGitCommitWithLocalChanges()
102# False positive for pytest fixtures
103# pylint: disable=redefined-outer-name
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 )
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
118 sha = "54849b5a8152b07e0809b8f90fc24d54262cb4d6"
119 os.environ["GIT_COMMIT"] = sha
120 assert (
121 get_git_commit(directory=git_commit_with_local_changes_test.repo_path)
122 == f"{sha[0:12]} (local changes present)"
123 )
125 if old_env is None:
126 del os.environ["GIT_COMMIT"]
127 else:
128 os.environ["GIT_COMMIT"] = old_env
131def test_get_git_commit_without_local_changes(git_commit_with_local_changes_test):
132 git_commit_with_local_changes_test.repo.index.commit(
133 "Trash commit",
134 author=git_commit_with_local_changes_test.actor,
135 committer=git_commit_with_local_changes_test.actor,
136 )
137 assert not get_git_commit(directory=git_commit_with_local_changes_test.repo_path).endswith(
138 " (local changes present)"
139 )
142def test_get_git_commit_from_child_directory(git_commit_with_local_changes_test):
143 child_directory = create_directory(
144 git_commit_with_local_changes_test.repo_path / "child_directory"
145 )
146 assert get_git_commit(directory=child_directory).endswith(" (local changes present)")