Skip to content

Commit 0cbddc2

Browse files
authored
moving test_incoming_retval to separate file (#3344)
1 parent 61034ad commit 0cbddc2

File tree

2 files changed

+103
-68
lines changed

2 files changed

+103
-68
lines changed

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

Lines changed: 0 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,17 @@
2424
# Portions Copyright (c) 2020, Krystof Tulinger <[email protected]>
2525
#
2626

27-
import multiprocessing
2827
import os
2928
import stat
30-
import sys
3129
import tempfile
3230

3331
import pytest
3432
import requests
35-
from git import Repo
3633
from mockito import verify, patch, spy2, mock, ANY, when
3734

3835
import opengrok_tools.mirror
3936
from opengrok_tools.scm import Repository
4037
from opengrok_tools.scm.git import GitRepository
41-
from opengrok_tools.scm.repofactory import get_repository
4238
from opengrok_tools.scm.repository import RepositoryException
4339
from opengrok_tools.utils.command import Command
4440
from opengrok_tools.utils.exitvals import (
@@ -129,70 +125,6 @@ def test_valid_config():
129125
})
130126

131127

132-
@pytest.mark.skipif(not os.name.startswith("posix"), reason="requires posix")
133-
def test_incoming_retval(monkeypatch):
134-
"""
135-
Test that the special CONTINUE_EXITVAL value bubbles all the way up to
136-
the mirror.py return value.
137-
"""
138-
139-
class MockResponse:
140-
141-
# mock json() method always returns a specific testing dictionary
142-
@staticmethod
143-
def json():
144-
return "true"
145-
146-
@staticmethod
147-
def raise_for_status():
148-
pass
149-
150-
with tempfile.TemporaryDirectory() as source_root:
151-
repo_name = "parent_repo"
152-
repo_path = os.path.join(source_root, repo_name)
153-
cloned_repo_name = "cloned_repo"
154-
cloned_repo_path = os.path.join(source_root, cloned_repo_name)
155-
project_name = "foo" # does not matter for this test
156-
157-
os.mkdir(repo_path)
158-
159-
def mock_get_repos(*args, **kwargs):
160-
return [get_repository(cloned_repo_path,
161-
"git", project_name)]
162-
163-
def mock_get(*args, **kwargs):
164-
return MockResponse()
165-
166-
# Clone a Git repository so that it can pull.
167-
repo = Repo.init(repo_path)
168-
with repo.config_writer() as git_config:
169-
git_config.set_value('user', 'email', '[email protected]')
170-
git_config.set_value('user', 'name', 'John Doe')
171-
172-
new_file_path = os.path.join(repo_path, 'foo')
173-
with open(new_file_path, 'w'):
174-
pass
175-
assert os.path.isfile(new_file_path)
176-
index = repo.index
177-
index.add([new_file_path])
178-
index.commit("add file")
179-
repo.clone(cloned_repo_path)
180-
181-
with monkeypatch.context() as m:
182-
multiprocessing.set_start_method('fork')
183-
m.setattr(sys, 'argv', ['prog', "-I", project_name])
184-
185-
# With mocking done via pytest it is necessary to patch
186-
# the call-site rather than the absolute object path.
187-
m.setattr("opengrok_tools.mirror.get_config_value",
188-
lambda x, y, z: source_root)
189-
m.setattr("opengrok_tools.utils.mirror.get_repos_for_project",
190-
mock_get_repos)
191-
m.setattr("opengrok_tools.utils.mirror.do_api_call", mock_get)
192-
193-
assert opengrok_tools.mirror.main() == CONTINUE_EXITVAL
194-
195-
196128
def test_empty_project_config():
197129
assert check_project_configuration({'foo': None})
198130

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
#!/usr/bin/env python3
2+
3+
#
4+
# CDDL HEADER START
5+
#
6+
# The contents of this file are subject to the terms of the
7+
# Common Development and Distribution License (the "License").
8+
# You may not use this file except in compliance with the License.
9+
#
10+
# See LICENSE.txt included in this distribution for the specific
11+
# language governing permissions and limitations under the License.
12+
#
13+
# When distributing Covered Code, include this CDDL HEADER in each
14+
# file and include the License file at LICENSE.txt.
15+
# If applicable, add the following below this CDDL HEADER, with the
16+
# fields enclosed by brackets "[]" replaced with your own identifying
17+
# information: Portions Copyright [yyyy] [name of copyright owner]
18+
#
19+
# CDDL HEADER END
20+
#
21+
22+
#
23+
# Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
24+
# Portions Copyright (c) 2020, Krystof Tulinger <[email protected]>
25+
#
26+
27+
import multiprocessing
28+
import os
29+
import sys
30+
import tempfile
31+
32+
import pytest
33+
from git import Repo
34+
35+
import opengrok_tools.mirror
36+
from opengrok_tools.scm import get_repository
37+
from opengrok_tools.utils.exitvals import CONTINUE_EXITVAL
38+
39+
40+
@pytest.mark.skipif(not os.name.startswith("posix"), reason="requires posix")
41+
def test_incoming_retval(monkeypatch):
42+
"""
43+
Test that the special CONTINUE_EXITVAL value bubbles all the way up to
44+
the mirror.py return value.
45+
"""
46+
47+
# The default has changed for python 3.8 (see https://github.com/oracle/opengrok/issues/3296).
48+
# Because of the mocking we need to use the "fork" type to propagate all mocks to the
49+
# processes spawned by mirror command
50+
multiprocessing.set_start_method('fork')
51+
52+
class MockResponse:
53+
54+
# mock json() method always returns a specific testing dictionary
55+
@staticmethod
56+
def json():
57+
return "true"
58+
59+
@staticmethod
60+
def raise_for_status():
61+
pass
62+
63+
with tempfile.TemporaryDirectory() as source_root:
64+
repo_name = "parent_repo"
65+
repo_path = os.path.join(source_root, repo_name)
66+
cloned_repo_name = "cloned_repo"
67+
cloned_repo_path = os.path.join(source_root, cloned_repo_name)
68+
project_name = "foo" # does not matter for this test
69+
70+
os.mkdir(repo_path)
71+
72+
def mock_get_repos(*args, **kwargs):
73+
return [get_repository(cloned_repo_path,
74+
"git", project_name)]
75+
76+
def mock_get(*args, **kwargs):
77+
return MockResponse()
78+
79+
# Clone a Git repository so that it can pull.
80+
repo = Repo.init(repo_path)
81+
with repo.config_writer() as git_config:
82+
git_config.set_value('user', 'email', '[email protected]')
83+
git_config.set_value('user', 'name', 'John Doe')
84+
85+
new_file_path = os.path.join(repo_path, 'foo')
86+
with open(new_file_path, 'w'):
87+
pass
88+
assert os.path.isfile(new_file_path)
89+
index = repo.index
90+
index.add([new_file_path])
91+
index.commit("add file")
92+
repo.clone(cloned_repo_path)
93+
94+
with monkeypatch.context() as m:
95+
m.setattr(sys, 'argv', ['prog', "-I", project_name])
96+
97+
# With mocking done via pytest it is necessary to patch
98+
# the call-site rather than the absolute object path.
99+
m.setattr("opengrok_tools.mirror.get_config_value", lambda x, y, z: source_root)
100+
m.setattr("opengrok_tools.utils.mirror.get_repos_for_project", mock_get_repos)
101+
m.setattr("opengrok_tools.utils.mirror.do_api_call", mock_get)
102+
103+
assert opengrok_tools.mirror.main() == CONTINUE_EXITVAL

0 commit comments

Comments
 (0)