Exemple #1
0
def test_upgrade_to_latest(tmpdir):
    # Copy logme.ini to tmpdir
    logme_file = current_dir / 'logme.ini'
    tmp_logme = tmpdir.join('logme.ini')
    shutil.copyfile(logme_file, tmp_logme)

    upgrade_to_latest(tmp_logme)

    # Validate upgraded file
    config = ConfigParser.from_files(tmp_logme)
    config_before = ConfigParser.from_files(logme_file)

    assert set(config.sections()) == set(NONLOGGER_CONFIGS +
                                         config_before.sections())

    # Validate the latest version has not been changed
    assert config.to_dict(section='latest') == \
           config_before.to_dict(section='latest') == \
           ver11_config

    for i in config.sections():
        if i == 'colors':
            continue

        conf_dict = config.to_dict(section=i)

        for k, v in conf_dict.items():
            if isinstance(v, dict):
                assert v.get('type') is not None

            assert all(c.islower() for c in k)
Exemple #2
0
    def test_init_from_files_empty(self, tmpdir):
        file = tmpdir.join('foo.ini')
        file.open('w').close()

        assert Path(file).exists()

        with pytest.raises(InvalidConfig):
            ConfigParser.from_files(file)
Exemple #3
0
    def test_remove(self, tmp_config):
        self.runner.invoke(cli, ['set', 'to_remove="command to be removed"'])

        config = ConfigParser.from_files(tmp_config)
        assert 'to_remove' in config.options('fetchme')

        self.runner.invoke(cli, ['remove', 'to_remove'])

        config_removed = ConfigParser.from_files(tmp_config)
        assert 'to_remove' not in config_removed.options('fetchme')
Exemple #4
0
def test_upgrade_colors_config_not_changed(tmpdir):
    local_logme_file = current_dir / 'logme_with_color.ini'
    tmp_logme = tmpdir.join('logme.ini')

    shutil.copyfile(local_logme_file, tmp_logme)

    upgrade_to_latest(tmp_logme)

    config_before = ConfigParser.from_files(local_logme_file)
    config_after = ConfigParser.from_files(tmp_logme)

    assert config_before.items('colors') == config_after.items('colors')
Exemple #5
0
    def test_remove_command(self, tmpdir):

        with cd(tmpdir):
            self.runner.invoke(cli, ['init'])
            self.runner.invoke(cli, ['add', 'test'])

            config_path = tmpdir.join('logme.ini')
            config_before = ConfigParser.from_files(config_path)

            assert set(config_before.sections()) == {'colors', 'logme', 'test'}

            result = self.runner.invoke(cli, ['remove', 'test'])
            config_after = ConfigParser.from_files(config_path)

            assert result.exit_code == 0
            assert config_after.sections() == ['colors', 'logme']
Exemple #6
0
    def test_init_file_change(self, tmpdir, option, key, expected):

        self.runner.invoke(cli, ['init', '-p', tmpdir] + option)

        conf = ConfigParser.from_files(tmpdir.join('logme.ini'))

        assert conf.get(*key) == expected
Exemple #7
0
def set(ctx, content, override):
    """
    Command for setting an alias to a long command

    :argument: content: key=value, e.g: ssh="ssh -i /path/to/my/key/file [email protected]"

    :option: --override, -o: option for overriding existing key

    :raises: ValueError, if the alias with provided name has already being set in .fetchmerc file
    """
    name, val = content.split('=', 1)

    if name in DEFAULT_COMMANDS:
        raise ValueError(F"'{name}' is a default command, and it cannot be set!")

    config_path = _get_config_path()
    config = ConfigParser.from_files(config_path)

    try:
        config.get('fetchme', name)
        if not override:
            raise ValueError(f"'{name}' already exist! use --override, or -o flag to override")
    except NoOptionError:
        pass

    config['fetchme'][name] = val

    with config_path.open('w') as file:
        config.write(file)
Exemple #8
0
    def test_set(self, tmp_config):
        result = self.runner.invoke(cli, ['set', 'blah=hello'])

        config = ConfigParser.from_files(tmp_config)

        assert result.exit_code == 0
        assert config.has_option('fetchme', 'blah')
        assert config.get('fetchme', 'blah') == 'hello'
Exemple #9
0
    def test_set_override_existing(self, tmp_config):
        self.runner.invoke(cli, ['set', 'blah=hello'])
        result = self.runner.invoke(cli, ["set", 'blah=overriden value', '-o'])

        config = ConfigParser.from_files(tmp_config)

        assert result.exit_code == 0
        assert config.get('fetchme', 'blah') == 'overriden value'
Exemple #10
0
 def test_to_dict_with_datefmt(self):
     config = ConfigParser.from_files(data.config_path)
     expected = {
         'formatter': {
             'fmt': '{asctime} - {name} - {levelname} - {message}',
             'datefmt': '%Y/%m/%d',
             'style': '{'
         },
         'Active': True
     }
     assert config.to_dict(section='date_format') == expected
Exemple #11
0
def test_get_command_func(tmp_config, mock_subprocess):
    config = ConfigParser.from_files(tmp_config)
    subcommand = _get_command_func('test_command', config)

    assert inspect.isfunction(subcommand)

    subcommand(None)

    mock_subprocess.assert_called_with(['ls', '-al'])

    assert subcommand.__doc__.strip(
    ) == "Execute command alias 'test_command'; command: ls -al"
Exemple #12
0
def _set_commands(click_group: click.core.Group):
    """
    Set commands to click group based on the options in .fetchmerc file
    """
    config_path = _get_config_path()
    config = ConfigParser.from_files(config_path)

    option_names = config.options('fetchme')

    for i in option_names:
        func = _get_command_func(i, config)

        click_group.command(name=i)(click.pass_context(func))
