Skip to content

Commit 13dcf26

Browse files
author
Vladimir Kotal
committed
make get_repos_for_project(), get_repository use kwargs
add tests
1 parent a555278 commit 13dcf26

File tree

4 files changed

+75
-25
lines changed

4 files changed

+75
-25
lines changed

opengrok-tools/src/main/python/opengrok_tools/scm/repofactory.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
#
1919

2020
#
21-
# Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
21+
# Copyright (c) 2018, 2020, Oracle and/or its affiliates. All rights reserved.
2222
#
2323

2424
import logging
@@ -33,14 +33,14 @@
3333
logger = logging.getLogger(__name__)
3434

3535

36-
def get_repository(path, repo_type, project, commands, env, hooks,
37-
timeout):
36+
def get_repository(path, repo_type, project,
37+
commands=None, env=None, hooks=None, timeout=None):
3838
"""
3939
:param path: full path
4040
:param repo_type: repository type
4141
:param project: project name
42-
:param commands: commands list
43-
:param env: environment varibables dictionary
42+
:param commands: commands dictionary with paths to SCM utilities
43+
:param env: environment variables dictionary
4444
:param hooks: hook dictionary
4545
:param timeout: timeout in seconds
4646
:return: a Repository derived object according to the type specified
@@ -62,7 +62,7 @@ def get_repository(path, repo_type, project, commands, env, hooks,
6262
return TeamwareRepository(logger, path, project,
6363
commands.get("teamware"),
6464
env, hooks, timeout)
65-
elif repo_lower.lower() == "cvs":
65+
elif repo_lower == "cvs":
6666
return CVSRepository(logger, path, project,
6767
commands.get("cvs"),
6868
env, hooks, timeout)

opengrok-tools/src/main/python/opengrok_tools/utils/mirror.py

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,18 @@
6161
DISABLED_CMD_PROPERTY = 'disabled_command'
6262

6363

64-
def get_repos_for_project(project_name, ignored_repos, uri, source_root,
65-
**kwargs):
64+
def get_repos_for_project(project_name, uri, source_root,
65+
ignored_repos=None,
66+
commands=None, proxy=None, command_timeout=None):
6667
"""
6768
:param project_name: project name
68-
:param ignored_repos: list of ignored repositories
6969
:param uri: web application URI
7070
:param source_root source root
71-
:param kwargs: argument dictionary
71+
:param ignored_repos: list of ignored repositories
72+
:param commands: dictionary of commands - paths to SCM programs
73+
:param proxy: dictionary of proxy servers - to be used as environment
74+
variables
75+
:param command_timeout: command timeout value in seconds
7276
:return: list of Repository objects
7377
"""
7478

@@ -78,10 +82,11 @@ def get_repos_for_project(project_name, ignored_repos, uri, source_root,
7882
for repo_path in get_repos(logger, project_name, uri):
7983
logger.debug("Repository path = {}".format(repo_path))
8084

81-
r_path = os.path.relpath(repo_path, '/' + project_name)
82-
if any(map(lambda repo: fnmatch.fnmatch(r_path, repo), ignored_repos)):
83-
logger.info("repository {} ignored".format(repo_path))
84-
continue
85+
if ignored_repos:
86+
r_path = os.path.relpath(repo_path, '/' + project_name)
87+
if any(map(lambda repo: fnmatch.fnmatch(r_path, repo), ignored_repos)):
88+
logger.info("repository {} ignored".format(repo_path))
89+
continue
8590

8691
repo_type = get_repo_type(logger, repo_path, uri)
8792
if not repo_type:
@@ -92,14 +97,20 @@ def get_repos_for_project(project_name, ignored_repos, uri, source_root,
9297

9398
repo = None
9499
try:
95-
# Not joining the path since the form of repo_path is absolute.
96-
repo = get_repository(source_root + repo_path,
100+
# The OpenGrok convention is that the form of repo_path is absolute
101+
# so joining would the paths would actually spoil things. Hence, be
102+
# careful.
103+
if repo_path.startswith(os.path.sep):
104+
path = source_root + repo_path
105+
else:
106+
path = os.path.join(source_root, repo_path)
107+
108+
repo = get_repository(path,
97109
repo_type,
98110
project_name,
99-
kwargs[COMMANDS_PROPERTY],
100-
kwargs[PROXY_PROPERTY],
101-
None,
102-
kwargs[CMD_TIMEOUT_PROPERTY])
111+
env=proxy,
112+
timeout=command_timeout,
113+
commands=commands)
103114
except (RepositoryException, OSError) as e:
104115
logger.error("Cannot get repository for {}: {}".
105116
format(repo_path, e))
@@ -361,9 +372,9 @@ def mirror_project(config, project_name, check_changes, uri,
361372
# something is not right, avoiding any needless pre-hook run.
362373
#
363374
repos = get_repos_for_project(project_name,
364-
ignored_repos,
365375
uri,
366376
source_root,
377+
ignored_repos=ignored_repos,
367378
commands=config.
368379
get(COMMANDS_PROPERTY),
369380
proxy=proxy,

opengrok-tools/src/test/python/test_mirror.py

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535

3636
from opengrok_tools.scm.repofactory import get_repository
3737
from opengrok_tools.utils.mirror import check_project_configuration, \
38-
check_configuration, mirror_project, run_command, \
38+
check_configuration, mirror_project, run_command, get_repos_for_project, \
3939
HOOKS_PROPERTY, PROXY_PROPERTY, IGNORED_REPOS_PROPERTY, \
4040
PROJECTS_PROPERTY, DISABLED_CMD_PROPERTY, DISABLED_PROPERTY, \
4141
CMD_TIMEOUT_PROPERTY, HOOK_TIMEOUT_PROPERTY
@@ -152,8 +152,7 @@ def raise_for_status():
152152

153153
def mock_get_repos(*args, **kwargs):
154154
return [get_repository(cloned_repo_path,
155-
"git", project_name,
156-
None, None, None, None)]
155+
"git", project_name)]
157156

158157
def mock_get(*args, **kwargs):
159158
return MockResponse()
@@ -280,3 +279,43 @@ def test_mirror_project(config):
280279

281280
test_mirror_project(global_config_1)
282281
test_mirror_project(global_config_2)
282+
283+
284+
def test_get_repos_for_project(monkeypatch):
285+
"""
286+
Test argument passing between get_repos_for_project() and get_repository()
287+
"""
288+
project_name = 'foo'
289+
proxy_dict = {}
290+
commands = {}
291+
timeout = 314159
292+
test_repo = "/" + project_name
293+
294+
def mock_get_repos(*args):
295+
return [test_repo]
296+
297+
def mock_get_repo_type(*args):
298+
return "Git"
299+
300+
with tempfile.TemporaryDirectory() as source_root:
301+
# Note that it is actually not necessary to create real
302+
# Git repository for the test to work. This is due to
303+
# the way how Repository objects are created.
304+
with monkeypatch.context() as m:
305+
m.setattr("opengrok_tools.utils.mirror.get_repos",
306+
mock_get_repos)
307+
m.setattr("opengrok_tools.utils.mirror.get_repo_type",
308+
mock_get_repo_type)
309+
310+
repos = get_repos_for_project(project_name, None, source_root,
311+
commands=commands,
312+
proxy=proxy_dict,
313+
command_timeout=timeout)
314+
print(repos)
315+
assert len(repos) == 1
316+
317+
# Now ignore the repository
318+
repos = get_repos_for_project(project_name, None, source_root,
319+
ignored_repos=['.'])
320+
print(repos)
321+
assert len(repos) == 0

opengrok-tools/src/test/python/test_repofactory.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def test_repofactory_timeout():
4141
project_name = "foo" # does not matter for this test
4242
repo = get_repository(repo_path,
4343
"git", project_name,
44-
None, None, None, timeout)
44+
timeout=timeout)
4545
assert repo is not None
4646
assert isinstance(repo, GitRepository)
4747
assert repo.timeout == timeout

0 commit comments

Comments
 (0)