forked from degauden/Elements
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCppProgramTest.py
More file actions
114 lines (95 loc) · 5.18 KB
/
Copy pathCppProgramTest.py
File metadata and controls
114 lines (95 loc) · 5.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#
# Copyright (C) 2012-2020 Euclid Science Ground Segment
#
# This library is free software; you can redistribute it and/or modify it under
# the terms of the GNU Lesser General Public License as published by the Free
# Software Foundation; either version 3.0 of the License, or (at your option)
# any later version.
#
# This library is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
# details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this library; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
#
"""
:date: May 29, 2018
:author: Hubert Degaudenzi
"""
__updated__ = "2022-02-22"
from subprocess import Popen, PIPE
import shlex
import unittest
import pytest
from ElementsKernel.Auxiliary import getAuxiliaryPath
class TestCppProgramTest(object):
@pytest.mark.parametrize(
"default, default_conf, cmd_line_conf, cmd_line_arg, arg_name, expected",
[
("default", None, None, None, "int-option-with-default-no-default-in-conf", 444),
("default", "default_conf", None, None, "int-option-with-default-and-default-in-conf", 333),
("default", "default_conf", "cmd_line_conf", None, "int-option-with-default-and-default-in-conf", 777),
("default", "default_conf", "cmd_line_conf", "regular", "int-option-with-default-and-default-in-conf", 666),
("default", "default_conf", "cmd_line_conf", "with_space", "int-option-with-default-and-default-in-conf", 666),
("default", "default_conf", None, "regular", "int-option-with-default-and-default-in-conf", 666),
("default", "default_conf", None, "with_space", "int-option-with-default-and-default-in-conf", 666),
("default", None, "cmd_line_conf", None, "int-option-with-default-no-default-in-conf", 888),
("default", None, "cmd_line_conf", "regular", "int-option-with-default-no-default-in-conf", 666),
("default", None, "cmd_line_conf", "with_space", "int-option-with-default-no-default-in-conf", 666),
("default", None, None, "regular", "int-option-with-default-no-default-in-conf", 666),
("default", None, None, "with_space", "int-option-with-default-no-default-in-conf", 666),
(None, "default_conf", None, None, "int-option", 3),
(None, "default_conf", "cmd_line_conf", None, "int-option", 4),
(None, "default_conf", "cmd_line_conf", "regular", "int-option", 666),
(None, "default_conf", "cmd_line_conf", "with_space", "int-option", 666),
(None, "default_conf", None, "regular", "int-option", 666),
(None, "default_conf", None, "with_space", "int-option", 666),
(None, None, "cmd_line_conf", None, "int-option-no-default-not-defined-in-conf", 555),
(None, None, "cmd_line_conf", "regular", "int-option-no-default-not-defined-in-conf", 666),
(None, None, "cmd_line_conf", "with_space", "int-option-no-default-not-defined-in-conf", 666),
(None, None, None, "regular", "int-option-with-no-defaults-anywhere", 666),
(None, None, None, "with_space", "int-option-with-no-defaults-anywhere", 666),
]
)
def test_that_parameter_precedence_is_respected(self,
default,
default_conf,
cmd_line_conf,
cmd_line_arg,
arg_name,
expected):
test_program_script = 'CppProgramExample'
cmd = '{} --log-level=DEBUG'.format(test_program_script)
if cmd_line_conf is not None:
cmd_line_conf_fpath = getAuxiliaryPath("ElementsExamples/tests/conf/%s.conf" % test_program_script)
cmd = '{} --config-file={}'.format(cmd, cmd_line_conf_fpath)
if cmd_line_arg is not None:
if cmd_line_arg == "regular":
cmd = '{} --{}=666'.format(cmd, arg_name)
else:
cmd = '{} --{} 666'.format(cmd, arg_name)
process = Popen(shlex.split(cmd), stdout=PIPE, stderr=PIPE)
_, stderr = process.communicate()
output_arg_line = list(
filter(
lambda line: ' {} = '.format(arg_name) in line,
stderr.decode().splitlines()
)
)
assert len(output_arg_line) == 1, 'the argument {} should appear exactly once'.format(arg_name)
output_arg_value = int(output_arg_line.pop().split('=')[-1])
msg = (
'The command\n\t{}\nfailed while checking for the value of argument\n\t{}\n'
'The obtained value is "{}" while the expected value is "{}"'
).format(
cmd,
arg_name,
output_arg_value,
expected
)
assert output_arg_value == expected, msg
if __name__ == "__main__":
unittest.main()