Example #1
0
def test_git_hook(src_dir):
    """Simple smoke level testing of git hooks"""

    # Ensure correct subprocess command is called
    with patch("subprocess.run", MagicMock()) as run_mock:
        hooks.git_hook()
        assert run_mock.called_once()
        assert run_mock.call_args[0][0] == [
            "git",
            "diff-index",
            "--cached",
            "--name-only",
            "--diff-filter=ACMRTUXB",
            "HEAD",
        ]

        hooks.git_hook(lazy=True)
        assert run_mock.called_once()
        assert run_mock.call_args[0][0] == [
            "git",
            "diff-index",
            "--name-only",
            "--diff-filter=ACMRTUXB",
            "HEAD",
        ]

    # Test with incorrectly sorted file returned from git
    with patch(
            "isort.hooks.get_lines",
            MagicMock(
                return_value=[os.path.join(src_dir, "main.py")])) as run_mock:

        class FakeProcessResponse(object):
            stdout = b"import b\nimport a"

        with patch("subprocess.run",
                   MagicMock(return_value=FakeProcessResponse())) as run_mock:
            with patch("isort.api", MagicMock(return_value=False)):
                hooks.git_hook(modify=True)

    # Test with skipped file returned from git
    with patch(
            "isort.hooks.get_lines",
            MagicMock(
                return_value=[os.path.join(src_dir, "main.py")])) as run_mock:

        class FakeProcessResponse(object):
            stdout = b"# isort: skip-file\nimport b\nimport a\n"

        with patch("subprocess.run",
                   MagicMock(return_value=FakeProcessResponse())) as run_mock:
            with patch("isort.api",
                       MagicMock(side_effect=exceptions.FileSkipped("", ""))):
                hooks.git_hook(modify=True)
Example #2
0
 def setup_class(self):
     self.instance = exceptions.FileSkipped("message", "file_path")
Example #3
0
def test_git_hook(src_dir):
    """Simple smoke level testing of git hooks"""

    # Ensure correct subprocess command is called
    with patch("subprocess.run", MagicMock()) as run_mock:
        hooks.git_hook()
        assert run_mock.called_once()
        assert run_mock.call_args[0][0] == [
            "git",
            "diff-index",
            "--cached",
            "--name-only",
            "--diff-filter=ACMRTUXB",
            "HEAD",
        ]

        hooks.git_hook(lazy=True)
        assert run_mock.called_once()
        assert run_mock.call_args[0][0] == [
            "git",
            "diff-index",
            "--name-only",
            "--diff-filter=ACMRTUXB",
            "HEAD",
        ]

    # Test that non python files aren't processed
    with patch(
        "isort.hooks.get_lines",
        MagicMock(return_value=["README.md", "setup.cfg", "LICDENSE", "mkdocs.yml", "test"]),
    ):
        with patch("subprocess.run", MagicMock()) as run_mock:
            hooks.git_hook(modify=True)
            run_mock.assert_not_called()

    mock_main_py = MagicMock(return_value=[os.path.join(src_dir, "main.py")])

    mock_imperfect = MagicMock()
    mock_imperfect.return_value.stdout = b"import b\nimport a"

    # Test with incorrectly sorted file returned from git
    with patch("isort.hooks.get_lines", mock_main_py):
        with patch("subprocess.run", mock_imperfect):
            with patch("isort.api.sort_file", MagicMock(return_value=False)) as api_mock:
                hooks.git_hook(modify=True)
                api_mock.assert_called_once()
                assert api_mock.call_args[0][0] == mock_main_py.return_value[0]

    # Test with sorted file returned from git and modify=False
    with patch("isort.hooks.get_lines", mock_main_py):
        with patch("subprocess.run", mock_imperfect):
            with patch("isort.api.sort_file", MagicMock(return_value=False)) as api_mock:
                hooks.git_hook(modify=False)
                api_mock.assert_not_called()

    # Test with skipped file returned from git
    with patch(
        "isort.hooks.get_lines", MagicMock(return_value=[os.path.join(src_dir, "main.py")])
    ) as run_mock:

        class FakeProcessResponse(object):
            stdout = b"# isort: skip-file\nimport b\nimport a\n"

        with patch("subprocess.run", MagicMock(return_value=FakeProcessResponse())) as run_mock:
            with patch("isort.api", MagicMock(side_effect=exceptions.FileSkipped("", ""))):
                hooks.git_hook(modify=True)