Skip to content

Commit 349f0c2

Browse files
authored
Merge pull request #23 from nginxinc/uppercase-flag-args
Added support for uppercase flag args
2 parents 8709d93 + bb4f8b3 commit 349f0c2

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

crossplane/analyzer.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1920,7 +1920,7 @@ def analyze(fname, stmt, term, ctx=()):
19201920
reason = '"%s" directive is not allowed here' % directive
19211921
raise NgxParserDirectiveContextError(reason, fname, line)
19221922

1923-
valid_flag = lambda x: x in ('on', 'off')
1923+
valid_flag = lambda x: x.lower() in ('on', 'off')
19241924

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

tests/test_analyze.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,30 @@ def test_state_directive():
3131
raise Exception("bad context for 'state' passed: " + repr(ctx))
3232
except crossplane.errors.NgxParserDirectiveContextError:
3333
continue
34+
35+
36+
def test_flag_directive_args():
37+
fname = '/path/to/nginx.conf'
38+
ctx = ('events',)
39+
40+
# an NGINX_CONF_FLAG directive
41+
stmt = {
42+
'directive': 'accept_mutex',
43+
'line': 2 # this is arbitrary
44+
}
45+
46+
good_args = [['on'], ['off'], ['On'], ['Off'], ['ON'], ['OFF']]
47+
48+
for args in good_args:
49+
stmt['args'] = args
50+
crossplane.analyzer.analyze(fname, stmt, term=';', ctx=ctx)
51+
52+
bad_args = [['1'], ['0'], ['true'], ['okay'], ['']]
53+
54+
for args in bad_args:
55+
stmt['args'] = args
56+
try:
57+
crossplane.analyzer.analyze(fname, stmt, term=';', ctx=ctx)
58+
raise Exception('bad args for flag directive: ' + repr(args))
59+
except crossplane.errors.NgxParserDirectiveArgumentsError as e:
60+
assert e.strerror.endswith('it must be "on" or "off"')

0 commit comments

Comments
 (0)