1
0
mirror of https://github.com/deadc0de6/dotdrop.git synced 2026-02-04 16:49:42 +00:00

Fixing src path in dotfiles to allow different dotpaths than the current one

This commit is contained in:
Davide Laezza
2019-04-23 12:55:23 +02:00
parent 2d66e1847e
commit 649575116b
4 changed files with 114 additions and 9 deletions

View File

@@ -352,8 +352,12 @@ class Cfg:
dotfiles = self.content[self.key_dotfiles]
noempty_default = self.lnk_settings[self.key_ignoreempty]
dotpath = self.content['config']['dotpath']
for k, v in dotfiles.items():
src = os.path.normpath(v[self.key_dotfiles_src])
src = v[self.key_dotfiles_src]
if dotpath not in src:
src = os.path.join(dotpath, src)
src = os.path.normpath(self._abs_path(src))
dst = os.path.normpath(v[self.key_dotfiles_dst])
# Fail if both `link` and `link_children` present

View File

@@ -148,7 +148,6 @@ def load_options(confpath, profile):
o = Options(args=args)
o.profile = profile
o.dry = False
o.profile = profile
o.safe = True
o.install_diff = True
o.import_link = LinkTypes.NOLINK
@@ -221,9 +220,9 @@ def create_yaml_keyval(pairs, parent_dir=None, top_key=None):
return file_name
def populate_fake_config(config, dotfiles=(), profiles=(), actions=(),
trans=(), trans_write=(), variables=(),
dynvariables=()):
def populate_fake_config(config, dotfiles={}, profiles={}, actions={},
trans={}, trans_write={}, variables={},
dynvariables={}):
"""Adds some juicy content to config files"""
is_path = isinstance(config, str)
if is_path:

View File

@@ -39,8 +39,8 @@ class TestImport(unittest.TestCase):
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 o.dotfiles])
dsts = [os.path.expanduser(x.dst) for x in o.dotfiles]
self.assertTrue(any(x.src.endswith(strip) 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):

View File

@@ -10,8 +10,9 @@ from unittest.mock import MagicMock, patch
import filecmp
from dotdrop.config import Cfg
from tests.helpers import create_dir, get_string, get_tempdir, clean, \
create_random_file, load_options
from tests.helpers import (clean, create_dir, create_fake_config,
create_random_file, get_string, get_tempdir,
load_options, populate_fake_config)
from dotdrop.dotfile import Dotfile
from dotdrop.installer import Installer
from dotdrop.action import Action
@@ -231,6 +232,107 @@ exec bspwm
tempcontent = open(dst10, 'r').read().rstrip()
self.assertTrue(tempcontent == header())
def test_install_import_configs(self):
"""Test the install function with imported configs"""
# dotpath location
tmp = get_tempdir()
self.assertTrue(os.path.exists(tmp))
self.addCleanup(clean, tmp)
os.mkdir(os.path.join(tmp, 'importing'))
os.mkdir(os.path.join(tmp, 'imported'))
# where dotfiles will be installed
dst = get_tempdir()
self.assertTrue(os.path.exists(dst))
self.addCleanup(clean, dst)
# creating random dotfiles
imported_dotfile, _ = create_random_file(os.path.join(tmp, 'imported'))
imported_dotfile = {
'dst': os.path.join(dst, imported_dotfile),
'key': 'f_{}'.format(imported_dotfile),
'name': imported_dotfile,
'src': os.path.join(tmp, 'imported', imported_dotfile),
}
importing_dotfile, _ = \
create_random_file(os.path.join(tmp, 'importing'))
importing_dotfile = {
'dst': os.path.join(dst, importing_dotfile),
'key': 'f_{}'.format(importing_dotfile),
'name': importing_dotfile,
'src': os.path.join(tmp, 'imported', importing_dotfile),
}
imported = {
'config': {
'dotpath': 'imported',
},
'dotfiles': {
imported_dotfile['key']: {
'dst': imported_dotfile['dst'],
'src': imported_dotfile['name'],
},
},
'profiles': {
'host1': {
'dotfiles': [imported_dotfile['key']],
},
},
}
importing = {
'config': {
'dotpath': 'importing',
},
'dotfiles': {
importing_dotfile['key']: {
'dst': importing_dotfile['dst'],
'src': importing_dotfile['src'],
},
},
'profiles': {
'host2': {
'dotfiles': [importing_dotfile['key']],
'include': ['host1'],
},
},
}
# create the imported base config file
imported_path = create_fake_config(tmp,
configname='config-2.yaml',
**imported['config'])
# create the importing base config file
importing_path = create_fake_config(tmp,
configname='config.yaml',
import_configs=('config-*.yaml',),
**importing['config'])
# edit the imported config
populate_fake_config(imported_path, **{
k: v
for k, v in imported.items()
if k != 'config'
})
# edit the importing config
populate_fake_config(importing_path, **{
k: v
for k, v in importing.items()
if k != 'config'
})
# install them
o = load_options(importing_path, 'host2')
o.safe = False
o.install_showdiff = True
o.variables = {}
cmd_install(o)
# now compare the generated files
self.assertTrue(os.path.exists(importing_dotfile['dst']))
self.assertTrue(os.path.exists(imported_dotfile['dst']))
def test_link_children(self):
"""test the link children"""
# create source dir