Skip to content

Commit 28a58ac

Browse files
authored
Merge pull request #721 from powerapi-ng/test/fix-deadlock-forkserver
test(unit): Fix deadlock caused by `sys.argv` changes under `forkserver`
2 parents 396e0b0 + 92ddd49 commit 28a58ac

File tree

2 files changed

+23
-50
lines changed

2 files changed

+23
-50
lines changed

tests/unit/cli/conftest.py

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -485,27 +485,19 @@ def test_files_path():
485485

486486

487487
@pytest.fixture
488-
def cli_configuration(config_file: str):
488+
def cli_configuration(config_file: str, monkeypatch):
489489
"""
490490
Load in sys.argv a configuration with arguments extracted from a json file
491491
"""
492-
sys.argv = generate_cli_configuration_from_json_file(file_name=config_file)
493-
494-
yield None
495-
496-
sys.argv = []
492+
monkeypatch.setattr(sys, 'argv', generate_cli_configuration_from_json_file(file_name=config_file))
497493

498494

499495
@pytest.fixture
500-
def empty_cli_configuration():
496+
def empty_cli_configuration(monkeypatch):
501497
"""
502498
Clean the CLI arguments
503499
"""
504-
sys.argv = []
505-
506-
yield None
507-
508-
sys.argv = []
500+
monkeypatch.setattr(sys, 'argv', [])
509501

510502

511503
@pytest.fixture

tests/unit/cli/test_parsing_manager.py

Lines changed: 19 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -857,7 +857,7 @@ def test_parsing_environment_variables_with_unknown_argument_terminate_execution
857857

858858

859859
def test_parsing_environment_variables_with_long_and_short_names_for_arguments_in_root_parsing_manager(
860-
root_config_parsing_manager_with_mandatory_and_optional_arguments, test_files_path):
860+
empty_cli_configuration, root_config_parsing_manager_with_mandatory_and_optional_arguments, test_files_path):
861861
"""
862862
Test that a configuration defined via environment variables with long and short names for arguments is correctly
863863
parsed
@@ -882,7 +882,7 @@ def test_parsing_environment_variables_with_long_and_short_names_for_arguments_i
882882

883883

884884
def test_parsing_environment_variables_with_no_argument_with_default_value_in_root_parsing_manager(
885-
root_config_parsing_manager_with_mandatory_and_optional_arguments):
885+
empty_cli_configuration, root_config_parsing_manager_with_mandatory_and_optional_arguments):
886886
"""
887887
Test that the parsing of a configuration defined via environment variables missing arguments with
888888
default values results in a dict with the default values for those arguments
@@ -934,12 +934,12 @@ def test_configuration_priority_between_cli_and_environment_variables_in_root_pa
934934
def test_configuration_priority_between_cli_and_configuration_file_in_root_parsing_manager(config_file,
935935
cli_configuration,
936936
root_config_parsing_manager_with_mandatory_and_optional_arguments,
937-
test_files_path):
937+
test_files_path,
938+
monkeypatch):
938939
"""
939940
Test that arguments values defined via the CLI are preserved regarding values defined via a configuration file
940941
"""
941-
sys.argv.append('--config-file')
942-
sys.argv.append(test_files_path + '/root_manager_basic_configuration.json')
942+
monkeypatch.setattr(sys, 'argv', [*sys.argv, '--config-file', test_files_path + '/root_manager_basic_configuration.json'])
943943

944944
expected_dict = load_configuration_from_json_file(config_file)
945945
expected_dict["arg5"] = "this is a value" # This value is not defined by the CLI but it has to be present
@@ -950,8 +950,8 @@ def test_configuration_priority_between_cli_and_configuration_file_in_root_parsi
950950

951951

