Skip to content

Commit

Permalink
Fix Namespace check
Browse files Browse the repository at this point in the history
Follow-up from #150.
  • Loading branch information
fsouza committed Oct 3, 2022
1 parent e2266f3 commit f27565d
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 36 deletions.
2 changes: 1 addition & 1 deletion autoflake.py
Original file line number Diff line number Diff line change
Expand Up @@ -1181,7 +1181,7 @@ def merge_configuration_file(args):
"off": False,
}

if getattr(args, "config_file", None):
if args.config_file:
config_file = pathlib.Path(args.config_file).resolve()
config = process_config_file(config_file)

Expand Down
73 changes: 38 additions & 35 deletions test_autoflake.py
Original file line number Diff line number Diff line change
Expand Up @@ -3043,7 +3043,10 @@ def assert_namespace(self, namespace, expected_props):

def test_no_config_file(self):
self.create_file("test_me.py")
original_args = {"files": [self.effective_path("test_me.py")]}
original_args = {
"files": [self.effective_path("test_me.py")],
"config_file": None,
}
args = Namespace(**original_args)
assert autoflake.merge_configuration_file(args)
self.assert_namespace(args, original_args)
Expand All @@ -3052,9 +3055,9 @@ def test_non_nested_pyproject_toml_empty(self):
self.create_file("test_me.py")
self.create_file("pyproject.toml", '[tool.other]\nprop="value"\n')
files = [self.effective_path("test_me.py")]
args = Namespace(files=files)
args = Namespace(files=files, config_file=None)
assert autoflake.merge_configuration_file(args)
self.assert_namespace(args, {"files": files})
self.assert_namespace(args, {"files": files, "config_file": None})

def test_non_nested_pyproject_toml_non_empty(self):
self.create_file("test_me.py")
Expand All @@ -3063,11 +3066,11 @@ def test_non_nested_pyproject_toml_non_empty(self):
"[tool.autoflake]\nexpand-star-imports=true\n",
)
files = [self.effective_path("test_me.py")]
args = Namespace(files=files)
args = Namespace(files=files, config_file=None)
assert autoflake.merge_configuration_file(args)
self.assert_namespace(
args,
{"files": files, "expand_star_imports": True},
{"files": files, "expand_star_imports": True, "config_file": None},
)

def test_non_nested_setup_cfg_non_empty(self):
Expand All @@ -3077,11 +3080,11 @@ def test_non_nested_setup_cfg_non_empty(self):
"[other]\nexpand-star-imports = yes\n",
)
files = [self.effective_path("test_me.py")]
args = Namespace(files=files)
args = Namespace(files=files, config_file=None)
assert autoflake.merge_configuration_file(args)
self.assert_namespace(
args,
{"files": files},
{"files": files, "config_file": None},
)

def test_non_nested_setup_cfg_empty(self):
Expand All @@ -3091,11 +3094,11 @@ def test_non_nested_setup_cfg_empty(self):
"[autoflake]\nexpand-star-imports = yes\n",
)
files = [self.effective_path("test_me.py")]
args = Namespace(files=files)
args = Namespace(files=files, config_file=None)
assert autoflake.merge_configuration_file(args)
self.assert_namespace(
args,
{"files": files, "expand_star_imports": True},
{"files": files, "expand_star_imports": True, "config_file": None},
)

def test_nested_file(self):
Expand All @@ -3105,11 +3108,11 @@ def test_nested_file(self):
"[tool.autoflake]\nexpand-star-imports=true\n",
)
files = [self.effective_path("nested/file/test_me.py")]
args = Namespace(files=files)
args = Namespace(files=files, config_file=None)
assert autoflake.merge_configuration_file(args)
self.assert_namespace(
args,
{"files": files, "expand_star_imports": True},
{"files": files, "expand_star_imports": True, "config_file": None},
)

def test_common_path_nested_file_do_not_load(self):
Expand All @@ -3123,11 +3126,11 @@ def test_common_path_nested_file_do_not_load(self):
self.effective_path("nested/file/test_me.py"),
self.effective_path("nested/other/test_me.py"),
]
args = Namespace(files=files)
args = Namespace(files=files, config_file=None)
assert autoflake.merge_configuration_file(args)
self.assert_namespace(
args,
{"files": files},
{"files": files, "config_file": None},
)

def test_common_path_nested_file_do_load(self):
Expand All @@ -3141,11 +3144,11 @@ def test_common_path_nested_file_do_load(self):
self.effective_path("nested/file/test_me.py"),
self.effective_path("nested/other/test_me.py"),
]
args = Namespace(files=files)
args = Namespace(files=files, config_file=None)
assert autoflake.merge_configuration_file(args)
self.assert_namespace(
args,
{"files": files, "expand_star_imports": True},
{"files": files, "expand_star_imports": True, "config_file": None},
)

def test_common_path_instead_of_common_prefix(self):
Expand All @@ -3160,11 +3163,11 @@ def test_common_path_instead_of_common_prefix(self):
self.effective_path("nested/file-foo/test_me.py"),
self.effective_path("nested/file-bar/test_me.py"),
]
args = Namespace(files=files)
args = Namespace(files=files, config_file=None)
assert autoflake.merge_configuration_file(args)
self.assert_namespace(
args,
{"files": files},
{"files": files, "config_file": None},
)

