Skip to content

Commit 6a7de24

Browse files
authored
Fix args passed to check_config script (home-assistant#155885)
1 parent 67ccdd3 commit 6a7de24

File tree

2 files changed

+35
-17
lines changed

2 files changed

+35
-17
lines changed

homeassistant/scripts/check_config.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,9 @@ def run(script_args: list) -> int:
9090
help="Exit non-zero if warnings are present",
9191
)
9292

93-
args, unknown = parser.parse_known_args(script_args)
93+
# Parse all args including --config & --script. Do not use script_args.
94+
# Example: python -m homeassistant --config "." --script check_config
95+
args, unknown = parser.parse_known_args()
9496
if unknown:
9597
print(color("red", "Unknown arguments:", ", ".join(unknown)))
9698

tests/scripts/test_check_config.py

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ def test_run_json_flag_only() -> None:
190190
with (
191191
patch("builtins.print") as mock_print,
192192
patch.object(check_config, "check") as mock_check,
193+
patch("sys.argv", ["", "--json"]),
193194
):
194195
mock_check.return_value = {
195196
"except": {"domain1": ["error1", "error2"]},
@@ -200,7 +201,7 @@ def test_run_json_flag_only() -> None:
200201
"yaml_files": {},
201202
}
202203

203-
exit_code = check_config.run(["--json"])
204+
exit_code = check_config.run(None)
204205

205206
# Should exit with code 1 (1 domain with errors)
206207
assert exit_code == 1
@@ -233,7 +234,10 @@ def test_run_json_flag_only() -> None:
233234
def test_run_fail_on_warnings_flag_only() -> None:
234235
"""Test that --fail-on-warnings flag works independently."""
235236
# Test with warnings only
236-
with patch.object(check_config, "check") as mock_check:
237+
with (
238+
patch.object(check_config, "check") as mock_check,
239+
patch("sys.argv", ["", "--fail-on-warnings"]),
240+
):
237241
mock_check.return_value = {
238242
"except": {},
239243
"warn": {"light": ["warning message"]},
@@ -243,7 +247,7 @@ def test_run_fail_on_warnings_flag_only() -> None:
243247
"yaml_files": {},
244248
}
245249

246-
exit_code = check_config.run(["--fail-on-warnings"])
250+
exit_code = check_config.run(None)
247251
assert exit_code == 1 # Should exit non-zero due to warnings
248252

249253
# Test with no warnings or errors
@@ -282,6 +286,7 @@ def test_run_json_output_structure() -> None:
282286
with (
283287
patch("builtins.print") as mock_print,
284288
patch.object(check_config, "check") as mock_check,
289+
patch("sys.argv", ["", "--json", "--config", "/test/path"]),
285290
):
286291
mock_check.return_value = {
287292
"except": {"domain1": ["error1", {"config": "bad"}]},
@@ -292,7 +297,7 @@ def test_run_json_output_structure() -> None:
292297
"yaml_files": {},
293298
}
294299

295-
exit_code = check_config.run(["--json", "--config", "/test/path"])
300+
exit_code = check_config.run(None)
296301

297302
json_output = mock_print.call_args[0][0]
298303
parsed_json = json.loads(json_output)
@@ -413,7 +418,11 @@ def test_run_exit_code_logic() -> None:
413418
]
414419

415420
for errors, warnings, flags, expected_exit in test_cases:
416-
with patch("builtins.print"), patch.object(check_config, "check") as mock_check:
421+
with (
422+
patch("builtins.print"),
423+
patch.object(check_config, "check") as mock_check,
424+
patch("sys.argv", ["", *flags]),
425+
):
417426
mock_check.return_value = {
418427
"except": errors,
419428
"warn": warnings,
@@ -423,7 +432,7 @@ def test_run_exit_code_logic() -> None:
423432
"yaml_files": {},
424433
}
425434

426-
exit_code = check_config.run(flags)
435+
exit_code = check_config.run(None)
427436
assert exit_code == expected_exit, (
428437
f"Failed for errors={errors}, warnings={warnings}, flags={flags}. "
429438
f"Expected {expected_exit}, got {exit_code}"
@@ -447,7 +456,7 @@ def test_run_human_readable_still_works() -> None:
447456
"yaml_files": {},
448457
}
449458

450-
check_config.run([])
459+
check_config.run(None)
451460

