def make_hierarchy(d): for key, val in d.items(): if isinstance(val, dict): os.mkdir(key) with cd(key): make_hierarchy(val) elif callable(val): val(key) else: with open(key, 'w') as fd: fd.write(val)
def _test_existing(content, modified=False, force=False): with temp_cwd(prefix='pot-test'): make_hierarchy({ 'pot': { 'dotfiles': { 'dotfile': '' }, 'config.yaml': 'dotfiles: [{name: dotfile}]' }, 'home': { 'dotfile': content } }) # default system mtime resolution is not sufficient # time.sleep(2) mgr = assert_modified if modified else assert_not_modified with mgr('home/dotfile'): with updated_env(HOME=os.path.abspath('home')): with cd('pot'): pot.install(['dotfile'], force=force)
def test_install(): config = """\ dotfiles: - name: .vimrc target: ~/.vimrc action: symlink - name: .bashrc target: ~/.bashrc action: include - name: .vim target: ../somedir/vimfiles action: copy """ with temp_cwd(prefix='pot-test'): make_hierarchy({ 'pot': { 'dotfiles': { '.vim': {}, '.vimrc': '1\n', '.bashrc': '2\n', '.gitconfig': '' }, 'config.yaml': config }, 'home': { '.gitconfig': 'already exists\n', '.bashrc': '' }, 'somedir': {} }) with assert_not_modified('home/.gitconfig'): with updated_env(HOME=os.path.abspath('home')): with cd('pot'): pot.install() # symlink pointing to correct location created eq_(os.readlink('home/.vimrc'), os.path.abspath('pot/dotfiles/.vimrc')) # new directory created ok_(os.path.isdir('somedir/vimfiles')) # configuration included eq_(open('home/.bashrc').read(), '. {}\n'.format(os.path.abspath('pot/dotfiles/.bashrc')))
def temp_cwd(suffix='', prefix='tmp', dir=None): with make_temp_dir(suffix, prefix, dir) as tempd: with cd(tempd): yield tempd