1
0
mirror of https://github.com/deadc0de6/dotdrop.git synced 2026-02-04 20:19:46 +00:00

wrap conf and args into a new Options class

This commit is contained in:
deadc0de6
2019-02-10 20:59:14 +01:00
parent 88c730157d
commit c82cc56dcf
10 changed files with 435 additions and 313 deletions

View File

@@ -10,9 +10,9 @@ import string
import random
import tempfile
from dotdrop.config import Cfg
from dotdrop.utils import *
from dotdrop.options import Options
from dotdrop.linktypes import LinkTypes
from dotdrop.utils import *
TMPSUFFIX = '.dotdrop'
@@ -81,20 +81,56 @@ def create_dir(path):
return path
def load_config(confpath, profile):
def _fake_args():
args = {}
args['--verbose'] = False
args['--no-banner'] = False
args['--dry'] = False
args['--force'] = False
args['--nodiff'] = False
args['--showdiff'] = True
args['--inv-link'] = False
args['--template'] = False
args['--temp'] = False
args['<key>'] = []
args['--dopts'] = ''
args['--file'] = []
args['--ignore'] = []
args['<path>'] = []
args['--key'] = False
args['--ignore'] = []
args['--show-patch'] = False
# cmds
args['list'] = False
args['listfiles'] = False
args['install'] = False
args['compare'] = False
args['import'] = False
args['update'] = False
args['detail'] = False
return args
def load_options(confpath, profile):
"""Load the config file from path"""
conf = Cfg(confpath)
opts = conf.get_settings()
opts['dry'] = False
opts['profile'] = profile
opts['safe'] = True
opts['installdiff'] = True
opts['link'] = LinkTypes.NOLINK.value
opts['showdiff'] = True
opts['debug'] = True
opts['dopts'] = ''
opts['variables'] = {}
return conf, opts
# create the fake args (bypass docopt)
args = _fake_args()
args['--cfg'] = confpath
args['--profile'] = profile
# and get the options
# TODO need to patch options
o = Options(args=args)
o.profile = profile
o.dry = False
o.profile = profile
o.safe = True
o.installdiff = True
o.link = LinkTypes.NOLINK.value
o.showdiff = True
o.debug = True
o.dopts = ''
o.variables = {}
return o
def get_path_strip_version(path):

View File

@@ -16,7 +16,7 @@ from dotdrop.templategen import Templategen
# from tests.helpers import *
from tests.helpers import create_dir, get_string, get_tempdir, clean, \
create_random_file, create_fake_config, load_config, edit_content
create_random_file, create_fake_config, load_options, edit_content
class TestCompare(unittest.TestCase):
@@ -26,12 +26,12 @@ class TestCompare(unittest.TestCase):
CONFIG_DOTPATH = 'dotfiles'
CONFIG_NAME = 'config.yaml'
def compare(self, opts, conf, tmp, nbdotfiles):
dotfiles = conf.get_dotfiles(opts['profile'])
def compare(self, o, tmp, nbdotfiles):
dotfiles = o.dotfiles
self.assertTrue(len(dotfiles) == nbdotfiles)
t = Templategen(base=opts['dotpath'], debug=True)
inst = Installer(create=opts['create'], backup=opts['backup'],
dry=opts['dry'], base=opts['dotpath'], debug=True)
t = Templategen(base=o.dotpath, debug=True)
inst = Installer(create=o.create, backup=o.backup,
dry=o.dry, base=o.dotpath, debug=True)
comp = Comparator()
results = {}
for dotfile in dotfiles:
@@ -98,50 +98,53 @@ class TestCompare(unittest.TestCase):
backup=self.CONFIG_BACKUP,
create=self.CONFIG_CREATE)
self.assertTrue(os.path.exists(confpath))
conf, opts = load_config(confpath, profile)
opts['longkey'] = True
o = load_options(confpath, profile)
o.longkey = True
dfiles = [d1, d2, d3, d4, d5]
# import the files
cmd_importer(opts, conf, dfiles)
conf, opts = load_config(confpath, profile)
o.import_path = dfiles
cmd_importer(o)
o = load_options(confpath, profile)
# compare the files
expected = {d1: True, d2: True, d3: True, d4: True, d5: True}
results = self.compare(opts, conf, tmp, len(dfiles))
results = self.compare(o, tmp, len(dfiles))
self.assertTrue(results == expected)
# modify file
edit_content(d1, get_string(20))
expected = {d1: False, d2: True, d3: True, d4: True, d5: True}
results = self.compare(opts, conf, tmp, len(dfiles))
results = self.compare(o, tmp, len(dfiles))
self.assertTrue(results == expected)
# modify binary file
edit_content(d4, bytes(get_string(20), 'ascii'), binary=True)
expected = {d1: False, d2: True, d3: True, d4: False, d5: True}
results = self.compare(opts, conf, tmp, len(dfiles))
results = self.compare(o, tmp, len(dfiles))
self.assertTrue(results == expected)
# add file in directory
d7, _ = create_random_file(d5)
self.assertTrue(os.path.exists(d7))
expected = {d1: False, d2: True, d3: True, d4: False, d5: False}
results = self.compare(opts, conf, tmp, len(dfiles))
results = self.compare(o, tmp, len(dfiles))
self.assertTrue(results == expected)
# modify all files
edit_content(d2, get_string(20))
edit_content(d3, get_string(21))
expected = {d1: False, d2: False, d3: False, d4: False, d5: False}
results = self.compare(opts, conf, tmp, len(dfiles))
results = self.compare(o, tmp, len(dfiles))
self.assertTrue(results == expected)
# test compare from dotdrop
self.assertFalse(cmd_compare(opts, conf, tmp))
self.assertFalse(cmd_compare(o, tmp))
# test focus
self.assertFalse(cmd_compare(opts, conf, tmp, focus=d4))
self.assertFalse(cmd_compare(opts, conf, tmp, focus='/tmp/fake'))
o.focus = d4
self.assertFalse(cmd_compare(o, tmp))
o.focus = '/tmp/fake'
self.assertFalse(cmd_compare(o, tmp))
def main():