952952
def test_configuration_priority_between_environment_variables_and_configuration_file_in_root_parsing_manager(
953-
root_config_parsing_manager_with_mandatory_and_optional_arguments,
954-
test_files_path):
953+
empty_cli_configuration, root_config_parsing_manager_with_mandatory_and_optional_arguments,
954+
test_files_path, monkeypatch):
955955
"""
956956
Test that arguments values defined via environment variables are preserved regarding values defined via
957957
a configuration file
@@ -964,9 +964,7 @@ def test_configuration_priority_between_environment_variables_and_configuration_
964964
group_arguments_prefix=root_config_parsing_manager_with_mandatory_and_optional_arguments.cli_parser.
965965
get_groups_prefixes())
966966

967-
sys.argv = []
968-
sys.argv.append('--config-file')
969-
sys.argv.append(test_files_path + '/root_manager_basic_configuration.json')
967+
monkeypatch.setattr(sys, 'argv', [*sys.argv, '--config-file', test_files_path + '/root_manager_basic_configuration.json'])
970968

971969
expected_dict = load_configuration_from_json_file(config_file_environment_variables)
972970
expected_dict["arg5"] = "this is a value" # This value is not defined by the CLI but it has to be present
@@ -975,15 +973,13 @@ def test_configuration_priority_between_environment_variables_and_configuration_
975973

976974
assert result == expected_dict
977975

978-
sys.argv = []
979-
980976
remove_environment_variables_configuration(variables_names=created_environment_variables)
981977

982978

983979
@pytest.mark.parametrize('config_file', ['root_manager_basic_configuration_with_no_argument_with_default_value.json'])
984980
def test_configuration_priority_between_cli_environment_variables_and_configuration_file_in_root_parsing_manager(
985981
config_file, cli_configuration, root_config_parsing_manager_with_mandatory_and_optional_arguments,
986-
test_files_path):
982+
test_files_path, monkeypatch):
987983
"""
988984
Test the following argument definition priority:
989985
1. CLI
@@ -999,8 +995,7 @@ def test_configuration_priority_between_cli_environment_variables_and_configurat
999995
group_arguments_prefix=root_config_parsing_manager_with_mandatory_and_optional_arguments.cli_parser.
1000996
get_groups_prefixes())
1001997

1002-
sys.argv.append('--config-file')
1003-
sys.argv.append(test_files_path + '/root_manager_basic_configuration.json')
998+
monkeypatch.setattr(sys, 'argv', [*sys.argv, '--config-file', test_files_path + '/root_manager_basic_configuration.json'])
1004999

10051000
expected_dict = load_configuration_from_json_file(config_file)
10061001
expected_dict["arg5"] = "this is a value 3"
@@ -1009,13 +1004,11 @@ def test_configuration_priority_between_cli_environment_variables_and_configurat
10091004

10101005
assert result == expected_dict
10111006

1012-
sys.argv = []
1013-
10141007
remove_environment_variables_configuration(variables_names=created_environment_variables)
10151008

10161009

10171010
def test_parsing_environment_variables_with_subgroups_in_root_parsing_manager(
1018-
root_config_parsing_manager_with_mandatory_and_optional_arguments, test_files_path):
1011+
empty_cli_configuration, root_config_parsing_manager_with_mandatory_and_optional_arguments, test_files_path):
10191012
"""
10201013
Test that a configuration defined via environment variables with subgroups is correctly parsed
10211014
"""
@@ -1037,7 +1030,7 @@ def test_parsing_environment_variables_with_subgroups_in_root_parsing_manager(
10371030

10381031

10391032
def test_parsing_environment_variables_with_subgroups_and_long_and_short_names_in_root_parsing_manager(
1040-
root_config_parsing_manager_with_mandatory_and_optional_arguments, test_files_path):
1033+
empty_cli_configuration, root_config_parsing_manager_with_mandatory_and_optional_arguments, test_files_path):
10411034
"""
10421035
Test that a configuration defined via environment variables with subgroups is correctly parsed
10431036
"""
@@ -1084,7 +1077,7 @@ def test_parsing_environment_variables_with_subgroups_and_unknown_arguments_term
10841077

10851078

10861079
def test_parsing_environment_variables_with_subgroups_and_no_arguments_with_default_value_in_root_parsing_manager(
1087-
root_config_parsing_manager_with_mandatory_and_optional_arguments, test_files_path):
1080+
empty_cli_configuration, root_config_parsing_manager_with_mandatory_and_optional_arguments, test_files_path):
10881081
"""
10891082
Test that a configuration defined via environment variables with subgroups without variables with default values
10901083
is correctly parsed
@@ -1136,7 +1129,7 @@ def test_parsing_environment_variables_with_subgroups_and_wrong_type_terminate_e
11361129
['root_manager_configuration_with_subgroups_and_no_argument_default_value.json'])
11371130
def test_config_priority_between_cli_environ_variables_and_configuration_file_with_subgroups_in_root_parsing_manager(
11381131
config_file, cli_configuration, root_config_parsing_manager_with_mandatory_and_optional_arguments,
1139-
test_files_path):
1132+
test_files_path, monkeypatch):
11401133
"""
11411134
Test the following argument definition priority for a configuration with subgroups:
11421135
1. CLI
@@ -1152,8 +1145,7 @@ def test_config_priority_between_cli_environ_variables_and_configuration_file_wi
11521145
group_arguments_prefix=root_config_parsing_manager_with_mandatory_and_optional_arguments.cli_parser.
11531146
get_groups_prefixes())
11541147

