Skip to content

Commit ced24af

Browse files
tulinkryVladimir Kotal
authored andcommitted
using factory for system binaries
1 parent 5c9db40 commit ced24af

File tree

3 files changed

+30
-43
lines changed

3 files changed

+30
-43
lines changed
Lines changed: 11 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,18 @@
11
import os
2-
import pytest
3-
4-
5-
@pytest.fixture(
6-
params=[
7-
pytest.param('/bin/true', marks=pytest.mark.skipif(not os.path.exists('/bin/true'), reason="requires /bin binaries")),
8-
pytest.param('/usr/bin/true', marks=pytest.mark.skipif(not os.path.exists('/usr/bin/true'), reason="requires /usr/bin binaries"))
9-
]
10-
)
11-
def true_binary(request):
12-
return request.param
132

3+
import pytest
144

15-
@pytest.fixture(
16-
params=[
17-
pytest.param('/bin/false', marks=pytest.mark.skipif(not os.path.exists('/bin/false'), reason="requires /bin binaries")),
18-
pytest.param('/usr/bin/false', marks=pytest.mark.skipif(not os.path.exists('/usr/bin/false'), reason="requires /usr/bin binaries"))
19-
]
20-
)
21-
def false_binary(request):
22-
return request.param
235

6+
def system_binary(name):
7+
def decorator(fn):
8+
return pytest.mark.parametrize(
9+
('{}_binary'.format(name)), [
10+
pytest.param('/bin/{}'.format(name), marks=pytest.mark.skipif(not os.path.exists('/bin/{}'.format(name)), reason="requires /bin binaries")),
11+
pytest.param('/usr/bin/{}'.format(name), marks=pytest.mark.skipif(not os.path.exists('/usr/bin/{}'.format(name)), reason="requires /usr/bin binaries")),
12+
])(fn)
2413

25-
@pytest.fixture(
26-
params=[
27-
pytest.param('/bin/touch', marks=pytest.mark.skipif(not os.path.exists('/bin/touch'), reason="requires /bin binaries")),
28-
pytest.param('/usr/bin/touch', marks=pytest.mark.skipif(not os.path.exists('/usr/bin/touch'), reason="requires /usr/bin binaries"))
29-
]
30-
)
31-
def touch_binary(request):
32-
return request.param
14+
return decorator
3315

3416

35-
@pytest.fixture(
36-
params=[
37-
pytest.param('/bin/echo', marks=pytest.mark.skipif(not os.path.exists('/bin/echo'), reason="requires /bin binaries")),
38-
pytest.param('/usr/bin/echo', marks=pytest.mark.skipif(not os.path.exists('/usr/bin/echo'), reason="requires /usr/bin binaries"))
39-
]
40-
)
41-
def echo_binary(request):
42-
return request.param
17+
def posix_only(fn):
18+
return pytest.mark.skipif(not os.name.startswith("posix"), reason="requires posix")(fn)

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

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,13 @@
2424
#
2525

2626
import os
27+
import platform
2728
import tempfile
2829
import time
29-
import platform
3030

3131
import pytest
3232

33+
from conftest import posix_only, system_binary
3334
from opengrok_tools.utils.command import Command
3435

3536

@@ -74,14 +75,14 @@ def test_execute_nonexistent():
7475
assert cmd.getstate() == Command.ERRORED
7576

7677

77-
@pytest.mark.skipif(not os.name.startswith("posix"), reason="requires posix")
78+
@posix_only
7879
def test_getoutput():
7980
cmd = Command(['/bin/ls', '/etc/passwd'])
8081
cmd.execute()
8182
assert cmd.getoutput() == ['/etc/passwd\n']
8283

8384

84-
@pytest.mark.skipif(not os.name.startswith("posix"), reason="requires posix")
85+
@posix_only
8586
def test_work_dir():
8687
os.chdir("/")
8788
orig_cwd = os.getcwd()
@@ -101,6 +102,8 @@ def test_env():
101102
assert "FOO=BAR\n" in cmd.getoutput()
102103

103104

105+
@system_binary('true')
106+
@system_binary('false')
104107
def test_retcode(true_binary, false_binary):
105108
cmd = Command([false_binary])
106109
cmd.execute()
@@ -145,7 +148,7 @@ def test_command_notimeout():
145148
assert cmd.getretcode() == 0
146149

147150

148-
@pytest.mark.skipif(not os.name.startswith("posix"), reason="requires posix")
151+
@posix_only
149152
def test_stderr():
150153
cmd = Command(["/bin/cat", "/foo/bar", "/etc/passwd"],
151154
redirect_stderr=False)
@@ -160,7 +163,7 @@ def test_stderr():
160163

161164

