Esempio n. 1
0
    def test_invalid_section(self):
        cf = JSONConfigParser()

        try:
            cf.read_string((
                '[valid]\n'
                'irrelevant = "meh"\n'
                '[]'
            ))
        except ParseError as e:
            self.assertEqual(e.lineno, 3)

            # check that nothing was added
            self.assertEqual(sum(1 for _ in cf.sections()), 0)
        else:  # pragma: no cover
            self.fail()

        try:
            cf.read_string((
                '[nooooooooooooooooooo'
            ))
        except ParseError as e:
            self.assertEqual(e.lineno, 1)

            # check that nothing was added
            self.assertEqual(sum(1 for _ in cf.sections()), 0)
        else:  # pragma: no cover
            self.fail()
Esempio n. 2
0
    def test_invalid_values(self):
        cf = JSONConfigParser()

        try:
            cf.read_string((
                '[section]\n'
                'unmatched = [1,2,3}'
            ))
        except ParseError as e:
            self.assertEqual(e.lineno, 2)

            # check that nothing was added
            self.assertEqual(sum(1 for _ in cf.sections()), 0)
        else:  # pragma: no cover
            self.fail()

        try:
            cf.read_string((
                '[section]\n'
                'unterminated = "something\n'
            ))
        except ParseError as e:
            self.assertEqual(e.lineno, 2)

            # check that nothing was added
            self.assertEqual(sum(1 for _ in cf.sections()), 0)
        else:  # pragma: no cover
            self.fail()
Esempio n. 3
0
def test_initread(tmpdir):
    test_file = tmpdir.join('conf.json')
    test_file.write('{"test":"Yes"}')

    conf = JSONConfigParser(storage=test_file.strpath, source=test_file.strpath)

    assert "test" in conf.data
Esempio n. 4
0
def test_append_to_list(tmpdir):
    test_file = tmpdir.join("test.json")
    conf = JSONConfigParser(storage=test_file.strpath)
    conf['packages'] = []

    commands.append(conf, "$.packages", "jsonconfigparser")

    assert "jsonconfigparser" in conf['packages']
Esempio n. 5
0
def test_add_field(tmpdir):
    test_file = tmpdir.join("test.json")
    conf = JSONConfigParser(storage=test_file.strpath)

    commands.add_field(conf, "$.package", "jsonconfigparser")

    assert "package" in conf
    assert conf["package"] == "jsonconfigparser"
Esempio n. 6
0
def test_set_on_path(tmpdir):

    test_file = tmpdir.join("test.json")
    conf = JSONConfigParser(storage=test_file.strpath)

    utils.set_on_path(conf, '$.packages', ['jsonconfigparser'])

    assert 'packages' in conf
    assert 'jsonconfigparser' == conf['packages'][0]
Esempio n. 7
0
def test_append_immutable_raises_error(tmpdir):
    test_file = tmpdir.join("test.json")
    conf = JSONConfigParser(storage=test_file.strpath)
    conf['packages'] = 'jsonconfigparser'

    with pytest.raises(TypeError) as excinfo:
        commands.append(conf, "$.packages", "jsonconfigparser")

    assert "mutable container" in str(excinfo.value)
Esempio n. 8
0
def test_append_to_dict(tmpdir):
    test_file = tmpdir.join("test.json")
    conf = JSONConfigParser(storage=test_file.strpath)

    conf['settings'] = {}

    commands.append(conf, "$.settings", {"testing":"Yes"})

    assert conf['settings']['testing'] == "Yes"
Esempio n. 9
0
def test_append_to_multiple_dict(tmpdir):
    
    test_file = tmpdir.join("test.json")
    conf = JSONConfigParser(storage=test_file.strpath)

    conf['packages'] = [{}, {}]

    commands.append(conf, '$.packages.[*]', {"installed":True}, multi=True)

    assert all(n['installed'] for n in conf['packages'])
Esempio n. 10
0
def test_edit_convert(tmpdir):
    
    test_file = tmpdir.join("test.json")
    conf = JSONConfigParser(storage=test_file.strpath)

    conf['testing'] = False

    commands.edit(conf, "$.testing", "True", convert="bool")

    assert conf['testing']
