def test_saveconfig(tmpdir, cfg): import appconfig as apc appconfig = apc.appconfig import copy tcfg = xindir(tmpdir, 'save.cfg') print cfg ncfg = copy.deepcopy(cfg) ncfg['sounds'] = makesoundsindir(tmpdir, cfg['sounds']) print ncfg ac = appconfig(TESTAPPNAME, TESTAPPVER, tcfg) ac.setconfig(ncfg) assert equaldicts(ac.config, ncfg) ac.writeconfig() # test expected lines are present expect = ['[Sounds]\n', '[Profiles]\n', '[General]\n'] expect.extend(soundlines(ncfg['sounds'])) expect.extend(profilelines(ncfg['profiles'])) f = open(tcfg) for line in f: dline = line.decode(cfgenc) if dline in expect: expect.remove(dline) f.close() assert expect == []
def test_setdefaultactiveprofile(tmpdir, cfg): import appconfig as apc appconfig = apc.appconfig import os import copy writtencfg = copy.deepcopy(cfg) writtencfg['sounds'] = makesoundsindir(tmpdir, writtencfg['sounds']) writtencfg['profiles'][u'p1'] = copy.deepcopy(writtencfg['profiles'][u'p0']) writtencfg['profiles'][u'a0'] = copy.deepcopy(writtencfg['profiles'][u'p0']) profiles = writtencfg['profiles'].keys() profiles.sort() writtencfg['active_profile'] = None cfgfile = xindir(tmpdir, 'cfg.ini') ac = appconfig(TESTAPPNAME, TESTAPPVER, cfgfile) ac.setconfig(writtencfg) ac.writeconfig() ac.readconfig() assert ac.config['active_profile'] == profiles[0] # test config file override newfn = cfgfile + 'brk' del ac.config['profiles'][u'a0'] expectcfg = copy.deepcopy(ac.config) expectcfg['active_profile'] = u'p0' ac.writeconfig(newfn) ac.readconfig() assert equaldicts(ac.config, expectcfg) cfgcontent = """ [General] cfgversion = 1 active_profile = inexistent [Sounds] [Profiles] Profile.a0 = a0 Profile.p0 = p0 Profile.p1 = p1 [Profile.a0] [Profile.p0] [Profile.p1] """ f = open(newfn, "wt") f.write(cfgcontent) f.close() ac.readconfig() # this should not fail! #assert ac.config['active_profile'] == profiles[0] assert ac.config['active_profile'] in profiles
def test_cfgfromXDG(tmpdir, monkeypatch, cfg): """test config is correctly loaded if XDG_CONFIG_HOME is set""" import appconfig as apc appconfig = apc.appconfig import os import copy writtencfg = copy.deepcopy(cfg) writtencfg['sounds'] = makesoundsindir(tmpdir, writtencfg['sounds']) monkeypatch.setenv("XDG_CONFIG_HOME", tmpdir) expectcfgdir = xindir(tmpdir, TESTAPPNAME) ac = appconfig(TESTAPPNAME, TESTAPPVER) ac.setconfig(writtencfg) ac.writeconfig() expectcfg = xindir(expectcfgdir, 'config.ini') assert os.path.isfile(expectcfg) is True readac = appconfig(TESTAPPNAME, TESTAPPVER) readac.readconfig() readconf = readac.config assert equaldicts(writtencfg, readconf) monkeypatch.undo()