diff options
| author | Victor Uriarte <victor.m.uriarte@intel.com> | 2016-06-19 18:27:59 -0700 |
|---|---|---|
| committer | Victor Uriarte <victor.m.uriarte@intel.com> | 2016-06-20 07:41:13 -0700 |
| commit | 85349e68592964e66e5dfe7e48e9f76cb93d48fd (patch) | |
| tree | db402888277a06ca450eba2e57dd6f94c9b369ad | |
| parent | 3aa952d71952cbb0728698bb381dae74d1a1727b (diff) | |
| download | sqlparse-85349e68592964e66e5dfe7e48e9f76cb93d48fd.tar.gz | |
Format cli.py and add cli-tests
| -rw-r--r-- | sqlparse/cli.py | 12 | ||||
| -rw-r--r-- | tests/test_cli.py | 37 |
2 files changed, 42 insertions, 7 deletions
diff --git a/sqlparse/cli.py b/sqlparse/cli.py index 03a4f8f..80d547d 100644 --- a/sqlparse/cli.py +++ b/sqlparse/cli.py @@ -123,7 +123,8 @@ def create_parser(): def _error(msg): """Print msg and optionally exit with return code exit_.""" - sys.stderr.write('[ERROR] %s\n' % msg) + sys.stderr.write('[ERROR] {0}\n'.format(msg)) + return 1 def main(args=None): @@ -137,15 +138,13 @@ def main(args=None): # TODO: Needs to deal with encoding data = ''.join(open(args.filename).readlines()) except IOError as e: - _error('Failed to read %s: %s' % (args.filename, e)) - return 1 + return _error('Failed to read {0}: {1}'.format(args.filename, e)) if args.outfile: try: stream = open(args.outfile, 'w') except IOError as e: - _error('Failed to open %s: %s' % (args.outfile, e)) - return 1 + return _error('Failed to open {0}: {1}'.format(args.outfile, e)) else: stream = sys.stdout @@ -153,8 +152,7 @@ def main(args=None): try: formatter_opts = sqlparse.formatter.validate_options(formatter_opts) except SQLParseError as e: - _error('Invalid options: %s' % e) - return 1 + return _error('Invalid options: {0}'.format(e)) s = sqlparse.format(data, **formatter_opts) if PY2: diff --git a/tests/test_cli.py b/tests/test_cli.py index 51ee61b..77a764e 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -32,6 +32,43 @@ def test_valid_args(filepath): assert sqlparse.cli.main([path, '-r']) is not None +def test_invalid_choise(filepath): + path = filepath('function.sql') + with pytest.raises(SystemExit): + sqlparse.cli.main([path, '-l', 'spanish']) + + +def test_invalid_args(filepath, capsys): + path = filepath('function.sql') + sqlparse.cli.main([path, '-r', '--indent_width', '0']) + _, err = capsys.readouterr() + assert err == ("[ERROR] Invalid options: indent_width requires " + "a positive integer\n") + + +def test_invalid_infile(filepath, capsys): + path = filepath('missing.sql') + sqlparse.cli.main([path, '-r']) + _, err = capsys.readouterr() + assert err[:22] == "[ERROR] Failed to read" + + +def test_invalid_outfile(filepath, capsys): + path = filepath('function.sql') + outpath = filepath('/missing/function.sql') + sqlparse.cli.main([path, '-r', '-o', outpath]) + _, err = capsys.readouterr() + assert err[:22] == "[ERROR] Failed to open" + + +def test_stdout(filepath, load_file, capsys): + path = filepath('begintag.sql') + expected = load_file('begintag.sql') + sqlparse.cli.main([path]) + out, _ = capsys.readouterr() + assert out == expected + + def test_script(): # Call with the --help option as a basic sanity check. cmd = "{0:s} -m sqlparse.cli --help".format(sys.executable) |
