-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add: Improve test coverage for plugin git API #295
Merge pull request #295 from greenbone/improve-test-coverage
- Loading branch information
Showing
6 changed files
with
415 additions
and
12 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
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
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
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,66 @@ | ||
# Copyright (C) 2022 Greenbone Networks GmbH | ||
# | ||
# SPDX-License-Identifier: GPL-3.0-or-later | ||
# | ||
# This program is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU General Public License as published by | ||
# the Free Software Foundation, either version 3 of the License, or | ||
# (at your option) any later version. | ||
# | ||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU General Public License | ||
# along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
import os | ||
import tempfile | ||
import unittest | ||
from contextlib import contextmanager | ||
from pathlib import Path | ||
from typing import Generator | ||
|
||
from autohooks.utils import exec_git | ||
|
||
|
||
@contextmanager | ||
def temdir() -> Generator[Path, None, None]: | ||
tempdir = tempfile.TemporaryDirectory() | ||
yield Path(tempdir.name) | ||
tempdir.cleanup() | ||
|
||
|
||
@contextmanager | ||
def tempgitdir() -> Generator[Path, None, None]: | ||
cwd = Path.cwd() | ||
tempdir = tempfile.TemporaryDirectory() | ||
temppath = Path(tempdir.name) | ||
os.chdir(str(temppath)) | ||
exec_git("init", "-b", "main") | ||
exec_git("config", "--local", "user.email", "max.mustermann@example.com") | ||
exec_git("config", "--local", "user.name", "Max Mustermann") | ||
yield temppath | ||
tempdir.cleanup() | ||
os.chdir(str(cwd)) | ||
|
||
|
||
def git_add(*paths: Path) -> None: | ||
exec_git("add", *paths) | ||
|
||
|
||
def git_rm(*paths: Path) -> None: | ||
exec_git("rm", *paths) | ||
|
||
|
||
def git_mv(from_path: Path, to_path: Path) -> None: | ||
exec_git("mv", from_path, to_path) | ||
|
||
|
||
def git_commit(message: str = "Foo Bar"): | ||
exec_git("commit", "--no-gpg-sign", "-m", message) | ||
|
||
|
||
class GitTestCase(unittest.TestCase): | ||
pass |
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,66 @@ | ||
# Copyright (C) 2022 Greenbone Networks GmbH | ||
# | ||
# SPDX-License-Identifier: GPL-3.0-or-later | ||
# | ||
# This program is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU General Public License as published by | ||
# the Free Software Foundation, either version 3 of the License, or | ||
# (at your option) any later version. | ||
# | ||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU General Public License | ||
# along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
from autohooks.api.git import StatusEntry, get_diff | ||
from tests.api.git import GitTestCase, git_add, git_commit, tempgitdir | ||
|
||
|
||
class DiffTestCase(GitTestCase): | ||
def test_get_diff_from_status(self): | ||
with tempgitdir() as tmpdir: | ||
test_file = tmpdir / "foo.txt" | ||
test_file.write_text( | ||
"Lorem\nipsum\ndolor\nsit\namet", encoding="utf8" | ||
) | ||
|
||
git_add(test_file) | ||
git_commit() | ||
|
||
test_file.write_text("ipsum\ndolor\nsit\namet", encoding="utf8") | ||
status = StatusEntry("M foo.txt", tmpdir) | ||
diff = get_diff((status,)) | ||
|
||
expected_diff = """--- a/foo.txt | ||
+++ b/foo.txt | ||
@@ -1,4 +1,3 @@ | ||
-Lorem | ||
ipsum | ||
dolor | ||
sit""" | ||
self.assertIn(expected_diff, diff) | ||
|
||
def test_get_diff(self): | ||
with tempgitdir() as tmpdir: | ||
test_file = tmpdir / "foo.txt" | ||
test_file.write_text( | ||
"Lorem\nipsum\ndolor\nsit\namet", encoding="utf8" | ||
) | ||
|
||
git_add(test_file) | ||
git_commit() | ||
|
||
test_file.write_text("ipsum\ndolor\nsit\namet", encoding="utf8") | ||
diff = get_diff() | ||
|
||
expected_diff = """--- a/foo.txt | ||
+++ b/foo.txt | ||
@@ -1,4 +1,3 @@ | ||
-Lorem | ||
ipsum | ||
dolor | ||
sit""" | ||
self.assertIn(expected_diff, diff) |
Oops, something went wrong.