def test_link_children(self): # create source dir src_dir = get_tempdir() self.assertTrue(os.path.exists(src_dir)) self.addCleanup(clean, src_dir) # where dotfiles will be installed dst_dir = get_tempdir() self.assertTrue(os.path.exists(dst_dir)) self.addCleanup(clean, dst_dir) # create 3 random files in source srcs = [create_random_file(src_dir)[0] for _ in range(3)] installer = Installer() installer.linkall(templater=MagicMock(), src=src_dir, dst=dst_dir, actions=[]) # Ensure all destination files point to source for src in srcs: dst = os.path.join(dst_dir, src) self.assertEqual(os.path.realpath(dst), src)
def test_runs_templater(self, mocked_templategen): # create source dir src_dir = get_tempdir() self.assertTrue(os.path.exists(src_dir)) self.addCleanup(clean, src_dir) # where dotfiles will be installed dst_dir = get_tempdir() self.assertTrue(os.path.exists(dst_dir)) self.addCleanup(clean, dst_dir) # create 3 random files in source srcs = [create_random_file(src_dir)[0] for _ in range(3)] # setup installer and mocks installer = Installer() templater = MagicMock() templater.generate.return_value = b'content' # make templategen treat everything as a template mocked_templategen.is_template.return_value = True installer.linkall(templater=templater, src=src_dir, dst=dst_dir, actions=[]) for src in srcs: dst = os.path.join(dst_dir, os.path.basename(src)) # ensure dst is link self.assertTrue(os.path.islink(dst)) # ensure dst not directly linked to src # TODO: maybe check that its actually linked to template folder self.assertNotEqual(os.path.realpath(dst), src)
def test_fails_when_src_file(self): # create source dir src_dir = get_tempdir() self.assertTrue(os.path.exists(src_dir)) self.addCleanup(clean, src_dir) src = create_random_file(src_dir)[0] logger = MagicMock() templater = MagicMock() installer = Installer() installer.log.err = logger # pass src file not src dir res = installer.linkall(templater=templater, src=src, dst='/dev/null', actions=[]) # ensure nothing performed self.assertEqual(res, []) # ensure logger logged error logger.assert_called_with( 'source dotfile is not a directory: {}'.format(src))
def test_creates_dst(self): src_dir = get_tempdir() self.assertTrue(os.path.exists(src_dir)) self.addCleanup(clean, src_dir) # where dotfiles will be installed dst_dir = get_tempdir() self.addCleanup(clean, dst_dir) # move dst dir to new (uncreated) dir in dst dst_dir = os.path.join(dst_dir, get_string(6)) self.assertFalse(os.path.exists(dst_dir)) installer = Installer() installer.linkall(templater=MagicMock(), src=src_dir, dst=dst_dir, actions=[]) # ensure dst dir created self.assertTrue(os.path.exists(dst_dir))
def test_prompts_to_replace_dst(self): # create source dir src_dir = get_tempdir() self.assertTrue(os.path.exists(src_dir)) self.addCleanup(clean, src_dir) # where dotfiles will be installed dst_dir = get_tempdir() self.addCleanup(clean, dst_dir) # Create destination file to be replaced dst = os.path.join(dst_dir, get_string(6)) with open(dst, 'w'): pass self.assertTrue(os.path.isfile(dst)) # setup mocks ask = MagicMock() ask.return_value = True # setup installer installer = Installer() installer.safe = True installer.log.ask = ask installer.linkall(templater=MagicMock(), src=src_dir, dst=dst, actions=[]) # ensure destination now a directory self.assertTrue(os.path.isdir(dst)) # ensure prompted ask.assert_called_with( 'Remove regular file {} and replace with empty directory?'.format( dst))
def test_fails_without_src(self): src = '/some/non/existant/file' installer = Installer() logger = MagicMock() installer.log.err = logger res = installer.linkall(templater=MagicMock(), src=src, dst='/dev/null', actions=[]) self.assertEqual(res, []) logger.assert_called_with( 'source dotfile does not exist: {}'.format(src))
def cmd_install(opts, conf, temporary=False, keys=[]): """install dotfiles for this profile""" dotfiles = conf.get_dotfiles(opts['profile']) if keys: # filtered dotfiles to install dotfiles = [d for d in dotfiles if d.key in set(keys)] if not dotfiles: msg = 'no dotfile to install for this profile (\"{}\")' LOG.warn(msg.format(opts['profile'])) return False t = Templategen(base=opts['dotpath'], variables=opts['variables'], debug=opts['debug']) tmpdir = None if temporary: tmpdir = get_tmpdir() inst = Installer(create=opts['create'], backup=opts['backup'], dry=opts['dry'], safe=opts['safe'], base=opts['dotpath'], workdir=opts['workdir'], diff=opts['installdiff'], debug=opts['debug'], totemp=tmpdir, showdiff=opts['showdiff']) installed = [] for dotfile in dotfiles: preactions = [] if not temporary and dotfile.actions \ and Cfg.key_actions_pre in dotfile.actions: for action in dotfile.actions[Cfg.key_actions_pre]: preactions.append(action) if opts['debug']: LOG.dbg('installing {}'.format(dotfile)) if hasattr(dotfile, 'link') and dotfile.link == LinkTypes.PARENTS: r = inst.link(t, dotfile.src, dotfile.dst, actions=preactions) elif hasattr(dotfile, 'link') and dotfile.link == LinkTypes.CHILDREN: r = inst.linkall(t, dotfile.src, dotfile.dst, actions=preactions) else: src = dotfile.src tmp = None if dotfile.trans_r: tmp = apply_trans(opts, dotfile) if not tmp: continue src = tmp r = inst.install(t, src, dotfile.dst, actions=preactions, noempty=dotfile.noempty) if tmp: tmp = os.path.join(opts['dotpath'], tmp) if os.path.exists(tmp): remove(tmp) if len(r) > 0: if not temporary and Cfg.key_actions_post in dotfile.actions: actions = dotfile.actions[Cfg.key_actions_post] # execute action for action in actions: if opts['dry']: LOG.dry('would execute action: {}'.format(action)) else: if opts['debug']: LOG.dbg('executing post action {}'.format(action)) action.execute() installed.extend(r) if temporary: LOG.log('\nInstalled to tmp \"{}\".'.format(tmpdir)) LOG.log('\n{} dotfile(s) installed.'.format(len(installed))) return True