def test_defaults_not_list_exception() -> None: config_loader = ConfigLoaderImpl( config_search_path=create_config_search_path( "hydra/test_utils/configs")) with pytest.raises(ValueError): config_loader.load_configuration( config_name="defaults_not_list.yaml", overrides=[], strict=False, run_mode=RunMode.RUN, )
def test_apply_overrides_to_config( input_cfg: Any, strict: bool, overrides: List[str], expected: Any ) -> None: cfg = OmegaConf.create(input_cfg) OmegaConf.set_struct(cfg, strict) if isinstance(expected, dict): ConfigLoaderImpl._apply_overrides_to_config(overrides=overrides, cfg=cfg) assert cfg == expected else: with expected: ConfigLoaderImpl._apply_overrides_to_config(overrides=overrides, cfg=cfg)
def test_load_changing_group_and_package_in_default( self, path: str, overrides: List[str], expected: Any) -> None: config_loader = ConfigLoaderImpl( config_search_path=create_config_search_path( f"{path}/package_tests")) cfg = config_loader.load_configuration(config_name="pkg_override", overrides=overrides, run_mode=RunMode.RUN) with open_dict(cfg): del cfg["hydra"] assert cfg == expected
def test_assign_null(self, hydra_restore_singletons: Any, path: str, recwarn: Any) -> None: config_loader = ConfigLoaderImpl( config_search_path=create_config_search_path(path)) cfg = config_loader.load_configuration(config_name="config.yaml", overrides=["abc=null"], run_mode=RunMode.RUN) with open_dict(cfg): del cfg["hydra"] assert cfg == {"normal_yaml_config": True, "abc": None} assert len(recwarn) == 0
def test_load_changing_group_in_default(self, path: str) -> None: config_loader = ConfigLoaderImpl( config_search_path=create_config_search_path(path)) cfg = config_loader.load_configuration( config_name="optional-default.yaml", overrides=["group1=file2"], strict=False, ) with open_dict(cfg): del cfg["hydra"] assert cfg == {"foo": 20}
def test_complex_defaults(overrides: Any, expected: Any) -> None: config_loader = ConfigLoaderImpl( config_search_path=create_config_search_path( "tests/test_apps/sweep_complex_defaults/conf" ) ) cfg = config_loader.load_configuration(config_name="config", overrides=overrides) with open_dict(cfg): del cfg["hydra"] assert cfg == expected
def test_load_configuration(self, path: str) -> None: config_loader = ConfigLoaderImpl( config_search_path=create_config_search_path(path)) cfg = config_loader.load_configuration( config_name="config.yaml", overrides=["+abc=123"], run_mode=RunMode.RUN, ) with open_dict(cfg): del cfg["hydra"] assert cfg == {"normal_yaml_config": True, "abc": 123}
def test_override_compose_two_package_one_group(self, path: str, overrides: List[str], expected: Any) -> None: config_loader = ConfigLoaderImpl( config_search_path=create_config_search_path( f"{path}/package_tests")) cfg = config_loader.load_configuration( config_name="two_packages_one_group", overrides=overrides) with open_dict(cfg): del cfg["hydra"] assert cfg == expected
def test_compose_file_with_dot(self, path: str) -> None: config_loader = ConfigLoaderImpl( config_search_path=create_config_search_path(path)) cfg = config_loader.load_configuration( config_name="compose.yaml", overrides=["group1=abc.cde"], run_mode=RunMode.RUN, ) with open_dict(cfg): del cfg["hydra"] assert cfg == {"abc=cde": None, "bar": 100}
def test_override_hydra_config_value_from_config_file() -> None: config_loader = ConfigLoaderImpl( config_search_path=create_config_search_path( "hydra/test_utils/configs")) cfg = config_loader.load_configuration( config_name="overriding_output_dir.yaml", overrides=[], run_mode=RunMode.RUN, ) assert cfg.hydra.run.dir == "foo"
def test_load_adding_group_not_in_default(self, path: str) -> None: config_loader = ConfigLoaderImpl( config_search_path=create_config_search_path(path)) cfg = config_loader.load_configuration( config_name="optional-default.yaml", overrides=["+group2=file1"], run_mode=RunMode.RUN, ) with open_dict(cfg): del cfg["hydra"] assert cfg == {"foo": 10, "bar": 100}
def test_load_with_missing_default(self, path: str) -> None: config_loader = ConfigLoaderImpl( config_search_path=create_config_search_path(path) ) with pytest.raises(MissingConfigException): config_loader.load_configuration( config_name="missing-default.yaml", overrides=[], strict=False, run_mode=RunMode.RUN, )
def test_change_run_dir_with_config(self, path: str) -> None: config_loader = ConfigLoaderImpl( config_search_path=create_config_search_path(path) ) cfg = config_loader.load_configuration( config_name="overriding_run_dir.yaml", overrides=[], strict=False, run_mode=RunMode.RUN, ) assert cfg.hydra.run.dir == "cde"
def test_load_adding_group_not_in_default(self, path: str) -> None: config_loader = ConfigLoaderImpl( config_search_path=create_config_search_path(path) ) cfg = config_loader.load_configuration( config_file="optional-default.yaml", overrides=["group2=file1"], strict=False, ) del cfg["hydra"] assert cfg == dict(foo=10, bar=100)
def test_load_yml_file(self, path: str) -> None: config_loader = ConfigLoaderImpl( config_search_path=create_config_search_path(path) ) cfg = config_loader.load_configuration( config_name="config.yml", overrides=[], strict=False ) with open_dict(cfg): del cfg["hydra"] assert cfg == {"yml_file_here": True}
def test_load_with_optional_default(self, path: str) -> None: config_loader = ConfigLoaderImpl( config_search_path=create_config_search_path(path)) cfg = config_loader.load_configuration( config_name="optional-default.yaml", overrides=[], strict=False, run_mode=RunMode.RUN, ) with open_dict(cfg): del cfg["hydra"] assert cfg == {"foo": 10}
def test_overriding_with_dict(config: str, overrides: Any, expected: Any) -> None: config_loader = ConfigLoaderImpl( config_search_path=create_config_search_path( "tests/test_apps/app_with_cfg_groups/conf"), ) cfg = config_loader.load_configuration(config_name=config, overrides=overrides, run_mode=RunMode.RUN) with open_dict(cfg): del cfg["hydra"] assert cfg == expected
def test_override_with_equals(self, path: str) -> None: config_loader = ConfigLoaderImpl( config_search_path=create_config_search_path(path)) cfg = config_loader.load_configuration(config_name="config.yaml", overrides=["abc='cde=12'"], strict=False) with open_dict(cfg): del cfg["hydra"] assert cfg == OmegaConf.create({ "normal_yaml_config": True, "abc": "cde=12" })
def test_load_history(self, path: str) -> None: config_loader = ConfigLoaderImpl( config_search_path=create_config_search_path(path)) config_loader.load_configuration( config_name="missing-optional-default.yaml", overrides=[], strict=False) expected = hydra_load_list.copy() expected.append(("missing-optional-default.yaml", path, "main", None)) expected.append(("foo/missing", None, None, None)) assert config_loader.get_load_history() == expected
def test_load_history_with_basic_launcher(self, path: str) -> None: config_loader = ConfigLoaderImpl( config_search_path=create_config_search_path(path)) config_loader.load_configuration( config_name="custom_default_launcher.yaml", overrides=["hydra/launcher=basic"], strict=False, ) expected = hydra_load_list.copy() expected.append(("custom_default_launcher.yaml", path, "main", None)) assert config_loader.get_load_history() == expected
def test_invalid_plugin_merge(restore_singletons: Any) -> Any: cs = ConfigStore.instance() cs.store(name="config", node=Config) cs.store(group_path="plugin", name="invalid", node=InvalidPlugin, node_root="plugin") cl = ConfigLoaderImpl(config_search_path=create_config_search_path(None)) with pytest.raises(HydraException): cl.load_configuration(config_name="config", overrides=["plugin=invalid"])
def test_load_changing_group_in_default(self, path: str, override: str, expected: Dict[Any, Any]) -> None: config_loader = ConfigLoaderImpl( config_search_path=create_config_search_path(path)) cfg = config_loader.load_configuration( config_name="optional-default", overrides=[override], strict=False, run_mode=RunMode.RUN, ) with open_dict(cfg): del cfg["hydra"] assert cfg == expected
def test_default_removal(config_file: str, overrides: List[str]) -> None: config_loader = ConfigLoaderImpl( config_search_path=create_config_search_path("hydra/test_utils/configs") ) config_loader.load_configuration( config_name=config_file, overrides=overrides, strict=False ) expected = list( filter(lambda x: x[0] != "hydra/launcher/basic", hydra_load_list.copy()) ) expected.extend([(config_file, "file://hydra/test_utils/configs", "main", None)]) assert config_loader.get_load_history() == expected
def test_foo(restore_singletons: Any) -> Any: utils.setup_globals() config_loader = ConfigLoaderImpl( config_search_path=create_config_search_path( "pkg://hydra.test_utils.configs")) cfg = config_loader.load_configuration( config_name="accessing_hydra_config", overrides=[]) HydraConfig.instance().set_config(cfg) with open_dict(cfg): del cfg["hydra"] assert cfg.job_name == "UNKNOWN_NAME" assert cfg.config_name == "accessing_hydra_config"
def test_mixed_composition_order() -> None: """ Tests that the order of mixed composition (defaults contains both config group and non config group items) is correct """ config_loader = ConfigLoaderImpl( config_search_path=create_config_search_path("hydra/test_utils/configs") ) cfg = config_loader.load_configuration( config_name="mixed_compose.yaml", overrides=[], strict=False, run_mode=RunMode.RUN, ) expected = deepcopy(hydra_load_list) expected.extend( [ LoadTrace( config_path="some_config", package="", parent="mixed_compose.yaml", search_path="file://hydra/test_utils/configs", provider="main", ), LoadTrace( config_path="group1/file1", package="", parent="mixed_compose.yaml", search_path="file://hydra/test_utils/configs", provider="main", ), LoadTrace( config_path="config", package="", parent="mixed_compose.yaml", search_path="file://hydra/test_utils/configs", provider="main", ), LoadTrace( config_path="mixed_compose.yaml", package="", parent="<root>", is_self=True, search_path="file://hydra/test_utils/configs", provider="main", ), ] ) assert_same_composition_trace(cfg.hydra.composition_trace, expected)
def test_load_config_file_with_schema_validation( self, hydra_restore_singletons: Any, path: str ) -> None: with ConfigStoreWithProvider("this_test") as cs: cs.store(name="config", node=TopLevelConfig) cs.store(group="db", name="mysql", node=MySQLConfig, package="db") config_loader = ConfigLoaderImpl( config_search_path=create_config_search_path(path) ) cfg = config_loader.load_configuration( config_name="config", overrides=["+db=mysql"], strict=False, run_mode=RunMode.RUN, ) expected = deepcopy(hydra_load_list) expected.extend( [ LoadTrace( config_path="config", package="", parent="<root>", search_path=path, provider="main", ), LoadTrace( config_path="db/mysql", package="db", parent="<root>", search_path=path, provider="main", ), ] ) assert_same_composition_trace(cfg.hydra.composition_trace, expected) with open_dict(cfg): del cfg["hydra"] assert cfg == { "normal_yaml_config": True, "db": { "driver": "mysql", "host": "???", "port": "???", "user": "******", "password": "******", }, }
def test_apply_overrides_to_config(input_cfg: Any, overrides: List[str], expected: Any) -> None: cfg = OmegaConf.create(input_cfg) OmegaConf.set_struct(cfg, True) parser = OverridesParser.create() parsed = parser.parse_overrides(overrides=overrides) if isinstance(expected, dict): ConfigLoaderImpl._apply_overrides_to_config(overrides=parsed, cfg=cfg) assert cfg == expected else: with expected: ConfigLoaderImpl._apply_overrides_to_config(overrides=parsed, cfg=cfg)
def test_delete_by_assigning_null_is_deprecated() -> None: msg = ("\nRemoving from the defaults list by assigning 'null' " "is deprecated and will be removed in Hydra 1.1." "\nUse ~db") defaults = ConfigLoaderImpl._parse_defaults( OmegaConf.create({"defaults": [{ "db": "mysql" }]})) parsed_overrides = [ConfigLoaderImpl._parse_override("db=null")] with pytest.warns(expected_warning=UserWarning, match=re.escape(msg)): assert parsed_overrides[0].override.is_delete() ConfigLoaderImpl._apply_overrides_to_defaults( overrides=parsed_overrides, defaults=defaults) assert defaults == []
def test_setup_plugins( monkeypatch: Any, plugin: Union[Launcher, Sweeper], config: DictConfig ) -> None: task_function = Mock(spec=TaskFunction) config_loader = ConfigLoaderImpl(config_search_path=create_config_search_path(None)) hydra_context = HydraContext(config_loader=config_loader, callbacks=Callbacks()) plugin_instance = Plugins.instance() monkeypatch.setattr(Plugins, "check_usage", lambda _: None) monkeypatch.setattr(plugin_instance, "_instantiate", lambda _: plugin) msg = dedent( """ Plugin's setup() signature has changed in Hydra 1.1. Support for the old style will be removed in Hydra 1.2. For more info, check https://github.com/facebookresearch/hydra/pull/1581.""" ) with warns(expected_warning=UserWarning, match=re.escape(msg)): if isinstance(plugin, Launcher): Plugins.instance().instantiate_launcher( task_function=task_function, config=config, config_loader=config_loader, hydra_context=hydra_context, ) else: Plugins.instance().instantiate_sweeper( hydra_context=hydra_context, task_function=task_function, config=config, )
def test_default_removal(config_file: str, overrides: List[str]) -> None: config_loader = ConfigLoaderImpl( config_search_path=create_config_search_path("hydra/test_utils/configs") ) config_loader.load_configuration( config_file=config_file, overrides=overrides, strict=False ) assert config_loader.get_load_history() == [ ("hydra.yaml", "pkg://hydra.conf", "hydra"), ("hydra/hydra_logging/default.yaml", "pkg://hydra.conf", "hydra"), ("hydra/job_logging/default.yaml", "pkg://hydra.conf", "hydra"), ("hydra/sweeper/basic.yaml", "pkg://hydra.conf", "hydra"), ("hydra/output/default.yaml", "pkg://hydra.conf", "hydra"), ("hydra/help/default.yaml", "pkg://hydra.conf", "hydra"), ("hydra/hydra_help/default.yaml", "pkg://hydra.conf", "hydra"), (config_file, "file://hydra/test_utils/configs", "main"), ]