From e6d8a90e5d6e9f3156a4adfcdb65a9fb1d12788f Mon Sep 17 00:00:00 2001 From: "Eric N. Vander Weele" Date: Mon, 11 May 2020 23:43:39 -0400 Subject: [PATCH] options: Forward `--output-file` to be reparsed for BaseFormatter This fixes a regression introduced in daca2c8 and b14d47b. The --output-file` option was consumed by the preliminary option parser. However, the `BaseFormatter` class needs the option for setting the output filename. This special cases this option to ensure it gets re-parsed and respected when specified on the CLI. --- src/flake8/main/application.py | 7 ++++++- tests/integration/test_main.py | 14 ++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/flake8/main/application.py b/src/flake8/main/application.py index 001ad6c..e4b7930 100644 --- a/src/flake8/main/application.py +++ b/src/flake8/main/application.py @@ -112,7 +112,12 @@ class Application(object): :rtype: (argparse.Namespace, list) """ - return self.prelim_arg_parser.parse_known_args(argv) + args, rest = self.prelim_arg_parser.parse_known_args(argv) + # XXX (ericvw): Special case "forwarding" the output file option so + # that it can be reparsed again for the BaseFormatter.filename. + if args.output_file: + rest.extend(("--output-file", args.output_file)) + return args, rest def exit(self): # type: () -> None diff --git a/tests/integration/test_main.py b/tests/integration/test_main.py index b67992b..54254ef 100644 --- a/tests/integration/test_main.py +++ b/tests/integration/test_main.py @@ -246,3 +246,17 @@ def test_file_not_found(tmpdir, capsys): out, err = capsys.readouterr() assert out.startswith("i-do-not-exist:0:1: E902") assert err == "" + + +def test_output_file(tmpdir, capsys): + """Ensure that --output-file is honored.""" + tmpdir.join('t.py').write('import os\n') + + with tmpdir.as_cwd(): + _call_main(['t.py', '--output-file=f'], retv=1) + + out, err = capsys.readouterr() + assert out == err == "" + + expected = "t.py:1:1: F401 'os' imported but unused\n" + assert tmpdir.join('f').read() == expected