1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
|
"""
author: deadc0de6 (https://github.com/deadc0de6)
Copyright (c) 2017, deadc0de6
helpers for the unittests
"""
import os
import random
import shutil
import string
import tempfile
from unittest import TestCase
from ruamel.yaml import YAML as yaml
from dotdrop.options import Options
from dotdrop.linktypes import LinkTypes
from dotdrop.utils import strip_home
TMPSUFFIX = '-dotdrop-tests'
class SubsetTestCase(TestCase):
"""test case"""
def assert_is_subset(self, sub, sup):
"""ensure it's a subset"""
for sub_key, sub_val in sub.items():
self.assertIn(sub_key, sup)
subvalue = sup[sub_key]
if isinstance(sub_val, str):
self.assertEqual(sub_val, subvalue)
continue
if isinstance(sub_val, dict):
self.assert_is_subset(sub_val, subvalue)
continue
try:
iter(sub_val)
self.assertTrue(all(
subItem in subvalue
for subItem in sub_val
))
except TypeError:
self.assertEqual(sub_val, subvalue)
def clean(path):
"""Delete file or directory"""
if not os.path.exists(path):
return
if os.path.islink(path):
os.remove(path)
elif os.path.isdir(path):
shutil.rmtree(path)
else:
os.remove(path)
def get_string(length):
"""Get a random string of length 'length'"""
alpha = string.ascii_uppercase + string.digits
temp = ''.join(random.choice(alpha) for _ in range(length))
return f'tmp.{temp}{TMPSUFFIX}'
def get_tempdir():
"""Get a temporary directory"""
tmpdir = tempfile.mkdtemp(suffix=TMPSUFFIX)
os.chmod(tmpdir, 0o755)
return tmpdir
def create_random_file(directory, content=None,
binary=False, template=False):
"""Create a new file in directory with random content"""
fname = get_string(8)
mode = 'w'
if binary:
mode = 'wb'
if content is None:
if binary:
pre = bytes()
if template:
pre = bytes('{{@@ header() @@}}\n', 'ascii')
content = bytes(f'{pre}{get_string(100)}\n', 'ascii')
else:
pre = ''
if template:
pre = '{{@@ header() @@}}\n'
content = f'{pre}{get_string(100)}\n'
path = os.path.join(directory, fname)
# pylint: disable=W1514
with open(path, mode) as file:
file.write(content)
return path, content
def edit_content(path, newcontent, binary=False):
"""edit file content"""
mode = 'w'
if binary:
mode = 'wb'
# pylint: disable=W1514
with open(path, mode) as file:
file.write(newcontent)
def create_dir(path):
"""Create a directory"""
if not os.path.exists(path):
os.mkdir(path)
return path
def _fake_args():
args = {}
args['--verbose'] = True
args['--no-banner'] = False
args['--dry'] = False
args['--force'] = False
args['--nodiff'] = False
args['--showdiff'] = True
args['--link'] = 'nolink'
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
args['--force-actions'] = False
args['--grepable'] = False
args['--as'] = None
args['--file-only'] = False
args['--workers'] = 1
args['--preserve-mode'] = False
args['--ignore-missing'] = False
args['--workdir-clear'] = False
args['--transw'] = ''
args['--transr'] = ''
args['--remove-existing'] = False
args['--dkey'] = ''
# cmds
args['profiles'] = False
args['files'] = False
args['install'] = False
args['uninstall'] = False
args['compare'] = False
args['import'] = False
args['update'] = False
args['detail'] = False
args['remove'] = False
args['gencfg'] = False
return args
def load_options(confpath, profile):
"""Load the config file from path"""
# create the fake args (bypass docopt)
args = _fake_args()
args['--cfg'] = confpath
args['--profile'] = profile
args['--verbose'] = True
# and get the options
opt = Options(args=args)
opt.profile = profile
opt.dry = False
opt.safe = False
opt.install_diff = True
opt.import_link = LinkTypes.NOLINK
opt.install_showdiff = True
opt.debug = True
opt.dotpath = os.path.join(os.path.dirname(confpath), 'dotfiles')
return opt
def get_path_strip_version(path):
"""Return the path of a file as stored in yaml config"""
path = strip_home(path)
path = path.lstrip('.' + os.sep)
return path
def get_dotfile_from_yaml(dic, path):
"""Return the dotfile from the yaml dictionary"""
# path is not the file in dotpath but on the FS
dotfiles = dic['dotfiles']
# src = get_path_strip_version(path)
home = os.path.expanduser('~')
if path.startswith(home):
path = path.replace(home, '~')
dotfile = [d for d in dotfiles.values() if d['dst'] == path]
if dotfile:
return dotfile[0]
return None
def yaml_dashed_list(items, indent=0):
"""yaml dashed list"""
ind = ' ' * indent
return ('\n'.join(f'{ind}- {item}' for item in items)
+ '\n')
def create_fake_config(directory, configname='config.yaml',
dotpath='dotfiles', backup=True, create=True,
import_configs=(), import_actions=(),
import_variables=()):
"""Create a fake config file"""
path = os.path.join(directory, configname)
workdir = os.path.join(directory, 'workdir')
with open(path, 'w', encoding='utf-8') as file:
file.write('config:\n')
file.write(f' backup: {backup}\n')
file.write(f' create: {create}\n')
file.write(f' dotpath: {dotpath}\n')
file.write(f' workdir: {workdir}\n')
if import_actions:
file.write(' import_actions:\n')
file.write(yaml_dashed_list(import_actions, 4))
if import_configs:
file.write(' import_configs:\n')
file.write(yaml_dashed_list(import_configs, 4))
if import_variables:
file.write(' import_variables:\n')
file.write(yaml_dashed_list(import_variables, 4))
file.write('dotfiles:\n')
file.write('profiles:\n')
file.write('actions:\n')
return path
def create_yaml_keyval(pairs, parent_dir=None, top_key=None):
"""create key val for yaml"""
if top_key:
pairs = {top_key: pairs}
if not parent_dir:
parent_dir = get_tempdir()
_, file_name = tempfile.mkstemp(dir=parent_dir, suffix='.yaml', text=True)
yaml_dump(pairs, file_name)
return file_name
# pylint: disable=W0102
def populate_fake_config(config, dotfiles={}, profiles={}, actions={},
trans_install={}, trans_update={}, variables={},
dynvariables={}):
"""Adds some juicy content to config files"""
is_path = isinstance(config, str)
if is_path:
config_path = config
config = yaml_load(config_path)
config['dotfiles'] = dotfiles
config['profiles'] = profiles
config['actions'] = actions
config['trans_install'] = trans_install
config['trans_update'] = trans_update
config['variables'] = variables
config['dynvariables'] = dynvariables
if is_path:
yaml_dump(config, config_path)
def file_in_yaml(yaml_file, path, link=False):
"""Return whether path is in the given yaml file as a dotfile."""
strip = get_path_strip_version(path)
if isinstance(yaml_file, str):
yaml_conf = yaml_load(yaml_file)
else:
yaml_conf = yaml_file
dotfiles = yaml_conf['dotfiles'].values()
in_src = any(x['src'].endswith(strip) for x in dotfiles)
in_dst = path in (os.path.expanduser(x['dst']) for x in dotfiles)
if link:
dotfile = get_dotfile_from_yaml(yaml_conf, path)
has_link = False
if dotfile:
has_link = 'link' in dotfile
else:
return False
return in_src and in_dst and has_link
return in_src and in_dst
def yaml_load(path):
"""load yaml"""
with open(path, 'r', encoding='utf-8') as file:
content = yaml(typ='safe').load(file)
return content
def yaml_dump(content, path):
"""dump yaml"""
with open(path, 'w', encoding='utf-8') as file:
cont = yaml()
cont.default_flow_style = False
cont.indent = 2
cont.typ = 'safe'
cont.dump(content, file)
|