|
| 1 | +from os import chdir |
| 2 | +from os import getcwd |
| 3 | +from pathlib import Path |
| 4 | +from tempfile import NamedTemporaryFile |
| 5 | +from tempfile import TemporaryDirectory |
| 6 | +from typing import List |
| 7 | +from unittest.mock import Mock |
| 8 | +from unittest.mock import patch |
| 9 | + |
| 10 | +from pytest import mark |
| 11 | +from pytest import raises |
| 12 | + |
| 13 | +from libkeyringctl import util |
| 14 | +from libkeyringctl.types import Fingerprint |
| 15 | + |
| 16 | + |
| 17 | +def test_cwd() -> None: |
| 18 | + with TemporaryDirectory() as tmp_dir_name: |
| 19 | + tmp_dir = Path(tmp_dir_name) |
| 20 | + test_dir = tmp_dir / "test" |
| 21 | + test_dir.mkdir() |
| 22 | + chdir(tmp_dir) |
| 23 | + with util.cwd(new_dir=test_dir): |
| 24 | + assert getcwd() == str(test_dir) |
| 25 | + assert getcwd() == str(tmp_dir) |
| 26 | + |
| 27 | + |
| 28 | +@mark.parametrize( |
| 29 | + "input_list, output_list", |
| 30 | + [ |
| 31 | + ([Path("/foo"), Path("/bar/foo"), Path("/foo/20")], [Path("/foo/20"), Path("/foo"), Path("/bar/foo")]), |
| 32 | + ], |
| 33 | +) |
| 34 | +def test_natural_sort_path(input_list: List[Path], output_list: List[Path]) -> None: |
| 35 | + assert util.natural_sort_path(_list=input_list) == output_list |
| 36 | + |
| 37 | + |
| 38 | +@mark.parametrize( |
| 39 | + "raise_on_cmd, exit_on_error", |
| 40 | + [ |
| 41 | + (False, True), |
| 42 | + (False, False), |
| 43 | + (True, True), |
| 44 | + (True, False), |
| 45 | + ], |
| 46 | +) |
| 47 | +@patch("libkeyringctl.util.exit") |
| 48 | +@patch("libkeyringctl.util.check_output") |
| 49 | +def test_system(check_output_mock: Mock, exit_mock: Mock, raise_on_cmd: bool, exit_on_error: bool) -> None: |
| 50 | + if raise_on_cmd: |
| 51 | + check_output_mock.side_effect = util.CalledProcessError(returncode=1, cmd="foo", output="output") |
| 52 | + |
| 53 | + with raises(util.CalledProcessError): |
| 54 | + util.system(["foo"], exit_on_error=exit_on_error) |
| 55 | + if exit_on_error: |
| 56 | + exit_mock.assert_called_once_with(1) |
| 57 | + |
| 58 | + else: |
| 59 | + check_output_mock.return_value = b"output" |
| 60 | + assert util.system(["foo"], exit_on_error=exit_on_error) == "output" |
| 61 | + |
| 62 | + |
| 63 | +def test_absolute_path() -> None: |
| 64 | + with TemporaryDirectory() as tmp_dir_name: |
| 65 | + tmp_dir = Path(tmp_dir_name) |
| 66 | + test_dir = tmp_dir / "test" |
| 67 | + test_dir.mkdir() |
| 68 | + chdir(tmp_dir) |
| 69 | + assert util.absolute_path(path="test") == test_dir |
| 70 | + |
| 71 | + |
| 72 | +def test_transform_fd_to_tmpfile() -> None: |
| 73 | + with TemporaryDirectory() as tmp_dir_name: |
| 74 | + tmp_dir = Path(tmp_dir_name) |
| 75 | + with NamedTemporaryFile(dir=tmp_dir) as tmp_file: |
| 76 | + tmp_file_fd = tmp_file.fileno() |
| 77 | + util.transform_fd_to_tmpfile( |
| 78 | + working_dir=tmp_dir, |
| 79 | + sources=[Path("/foo"), Path(f"/proc/self/fd/{tmp_file_fd}")], |
| 80 | + ) |
| 81 | + |
| 82 | + |
| 83 | +def test_get_cert_paths() -> None: |
| 84 | + with TemporaryDirectory() as tmp_dir_name: |
| 85 | + tmp_dir = Path(tmp_dir_name) |
| 86 | + |
| 87 | + cert_dir1 = tmp_dir / "cert1" |
| 88 | + cert_dir1.mkdir() |
| 89 | + cert1 = cert_dir1 / "cert1.asc" |
| 90 | + cert1.touch() |
| 91 | + cert_dir2 = tmp_dir / "cert2" |
| 92 | + cert_dir2.mkdir() |
| 93 | + cert2 = cert_dir2 / "cert2.asc" |
| 94 | + cert2.touch() |
| 95 | + |
| 96 | + assert util.get_cert_paths(paths=[tmp_dir]) == set([cert_dir1, cert_dir2]) |
| 97 | + |
| 98 | + |
| 99 | +def test_get_parent_cert_paths() -> None: |
| 100 | + with TemporaryDirectory() as tmp_dir_name: |
| 101 | + tmp_dir = Path(tmp_dir_name) |
| 102 | + |
| 103 | + keyring_dir = tmp_dir / "keyring" |
| 104 | + group_dir = keyring_dir / "parent" |
| 105 | + user_dir = group_dir / "parent" |
| 106 | + cert_dir1 = user_dir / "cert1" |
| 107 | + cert_dir1.mkdir(parents=True) |
| 108 | + cert1 = cert_dir1 / "cert1.asc" |
| 109 | + cert1.touch() |
| 110 | + cert_dir2 = cert_dir1 / "cert2" |
| 111 | + cert_dir2.mkdir(parents=True) |
| 112 | + cert2 = cert_dir2 / "cert2.asc" |
| 113 | + cert2.touch() |
| 114 | + |
| 115 | + assert util.get_parent_cert_paths(paths=[cert1, cert2]) == set([cert_dir1]) |
| 116 | + |
| 117 | + |
| 118 | +@mark.parametrize( |
| 119 | + "fingerprints, fingerprint, result", |
| 120 | + [ |
| 121 | + ( |
| 122 | + [Fingerprint("foo"), Fingerprint("bar")], |
| 123 | + Fingerprint("foo"), |
| 124 | + True, |
| 125 | + ), |
| 126 | + ( |
| 127 | + [Fingerprint("foo"), Fingerprint("bar")], |
| 128 | + Fingerprint("baz"), |
| 129 | + False, |
| 130 | + ), |
| 131 | + ], |
| 132 | +) |
| 133 | +def test_contains_fingerprint(fingerprints: List[Fingerprint], fingerprint: Fingerprint, result: bool) -> None: |
| 134 | + assert util.contains_fingerprint(fingerprints=fingerprints, fingerprint=fingerprint) is result |
0 commit comments