Esempio n. 11
0
def test_append_convert_to_float(tmpdir):

    test_file = tmpdir.join("test.json")
    conf = JSONConfigParser(storage=test_file.strpath)

    conf['things'] = []

    commands.append(conf, "$.things", "3.14", convert='float')

    assert 3.14 in conf['things']
Esempio n. 12
0
def test_append_convert_to_list(tmpdir):

    test_file = tmpdir.join("test.json")
    conf = JSONConfigParser(storage=test_file.strpath)

    conf['things'] = []

    commands.append(conf, "$.things", "1 2 3", convert='list')

    assert ["1", "2", "3"] in conf['things']
Esempio n. 13
0
def test_append_convet_to_dict(tmpdir):

    test_file = tmpdir.join("test.json")
    conf = JSONConfigParser(storage=test_file.strpath)

    conf['things'] = []

    commands.append(conf, "$.things", "color=purple", convert='dict')

    assert {"color":"purple"} in conf['things']
Esempio n. 14
0
def test_write(tmpdir):
    test_file = tmpdir.join('test.conf') 
    conf = JSONConfigParser(storage=test_file.strpath)
    conf['test'] = "Yes"
    conf.write()

    contents = test_file.read()

    assert "test" in contents
    assert "Yes" in contents
Esempio n. 15
0
def test_append_multi_raise_error(tmpdir):
    test_file = tmpdir.join("test.join")
    conf = JSONConfigParser(storage=test_file.strpath)

    conf['packages'] = [[], []]

    with pytest.raises(AttributeError) as excinfo:
        commands.append(conf, '$.packages.[*]', "Test")

    assert excinfo.value
Esempio n. 16
0
def test_add_file(tmpdir):
    test_one = tmpdir.join("test1.json")
    test_two = tmpdir.join("test2.json")
    test_two.write('{"test":"Yes"}')
    conf = JSONConfigParser(storage=test_one.strpath)

    commands.add_file(conf, test_two.strpath)

    assert "test" in conf
    assert conf['test'] == "Yes"
Esempio n. 17
0
def test_view(tmpdir, capsys):
    test_file = tmpdir.join('test.conf')
    conf = JSONConfigParser(storage=test_file.strpath)
    conf['test'] = "Yes"
    conf.view('$')

    captured, _ = capsys.readouterr()

    assert "$" in captured
    assert "'test'" in captured
    assert "'Yes'" in captured
Esempio n. 18
0
def test_append_to_mutliple_list(tmpdir):

    test_file = tmpdir.join("test.json")
    conf = JSONConfigParser(storage=test_file.strpath)

    conf['packages'] = [[], []]

    commands.append(conf, '$.packages.[*]', "Test", multi=True)

    assert "Test" in conf['packages'][0]
    assert "Test" in conf['packages'][1]
Esempio n. 19
0
def test_add_field_convert(tmpdir):
    test_file = tmpdir.join("test.json")
    conf = JSONConfigParser(storage=test_file.strpath)

    commands.add_field(conf, "$.age", "25", convert="int")
    commands.add_field(conf, "$.friends", "louis", convert="list")
    commands.add_field(conf, "$.ids", "12 13 14", convert="list int")

    assert conf['age'] == 25
    assert conf['friends'] == ['louis']
    assert conf['ids'] == [12, 13, 14]
Esempio n. 20
0
def test_multiview(tmpdir, capsys):
    test_file = tmpdir.join('test.conf')
    conf = JSONConfigParser(storage=test_file.strpath)
    conf["packages"] = ["jsonconfigparser", "ply", "decorator"]

    conf.view("$.packages.[*]")

    captured, _ = capsys.readouterr()

    assert "packages.[1]" in captured
    assert "ply" in captured
Esempio n. 21
0
def test_view_command(tmpdir, capsys):
    
    test_file = tmpdir.join("test.json")
    conf = JSONConfigParser(storage=test_file.strpath)
    conf["package"] = "jsonconfigparser"

    commands.view(conf, '$.package')

    captured, _ = capsys.readouterr()

    assert "jsonconfigparser" in captured
Esempio n. 22
0
    def test_read_file(self):
        string = '[section]\n' + \
                 'foo = "bar"'

        fp = tempfile.NamedTemporaryFile('w+')
        fp.write(string)
        fp.seek(0)

        cf = JSONConfigParser()
        cf.read_file(fp)

        self.assertEqual(cf.get('section', 'foo'), 'bar')
