-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c43a47b
commit 4083c5b
Showing
4 changed files
with
149 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
# Author: Felix Fontein <[email protected]> | ||
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) | ||
# SPDX-License-Identifier: GPL-3.0-or-later | ||
# SPDX-FileCopyrightText: 2020, Ansible Project | ||
# SPDX-FileCopyrightText: 2024, Ansible Project | ||
|
||
""" | ||
Test utils module. | ||
Test vcs module. | ||
""" | ||
|
||
from __future__ import annotations | ||
|
@@ -14,33 +14,16 @@ | |
|
||
import pytest | ||
|
||
from antsibull_fileutils.vcs import detect_vcs | ||
from antsibull_fileutils.vcs import detect_vcs, list_git_files | ||
|
||
from .utils import collect_log | ||
|
||
|
||
def test_detect_vcs(): | ||
path = "/path/to/dir" | ||
git_bin_path = "/path/to/git" | ||
git_command = [git_bin_path, "-C", path, "rev-parse", "--is-inside-work-tree"] | ||
|
||
def collect_log(): | ||
debug = [] | ||
info = [] | ||
|
||
def log_debug(msg: str, *args) -> None: | ||
debug.append((msg, args)) | ||
|
||
def log_info(msg: str, *args) -> None: | ||
info.append((msg, args)) | ||
|
||
return ( | ||
{ | ||
"log_debug": log_debug, | ||
"log_info": log_info, | ||
}, | ||
debug, | ||
info, | ||
) | ||
|
||
with mock.patch( | ||
"subprocess.check_output", | ||
return_value="true\n", | ||
|
@@ -83,3 +66,70 @@ def log_info(msg: str, *args) -> None: | |
) as m: | ||
assert detect_vcs(path, git_bin_path=git_bin_path) == "none" | ||
m.assert_called_with(git_command, text=True, encoding="utf-8") | ||
|
||
|
||
TEST_LIST_GIT_FILES = [ | ||
(b"", [], False), | ||
(b"", [], True), | ||
(b"foo\nbar", [b"foo\nbar"], False), | ||
(b"foo\x00", [b"foo"], True), | ||
(b"foo\x00bar", [b"foo", b"bar"], True), | ||
( | ||
b"link\x00foobar\x00dir/binary_file", | ||
[b"link", b"foobar", b"dir/binary_file"], | ||
False, | ||
), | ||
] | ||
|
||
|
||
@pytest.mark.parametrize("stdout, expected, with_logging", TEST_LIST_GIT_FILES) | ||
def test_list_git_files(stdout: bytes, expected: list[str], with_logging: bool): | ||
path = "/path/to/dir" | ||
git_bin_path = "/path/to/git" | ||
git_command = [ | ||
git_bin_path, | ||
"ls-files", | ||
"-z", | ||
"--cached", | ||
"--others", | ||
"--exclude-standard", | ||
"--deduplicate", | ||
] | ||
|
||
with mock.patch( | ||
"subprocess.check_output", | ||
return_value=stdout, | ||
) as m: | ||
kwargs, debug, info = collect_log(with_debug=with_logging, with_info=False) | ||
assert list_git_files(path, git_bin_path=git_bin_path, **kwargs) == expected | ||
m.assert_called_with(git_command, cwd=path) | ||
if with_logging: | ||
assert debug == [("Identifying files not ignored by Git in {!r}", (path,))] | ||
|
||
|
||
def test_list_git_files_fail(): | ||
path = "/path/to/dir" | ||
git_bin_path = "/path/to/git" | ||
git_command = [ | ||
git_bin_path, | ||
"ls-files", | ||
"-z", | ||
"--cached", | ||
"--others", | ||
"--exclude-standard", | ||
"--deduplicate", | ||
] | ||
|
||
with mock.patch( | ||
"subprocess.check_output", | ||
side_effect=subprocess.CalledProcessError(128, path), | ||
) as m: | ||
with pytest.raises(ValueError, match="^Error while running git$") as exc: | ||
list_git_files(path, git_bin_path=git_bin_path) | ||
|
||
with mock.patch( | ||
"subprocess.check_output", | ||
side_effect=FileNotFoundError(), | ||
) as m: | ||
with pytest.raises(ValueError, match="^Cannot find git executable$") as exc: | ||
list_git_files(path, git_bin_path=git_bin_path) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# Author: Felix Fontein <[email protected]> | ||
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) | ||
# SPDX-License-Identifier: GPL-3.0-or-later | ||
# SPDX-FileCopyrightText: 2024, Ansible Project | ||
|
||
""" | ||
Utilities | ||
""" | ||
|
||
from __future__ import annotations | ||
|
||
|
||
def collect_log(with_debug: bool = True, with_info: bool = True): | ||
debug = [] | ||
info = [] | ||
|
||
def log_debug(msg: str, *args) -> None: | ||
debug.append((msg, args)) | ||
|
||
def log_info(msg: str, *args) -> None: | ||
info.append((msg, args)) | ||
|
||
kwargs = {} | ||
if with_debug: | ||
kwargs["log_debug"] = log_debug | ||
if with_info: | ||
kwargs["log_info"] = log_info | ||
return ( | ||
kwargs, | ||
debug, | ||
info, | ||
) |