Exemple #1
0
def test_get_repos_for_project(monkeypatch):
    """
    Test argument passing between get_repos_for_project() and get_repository()
    """
    project_name = 'foo'
    proxy_dict = {
        "http_proxy": "http://foo.bar:80",
        "https_proxy": "http://foo.bar:80"
    }
    git_cmd_path = "/foo/git"
    commands = {"git": git_cmd_path}
    timeout = 314159
    test_repo = "/" + project_name

    def mock_get_repos(*args):
        return [test_repo]

    def mock_get_repo_type(*args):
        return "Git"

    with tempfile.TemporaryDirectory() as source_root:
        # Note that it is actually not necessary to create real
        # Git repository for the test to work. This is due to
        # the way how Repository objects are created.
        with monkeypatch.context() as m:
            m.setattr("opengrok_tools.utils.mirror.get_repos", mock_get_repos)
            m.setattr("opengrok_tools.utils.mirror.get_repo_type",
                      mock_get_repo_type)

            repos = get_repos_for_project(project_name,
                                          None,
                                          source_root,
                                          commands=commands,
                                          proxy=proxy_dict,
                                          command_timeout=timeout)
            assert len(repos) == 1
            assert isinstance(repos[0], GitRepository)
            git_repo = repos[0]
            assert git_repo.timeout == timeout
            assert git_repo.command == git_cmd_path
            assert git_repo.env.items() >= proxy_dict.items()

            # Now ignore the repository
            repos = get_repos_for_project(project_name,
                                          None,
                                          source_root,
                                          ignored_repos=['.'])
            assert len(repos) == 0
def test_get_repos_for_project_first_repo(monkeypatch):
    """
    Test that get_repos_for_project() returns the list where the first item is the
    repository matching the project.
    """
    project_name = 'foo'
    test_repo = os.path.sep + project_name

    def mock_get_repos(*args, **kwargs):
        return [test_repo + os.path.sep + "x", test_repo, test_repo + os.path.sep + "y"]

    def mock_get_repo_type(*args, **kwargs):
        return "Git"

    with tempfile.TemporaryDirectory() as source_root:
        with monkeypatch.context() as m:
            m.setattr("opengrok_tools.utils.mirror.get_repos",
                      mock_get_repos)
            m.setattr("opengrok_tools.utils.mirror.get_repo_type",
                      mock_get_repo_type)

            repos = get_repos_for_project(project_name, None, source_root)
            assert len(repos) == 3
            assert isinstance(repos[0], GitRepository)
            assert repos[0].path == os.path.join(source_root, project_name)