Esempio n. 1
0
    def test_configfile__with_unbound_section(self, cli_runner_isolated):
        # -- DEFINITION: unbound section
        #   A config section without associated schema but that should be used.
        class ConfigFileProcessorWithUnboundSection(ConfigFileProcessor2):
            config_sections = ["unbound.section"] + \
                    ConfigFileProcessor2.collect_config_sections_from_schemas()

        assert ConfigFileProcessor2.config_files[0] == "hello2.ini"
        assert not os.path.exists("hello2.cfg")
        CONFIG_FILE_CONTENTS = """
                    [hello2]
                    name = Alice

                    [unbound.section]
                    numbers = 42
                    """
        write_configfile_with_contents("hello2.ini", CONFIG_FILE_CONTENTS)
        assert os.path.exists("hello2.ini")

        with pytest.raises(LookupError) as e:
            # -- POINT: CONTEXT_SETTINGS = dict(default_map=...)
            ConfigFileProcessorWithUnboundSection.read_config()

        expected = "No schema found for: section=unbound.section"
        assert expected in str(e)
Esempio n. 2
0
    def test_with_configfile12__prefers_configfile1(self, cli_runner_isolated):
        assert ConfigFileProcessor1.config_files == ["hello.ini", "hello.cfg"]
        CONFIG_FILE_CONTENTS1 = """
            [hello]
            name = alice
            """
        CONFIG_FILE_CONTENTS2 = """
            [hello]
            name = bob
            """
        write_configfile_with_contents("hello.ini", CONFIG_FILE_CONTENTS1)
        write_configfile_with_contents("hello.cfg", CONFIG_FILE_CONTENTS2)
        assert os.path.exists("hello.ini")
        assert os.path.exists("hello.cfg")

        CONTEXT_SETTINGS = dict(default_map=ConfigFileProcessor1.read_config())

        @click.command(context_settings=CONTEXT_SETTINGS)
        @click.option("-n", "--name", default="__CMDLINE__")
        def hello(name):
            click.echo("Hello %s" % name)

        result = cli_runner_isolated.invoke(hello)
        assert result.output == "Hello alice\n"
        assert result.exit_code == 0
Esempio n. 3
0
    def test_generate_configfile_names__without_searchpath_uses_curdir(
            self, isolated_filesystem):
        # -- SETUP:
        EMPTY_CONTENTS = "# -- EMPTY\n"
        write_configfile_with_contents("hello.ini", EMPTY_CONTENTS)
        write_configfile_with_contents("hello.cfg", EMPTY_CONTENTS)
        assert os.path.exists("hello.ini")
        assert os.path.exists("hello.cfg")

        # -- PERFROM TEST:
        given_config_files = ["hello.ini", "hello.cfg"]
        actual_config_files = list(
            generate_configfile_names(given_config_files))
        expected_config_files = [
            os.path.join(".", "hello.cfg"),
            os.path.join(".", "hello.ini"),
        ]
        assert actual_config_files == expected_config_files
Esempio n. 4
0
    def test_generate_configfile_names__when_searchpath_part_isfile(
            self, isolated_filesystem):
        # -- SETUP:
        EMPTY_CONTENTS = "# -- EMPTY\n"
        write_configfile_with_contents("hello.ini", EMPTY_CONTENTS)
        write_configfile_with_contents("BAD_PART", EMPTY_CONTENTS)
        assert os.path.exists("hello.ini")
        assert os.path.exists("BAD_PART")

        # -- PERFROM TEST:
        given_config_files = ["hello.ini", "hello.cfg"]
        config_searchpath = [".", "BAD_PART"]
        actual_config_files = list(
            generate_configfile_names(given_config_files, config_searchpath))
        expected_config_files = [
            os.path.join(".", "hello.ini"),
        ]
        assert actual_config_files == expected_config_files
Esempio n. 5
0
    def test_with_configfile2__usable_as_alternative(self, cli_runner_isolated):
        assert ConfigFileProcessor1.config_files[1] == "hello.cfg"
        CONFIG_FILE_CONTENTS2 = """
            [hello]
            name = Bob
            """
        write_configfile_with_contents("hello.cfg", CONFIG_FILE_CONTENTS2)
        assert not os.path.exists("hello.ini")
        assert os.path.exists("hello.cfg")

        CONTEXT_SETTINGS = dict(default_map=ConfigFileProcessor1.read_config())
        @click.command(context_settings=CONTEXT_SETTINGS)
        @click.option("-n", "--name", default="__CMDLINE__")
        def hello(name):
            click.echo("Hello %s" % name)

        result = cli_runner_isolated.invoke(hello)
        assert result.output == "Hello Bob\n"
        assert result.exit_code == 0
