From 16ca17388ab4a8136617390329328e1a13219f84 Mon Sep 17 00:00:00 2001 From: Charles Frye Date: Thu, 7 Mar 2019 20:27:59 -0800 Subject: [PATCH 1/2] Fixes handling of empty lists by Application `Application.parse_preliminary_options_and_args` was previously, against expectations, treating empty lists passed as the `argv` argument the same way it treated `None`s. This has been addressed and the correct behavior tested for in a unit test of the `Application` class. See issue #518 for details. --- src/flake8/main/application.py | 2 +- tests/unit/test_application.py | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/flake8/main/application.py b/src/flake8/main/application.py index 6e8ddaf..c7859b9 100644 --- a/src/flake8/main/application.py +++ b/src/flake8/main/application.py @@ -122,7 +122,7 @@ class Application(object): # do not need to worry and we can continue. If it is, we successfully # defer printing the version until just a little bit later. # Similarly we have to defer printing the help text until later. - args = (argv or sys.argv)[:] + args = (argv if argv is not None else sys.argv)[:] try: args.remove("--version") except ValueError: diff --git a/tests/unit/test_application.py b/tests/unit/test_application.py index 5ec9f1f..875ed6e 100644 --- a/tests/unit/test_application.py +++ b/tests/unit/test_application.py @@ -1,5 +1,6 @@ """Tests for the Application class.""" import optparse +import sys import mock import pytest @@ -97,3 +98,12 @@ def test_prelim_opts_args(application): assert application.prelim_opts.statistics assert application.prelim_opts.verbose assert application.prelim_args == ['src', 'setup.py'] + + +def test_prelim_opts_handles_empty(application): + """Verify empty argv lists are handled correctly.""" + irrelevant_args = ['myexe', '/path/to/foo'] + with mock.patch.object(sys, 'argv', irrelevant_args): + application.parse_preliminary_options_and_args([]) + + assert application.prelim_args != irrelevant_args From e9b9ebb58e1e45db3cb17f918a8269288a461100 Mon Sep 17 00:00:00 2001 From: Charles Frye Date: Mon, 11 Mar 2019 14:05:34 -0700 Subject: [PATCH 2/2] makes prelim_args test more explicit --- tests/unit/test_application.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/unit/test_application.py b/tests/unit/test_application.py index 875ed6e..cb8372b 100644 --- a/tests/unit/test_application.py +++ b/tests/unit/test_application.py @@ -106,4 +106,4 @@ def test_prelim_opts_handles_empty(application): with mock.patch.object(sys, 'argv', irrelevant_args): application.parse_preliminary_options_and_args([]) - assert application.prelim_args != irrelevant_args + assert application.prelim_args == []