Skip to content

Commit 37237d4

Browse files
author
David Noble
committed
Fixes an issue where missing required options are not detected
Also added test around this with more parser-related tests to come.
1 parent 3d463ad commit 37237d4

File tree

2 files changed

+29
-26
lines changed

2 files changed

+29
-26
lines changed

splunklib/searchcommands/decorators.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ def get_missing(self):
314314
missing = [
315315
item.name for item in self._items.itervalues()
316316
if item.is_required and not item.is_set]
317-
return missing.sort() if len(missing) > 0 else None
317+
return missing if len(missing) > 0 else None
318318

319319
def iteritems(self):
320320
return self._items.iteritems()

tests/test_searchcommands_app.py

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -97,13 +97,13 @@ class StubbedReportingCommand(ReportingCommand):
9797
doc='''
9898
**Syntax:** **duration=***<value>*
9999
**Description:** A length of time''',
100-
require=False, validate=validators.Duration())
100+
validate=validators.Duration())
101101

102102
fieldname = Option(
103103
doc='''
104104
**Syntax:** **fieldname=***<value>*
105105
**Description:** Name of a field''',
106-
require=True, validate=validators.Fieldname())
106+
validate=validators.Fieldname())
107107

108108
file = Option(
109109
doc='''
@@ -149,13 +149,13 @@ class StubbedStreamingCommand(StreamingCommand):
149149
doc='''
150150
**Syntax:** **boolean=***<value>*
151151
**Description:** A boolean value''',
152-
require=False, validate=validators.Boolean())
152+
require=True, validate=validators.Boolean())
153153

154154
duration = Option(
155155
doc='''
156156
**Syntax:** **duration=***<value>*
157157
**Description:** A length of time''',
158-
require=False, validate=validators.Duration())
158+
require=True, validate=validators.Duration())
159159

160160
fieldname = Option(
161161
doc='''
@@ -167,31 +167,31 @@ class StubbedStreamingCommand(StreamingCommand):
167167
doc='''
168168
**Syntax:** **file=***<value>*
169169
**Description:** Name of a file''',
170-
validate=validators.File(mode='r'))
170+
require=True, validate=validators.File(mode='r'))
171171

172172
integer = Option(
173173
doc='''
174174
**Syntax:** **integer=***<value>*
175175
**Description:** An integer value''',
176-
validate=validators.Integer())
176+
require=True, validate=validators.Integer())
177177

178178
optionname = Option(
179179
doc='''
180180
**Syntax:** **optionname=***<value>*
181181
**Description:** The name of an option (used internally)''',
182-
validate=validators.OptionName())
182+
require=True, validate=validators.OptionName())
183183

184184
regularexpression = Option(
185185
doc='''
186186
**Syntax:** **regularexpression=***<value>*
187187
**Description:** Regular expression pattern to match''',
188-
validate=validators.RegularExpression())
188+
require=True, validate=validators.RegularExpression())
189189

190190
set = Option(
191191
doc='''
192192
**Syntax:** **set=***<value>*
193193
**Description:** Regular expression pattern to match''',
194-
validate=validators.Set("foo", "bar", "test"))
194+
require=True, validate=validators.Set("foo", "bar", "test"))
195195

196196
def stream(self, records):
197197
pass
@@ -217,27 +217,30 @@ def test_command_parser(self):
217217
encoder = JSONEncoder()
218218
file_path = TestSearchCommandsApp._data_file(os.path.join('input', 'counts.csv'))
219219

220-
command = StubbedStreamingCommand()
220+
command = StubbedStreamingCommand() # All options are required
221221

222-
parser.parse(
223-
[
224-
'boolean=true',
225-
'duration=00:00:10',
226-
'fieldname=word_count',
227-
'file=%s' % encoder.encode(file_path),
228-
'integer=10',
229-
'optionname=foo_bar',
230-
'regularexpression="\\\\w+"',
231-
'set=foo',
232-
'field_1',
233-
'field_2',
234-
'field_3'
235-
],
236-
command)
222+
options = [
223+
'boolean=true',
224+
'duration=00:00:10',
225+
'fieldname=word_count',
226+
'file=%s' % encoder.encode(file_path),
227+
'integer=10',
228+
'optionname=foo_bar',
229+
'regularexpression="\\\\w+"',
230+
'set=foo']
231+
232+
fields = ['field_1', 'field_2', 'field_3']
233+
234+
parser.parse(options + fields, command)
237235
command_line = str(command)
236+
238237
self.assertEqual(
239238
'stubbedstreaming boolean=true duration=10 fieldname="word_count" file=%s integer=10 optionname="foo_bar" regularexpression="\\\\w+" set="foo" field_1 field_2 field_3' % encoder.encode(file_path),
240239
command_line)
240+
241+
for option in options:
242+
self.assertRaises(ValueError, parser.parse([x for x in options if x != option] + ['field_1', 'field_2', 'field_3'], command))
243+
241244
return
242245

243246
def disable_test_option_logging_configuration(self):

0 commit comments

Comments
 (0)