Esempio n. 23
0
    def test_get_from_fallback(self):
        cf = JSONConfigParser()
        cf.add_section('section')

        # returns from fallback if section exists
        self.assertEqual(cf.get('section', 'unset', 'fallback'), 'fallback')

        try:
            cf.get('nosection', 'unset', 'fallback')
        except NoSectionError:
            pass
        else:  # pragma: no cover
            self.fail()
Esempio n. 24
0
def test_delete(tmpdir):
    
    test_file = tmpdir.join("test.json")
    conf = JSONConfigParser(storage=test_file.strpath)
    conf['things'] = []

    commands.delete(conf, "$.things")

    assert "things" not in conf

    commands.delete(conf, "$")

    assert not conf.data
Esempio n. 25
0
    def test_read_string(self):
        cf = JSONConfigParser()

        cf.read_string((
            '[section]\n'
            '# comment comment\n'
            'foo = "bar"\n'
            '\n'
            '[section2]\n'
            'bar = "baz"\n'
        ))

        self.assertEqual(cf.get('section', 'foo'), 'bar')
Esempio n. 26
0
    def test_remove_option(self):
        cf = JSONConfigParser()

        cf.add_section('section')
        cf.set('section', 'normal', 'set-normally')
        cf.set(cf.default_section, 'default', 'set-in-defaults')

        # can remove normal options
        self.assertTrue(cf.remove_option('section', 'normal'))
        self.assertFalse(cf.has_option('section', 'normal'))

        # can't remove defaults accidentally (maybe there should be shadowing)
        self.assertFalse(cf.remove_option('section', 'default'))
        self.assertEqual(cf.get('section', 'default'), 'set-in-defaults')
Esempio n. 27
0
    def test_get_from_vars(self):
        cf = JSONConfigParser()
        cf.add_section('section')
        cf.set('section', 'option', 'set-in-section')

        self.assertEqual(cf.get('section', 'option',
                                vars={'option': 'set-in-vars'}),
                         'set-in-vars',
                         msg="vars should take priority over options in \
                              section")

        self.assertEqual(cf.get('section', 'option', vars={}),
                         'set-in-section',
                         msg="get should fall back to section if option not \
                              in vars")
Esempio n. 28
0
    def test_has_option(self):
        cf = JSONConfigParser()

        # option in nonexistant section does not exist
        self.assertFalse(cf.has_option('nonexistant', 'unset'))

        cf.add_section('section')
        self.assertFalse(cf.has_option('section', 'unset'),
                         msg="has_option should return False if section \
                              exists but option is unset")

        cf.set('section', 'set', 'set-normally')
        self.assertTrue(cf.has_option('section', 'set'),
                        msg="has option should return True if option is set \
                             normally")

        cf.set(cf.default_section, 'default', 'set-in-defaults')
        self.assertTrue(cf.has_option('section', 'default'),
                        msg="has_option should return True if option set in \
                             defaults")
Esempio n. 29
0
    def test_get_from_defaults(self):
        cf = JSONConfigParser()

        cf.set(cf.default_section, 'option', 'set-in-defaults')
        try:
            cf.get('section', 'option')
        except NoSectionError:
            pass
        else:  # pragma: no cover
            self.fail("Only fall back to defaults if section exists")

        cf.add_section('section')
        self.assertEqual(cf.get('section', 'option'), 'set-in-defaults',
                         msg="get should fall back to defaults if value not \
                              set in section")

        cf.set('section', 'option', 'set-normally')
        self.assertEqual(cf.get('section', 'option'), 'set-normally',
                         msg="get shouldn't fall back if option is set \
                              normally")
Esempio n. 30
0
def test_act_on_path(tmpdir, capsys):

    test_file = tmpdir.join('test.conf')
    conf = JSONConfigParser(storage=test_file.strpath)
    conf['test'] = "Yes"

    action = lambda j, f: print(j, f)

    utils.act_on_path(conf, '$', action)

    captured, _ = capsys.readouterr()

    assert '$' in captured
    assert 'test' in captured

    conf['packages'] = ['jsonconfigparser']

    utils.act_on_path(conf, "$.packages.[1]", action)

    captured, _ = capsys.readouterr()

    assert "1" in captured
    assert "jsonconfigparser" in captured