def test_continue_search_if_no_config_found(self):
Expand All @@ -3178,11 +3181,11 @@ def test_continue_search_if_no_config_found(self):
"[tool.autoflake]\nexpand-star-imports = true\n",
)
files = [self.effective_path("nested/test_me.py")]
args = Namespace(files=files)
args = Namespace(files=files, config_file=None)
assert autoflake.merge_configuration_file(args)
self.assert_namespace(
args,
{"files": files, "expand_star_imports": True},
{"files": files, "expand_star_imports": True, "config_file": None},
)

def test_stop_search_if_config_found(self):
Expand All @@ -3196,11 +3199,11 @@ def test_stop_search_if_config_found(self):
"[tool.autoflake]\nexpand-star-imports = true\n",
)
files = [self.effective_path("nested/test_me.py")]
args = Namespace(files=files)
args = Namespace(files=files, config_file=None)
assert autoflake.merge_configuration_file(args)
self.assert_namespace(
args,
{"files": files},
{"files": files, "config_file": None},
)

def test_config_option(self):
Expand Down Expand Up @@ -3229,11 +3232,11 @@ def test_dont_load_false(self):
"[autoflake]\nexpand-star-imports = no\n",
)
files = [self.effective_path("test_me.py")]
args = Namespace(files=files)
args = Namespace(files=files, config_file=None)
assert autoflake.merge_configuration_file(args)
self.assert_namespace(
args,
{"files": files},
{"files": files, "config_file": None},
)

def test_list_value_pyproject_toml(self):
Expand All @@ -3243,11 +3246,11 @@ def test_list_value_pyproject_toml(self):
'[tool.autoflake]\nimports=["my_lib", "other_lib"]\n',
)
files = [self.effective_path("test_me.py")]
args = Namespace(files=files)
args = Namespace(files=files, config_file=None)
assert autoflake.merge_configuration_file(args)
self.assert_namespace(
args,
{"files": files, "imports": "my_lib,other_lib"},
{"files": files, "imports": "my_lib,other_lib", "config_file": None},
)

def test_list_value_comma_sep_string_pyproject_toml(self):
Expand All @@ -3257,11 +3260,11 @@ def test_list_value_comma_sep_string_pyproject_toml(self):
'[tool.autoflake]\nimports="my_lib,other_lib"\n',
)
files = [self.effective_path("test_me.py")]
args = Namespace(files=files)
args = Namespace(files=files, config_file=None)
assert autoflake.merge_configuration_file(args)
self.assert_namespace(
args,
{"files": files, "imports": "my_lib,other_lib"},
{"files": files, "imports": "my_lib,other_lib", "config_file": None},
)

def test_list_value_setup_cfg(self):
Expand All @@ -3271,11 +3274,11 @@ def test_list_value_setup_cfg(self):
"[autoflake]\nimports=my_lib,other_lib\n",
)
files = [self.effective_path("test_me.py")]
args = Namespace(files=files)
args = Namespace(files=files, config_file=None)
assert autoflake.merge_configuration_file(args)
self.assert_namespace(
args,
{"files": files, "imports": "my_lib,other_lib"},
{"files": files, "imports": "my_lib,other_lib", "config_file": None},
)

def test_unknown_property(self):
Expand All @@ -3285,7 +3288,7 @@ def test_unknown_property(self):
"[tool.autoflake]\nunknown_prop=true\n",
)
files = [self.effective_path("test_me.py")]
args = Namespace(files=files)
args = Namespace(files=files, config_file=None)
assert not autoflake.merge_configuration_file(args)

def test_non_bool_value_for_bool_property(self):
Expand All @@ -3295,7 +3298,7 @@ def test_non_bool_value_for_bool_property(self):
'[tool.autoflake]\nexpand-star-imports="invalid"\n',
)
files = [self.effective_path("test_me.py")]
args = Namespace(files=files)
args = Namespace(files=files, config_file=None)
assert not autoflake.merge_configuration_file(args)

def test_non_bool_value_for_bool_property_in_setup_cfg(self):
Expand All @@ -3305,7 +3308,7 @@ def test_non_bool_value_for_bool_property_in_setup_cfg(self):
"[autoflake]\nexpand-star-imports=ok\n",
)
files = [self.effective_path("test_me.py")]
args = Namespace(files=files)
args = Namespace(files=files, config_file=None)
assert not autoflake.merge_configuration_file(args)

def test_non_list_value_for_list_property(self):
Expand All @@ -3315,7 +3318,7 @@ def test_non_list_value_for_list_property(self):
"[tool.autoflake]\nexclude=true\n",
)
files = [self.effective_path("test_me.py")]
args = Namespace(files=files)
args = Namespace(files=files, config_file=None)
assert not autoflake.merge_configuration_file(args)

def test_merge_with_cli_set_list_property(self):
Expand All @@ -3325,11 +3328,11 @@ def test_merge_with_cli_set_list_property(self):
'[tool.autoflake]\nimports=["my_lib"]\n',
)
files = [self.effective_path("test_me.py")]
args = Namespace(files=files, imports="other_lib")
args = Namespace(files=files, imports="other_lib", config_file=None)
assert autoflake.merge_configuration_file(args)
self.assert_namespace(
args,
{"files": files, "imports": "my_lib,other_lib"},
{"files": files, "imports": "my_lib,other_lib", "config_file": None},
)


Expand Down

0 comments on commit f27565d

Please sign in to comment.