Coverage for tsfpga/test/test_git_utils.py: 99%
73 statements
« prev ^ index » next coverage.py v7.6.12, created at 2025-02-21 20:51 +0000
« prev ^ index » next coverage.py v7.6.12, created at 2025-02-21 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# --------------------------------------------------------------------------------------------------
9import os
10from pathlib import Path
12import pytest
13from git import Actor, Repo
15from tsfpga import REPO_ROOT, TSFPGA_DOC
16from tsfpga.git_utils import find_git_files, get_git_commit, git_commands_are_available
17from tsfpga.system_utils import create_directory, create_file, system_is_windows
19THIS_FILE = Path(__file__)
20THIS_DIR = THIS_FILE.parent
23def test_this_file_is_listed_by_find_git_files():
24 git_files = find_git_files(directory=THIS_DIR, file_endings_include="py")
25 assert THIS_FILE in git_files
26 # Test with string as well as tuple
27 git_files = find_git_files(directory=REPO_ROOT, file_endings_include="py")
28 assert THIS_FILE in git_files
29 git_files = find_git_files(directory=REPO_ROOT, file_endings_include=("py",))
30 assert THIS_FILE in git_files
33def test_this_file_is_not_listed_by_find_git_files_with_bad_argument():
34 git_files = find_git_files(directory=TSFPGA_DOC)
35 assert THIS_FILE not in git_files
36 git_files = find_git_files(directory=REPO_ROOT, file_endings_include="vhd")
37 assert THIS_FILE not in git_files
40def test_this_file_is_not_listed_by_find_git_files_with_file_endings_avoid():
41 # Test with string as well as tuple
42 git_files = find_git_files(directory=REPO_ROOT, file_endings_avoid="py")
43 assert THIS_FILE not in git_files
44 git_files = find_git_files(directory=REPO_ROOT, file_endings_avoid=("py",))
45 assert THIS_FILE not in git_files
48def test_this_file_is_not_listed_by_find_git_files_with_exclude_directory():
49 git_files = find_git_files(directory=REPO_ROOT, exclude_directories=[THIS_DIR])
50 assert THIS_FILE not in git_files
52 git_files = find_git_files(directory=REPO_ROOT, exclude_directories=[THIS_DIR.parent])
53 assert THIS_FILE not in git_files
55 git_files = find_git_files(directory=REPO_ROOT, exclude_directories=[THIS_DIR.parent, THIS_DIR])
56 assert THIS_FILE not in git_files
58 git_files = find_git_files(directory=REPO_ROOT, exclude_directories=[THIS_FILE])
59 assert THIS_FILE not in git_files
62def test_this_file_is_listed_by_find_git_files_with_bad_exclude_directory():
63 git_files = find_git_files(directory=REPO_ROOT, exclude_directories=[THIS_DIR.parent / "apa"])
64 assert THIS_FILE in git_files
67def test_git_commands_are_available_should_pass():
68 assert git_commands_are_available(directory=THIS_DIR)
69 assert git_commands_are_available(directory=REPO_ROOT)
72def test_git_commands_are_available_with_invalid_directory_should_fail():
73 path_outside_of_repo = "c:/" if system_is_windows() else "/"
75 assert not git_commands_are_available(directory=path_outside_of_repo)
78@pytest.fixture
79def git_commit_with_local_changes_test(tmp_path):
80 class TestGitCommitWithLocalChanges:
81 def __init__(self):
82 self.repo_path = tmp_path / "my_repo"
83 self.repo = Repo.init(self.repo_path)
84 self.actor = Actor("A name", "an@email.com")
86 initial_file = create_file(self.repo_path / "initial_commit_file.txt")
87 self.repo.index.add(str(initial_file))
88 self.repo.index.commit("Initial commit", author=self.actor, committer=self.actor)
90 trash_file = create_file(self.repo_path / "local_file_for_git_test.apa")
91 self.repo.index.add(str(trash_file))
93 return TestGitCommitWithLocalChanges()
96def test_get_git_commit_with_local_changes(git_commit_with_local_changes_test):
97 assert get_git_commit(directory=git_commit_with_local_changes_test.repo_path).endswith(
98 " (local changes present)"
99 )
102def test_get_git_commit_with_env_variable_and_local_changes(git_commit_with_local_changes_test):
103 old_env = os.environ.get("GIT_COMMIT", None)
105 sha = "54849b5a8152b07e0809b8f90fc24d54262cb4d6"
106 os.environ["GIT_COMMIT"] = sha
107 assert (
108 get_git_commit(directory=git_commit_with_local_changes_test.repo_path)
109 == f"{sha[0:12]} (local changes present)"
110 )
112 if old_env is None:
113 del os.environ["GIT_COMMIT"]
114 else:
115 os.environ["GIT_COMMIT"] = old_env
118def test_get_git_commit_without_local_changes(git_commit_with_local_changes_test):
119 git_commit_with_local_changes_test.repo.index.commit(
120 "Trash commit",
121 author=git_commit_with_local_changes_test.actor,
122 committer=git_commit_with_local_changes_test.actor,
123 )
124 assert not get_git_commit(directory=git_commit_with_local_changes_test.repo_path).endswith(
125 " (local changes present)"
126 )
129def test_get_git_commit_from_child_directory(git_commit_with_local_changes_test):
130 child_directory = create_directory(
131 git_commit_with_local_changes_test.repo_path / "child_directory"
132 )
133 assert get_git_commit(directory=child_directory).endswith(" (local changes present)")