Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion crossplane/analyzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -1920,7 +1920,7 @@ def analyze(fname, stmt, term, ctx=()):
reason = '"%s" directive is not allowed here' % directive
raise NgxParserDirectiveContextError(reason, fname, line)

valid_flag = lambda x: x in ('on', 'off')
valid_flag = lambda x: x.lower() in ('on', 'off')

# do this in reverse because we only throw errors at the end if no masks
# are valid, and typically the first bit mask is what the parser expects
Expand All @@ -1942,6 +1942,8 @@ def analyze(fname, stmt, term, ctx=()):
(mask & NGX_CONF_1MORE and n_args >= 1) or
(mask & NGX_CONF_2MORE and n_args >= 2)):
return
elif mask & NGX_CONF_FLAG and n_args == 1 and not valid_flag(args[0]):
reason = 'invalid value "%s" in "%%s" directive, it must be "on" or "off"' % args[0]
else:
reason = 'invalid number of arguments in "%s" directive'

Expand Down
27 changes: 27 additions & 0 deletions tests/test_analyze.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,30 @@ def test_state_directive():
raise Exception("bad context for 'state' passed: " + repr(ctx))
except crossplane.errors.NgxParserDirectiveContextError:
continue


def test_flag_directive_args():
fname = '/path/to/nginx.conf'
ctx = ('events',)

# an NGINX_CONF_FLAG directive
stmt = {
'directive': 'accept_mutex',
'line': 2 # this is arbitrary
}

good_args = [['on'], ['off'], ['On'], ['Off'], ['ON'], ['OFF']]

for args in good_args:
stmt['args'] = args
crossplane.analyzer.analyze(fname, stmt, term=';', ctx=ctx)

bad_args = [['1'], ['0'], ['true'], ['okay'], ['']]

for args in bad_args:
stmt['args'] = args
try:
crossplane.analyzer.analyze(fname, stmt, term=';', ctx=ctx)
raise Exception('bad args for flag directive: ' + repr(args))
except crossplane.errors.NgxParserDirectiveArgumentsError as e:
assert e.strerror.endswith('it must be "on" or "off"')