Exemple #13
0
    def test_add_command(self, tmpdir):

        with cd(tmpdir):
            self.runner.invoke(cli, ['init'])
            result = self.runner.invoke(cli, ['add', 'blah'])

            config_path = tmpdir.join('logme.ini')
            config = ConfigParser.from_files(config_path)

            assert result.exit_code == 0
            assert Path(config_path).is_file()

            assert set(config.sections()) == {'colors', 'logme', 'blah'}
Exemple #14
0
    def test_init_chained_options(self, tmpdir):

        tmp = tmpdir.join('my_project')

        self.runner.invoke(
            cli,
            ['init', '-p', tmp, '-mk', '-lp',
             tmp.join('var/log/dummy.log')])

        config = ConfigParser.from_files(tmp.join('logme.ini'))

        fh_conf = config.to_dict(section='logme', option='file')

        assert fh_conf['filename'] == tmp.join('var/log/dummy.log')
        assert set(fh_conf.keys()) == {'active', 'level', 'filename', 'type'}
Exemple #15
0
def validate_conf(name: str, ini_file_path: Union[str, Path]):
    """
    Helper function for 'logme init' command,
    ensure the logger name passed in does not already exist in the ini file

    :param name: name of the section to be added
    :param ini_file_path: path of the logme.ini file
    """
    config = ConfigParser.from_files(ini_file_path)

    if config.has_section(name):
        raise LogmeError(
            f"'{name}' logging config already exists in config file: {ini_file_path}"
        )
    elif not config.has_section('logme'):
        raise LogmeError(f"{ini_file_path} is not a valid logme.ini file")
Exemple #16
0
    def test_init(self, tmpdir, file_path, cmd_args):

        expected_file = Path(tmpdir.join(file_path))

        with cd(tmpdir):
            result = self.runner.invoke(cli, cmd_args)

            assert result.exit_code == 0
            assert expected_file.is_file()

            conf = ConfigParser.from_files(expected_file)
            assert conf.sections() == ['colors', 'logme']

            # Assert the first section is the color config
            with open(expected_file) as file:
                line = file.readline()
                assert line == '[colors]\n'
Exemple #17
0
def get_config_content(caller_file_path: Union[str, Path], name: str) -> dict:
    """
    Get the config section as a dictionary

    :param caller_file_path: file path of the caller, __file__
    :param name: the section name in an .ini file

    :return: configuration as dict
    """

    init_file_path = get_ini_file_path(caller_file_path)

    config = ConfigParser.from_files(init_file_path)

    try:
        return config.to_dict(section=name)
    except NoSectionError:
        raise NoSectionError(
            f"'{name}' is not a valid configuration in {init_file_path}")
Exemple #18
0
def remove(ctx, name):
    """
    Commands for removing a set alias

    :param: name: The name of the alias, defined in .fetchmerc file

    :raises: ValueError: if such alias does not exist
    """
    config_path = _get_config_path()
    config = ConfigParser.from_files(config_path)

    try:
        config.get('fetchme', name)
    except NoOptionError:
        raise InvalidConfigOption(f"'{name}' is not a valid config option. Avalables: {config.options('fetchme')}")

    config.remove_option('fetchme', name)

    with config_path.open('w') as file:
        config.write(file)
Exemple #19
0
def remove(ctx, name, project_root):
    """
    Command for removing a logger configuration in a logme.ini file.
    logme configuration cannot be removed
    """
    none_removables = {
        'logme':
        "'logme' master configuration cannot be removed!",
        'colors':
        "'colors' configuration cannot be removed! To remove color "
        "logging, set all color values to 'None'",
    }

    if none_removables.get(name):
        raise LogmeError(none_removables[name])

    with ensure_conf_exist(project_root) as logme_conf:

        config = ConfigParser.from_files(logme_conf)
        config.remove_section(name)

        with logme_conf.open('w+') as conf:
            config.write(conf)
Exemple #20
0
def upgrade_to_latest(config_path: Union[str, Path]):
    """
    upgrade the existing logme.ini file to latest version
    *Will write to existing file*

    :param config_path: logme.ini file
    """
    # config = read_config(config_path)
    config_dict = ConfigParser.from_files(config_path).to_dict()

    config_dict_updated = {}

    _upgrade_with_color_config(config_dict, config_dict_updated)

    for k, v in config_dict.items():
        if k not in NONLOGGER_CONFIGS:
            updated = _upgrade_logging_config_section(v)

            config_dict_updated[k] = updated

    new_conf = ConfigParser.from_dict(config_dict_updated)

    with open(config_path, 'w') as file:
        new_conf.write(file)
Exemple #21
0
    def test_to_dict_raise(self):
        config = ConfigParser.from_files(data.config_path)

        with pytest.raises(ValueError):
            config.to_dict(option='profile')
Exemple #22
0
    def test_to_dict(self, parse_args, expected):
        config = ConfigParser.from_files(data.config_path)

        assert config.to_dict(**parse_args) == expected
Exemple #23
0
    def test_init_from_files_raise(self, tmpdir):
        ini_path = tmpdir.join('test.ini')

        with pytest.raises(InvalidConfig):
            ConfigParser.from_files(ini_path)
Exemple #24
0
 def test_init_from_files_non_exist(self):
     with pytest.raises(InvalidConfig):
         ConfigParser.from_files('blah.ini')
Exemple #25
0
 def test_init_from_files(self, filenames):
     config = ConfigParser.from_files(filenames)
     assert set(config.sections()) == {
         'my_config', 'Fish_Profiles', 'date_format'
     }