162165
# This test needs the "/bin/cat" command, therefore it is Unix only.
163-
@pytest.mark.skipif(not os.name.startswith("posix"), reason="requires posix")
166+
@posix_only
164167
def test_long_output():
165168
"""
166169
Test that output thread in the Command class captures all of the output.
@@ -188,7 +191,7 @@ def test_long_output():
188191
assert len("".join(cmd.getoutput())) == num_bytes
189192

190193

191-
@pytest.mark.skipif(not os.name.startswith("posix"), reason="requires posix")
194+
@posix_only
192195
def test_resource_limits():
193196
"""
194197
Simple smoke test for setting resource limits.

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
from mockito import verify, patch, spy2, mock, ANY, when
3434

3535
import opengrok_tools.mirror
36+
from conftest import posix_only, system_binary
3637
from opengrok_tools.scm import Repository
3738
from opengrok_tools.scm.git import GitRepository
3839
from opengrok_tools.scm.repository import RepositoryException
@@ -94,7 +95,7 @@ def test_invalid_project_config_hooknames():
9495
assert not check_project_configuration(config, hookdir=tmpdir)
9596

9697

97-
@pytest.mark.skipif(not os.name.startswith("posix"), reason="requires posix")
98+
@posix_only
9899
def test_invalid_project_config_nonexec_hook():
99100
with tempfile.TemporaryDirectory() as tmpdir:
100101
with open(os.path.join(tmpdir, "foo.sh"), 'w+') as tmpfile:
@@ -103,7 +104,7 @@ def test_invalid_project_config_nonexec_hook():
103104
assert not check_project_configuration(config, hookdir=tmpdir)
104105

105106

106-
@pytest.mark.skipif(not os.name.startswith("posix"), reason="requires posix")
107+
@posix_only
107108
def test_valid_project_config_hook():
108109
with tempfile.TemporaryDirectory() as tmpdir:
109110
with open(os.path.join(tmpdir, "foo.sh"), 'w+') as tmpfile:
@@ -359,6 +360,7 @@ def test_mirroring_custom_repository_command(expected_command, config):
359360
assert expected_command == Repository._repository_command(config, lambda: DEFAULT_COMMAND)
360361

361362

363+
@system_binary('touch')
362364
def test_mirroring_custom_incoming_invoke_command(touch_binary):
363365
checking_file = 'incoming.txt'
364366
with tempfile.TemporaryDirectory() as repository_root:
@@ -369,6 +371,7 @@ def test_mirroring_custom_incoming_invoke_command(touch_binary):
369371
assert checking_file in os.listdir(repository_root)
370372

371373

374+
@system_binary('echo')
372375
def test_mirroring_custom_incoming_changes(echo_binary):
373376
with tempfile.TemporaryDirectory() as repository_root:
374377
repository = GitRepository(mock(), repository_root, 'test-1', {
@@ -377,6 +380,7 @@ def test_mirroring_custom_incoming_changes(echo_binary):
377380
assert repository.incoming() is True
378381

379382

383+
@system_binary('true')
380384
def test_mirroring_custom_incoming_no_changes(true_binary):
381385
with tempfile.TemporaryDirectory() as repository_root:
382386
repository = GitRepository(mock(), repository_root, 'test-1', {
@@ -385,6 +389,7 @@ def test_mirroring_custom_incoming_no_changes(true_binary):
385389
assert repository.incoming() is False
386390

387391

392+
@system_binary('false')
388393
def test_mirroring_custom_incoming_error(false_binary):
389394
with pytest.raises(RepositoryException):
390395
with tempfile.TemporaryDirectory() as repository_root:
@@ -403,6 +408,7 @@ def test_mirroring_incoming_invoke_original_command():
403408
verify(repository).incoming_check()
404409

405410

411+
@system_binary('touch')
406412
def test_mirroring_custom_sync_invoke_command(touch_binary):
407413
checking_file = 'sync.txt'
408414
with tempfile.TemporaryDirectory() as repository_root:
@@ -413,6 +419,7 @@ def test_mirroring_custom_sync_invoke_command(touch_binary):
413419
assert checking_file in os.listdir(repository_root)
414420

415421

422+
@system_binary('true')
416423
def test_mirroring_custom_sync_success(true_binary):
417424
with tempfile.TemporaryDirectory() as repository_root:
418425
repository = GitRepository(mock(), repository_root, 'test-1', {
@@ -421,6 +428,7 @@ def test_mirroring_custom_sync_success(true_binary):
421428
assert 0 == repository.sync()
422429

423430

431+
@system_binary('false')
424432
def test_mirroring_custom_sync_error(false_binary):
425433
with tempfile.TemporaryDirectory() as repository_root:
426434
repository = GitRepository(mock(), repository_root, 'test-1', {

0 commit comments

Comments
 (0)