Example #1
0
    def test_skipping_undone_commits(self, setup: Any,
                                     caplog: pytest.LogCaptureFixture) -> None:
        """Test skipping already undone commits.

        Args:
            setup: the `tests.commands.command_test.CommandTest.setup` fixture.
            caplog: the built-in pytest fixture.
        """
        AddCommand().execute(["-b", EXAMPLE_MULTI_FILE_ENTRY_BIB])
        AddCommand().execute(["-b", get_resource("example_entry.bib")])
        UndoCommand().execute([])
        caplog.clear()

        UndoCommand().execute([])
        self._assert()
        assert "Storing undone commit" in caplog.record_tuples[4][2]
        assert "Skipping" in caplog.record_tuples[6][2]
Example #2
0
    def test_event_pre_undo_command(self, setup: Any) -> None:
        """Tests the PreUndoCommand event."""
        @Event.PreUndoCommand.subscribe
        def hook(largs: Namespace) -> None:
            print("Hello world!")

        assert Event.PreUndoCommand.validate()

        with contextlib.redirect_stdout(StringIO()) as out:
            with pytest.raises(SystemExit):
                UndoCommand().execute([])

            assert out.getvalue() == "Hello world!\n"
Example #3
0
    def test_command(self, setup: Any, expected_exit: bool,
                     caplog: pytest.LogCaptureFixture) -> None:
        """Test the command itself.

        Args:
            setup: the `tests.commands.command_test.CommandTest.setup` fixture.
            expected_exit: whether to expect an early exit.
            caplog: the built-in pytest fixture.
        """
        git = setup.get("git", False)

        if not git:
            UndoCommand().execute([])
            for (source, level, message) in caplog.record_tuples:
                if ("cobib.commands.undo", logging.ERROR) == (
                        source,
                        level,
                ) and "git-tracking" in message:
                    break
            else:
                pytest.fail("No Error logged from UndoCommand.")
        elif expected_exit:
            # Regression test related to #65
            with pytest.raises(SystemExit):
                UndoCommand().execute([])
            for (source, level, message) in caplog.record_tuples:
                if ("cobib.commands.undo", logging.WARNING) == (
                        source,
                        level,
                ) and "Could not find a commit to undo." in message:
                    break
            else:
                pytest.fail("No Error logged from UndoCommand.")
        else:
            AddCommand().execute(["-b", EXAMPLE_MULTI_FILE_ENTRY_BIB])
            UndoCommand().execute([])

            self._assert()
Example #4
0
    def test_event_post_undo_command(self, setup: Any) -> None:
        """Tests the PostUndoCommand event."""
        @Event.PostUndoCommand.subscribe
        def hook(root: Path, sha: str) -> None:
            print(root)

        assert Event.PostUndoCommand.validate()

        with contextlib.redirect_stdout(StringIO()) as out:
            AddCommand().execute(["-b", EXAMPLE_MULTI_FILE_ENTRY_BIB])
            UndoCommand().execute([])

            self._assert()

            assert out.getvalue() == f"{self.COBIB_TEST_DIR}\n"
Example #5
0
    def test_command(
        self, setup: Any, expected_exit: bool, caplog: pytest.LogCaptureFixture
    ) -> None:
        """Test the command itself.

        Args:
            setup: the `tests.commands.command_test.CommandTest.setup` fixture.
            expected_exit: whether to expect an early exit.
            caplog: the built-in pytest fixture.
        """
        git = setup.get("git", False)

        if not git:
            RedoCommand().execute([])
            for (source, level, message) in caplog.record_tuples:
                if ("cobib.commands.redo", logging.ERROR) == (
                    source,
                    level,
                ) and "git-tracking" in message:
                    break
            else:
                pytest.fail("No Error logged from RedoCommand.")
        elif expected_exit:
            # Regression test against #65
            AddCommand().execute(["-b", EXAMPLE_MULTI_FILE_ENTRY_BIB])
            with pytest.raises(SystemExit):
                RedoCommand().execute([])
            for (source, level, message) in caplog.record_tuples:
                if ("cobib.commands.redo", logging.WARNING) == (
                    source,
                    level,
                ) and "Could not find a commit to redo." in message:
                    break
            else:
                pytest.fail("No Error logged from UndoCommand.")
        else:
            AddCommand().execute(["-b", EXAMPLE_MULTI_FILE_ENTRY_BIB])
            UndoCommand().execute([])

            if Database().get("example_multi_file_entry", None) is not None:
                pytest.skip("UndoCommand failed. No point in attempting Redo.")

            RedoCommand().execute([])

            self._assert()
Example #6
0
    def test_warn_insufficient_setup(self, setup: Any,
                                     caplog: pytest.LogCaptureFixture) -> None:
        """Test warning in case of insufficient setup.

        Args:
            setup: the `tests.commands.command_test.CommandTest.setup` fixture.
            caplog: the built-in pytest fixture.
        """
        rmtree(self.COBIB_TEST_DIR_GIT)
        UndoCommand().execute([])
        for (source, level, message) in caplog.record_tuples:
            if ("cobib.commands.undo", logging.ERROR) == (
                    source,
                    level,
            ) and "configured, but not initialized" in message:
                break
        else:
            pytest.fail("No Error logged from UndoCommand.")
Example #7
0
    def test_cmdline(
        self, setup: Any, monkeypatch: pytest.MonkeyPatch, caplog: pytest.LogCaptureFixture
    ) -> None:
        """Test the command-line access of the command.

        Args:
            setup: the `tests.commands.command_test.CommandTest.setup` fixture.
            monkeypatch: the built-in pytest fixture.
            caplog: the built-in pytest fixture.
        """
        AddCommand().execute(["-b", EXAMPLE_MULTI_FILE_ENTRY_BIB])
        UndoCommand().execute([])

        if Database().get("example_multi_file_entry", None) is not None:
            pytest.skip("UndoCommand failed. No point in attempting Redo.")

        self.run_module(monkeypatch, "main", ["cobib", "redo"])

        self._assert()
Example #8
0
    def test_event_post_redo_command(self, setup: Any) -> None:
        """Tests the PostRedoCommand event."""

        @Event.PostRedoCommand.subscribe
        def hook(root: Path, sha: str) -> None:
            print(root)

        assert Event.PostRedoCommand.validate()

        with contextlib.redirect_stdout(StringIO()) as out:
            AddCommand().execute(["-b", EXAMPLE_MULTI_FILE_ENTRY_BIB])
            UndoCommand().execute([])

            if Database().get("example_multi_file_entry", None) is not None:
                pytest.skip("UndoCommand failed. No point in attempting Redo.")

            RedoCommand().execute([])

            self._assert()

            assert out.getvalue() == f"{self.COBIB_TEST_DIR}\n"
Example #9
0
    def test_tui(self, setup: Any) -> None:
        """Test the TUI access of the command.

        Args:
            setup: the `tests.commands.command_test.CommandTest.setup` fixture.
        """

        def assertion(screen, logs, **kwargs):  # type: ignore
            assert "example_multi_file_entry" in screen.display[1]

            expected_log = [
                ("cobib.commands.redo", 10, "Redo command triggered from TUI."),
                ("cobib.commands.redo", 10, "Starting Redo command."),
                ("cobib.commands.redo", 10, "Obtaining git log."),
            ]
            # we only assert the first three messages because the following ones will contain always
            # changing commit SHAs
            assert [log for log in logs if log[0] == "cobib.commands.redo"][0:3] == expected_log

        AddCommand().execute(["-b", EXAMPLE_MULTI_FILE_ENTRY_BIB])
        UndoCommand().execute([])
        self.run_tui("r", assertion, {})