Skip to content

Commit

Permalink
fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
jmelahman committed Jun 1, 2022
1 parent 814233e commit 4ca4720
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 27 deletions.
4 changes: 3 additions & 1 deletion pysnapify/manager/snap.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ def __init__(
def get_installed_packages(self) -> list[str]:
if self._installed_packages != []:
return self._installed_packages
snap_list = subprocess.check_output([self._bin, "list"]).decode().rstrip().split("\n")
snap_list = (
subprocess.check_output([self._bin, "list"]).decode().rstrip().split("\n")
)
snap_list.pop(0) # Remove header
self._installed_packages = [package.split(" ")[0] for package in snap_list]
return self._installed_packages
Expand Down
54 changes: 37 additions & 17 deletions tests/pacman_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def setUp(self, mock_get_executable) -> None:
_SUDO_BIN,
]
mock_get_executable.side_effect = self._default_get_executable
self.pacman = pacman.Pacman(noninteractive = False, ignored_packages = [])
self.pacman = pacman.Pacman(noninteractive=False, ignored_packages=[])

@mock.patch("subprocess.check_output", return_value=_FAKE_PACKAGES)
def test_get_installed(self, mock_subprocess: mock.MagicMock) -> None:
Expand All @@ -40,7 +40,6 @@ def test_get_installed(self, mock_subprocess: mock.MagicMock) -> None:
def test_filter_removeable(self) -> None:
self.pacman.filter_removeable(["foo", "bar"])


