Пример #1
0
async def test_git_clone_with_auth_and_cache_credentials():
    with patch("jupyterlab_git.git.Git.ensure_git_credential_cache_daemon"
               ) as mock_ensure_daemon:
        mock_ensure_daemon.return_value = 0
        with patch("jupyterlab_git.git.execute") as mock_execute:
            # Given
            default_config = JupyterLabGit()
            default_config.credential_helper = "cache"
            credential_helper = default_config.credential_helper
            test_path = "test_curr_path"
            mock_execute.side_effect = [
                maybe_future((0, "", "")),
                maybe_future((0, "", "")),
                maybe_future((0, "", "")),
            ]
            # When
            auth = {
                "username": "******",
                "password": "******",
                "cache_credentials": True,
            }
            actual_response = await Git(config=default_config).clone(
                path=test_path,
                repo_url="ghjkhjkl",
                auth=auth,
            )

            # Then
            assert mock_execute.call_count == 3
            mock_execute.assert_has_calls([
                call(["git", "config", "--list"], cwd=test_path),
                call(
                    [
                        "git",
                        "config",
                        "--add",
                        "credential.helper",
                        credential_helper,
                    ],
                    cwd=test_path,
                ),
                call(
                    ["git", "clone", "ghjkhjkl", "-q"],
                    username="******",
                    password="******",
                    cwd=test_path,
                    env={
                        **os.environ, "GIT_TERMINAL_PROMPT": "1"
                    },
                ),
            ])
            mock_ensure_daemon.assert_called_once_with(cwd=test_path, env=None)
            assert {"code": 0, "message": ""} == actual_response
Пример #2
0
async def test_init_and_post_init_fail_to_run():
    with patch("jupyterlab_git.git.execute") as mock_execute:
        # Given
        mock_execute.side_effect = [
            maybe_future((0, "", "")),
            Exception("Not a command!"),
        ]

        # When
        actual_response = await Git(
            FakeContentManager(Path("/bin")),
            JupyterLabGit(actions={"post_init": ["not_there arg"]}),
        ).init("test_curr_path")

        mock_execute.assert_called_with(
            ["not_there", "arg"], cwd=str(Path("/bin") / "test_curr_path")
        )

        assert {
            "code": 1,
            "message": "",
            "command": "git init",
            "actions": [
                {
                    "stderr": "Exception: Not a command!",
                    "stdout": None,
                    "code": 1,
                    "cmd": "not_there arg",
                }
            ],
        } == actual_response
Пример #3
0
async def test_init_and_post_init_fail():
    with patch("jupyterlab_git.git.execute") as mock_execute:
        # Given
        mock_execute.side_effect = [
            maybe_future((0, "", "")),
            maybe_future((1, "", "not_there: command not found")),
        ]

        # When
        actual_response = await Git(
            JupyterLabGit(actions={"post_init": ["not_there arg"]}), ).init(
                "test_curr_path")

        mock_execute.assert_called_with(["not_there", "arg"],
                                        cwd="test_curr_path")

        assert {
            "code":
            1,
            "message":
            "",
            "command":
            "git init",
            "actions": [{
                "stderr": "not_there: command not found",
                "stdout": "",
                "code": 1,
                "cmd": "not_there arg",
            }],
        } == actual_response
Пример #4
0
async def test_init_and_post_init():
    with patch("jupyterlab_git.git.execute") as mock_execute:
        # Given
        mock_execute.side_effect = [
            maybe_future((0, "", "")),
            maybe_future((0, "hello", "")),
        ]

        # When
        actual_response = await Git(
            JupyterLabGit(actions={"post_init": ['echo "hello"']}), ).init(
                "test_curr_path")

        mock_execute.assert_called_with(["echo", "hello"],
                                        cwd="test_curr_path")

        assert {
            "code":
            0,
            "actions": [{
                "cmd": 'echo "hello"',
                "code": 0,
                "stderr": "",
                "stdout": "hello"
            }],
        } == actual_response
Пример #5
0
async def test_git_fetch_with_auth_and_cache_credentials_and_existing_credential_helper():
    with patch("jupyterlab_git.git.execute") as mock_execute:
        # Given
        default_config = JupyterLabGit()
        test_path = "test_path"
        mock_execute.side_effect = [
            maybe_future((0, "credential.helper=something", "")),
            maybe_future((0, "", "")),
        ]
        # When
        actual_response = await Git(config=default_config).fetch(
            path="test_path",
            auth={
                "username": "******",
                "password": "******",
                "cache_credentials": True,
            },
        )

        # Then
        assert mock_execute.call_count == 2
        mock_execute.assert_has_calls(
            [
                call(["git", "config", "--list"], cwd=test_path),
                call(
                    ["git", "fetch", "--all", "--prune"],
                    username="******",
                    password="******",
                    cwd=test_path,
                    env={**os.environ, "GIT_TERMINAL_PROMPT": "1"},
                ),
            ]
        )
        assert {"code": 0} == actual_response
Пример #6
0
async def test_git_clone_with_auth_and_cache_credentials_and_existing_credential_helper(
):
    with patch("jupyterlab_git.git.execute") as mock_execute:
        # Given
        default_config = JupyterLabGit()
        test_path = str(Path("/bin") / "test_curr_path")
        mock_execute.side_effect = [
            maybe_future((0, "credential.helper=something", "")),
            maybe_future((0, "", "")),
        ]
        # When
        auth = {
            "username": "******",
            "password": "******",
            "cache_credentials": True
        }
        actual_response = await Git(config=default_config
                                    ).clone(path=test_path,
                                            repo_url="ghjkhjkl",
                                            auth=auth)

        # Then
        assert mock_execute.call_count == 2
        mock_execute.assert_has_calls([
            call(["git", "config", "--list"], cwd=test_path),
            call(
                ["git", "clone", "ghjkhjkl", "-q"],
                username="******",
                password="******",
                cwd=test_path,
                env={
                    **os.environ, "GIT_TERMINAL_PROMPT": "1"
                },
            ),
        ])
        assert {"code": 0, "message": ""} == actual_response