Esempio n. 6
0
    def test_param_without_default__uses_cmdline_default_when_missing(
            self, cli_runner_isolated):
        assert ConfigFileProcessor1.config_files[0] == "hello.ini"
        CONFIG_FILE_CONTENTS = """
            [hello]
            number = 1234
            """
        write_configfile_with_contents("hello.ini", CONFIG_FILE_CONTENTS)
        assert os.path.exists("hello.ini")

        CONTEXT_SETTINGS = dict(default_map=ConfigFileProcessor1.read_config())

        @click.command(context_settings=CONTEXT_SETTINGS)
        @click.option("-n", "--name", type=str, default="__CMDLINE__")
        def hello_with_config(name):
            click.echo("param: name= %s" % name)

        result = cli_runner_isolated.invoke(hello_with_config)
        assert result.output == "param: name= __CMDLINE__\n"
        assert result.exit_code == 0
Esempio n. 7
0
    def test_generate_configfile_names__provides_reversed_order(
            self, isolated_filesystem):
        # -- SETUP:
        EMPTY_CONTENTS = "# -- EMPTY\n"
        write_configfile_with_contents("hello.ini", EMPTY_CONTENTS)
        write_configfile_with_contents("hello.cfg", EMPTY_CONTENTS)
        assert os.path.exists("hello.ini")
        assert os.path.exists("hello.cfg")

        # -- PERFROM TEST:
        given_config_files = ["hello.ini", "hello.cfg"]
        actual_config_files = list(
            generate_configfile_names(given_config_files))
        expected_config_files = [
            os.path.join(".", "hello.cfg"),
            os.path.join(".", "hello.ini"),
        ]
        expected_config_files2 = [
            os.path.normpath(p) for p in expected_config_files
        ]
        assert actual_config_files == expected_config_files
        assert list(reversed(given_config_files)) == expected_config_files2
Esempio n. 8
0
    def test_config_searchpath__param_from_primary_file_overrides_secondary(self,
            cli_runner_isolated):
        assert not os.path.exists("hello3.ini")
        CONFIG_FILE_CONTENTS1 = """
            [hello]
            name = Alice
            """
        CONFIG_FILE_CONTENTS2 = """
            [hello]
            name = Bob      # Will be overridden.
            """
        config_filename2 = os.path.join("config", "profile", "hello3.ini")
        config_dirname2 = os.path.dirname(config_filename2)
        write_configfile_with_contents("hello3.ini", CONFIG_FILE_CONTENTS1)
        write_configfile_with_contents(config_filename2, CONFIG_FILE_CONTENTS2)
        assert os.path.exists("hello3.ini")
        assert os.path.exists(config_filename2)
        assert posix_normpath(config_dirname2) in ConfigFileProcessor3.config_searchpath

        config = ConfigFileProcessor3.read_config()
        assert config == dict(name="Alice")
        assert config["name"] == "Alice"    # -- FROM: config_file1 (prefered)
Esempio n. 9
0
    def test_config_searchpath__merges_sections(self, cli_runner_isolated):
        assert not os.path.exists("hello3.ini")
        CONFIG_FILE_CONTENTS1 = """
            [hello]
            name = Alice
            """
        CONFIG_FILE_CONTENTS2 = """
            [hello]
            number = 2
            """
        config_filename2 = os.path.join("config", "profile", "hello3.ini")
        config_dirname2 = os.path.dirname(config_filename2)
        write_configfile_with_contents("hello3.ini", CONFIG_FILE_CONTENTS1)
        write_configfile_with_contents(config_filename2, CONFIG_FILE_CONTENTS2)
        assert os.path.exists("hello3.ini")
        assert os.path.exists(config_filename2)
        assert posix_normpath(config_dirname2) in ConfigFileProcessor3.config_searchpath

        config = ConfigFileProcessor3.read_config()
        assert config == dict(name="Alice", number=2)
        assert config["name"] == "Alice"    # -- FROM: config_file1 (prefered)
        assert config["number"] == 2        # -- FROM: config_file2