@mock.patch("subprocess.check_output", return_value=_FAKE_PACKAGES)
def test_has_installed(self, mock_subprocess: mock.MagicMock) -> None:
subtests = [
Expand All @@ -66,48 +65,69 @@ def test_not_has_available(self, mock_subprocess: mock.MagicMock) -> None:
@mock.patch("snapify.pysnapify.manager.utils.get_executable")
def test_never_has_available(self, mock_get_executable: mock.MagicMock) -> None:
mock_get_executable.side_effect = self._default_get_executable
self.pacman = pacman.Pacman(noninteractive = False, ignored_packages = [_NEVER_AVAILABLE])
self.pacman = pacman.Pacman(
noninteractive=False, ignored_packages=[_NEVER_AVAILABLE]
)
package_available = self.pacman.has_available(_NEVER_AVAILABLE)
self.assertFalse(package_available)

@mock.patch("subprocess.check_call")
def test_install(self, mock_subprocess: mock.MagicMock) -> None:
self.pacman.install(["fake-snapify"])
mock_subprocess.assert_called_once_with([_SUDO_BIN, _PACMAN_BIN, "-S", "fake-snapify"])
mock_subprocess.assert_called_once_with(
[_SUDO_BIN, _PACMAN_BIN, "-S", "fake-snapify"]
)

@mock.patch("snapify.pysnapify.manager.utils.get_executable")
@mock.patch("subprocess.check_call")
def test_install_noninteractive(self, mock_subprocess: mock.MagicMock, mock_get_executable: mock.MagicMock) -> None:
def test_install_noninteractive(
self, mock_subprocess: mock.MagicMock, mock_get_executable: mock.MagicMock
) -> None:
mock_get_executable.side_effect = self._default_get_executable
self.pacman = pacman.Pacman(noninteractive = True, ignored_packages = [])
self.pacman = pacman.Pacman(noninteractive=True, ignored_packages=[])
self.pacman.install(["fake-snapify"])
mock_subprocess.assert_called_once_with([_SUDO_BIN, _PACMAN_BIN, "-S", "--noconfirm", "fake-snapify"])
mock_subprocess.assert_called_once_with(
[_SUDO_BIN, _PACMAN_BIN, "-S", "--noconfirm", "fake-snapify"]
)

@mock.patch("subprocess.check_call")
def test_remove(self, mock_subprocess: mock.MagicMock) -> None:
self.pacman.remove(["fake-snapify"])
mock_subprocess.assert_called_once_with([_SUDO_BIN, _PACMAN_BIN, "-Rs", "fake-snapify"])
mock_subprocess.assert_called_once_with(
[_SUDO_BIN, _PACMAN_BIN, "-Rs", "fake-snapify"]
)

@mock.patch("subprocess.check_call")
def test_purge(self, mock_subprocess: mock.MagicMock) -> None:
self.pacman.remove(["fake-snapify"], purge = True)
mock_subprocess.assert_called_once_with([_SUDO_BIN, _PACMAN_BIN, "-Rsn", "fake-snapify"])
self.pacman.remove(["fake-snapify"], purge=True)
mock_subprocess.assert_called_once_with(
[_SUDO_BIN, _PACMAN_BIN, "-Rsn", "fake-snapify"]
)

@mock.patch("snapify.pysnapify.manager.utils.get_executable")
@mock.patch("subprocess.check_call")
def test_remove_noninteractive(self, mock_subprocess: mock.MagicMock, mock_get_executable: mock.MagicMock) -> None:
def test_remove_noninteractive(
self, mock_subprocess: mock.MagicMock, mock_get_executable: mock.MagicMock
) -> None:
mock_get_executable.side_effect = self._default_get_executable
self.pacman = pacman.Pacman(noninteractive = True, ignored_packages = [])
self.pacman = pacman.Pacman(noninteractive=True, ignored_packages=[])
self.pacman.remove(["fake-snapify"])
mock_subprocess.assert_called_once_with([_SUDO_BIN, _PACMAN_BIN, "-Rs", "--noconfirm", "fake-snapify"])
mock_subprocess.assert_called_once_with(
[_SUDO_BIN, _PACMAN_BIN, "-Rs", "--noconfirm", "fake-snapify"]
)

@mock.patch("snapify.pysnapify.manager.utils.get_executable")
@mock.patch("subprocess.check_call")
def test_purge_noninteractive(self, mock_subprocess: mock.MagicMock, mock_get_executable: mock.MagicMock) -> None:
def test_purge_noninteractive(
self, mock_subprocess: mock.MagicMock, mock_get_executable: mock.MagicMock
) -> None:
mock_get_executable.side_effect = self._default_get_executable
self.pacman = pacman.Pacman(noninteractive = True, ignored_packages = [])
self.pacman.remove(["fake-snapify"], purge = True)
mock_subprocess.assert_called_once_with([_SUDO_BIN, _PACMAN_BIN, "-Rsn", "--noconfirm", "fake-snapify"])
self.pacman = pacman.Pacman(noninteractive=True, ignored_packages=[])
self.pacman.remove(["fake-snapify"], purge=True)
mock_subprocess.assert_called_once_with(
[_SUDO_BIN, _PACMAN_BIN, "-Rsn", "--noconfirm", "fake-snapify"]
)


if __name__ == "__main__":
unittest.main()
17 changes: 9 additions & 8 deletions tests/snap_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,15 @@
"""



class SnapTest(unittest.TestCase):
@mock.patch("subprocess.check_output", return_value=_SNAP_LIST)
@mock.patch("snapify.pysnapify.manager.snap.Snapd._names_exists", return_value=False)
def setUp(self, mock_names_exists: mock.MagicMock, mock_subprocess: mock.MagicMock) -> None:
self.snap = snap.Snapd(noninteractive = False, ignored_packages = [])
@mock.patch(
"snapify.pysnapify.manager.snap.Snapd._names_exists", return_value=False
)
def setUp(
self, mock_names_exists: mock.MagicMock, mock_subprocess: mock.MagicMock
) -> None:
self.snap = snap.Snapd(noninteractive=False, ignored_packages=[])

def test_get_installed(self) -> None:
expected_packages = ["bare", "core", "core18", "core20"]
Expand All @@ -27,11 +30,9 @@ def test_get_installed(self) -> None:
@mock.patch("snapify.pysnapify.manager.snap.Snapd._names_exists", return_value=True)
def test_get_available(self, mock_names_exists: mock.MagicMock) -> None:
available_packages = self.snap.get_available_packages()
with mock.patch(
"builtins.open", mock.mock_open(read_data=names.NAMES)
):
with mock.patch("builtins.open", mock.mock_open(read_data=names.NAMES)):
pass
#self.assertIn("fake-snapify", available_packages)
# self.assertIn("fake-snapify", available_packages)


if __name__ == "__main__":
Expand Down
2 changes: 1 addition & 1 deletion tests/snapifier_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def test_snapifier(

for mock_open, expected_distro in subtests:
with self.subTest(expected_distro=expected_distro.value), mock.patch(
"builtins.open", side_effect = mock_open
"builtins.open", side_effect=mock_open
):
snapify = snapifier.Snapifier(noninteractive=False)
self.assertEqual(snapify._distro, expected_distro)
Expand Down

0 comments on commit 4ca4720

Please sign in to comment.