View File

@@ -12,7 +12,6 @@ import yaml
from dotdrop.dotdrop import cmd_importer
from dotdrop.dotdrop import cmd_list_profiles
from dotdrop.dotdrop import cmd_list_files
from dotdrop.dotdrop import _header
from dotdrop.dotdrop import cmd_update
from dotdrop.config import Cfg
@@ -34,11 +33,11 @@ class TestImport(unittest.TestCase):
content = yaml.load(f)
return content
def assert_file(self, path, conf, profile):
def assert_file(self, path, o, profile):
"""Make sure path has been inserted in conf for profile"""
strip = get_path_strip_version(path)
self.assertTrue(strip in [x.src for x in conf.get_dotfiles(profile)])
dsts = [os.path.expanduser(x.dst) for x in conf.get_dotfiles(profile)]
self.assertTrue(strip in [x.src for x in o.dotfiles])
dsts = [os.path.expanduser(x.dst) for x in o.dotfiles]
self.assertTrue(path in dsts)
def assert_in_yaml(self, path, dic, link=False):
@@ -69,7 +68,7 @@ class TestImport(unittest.TestCase):
backup=self.CONFIG_BACKUP,
create=self.CONFIG_CREATE)
self.assertTrue(os.path.exists(confpath))
conf, opts = load_config(confpath, profile)
o = load_options(confpath, profile)
# create some random dotfiles
dotfile1, content1 = create_random_file(src)
@@ -107,25 +106,27 @@ class TestImport(unittest.TestCase):
# import the dotfiles
dfiles = [dotfile1, dotfile2, dotfile3, dotfile4, dotfile5]
cmd_importer(opts, conf, dfiles)
o.import_path = dfiles
cmd_importer(o)
# import symlink
opts[Cfg.key_dotfiles_link] = True
o.link = LinkTypes.PARENTS
sfiles = [dotfile6, dotfile7]
cmd_importer(opts, conf, sfiles)
opts[Cfg.key_dotfiles_link] = False
o.import_path = sfiles
cmd_importer(o)
o.link = LinkTypes.NOLINK
# reload the config
conf, opts = load_config(confpath, profile)
o = load_options(confpath, profile)
# test dotfiles in config class
self.assertTrue(profile in conf.get_profiles())
self.assert_file(dotfile1, conf, profile)
self.assert_file(dotfile2, conf, profile)
self.assert_file(dotfile3, conf, profile)
self.assert_file(dotfile4, conf, profile)
self.assert_file(dotfile5, conf, profile)
self.assert_file(dotfile6, conf, profile)
self.assert_file(dotfile7, conf, profile)
self.assertTrue(profile in o.profiles)
self.assert_file(dotfile1, o, profile)
self.assert_file(dotfile2, o, profile)
self.assert_file(dotfile3, o, profile)
self.assert_file(dotfile4, o, profile)
self.assert_file(dotfile5, o, profile)
self.assert_file(dotfile6, o, profile)
self.assert_file(dotfile7, o, profile)
# test dotfiles in yaml file
y = self.load_yaml(confpath)
@@ -193,15 +194,15 @@ class TestImport(unittest.TestCase):
self.assertTrue(os.path.islink(dotfile7))
self.assertTrue(os.path.realpath(dotfile7) == indt7)
cmd_list_profiles(conf)
cmd_list_files(opts, conf)
_header()
cmd_list_profiles(o)
cmd_list_files(o)
# fake test update
editcontent = 'edited'
edit_content(dotfile1, editcontent)
opts['safe'] = False
cmd_update(opts, conf, [dotfile1])
o.safe = False
o.update_path = [dotfile1]
cmd_update(o)
c2 = open(indt1, 'r').read()
self.assertTrue(editcontent == c2)

View File

