def test_program_config_default_option_ignored(tmpdir, mod_config0):
    pack0 = __import__("pack0")
    reload(pack0)
    assert not pack0.CONF.configured()
    program_config = ProgramConfig(pack0.comp1.mod0, pack0.comp1.mod1, default_option=Any())
    tmpfile0 = tmpdir.join("pverr_0.zirkon").strpath
    mod_config0 = mod_config0.copy()
    mod_config0["unexpected_opt"] = 100
    mod_config0.write(tmpfile0, fmt="zirkon")
    program_config.parse_args(["--config", tmpfile0], raise_on_error=True)
    assert pack0.comp1.mod0.get_opt0() == "abc"
    assert pack0.comp1.mod0.get_opt1() == 10
    assert pack0.comp1.mod1.get_opt0() == 5.2
def test_program_config_existing_parser(tmpdir, mod_config0, mod_config1):
    pack0 = __import__("pack0")
    reload(pack0)
    assert not pack0.CONF.configured()
    parser = argparse.ArgumentParser()
    parser.add_argument(
        "--max", "-m",
        type=int,
        default=12,
        help="max value")
    program_config = ProgramConfig(pack0.comp1.mod0, pack0.comp1.mod1)
    tmpfile0 = tmpdir.join("pexparser_0.zirkon").strpath
    mod_config0.write(tmpfile0, fmt="zirkon")
    tmpfile1 = tmpdir.join("pexparser_1.zirkon").strpath
    mod_config1.write(tmpfile1, fmt="zirkon")
    config, validation_result, namespace = program_config.parse_args(
        args=["--config", tmpfile1, "--config", tmpfile0, "--mod0-opt0=wow", "--mod1-opt0", "9.97", "--max=18"],
        argparser=parser)

    print("--- config:")
    config.dump()
    print("--- validation_result:")
    validation_result.dump()
    print("--- namespace:", namespace)

    assert namespace_len(namespace) == 1
    assert namespace.max == 18
    assert pack0.comp1.mod0.get_opt0() == "wow"
    assert pack0.comp1.mod0.get_opt1() == 80
    assert pack0.comp1.mod1.get_opt0() == 9.97
def test_program_config_default_option(tmpdir, mod_config0, raise_on_error):
    pack0 = __import__("pack0")
    reload(pack0)
    assert not pack0.CONF.configured()
    program_config = ProgramConfig(pack0.comp1.mod0, pack0.comp1.mod1)
    tmpfile0 = tmpdir.join("pverr_0.zirkon").strpath
    mod_config0 = mod_config0.copy()
    mod_config0["unexpected_opt"] = 100
    mod_config0.write(tmpfile0, fmt="zirkon")
    if raise_on_error:
        with pytest.raises(UnexpectedOptionError) as exc_info:
            program_config.parse_args(["--config", tmpfile0], raise_on_error=raise_on_error)
        assert str(exc_info.value) == "unexpected_opt=100: unexpected option"
    else:
        config, validation_result, namespace = program_config.parse_args(["--config", tmpfile0], raise_on_error=raise_on_error)
        assert validation_result
        assert 'unexpected_opt' in validation_result
def test_program_config_validation_error(tmpdir, mod_config0, raise_on_error):
    pack0 = __import__("pack0")
    reload(pack0)
    assert not pack0.CONF.configured()
    program_config = ProgramConfig(pack0.comp1.mod0, pack0.comp1.mod1)
    tmpfile0 = tmpdir.join("pverr_0.zirkon").strpath
    mod_config0 = mod_config0.copy()
    mod_config0["mod0"]["opt0"] = 7.5
    mod_config0.write(tmpfile0, fmt="zirkon")
    if raise_on_error:
        with pytest.raises(InvalidTypeError) as exc_info:
            program_config.parse_args(["--config", tmpfile0], raise_on_error=raise_on_error)
        assert str(exc_info.value) == "mod0.opt0=7.5: invalid type float - expected type is str"
    else:
        config, validation_result, namespace = program_config.parse_args(["--config", tmpfile0], raise_on_error=raise_on_error)
        assert validation_result
        assert 'opt0' in validation_result['mod0']
def test_program_config_package(tmpdir, pack0_config0):
    pack0 = __import__("pack0")
    reload(pack0)
    assert not pack0.CONF.configured()
    program_config = ProgramConfig(pack0)
    tmpfile0 = tmpdir.join("ppack_0.zirkon").strpath
    pack0_config0.write(tmpfile0, fmt="zirkon")
    program_config.parse_args(["--config", tmpfile0], raise_on_error=True)

    assert pack0.get_url() == "sqlite://x.y.z"
    assert pack0.get_max_workers() == 7
    assert pack0.get_max_delay() == 9.78
    assert pack0.comp0.get_opt0() == 20
    assert pack0.comp0.get_opt1() == 30
    assert pack0.comp0.get_opt2() == False
    assert pack0.comp1.get_opt0() == 555555
    assert pack0.comp1.get_opt2() == True
    assert pack0.comp1.mod0.get_opt0() == "abc"
    assert pack0.comp1.mod1.get_opt0() == 2003.5
    assert pack0.comp1.mod2.get_opt0() == 3003.5
    assert pack0.comp2.mod0.get_opt0() == 4003.5
def test_program_config_command_line(tmpdir, mod_config0, mod_config1):
    pack0 = __import__("pack0")
    reload(pack0)
    assert not pack0.CONF.configured()
    program_config = ProgramConfig(pack0.comp1.mod0, pack0.comp1.mod1)
    tmpfile0 = tmpdir.join("pcommand_line_0.zirkon").strpath
    mod_config0.write(tmpfile0, fmt="zirkon")
    tmpfile1 = tmpdir.join("pcommand_line_1.zirkon").strpath
    mod_config1.write(tmpfile1, fmt="zirkon")
    config, validation_result, namespace = program_config.parse_args(["--config", tmpfile1, "--config", tmpfile0, "--mod0-opt0=wow", "--mod1-opt0", "9.97"])

    print("--- config:")
    config.dump()
    print("--- validation_result:")
    validation_result.dump()
    print("--- namespace:", namespace)

    assert namespace_len(namespace) == 0
    assert pack0.comp1.mod0.get_opt0() == "wow"
    assert pack0.comp1.mod0.get_opt1() == 80
    assert pack0.comp1.mod1.get_opt0() == 9.97