1155-
sys.argv.append('--config-file')
1156-
sys.argv.append(test_files_path + '/root_manager_configuration_with_subgroups.json')
1148+
monkeypatch.setattr(sys, 'argv', [*sys.argv, '--config-file', test_files_path + '/root_manager_configuration_with_subgroups.json'])
11571149

11581150
expected_dict = load_configuration_from_json_file(config_file)
11591151
expected_dict['input']['in1']['name'] = 'i1_name'
@@ -1164,8 +1156,6 @@ def test_config_priority_between_cli_environ_variables_and_configuration_file_wi
11641156

11651157
assert result == expected_dict
11661158

1167-
sys.argv = []
1168-
11691159
remove_environment_variables_configuration(variables_names=created_environment_variables)
11701160

11711161

@@ -1196,22 +1186,19 @@ def test_config_priority_between_cli_and_environ_variables_with_subgroups_in_roo
11961186

11971187
assert result == expected_dict
11981188

1199-
sys.argv = []
1200-
12011189
remove_environment_variables_configuration(variables_names=created_environment_variables)
12021190

12031191

12041192
@pytest.mark.parametrize('config_file',
12051193
['root_manager_configuration_with_subgroups_and_no_argument_default_value.json'])
12061194
def test_config_priority_between_cli_and_configuration_file_with_subgroups_in_root_parsing_manager(
12071195
config_file, cli_configuration, root_config_parsing_manager_with_mandatory_and_optional_arguments,
1208-
test_files_path):
1196+
test_files_path, monkeypatch):
12091197
"""
12101198
Test that arguments values defined via the CLI are preserved regarding values defined via a config file
12111199
with subgroups in configuration
12121200
"""
1213-
sys.argv.append('--config-file')
1214-
sys.argv.append(test_files_path + '/root_manager_configuration_with_subgroups.json')
1201+
monkeypatch.setattr(sys, 'argv', [*sys.argv, '--config-file', test_files_path + '/root_manager_configuration_with_subgroups.json'])
12151202

12161203
expected_dict = load_configuration_from_json_file(config_file)
12171204
expected_dict['input']['in1']['name'] = 'in1_name'
@@ -1222,12 +1209,10 @@ def test_config_priority_between_cli_and_configuration_file_with_subgroups_in_ro
12221209

12231210
assert result == expected_dict
12241211

1225-
sys.argv = []
1226-
12271212

12281213
def test_config_priority_between_environ_variables_and_configuration_file_with_subgroups_in_root_parsing_manager(
12291214
root_config_parsing_manager_with_mandatory_and_optional_arguments,
1230-
test_files_path):
1215+
test_files_path, monkeypatch):
12311216
"""
12321217
Test that arguments values defined via the environment variables are preserved regarding values defined via a config
12331218
file with subgroups in configuration
@@ -1241,9 +1226,7 @@ def test_config_priority_between_environ_variables_and_configuration_file_with_s
12411226
group_arguments_prefix=root_config_parsing_manager_with_mandatory_and_optional_arguments.cli_parser.
12421227
get_groups_prefixes())
12431228

1244-
sys.argv.append('--config-file')
1245-
1246-
sys.argv.append(test_files_path + '/root_manager_configuration_with_subgroups_and_long_and_short_names.json')
1229+
monkeypatch.setattr(sys, 'argv', ['--config-file', test_files_path + '/root_manager_configuration_with_subgroups_and_long_and_short_names.json'])
12471230

12481231
expected_dict = load_configuration_from_json_file(config_file_environment_variables)
12491232
expected_dict['input']['in1']['name'] = 'i1_name'
@@ -1254,8 +1237,6 @@ def test_config_priority_between_environ_variables_and_configuration_file_with_s
12541237

12551238
assert result == expected_dict
12561239

1257-
sys.argv = []
1258-
12591240
remove_environment_variables_configuration(variables_names=created_environment_variables)
12601241

12611242

0 commit comments

Comments
 (0)