@@ -11,7 +11,7 @@ import filecmp
from dotdrop.config import Cfg
from tests.helpers import create_dir, get_string, get_tempdir, clean, \
create_random_file, load_config
create_random_file, load_options
from dotdrop.dotfile import Dotfile
from dotdrop.installer import Installer
from dotdrop.action import Action
@@ -181,12 +181,12 @@ exec bspwm
self.assertTrue(conf is not None)
# install them
conf, opts = load_config(confpath, profile)
opts['safe'] = False
opts['debug'] = True
opts['showdiff'] = True
opts['variables'] = {}
cmd_install(opts, conf)
o = load_options(confpath, profile)
o.safe = False
o.debug = True
o.showdiff = True
o.variables = {}
cmd_install(o)
# now compare the generated files
self.assertTrue(os.path.exists(dst1))
@@ -233,7 +233,7 @@ exec bspwm
self.assertTrue(tempcontent == header())
def test_link_children(self):
"""test the link children"""
# create source dir
src_dir = get_tempdir()
self.assertTrue(os.path.exists(src_dir))
@@ -257,6 +257,7 @@ exec bspwm
self.assertEqual(os.path.realpath(dst), src)
def test_fails_without_src(self):
"""test fails without src"""
src = '/some/non/existant/file'
installer = Installer()
@@ -272,7 +273,7 @@ exec bspwm
.format(src))
def test_fails_when_src_file(self):
"""test fails when src file"""
# create source dir
src_dir = get_tempdir()
self.assertTrue(os.path.exists(src_dir))
@@ -296,6 +297,7 @@ exec bspwm
.format(src))
def test_creates_dst(self):
"""test creates dst"""
src_dir = get_tempdir()
self.assertTrue(os.path.exists(src_dir))
self.addCleanup(clean, src_dir)
@@ -316,7 +318,7 @@ exec bspwm
self.assertTrue(os.path.exists(dst_dir))
def test_prompts_to_replace_dst(self):
"""test prompts to replace dst"""
# create source dir
src_dir = get_tempdir()
self.assertTrue(os.path.exists(src_dir))
@@ -354,7 +356,7 @@ exec bspwm
@patch('dotdrop.installer.Templategen')
def test_runs_templater(self, mocked_templategen):
"""test runs templater"""
# create source dir
src_dir = get_tempdir()
self.assertTrue(os.path.exists(src_dir))

View File

@@ -74,22 +74,26 @@ class TestListings(unittest.TestCase):
backup=self.CONFIG_BACKUP,
create=self.CONFIG_CREATE)
self.assertTrue(os.path.exists(confpath))
conf, opts = load_config(confpath, profile)
o = load_options(confpath, profile)
dfiles = [d1, d2, d3, d4, d5]
# import the files
cmd_importer(opts, conf, dfiles)
conf, opts = load_config(confpath, profile)
o.import_path = dfiles
cmd_importer(o)
o = load_options(confpath, profile)
# listfiles
cmd_list_profiles(conf)
cmd_list_profiles(o)
# list files
cmd_list_files(opts, conf, templateonly=False)
cmd_list_files(opts, conf, templateonly=True)
o.listfiles_templateonly = False
cmd_list_files(o)
o.listfiles_templateonly = True
cmd_list_files(o)
# details
cmd_detail(opts, conf, keys=None)
o.detail_keys = None
cmd_detail(o)
def main():

View File

@@ -12,7 +12,7 @@ from dotdrop.dotdrop import cmd_update
from dotdrop.dotdrop import cmd_importer
from tests.helpers import create_dir, get_string, get_tempdir, clean, \
create_random_file, create_fake_config, load_config, edit_content
create_random_file, create_fake_config, load_options, edit_content
class TestUpdate(unittest.TestCase):
@@ -67,12 +67,13 @@ class TestUpdate(unittest.TestCase):
backup=self.CONFIG_BACKUP,
create=self.CONFIG_CREATE)
self.assertTrue(os.path.exists(confpath))
conf, opts = load_config(confpath, profile)
o = load_options(confpath, profile)
dfiles = [d1, dir1, d2]
# import the files
cmd_importer(opts, conf, dfiles)
conf, opts = load_config(confpath, profile)
o.import_path = dfiles
cmd_importer(o)
o = load_options(confpath, profile)
# edit the files
edit_content(d1, 'newcontent')
@@ -87,9 +88,10 @@ class TestUpdate(unittest.TestCase):
create_random_file(dpath)
# update it
opts['safe'] = False
opts['debug'] = True
cmd_update(opts, conf, [d1, dir1])
o.safe = False
o.debug = True
o.update_path = [d1, dir1]
cmd_update(o)
# test content
newcontent = open(d1, 'r').read()
@@ -100,7 +102,7 @@ class TestUpdate(unittest.TestCase):
edit_content(d2, 'newcontentbykey')
# update it by key
dfiles = conf.get_dotfiles(profile)
dfiles = o.dotfiles
d2key = ''
for ds in dfiles:
t = os.path.expanduser(ds.dst)
@@ -108,9 +110,11 @@ class TestUpdate(unittest.TestCase):
d2key = ds.key
break
self.assertTrue(d2key != '')
opts['safe'] = False
opts['debug'] = True
cmd_update(opts, conf, [d2key], iskey=True)
o.safe = False
o.debug = True
o.update_path = [d2key]
o.iskey = True
cmd_update(o)
# test content
newcontent = open(d2, 'r').read()