20
20
#
21
21
22
22
#
23
- # Copyright (c) 2017, 2022 , Oracle and/or its affiliates. All rights reserved.
23
+ # Copyright (c) 2017, 2023 , Oracle and/or its affiliates. All rights reserved.
24
24
#
25
25
26
26
import os
38
38
39
39
def test_str ():
40
40
cmds = CommandSequence (CommandSequenceBase ("opengrok-master" ,
41
- [{"command" : ['foo' ]},
42
- {"command" : ["bar" ]}]))
41
+ [{"command" : { "args" : ['foo' ]} },
42
+ {"command" : { "args" : ["bar" ]} }]))
43
43
assert str (cmds ) == "opengrok-master"
44
44
45
45
@@ -50,32 +50,25 @@ def test_invalid_configuration_commands_none():
50
50
assert str (exc_info .value ) == "commands is None"
51
51
52
52
53
- def test_invalid_configuration_commands_not_list ():
54
- with pytest .raises (CommandConfigurationException ) as exc_info :
55
- CommandSequence (CommandSequenceBase ("foo" , {"foo" : "bar" }))
56
-
57
- assert str (exc_info .value ) == "commands is not a list"
58
-
59
-
60
53
def test_invalid_configuration_commands_no_command ():
61
54
with pytest .raises (CommandConfigurationException ) as exc_info :
62
- CommandSequence (CommandSequenceBase ("foo" , [{"command" : ['foo' ]},
55
+ CommandSequence (CommandSequenceBase ("foo" , [{"command" : { "args" : ['foo' ]} },
63
56
{"foo" : "bar" }]))
64
57
65
58
assert str (exc_info .value ).startswith ("command dictionary has unknown key" )
66
59
67
60
68
- def test_invalid_configuration_commands_no_list ():
61
+ def test_invalid_configuration_commands_no_dict1 ():
69
62
with pytest .raises (CommandConfigurationException ) as exc_info :
70
- CommandSequence (CommandSequenceBase ("foo" , [{"command" : ['foo' ]},
71
- {"command" : "bar" }]))
63
+ CommandSequence (CommandSequenceBase ("foo" , [{"command" : { "args" : ['foo' ]} },
64
+ {"command" : [ "bar" ] }]))
72
65
73
- assert str (exc_info .value ).startswith ("command value not a list " )
66
+ assert str (exc_info .value ).startswith ("command value not a dictionary " )
74
67
75
68
76
- def test_invalid_configuration_commands_no_dict ():
69
+ def test_invalid_configuration_commands_no_dict2 ():
77
70
with pytest .raises (CommandConfigurationException ) as exc_info :
78
- CommandSequence (CommandSequenceBase ("foo" , [{"command" : ['foo' ]},
71
+ CommandSequence (CommandSequenceBase ("foo" , [{"command" : { "args" : ['foo' ]} },
79
72
"command" ]))
80
73
81
74
assert str (exc_info .value ).find ("is not a dictionary" ) != - 1
@@ -87,7 +80,7 @@ def test_timeout_propagation():
87
80
"""
88
81
expected_timeout = 11
89
82
expected_api_timeout = 22
90
- cmd_seq_base = CommandSequenceBase ("foo" , [{"command" : ['foo' ]}],
83
+ cmd_seq_base = CommandSequenceBase ("foo" , [{"command" : { "args" : ['foo' ]} }],
91
84
api_timeout = expected_timeout ,
92
85
async_api_timeout = expected_api_timeout )
93
86
cmd_seq = CommandSequence (cmd_seq_base )
@@ -99,11 +92,11 @@ def test_timeout_propagation():
99
92
or not os .path .exists ('/bin/echo' ),
100
93
reason = "requires Unix" )
101
94
def test_run_retcodes ():
102
- cmd_list = [{"command" : ["/bin/echo" ]},
103
- {"command" : ["/bin/sh" , "-c" ,
104
- "echo " + PROJECT_SUBST + "; exit 0" ]},
105
- {"command" : ["/bin/sh" , "-c" ,
106
- "echo " + PROJECT_SUBST + "; exit 1" ]}]
95
+ cmd_list = [{"command" : { "args" : ["/bin/echo" ]} },
96
+ {"command" : { "args" : ["/bin/sh" , "-c" ,
97
+ "echo " + PROJECT_SUBST + "; exit 0" ]}} ,
98
+ {"command" : { "args" : ["/bin/sh" , "-c" ,
99
+ "echo " + PROJECT_SUBST + "; exit 1" ]}} ]
107
100
cmds = CommandSequence (CommandSequenceBase ("opengrok-master" , cmd_list ))
108
101
cmds .run ()
109
102
assert cmds .retcodes == {
@@ -117,9 +110,9 @@ def test_run_retcodes():
117
110
or not os .path .exists ('/bin/echo' ),
118
111
reason = "requires Unix" )
119
112
def test_terminate_after_non_zero_code ():
120
- cmd_list = [{"command" : ["/bin/sh" , "-c" ,
121
- "echo " + PROJECT_SUBST + "; exit 255" ]},
122
- {"command" : ["/bin/echo" ]}]
113
+ cmd_list = [{"command" : { "args" : ["/bin/sh" , "-c" ,
114
+ "echo " + PROJECT_SUBST + "; exit 255" ]}} ,
115
+ {"command" : { "args" : ["/bin/echo" ]} }]
123
116
cmds = CommandSequence (CommandSequenceBase ("opengrok-master" , cmd_list ))
124
117
cmds .run ()
125
118
assert cmds .retcodes == {
@@ -131,9 +124,9 @@ def test_terminate_after_non_zero_code():
131
124
or not os .path .exists ('/bin/echo' ),
132
125
reason = "requires Unix" )
133
126
def test_exit_2_handling ():
134
- cmd_list = [{"command" : ["/bin/sh" , "-c" ,
135
- "echo " + PROJECT_SUBST + "; exit 2" ]},
136
- {"command" : ["/bin/echo" ]}]
127
+ cmd_list = [{"command" : { "args" : ["/bin/sh" , "-c" ,
128
+ "echo " + PROJECT_SUBST + "; exit 2" ]}} ,
129
+ {"command" : { "args" : ["/bin/echo" ]} }]
137
130
cmds = CommandSequence (CommandSequenceBase ("opengrok-master" , cmd_list ))
138
131
cmds .run ()
139
132
assert cmds .retcodes == {
@@ -146,14 +139,14 @@ def test_exit_2_handling():
146
139
or not os .path .exists ('/bin/echo' ),
147
140
reason = "requires Unix" )
148
141
def test_driveon_flag ():
149
- cmd_list = [{"command" : ["/bin/sh" , "-c" ,
150
- "echo " + PROJECT_SUBST + "; exit 2" ]},
151
- {"command" : ["/bin/echo" ]},
152
- {"command" : ["/bin/sh" , "-c" ,
153
- "echo " + PROJECT_SUBST +
154
- "; exit 1" ]},
155
- {"command" : ["/bin/sh" , "-c" ,
156
- "echo " + PROJECT_SUBST ]}]
142
+ cmd_list = [{"command" : { "args" : ["/bin/sh" , "-c" ,
143
+ "echo " + PROJECT_SUBST + "; exit 2" ]}} ,
144
+ {"command" : { "args" : ["/bin/echo" ]} },
145
+ {"command" : { "args" : ["/bin/sh" , "-c" ,
146
+ "echo " + PROJECT_SUBST +
147
+ "; exit 1" ]} },
148
+ {"command" : { "args" : ["/bin/sh" , "-c" ,
149
+ "echo " + PROJECT_SUBST ]}} ]
157
150
cmds = CommandSequence (CommandSequenceBase ("opengrok-master" ,
158
151
cmd_list , driveon = True ))
159
152
cmds .run ()
@@ -168,13 +161,37 @@ def test_driveon_flag():
168
161
@pytest .mark .skipif (not os .path .exists ('/bin/echo' ),
169
162
reason = "requires Unix" )
170
163
def test_project_subst ():
171
- cmd_list = [{"command" : ["/bin/echo" , PROJECT_SUBST ]}]
164
+ cmd_list = [{"command" : { "args" : ["/bin/echo" , PROJECT_SUBST ]} }]
172
165
cmds = CommandSequence (CommandSequenceBase ("test-subst" , cmd_list ))
173
166
cmds .run ()
174
167
175
168
assert cmds .outputs ['/bin/echo test-subst' ] == ['test-subst\n ' ]
176
169
177
170
171
+ @pytest .mark .skipif (not os .path .exists ('/bin/echo' ),
172
+ reason = "requires Unix" )
173
+ def test_args_subst ():
174
+ cmd_list = [{"command" : {"args" : ["/bin/echo" , "%PATTERN%" ],
175
+ "args_subst" : {"%PATTERN%" : "foo" }}}]
176
+ cmds = CommandSequence (CommandSequenceBase ("test-subst" , cmd_list ))
177
+ cmds .run ()
178
+
179
+ assert cmds .outputs ['/bin/echo foo' ] == ['foo\n ' ]
180
+
181
+
182
+ @pytest .mark .skipif (not os .path .exists ('/bin/echo' ),
183
+ reason = "requires Unix" )
184
+ def test_args_subst_env ():
185
+ cmd_list = [{"command" : {"args" : ["/bin/echo" , "%PATTERN%" ],
186
+ "args_subst" : {"%PATTERN%" : "$FOO" }}}]
187
+ os .environ ["FOO" ] = "bar"
188
+ cmds = CommandSequence (CommandSequenceBase ("test-subst" , cmd_list ))
189
+ cmds .run ()
190
+ os .environ .pop ("FOO" )
191
+
192
+ assert cmds .outputs ['/bin/echo bar' ] == ['bar\n ' ]
193
+
194
+
178
195
def test_cleanup_exception ():
179
196
"""
180
197
If cleanup is not a list, exception should be thrown when initializing
@@ -195,12 +212,12 @@ def test_cleanup():
195
212
with tempfile .TemporaryDirectory () as tmpdir :
196
213
file_foo = os .path .join (tmpdir , "foo" )
197
214
file_bar = os .path .join (tmpdir , "bar" )
198
- cleanup_list = [{"command" : ["/usr/bin/touch" , file_foo ]},
199
- {"command" : ["/bin/cat" , "/totallynonexistent" ]},
200
- {"command" : ["/usr/bin/touch" , file_bar ]}]
215
+ cleanup_list = [{"command" : { "args" : ["/usr/bin/touch" , file_foo ]} },
216
+ {"command" : { "args" : ["/bin/cat" , "/totallynonexistent" ]} },
217
+ {"command" : { "args" : ["/usr/bin/touch" , file_bar ]} }]
201
218
# Running 'cat' on non-existing entry causes it to return 1.
202
219
cmd = ["/bin/cat" , "/foobar" ]
203
- cmd_list = [{"command" : cmd }]
220
+ cmd_list = [{"command" : { "args" : cmd } }]
204
221
commands = CommandSequence (CommandSequenceBase ("test-cleanup-list" ,
205
222
cmd_list ,
206
223
cleanup = cleanup_list ))
0 commit comments