Esempio n. 10
0
    def test_configfile__use_default_section_to_storage_name_mapping(
            self, cli_runner_isolated):
        assert ConfigFileProcessor2.config_files[0] == "hello2.ini"
        assert not os.path.exists("hello2.cfg")
        CONFIG_FILE_CONTENTS = """
                [hello2]
                name = Alice

                [hello2.foo]
                numbers = 1 2 3

                [hello2.bar]
                numbers = 42
                """
        write_configfile_with_contents("hello2.ini", CONFIG_FILE_CONTENTS)
        assert os.path.exists("hello2.ini")

        CONTEXT_SETTINGS = dict(default_map=ConfigFileProcessor2.read_config())

        @click.command(context_settings=CONTEXT_SETTINGS)
        @click.option("-n", "--name", default="__CMDLINE__")
        @click.pass_context
        def hello2(ctx, name):
            click.echo("Hello2 %s" % name)
            hello2_foo = ctx.default_map["hello2.foo"]
            hello2_bar = ctx.default_map["hello2.bar"]
            click.echo("foo.numbers: %s" % repr(hello2_foo["numbers"]))
            click.echo("bar.numbers: %s" % repr(hello2_bar["numbers"]))

        assert os.path.exists("hello2.ini")
        result = cli_runner_isolated.invoke(hello2)
        expected_output = """\
Hello2 Alice
foo.numbers: [1, 2, 3]
bar.numbers: [42]
"""
        assert result.output == expected_output
        assert result.exit_code == 0
Esempio n. 11
0
    def test_configfile__can_pass_additional_params_in_context(
            self, cli_runner_isolated):
        assert ConfigFileProcessor1.config_files[0] == "hello.ini"
        assert not os.path.exists("hello.cfg")
        CONFIG_FILE_CONTENTS = """
            [hello]
            name = Alice

            [hello.more.foo]
            numbers = 1 2 3

            [hello.more.bar]
            numbers = 1
            """
        write_configfile_with_contents("hello.ini", CONFIG_FILE_CONTENTS)
        assert os.path.exists("hello.ini")

        CONTEXT_SETTINGS = dict(default_map=ConfigFileProcessor1.read_config())

        @click.command(context_settings=CONTEXT_SETTINGS)
        @click.option("-n", "--name", default="__CMDLINE__")
        @click.pass_context
        def hello(ctx, name):
            click.echo("Hello %s" % name)
            hello_foo = ctx.default_map["foo"]
            hello_bar = ctx.default_map["bar"]
            click.echo("foo.numbers: %s" % repr(hello_foo["numbers"]))
            click.echo("bar.numbers: %s" % repr(hello_bar["numbers"]))

        assert os.path.exists("hello.ini")
        result = cli_runner_isolated.invoke(hello)
        expected_output = """\
Hello Alice
foo.numbers: [1, 2, 3]
bar.numbers: [1]
"""
        assert result.output == expected_output
        assert result.exit_code == 0
Esempio n. 12
0
    def test_inline_comments(self, cli_runner_isolated):
        assert ConfigFileProcessor1.config_files[0] == "hello.ini"
        CONFIG_FILE_CONTENTS1 = """
            [default]
            dummy = nothing
            ; this is a comment
            [hello]
            name = Alice ; this is an inline comment
            """
        write_configfile_with_contents("hello.ini", CONFIG_FILE_CONTENTS1)
        assert os.path.exists("hello.ini")
        assert not os.path.exists("hello.cfg")

        CONTEXT_SETTINGS = dict(default_map=ConfigFileProcessor1.read_config(
            inline_comment_prefixes=[";", "#"]))

        @click.command(context_settings=CONTEXT_SETTINGS)
        @click.pass_context
        def hello(ctx):
            click.echo("Hello %s" % ctx.default_map["name"])

        result = cli_runner_isolated.invoke(hello, [])
        assert result.output == "Hello Alice\n"
        assert result.exit_code == 0
Esempio n. 13
0
    def test_generate_configfile_names__provides_existing_variants_with_searchpath(
            self, isolated_filesystem):
        # -- SETUP:
        EMPTY_CONTENTS = "# -- EMPTY\n"
        write_configfile_with_contents("hello.ini", EMPTY_CONTENTS)
        write_configfile_with_contents("more/hello.ini", EMPTY_CONTENTS)
        write_configfile_with_contents("more/hello.cfg", EMPTY_CONTENTS)
        assert os.path.exists("hello.ini")
        assert os.path.exists("more/hello.ini")
        assert os.path.exists("more/hello.cfg")

        # -- PERFROM TEST:
        given_config_files = ["hello.ini", "hello.cfg"]
        config_searchpath = [".", "more"]
        actual_config_files = list(
            generate_configfile_names(given_config_files, config_searchpath))
        expected_config_files = [
            os.path.join("more", "hello.cfg"),
            os.path.join("more", "hello.ini"),
            os.path.join(".", "hello.ini"),
        ]
        assert actual_config_files == expected_config_files