Skip to content

Commit 85349e6

Browse files
committed
Format cli.py and add cli-tests
1 parent 3aa952d commit 85349e6

File tree

2 files changed

+42
-7
lines changed

2 files changed

+42
-7
lines changed

sqlparse/cli.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,8 @@ def create_parser():
123123

124124
def _error(msg):
125125
"""Print msg and optionally exit with return code exit_."""
126-
sys.stderr.write('[ERROR] %s\n' % msg)
126+
sys.stderr.write('[ERROR] {0}\n'.format(msg))
127+
return 1
127128

128129

129130
def main(args=None):
@@ -137,24 +138,21 @@ def main(args=None):
137138
# TODO: Needs to deal with encoding
138139
data = ''.join(open(args.filename).readlines())
139140
except IOError as e:
140-
_error('Failed to read %s: %s' % (args.filename, e))
141-
return 1
141+
return _error('Failed to read {0}: {1}'.format(args.filename, e))
142142

143143
if args.outfile:
144144
try:
145145
stream = open(args.outfile, 'w')
146146
except IOError as e:
147-
_error('Failed to open %s: %s' % (args.outfile, e))
148-
return 1
147+
return _error('Failed to open {0}: {1}'.format(args.outfile, e))
149148
else:
150149
stream = sys.stdout
151150

152151
formatter_opts = vars(args)
153152
try:
154153
formatter_opts = sqlparse.formatter.validate_options(formatter_opts)
155154
except SQLParseError as e:
156-
_error('Invalid options: %s' % e)
157-
return 1
155+
return _error('Invalid options: {0}'.format(e))
158156

159157
s = sqlparse.format(data, **formatter_opts)
160158
if PY2:

tests/test_cli.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,43 @@ def test_valid_args(filepath):
3232
assert sqlparse.cli.main([path, '-r']) is not None
3333

3434

35+
def test_invalid_choise(filepath):
36+
path = filepath('function.sql')
37+
with pytest.raises(SystemExit):
38+
sqlparse.cli.main([path, '-l', 'spanish'])
39+
40+
41+
def test_invalid_args(filepath, capsys):
42+
path = filepath('function.sql')
43+
sqlparse.cli.main([path, '-r', '--indent_width', '0'])
44+
_, err = capsys.readouterr()
45+
assert err == ("[ERROR] Invalid options: indent_width requires "
46+
"a positive integer\n")
47+
48+
49+
def test_invalid_infile(filepath, capsys):
50+
path = filepath('missing.sql')
51+
sqlparse.cli.main([path, '-r'])
52+
_, err = capsys.readouterr()
53+
assert err[:22] == "[ERROR] Failed to read"
54+
55+
56+
def test_invalid_outfile(filepath, capsys):
57+
path = filepath('function.sql')
58+
outpath = filepath('/missing/function.sql')
59+
sqlparse.cli.main([path, '-r', '-o', outpath])
60+
_, err = capsys.readouterr()
61+
assert err[:22] == "[ERROR] Failed to open"
62+
63+
64+
def test_stdout(filepath, load_file, capsys):
65+
path = filepath('begintag.sql')
66+
expected = load_file('begintag.sql')
67+
sqlparse.cli.main([path])
68+
out, _ = capsys.readouterr()
69+
assert out == expected
70+
71+
3572
def test_script():
3673
# Call with the --help option as a basic sanity check.
3774
cmd = "{0:s} -m sqlparse.cli --help".format(sys.executable)

0 commit comments

Comments
 (0)