452461
# Should print the "Testing configuration at" message
453462
printed_outputs = [
@@ -463,9 +472,11 @@ def test_run_human_readable_still_works() -> None:
463472

464473
def test_run_with_config_path() -> None:
465474
"""Test that config path is correctly included in JSON output."""
475+
test_config_path = "/custom/config/path"
466476
with (
467477
patch("builtins.print") as mock_print,
468478
patch.object(check_config, "check") as mock_check,
479+
patch("sys.argv", ["", "--json", "--config", test_config_path]),
469480
):
470481
mock_check.return_value = {
471482
"except": {},
@@ -476,8 +487,7 @@ def test_run_with_config_path() -> None:
476487
"yaml_files": {},
477488
}
478489

479-
test_config_path = "/custom/config/path"
480-
check_config.run(["--json", "--config", test_config_path])
490+
check_config.run(None)
481491

482492
json_output = mock_print.call_args[0][0]
483493
parsed_json = json.loads(json_output)
@@ -495,6 +505,7 @@ def test_unknown_arguments_with_json() -> None:
495505
with (
496506
patch("builtins.print") as mock_print,
497507
patch.object(check_config, "check") as mock_check,
508+
patch("sys.argv", ["", "--json", "--unknown-flag", "value"]),
498509
):
499510
mock_check.return_value = {
500511
"except": {},
@@ -505,7 +516,7 @@ def test_unknown_arguments_with_json() -> None:
505516
"yaml_files": {},
506517
}
507518

508-
check_config.run(["--json", "--unknown-flag", "value"])
519+
check_config.run(None)
509520

510521
# Should still print unknown argument warning AND JSON
511522
assert mock_print.call_count == 2
@@ -528,6 +539,7 @@ def test_info_flag_with_json() -> None:
528539
with (
529540
patch("builtins.print") as mock_print,
530541
patch.object(check_config, "check") as mock_check,
542+
patch("sys.argv", ["", "--json", "--info", "light"]),
531543
):
532544
mock_check.return_value = {
533545
"except": {},
@@ -539,7 +551,7 @@ def test_info_flag_with_json() -> None:
539551
}
540552

541553
# Test --json with --info - JSON should take precedence
542-
exit_code = check_config.run(["--json", "--info", "light"])
554+
exit_code = check_config.run(None)
543555

544556
assert exit_code == 0
545557
assert mock_print.call_count == 1
@@ -564,6 +576,7 @@ def test_config_flag_variations() -> None:
564576
with (
565577
patch("builtins.print") as mock_print,
566578
patch.object(check_config, "check") as mock_check,
579+
patch("sys.argv", ["", *flags]),
567580
):
568581
mock_check.return_value = {
569582
"except": {},
@@ -574,7 +587,7 @@ def test_config_flag_variations() -> None:
574587
"yaml_files": {},
575588
}
576589

577-
check_config.run(flags)
590+
check_config.run(None)
578591

579592
if "--json" in flags:
580593
json_output = json.loads(mock_print.call_args[0][0])
@@ -587,6 +600,10 @@ def test_multiple_config_flags() -> None:
587600
with (
588601
patch("builtins.print") as mock_print,
589602
patch.object(check_config, "check") as mock_check,
603+
patch(
604+
"sys.argv",
605+
["", "--json", "--config", "/first/path", "--config", "/second/path"],
606+
),
590607
):
591608
mock_check.return_value = {
592609
"except": {},
@@ -598,9 +615,7 @@ def test_multiple_config_flags() -> None:
598615
}
599616

600617
# Last config flag should win
601-
check_config.run(
602-
["--json", "--config", "/first/path", "--config", "/second/path"]
603-
)
618+
check_config.run(None)
604619

605620
json_output = json.loads(mock_print.call_args[0][0])
606621
expected_path = os.path.join(os.getcwd(), "/second/path")
@@ -622,6 +637,7 @@ def test_fail_on_warnings_with_json_combinations() -> None:
622637
with (
623638
patch("builtins.print") as mock_print,
624639
patch.object(check_config, "check") as mock_check,
640+
patch("sys.argv", ["", "--json", "--fail-on-warnings"]),
625641
):
626642
mock_check.return_value = {
627643
"except": errors,
@@ -632,7 +648,7 @@ def test_fail_on_warnings_with_json_combinations() -> None:
632648
"yaml_files": {},
633649
}
634650

635-
exit_code = check_config.run(["--json", "--fail-on-warnings"])
651+
exit_code = check_config.run(None)
636652
assert exit_code == expected_exit
637653

638654
# Should still output valid JSON

0 commit comments

Comments
 (0)