本文整理汇总了Python中tools.mock_xonsh_env函数的典型用法代码示例。如果您正苦于以下问题:Python mock_xonsh_env函数的具体用法?Python mock_xonsh_env怎么用?Python mock_xonsh_env使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了mock_xonsh_env函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_env_path
def test_env_path():
def expand(path):
return os.path.expanduser(os.path.expandvars(path))
getitem_cases = [
('xonsh_dir', 'xonsh_dir'),
('.', '.'),
('../', '../'),
('~/', '~/'),
(b'~/../', '~/../'),
]
with mock_xonsh_env(TOOLS_ENV):
for inp, exp in getitem_cases:
obs = EnvPath(inp)[0] # call to __getitem__
assert expand(exp) == obs
with mock_xonsh_env(ENCODE_ENV_ONLY):
for inp, exp in getitem_cases:
obs = EnvPath(inp)[0] # call to __getitem__
assert exp == obs
# cases that involve path-separated strings
multipath_cases = [
(os.pathsep.join(['xonsh_dir', '../', '.', '~/']),
['xonsh_dir', '../', '.', '~/']),
('/home/wakka' + os.pathsep + '/home/jakka' + os.pathsep + '~/',
['/home/wakka', '/home/jakka', '~/'])
]
with mock_xonsh_env(TOOLS_ENV):
for inp, exp in multipath_cases:
obs = [i for i in EnvPath(inp)]
assert [expand(i) for i in exp] == obs
with mock_xonsh_env(ENCODE_ENV_ONLY):
for inp, exp in multipath_cases:
obs = [i for i in EnvPath(inp)]
assert [i for i in exp] == obs
# cases that involve pathlib.Path objects
pathlib_cases = [
(pathlib.Path('/home/wakka'), ['/home/wakka'.replace('/', os.sep)]),
(pathlib.Path('~/'), ['~']),
(pathlib.Path('.'), ['.']),
(['/home/wakka', pathlib.Path('/home/jakka'), '~/'],
['/home/wakka', '/home/jakka'.replace('/', os.sep), '~/']),
(['/home/wakka', pathlib.Path('../'), '../'],
['/home/wakka', '..', '../']),
(['/home/wakka', pathlib.Path('~/'), '~/'],
['/home/wakka', '~', '~/']),
]
with mock_xonsh_env(TOOLS_ENV):
for inp, exp in pathlib_cases:
# iterate over EnvPath to acquire all expanded paths
obs = [i for i in EnvPath(inp)]
assert [expand(i) for i in exp] == obs
示例2: test_login_shell
def test_login_shell():
def Shell(*args, **kwargs):
pass
with patch('xonsh.main.Shell', Shell), mock_xonsh_env({}):
xonsh.main.premain([])
assert_false(builtins.__xonsh_env__.get('XONSH_LOGIN'))
with patch('xonsh.main.Shell', Shell), mock_xonsh_env({}):
xonsh.main.premain(['-l'])
assert_true(builtins.__xonsh_env__.get('XONSH_LOGIN'))
示例3: test_repath_home_var_brace
def test_repath_home_var_brace():
exp = os.path.expanduser('~')
env = Env(HOME=exp)
with mock_xonsh_env(env):
obs = pathsearch(regexsearch, '${"HOME"}')
assert 1 == len(obs)
assert exp == obs[0]
示例4: test_histcontrol
def test_histcontrol():
"""Test HISTCONTROL=ignoredups,ignoreerr"""
FNAME = 'xonsh-SESSIONID.json'
FNAME += '.append'
hist = History(filename=FNAME, here='yup', **HIST_TEST_KWARGS)
with mock_xonsh_env({'HISTCONTROL': 'ignoredups,ignoreerr'}):
assert len(hist.buffer) == 0
# An error, buffer remains empty
hist.append({'inp': 'ls foo', 'rtn': 2})
assert len(hist.buffer) == 0
# Success
hist.append({'inp': 'ls foobazz', 'rtn': 0})
assert len(hist.buffer) == 1
assert 'ls foobazz' == hist.buffer[-1]['inp']
assert 0 == hist.buffer[-1]['rtn']
# Error
hist.append({'inp': 'ls foo', 'rtn': 2})
assert len(hist.buffer) == 1
assert 'ls foobazz' == hist.buffer[-1]['inp']
assert 0 == hist.buffer[-1]['rtn']
# File now exists, success
hist.append({'inp': 'ls foo', 'rtn': 0})
assert len(hist.buffer) == 2
assert 'ls foo' == hist.buffer[-1]['inp']
assert 0 == hist.buffer[-1]['rtn']
# Success
hist.append({'inp': 'ls', 'rtn': 0})
assert len(hist.buffer) == 3
assert 'ls' == hist.buffer[-1]['inp']
assert 0 == hist.buffer[-1]['rtn']
# Dup
hist.append({'inp': 'ls', 'rtn': 0})
assert len(hist.buffer) == 3
# Success
hist.append({'inp': '/bin/ls', 'rtn': 0})
assert len(hist.buffer) == 4
assert '/bin/ls' == hist.buffer[-1]['inp']
assert 0 == hist.buffer[-1]['rtn']
# Error
hist.append({'inp': 'ls bazz', 'rtn': 1})
assert len(hist.buffer) == 4
assert '/bin/ls' == hist.buffer[-1]['inp']
assert 0 == hist.buffer[-1]['rtn']
# Error
hist.append({'inp': 'ls bazz', 'rtn': -1})
assert len(hist.buffer) == 4
assert '/bin/ls' == hist.buffer[-1]['inp']
assert 0 == hist.buffer[-1]['rtn']
os.remove(FNAME)
示例5: test_iglobpath
def test_iglobpath():
with TemporaryDirectory() as test_dir:
# Create files 00.test to 99.test in unsorted order
num = 18
for _ in range(100):
s = str(num).zfill(2)
path = os.path.join(test_dir, s + '.test')
with open(path, 'w') as file:
file.write(s + '\n')
num = (num + 37) % 100
# Create one file not matching the '*.test'
with open(os.path.join(test_dir, '07'), 'w') as file:
file.write('test\n')
with mock_xonsh_env(Env(EXPAND_ENV_VARS=False)):
builtins.__xonsh_expand_path__ = expand_path
paths = list(iglobpath(os.path.join(test_dir, '*.test'),
ignore_case=False, sort_result=False))
assert len(paths) == 100
paths = list(iglobpath(os.path.join(test_dir, '*'),
ignore_case=True, sort_result=False))
assert len(paths) == 101
paths = list(iglobpath(os.path.join(test_dir, '*.test'),
ignore_case=False, sort_result=True))
assert len(paths) == 100
assert paths == sorted(paths)
paths = list(iglobpath(os.path.join(test_dir, '*'),
ignore_case=True, sort_result=True))
assert len(paths) == 101
assert paths == sorted(paths)
示例6: test_histcontrol
def test_histcontrol():
"""Test HISTCONTROL=ignoredups,ignoreerr"""
FNAME = 'xonsh-SESSIONID.json'
FNAME += '.append'
hist = History(filename=FNAME, here='yup', **HIST_TEST_KWARGS)
with mock_xonsh_env({'HISTCONTROL': 'ignoredups,ignoreerr'}):
yield assert_equal, len(hist.buffer), 0
# An error, buffer remains empty
hist.append({'inp': 'ls foo', 'rtn': 2})
yield assert_equal, len(hist.buffer), 0
# Success
hist.append({'inp': 'ls foobazz', 'rtn': 0})
yield assert_equal, len(hist.buffer), 1
yield assert_equal, 'ls foobazz', hist.buffer[-1]['inp']
yield assert_equal, 0, hist.buffer[-1]['rtn']
# Error
hist.append({'inp': 'ls foo', 'rtn': 2})
yield assert_equal, len(hist.buffer), 1
yield assert_equal, 'ls foobazz', hist.buffer[-1]['inp']
yield assert_equal, 0, hist.buffer[-1]['rtn']
# File now exists, success
hist.append({'inp': 'ls foo', 'rtn': 0})
yield assert_equal, len(hist.buffer), 2
yield assert_equal, 'ls foo', hist.buffer[-1]['inp']
yield assert_equal, 0, hist.buffer[-1]['rtn']
# Success
hist.append({'inp': 'ls', 'rtn': 0})
yield assert_equal, len(hist.buffer), 3
yield assert_equal, 'ls', hist.buffer[-1]['inp']
yield assert_equal, 0, hist.buffer[-1]['rtn']
# Dup
hist.append({'inp': 'ls', 'rtn': 0})
yield assert_equal, len(hist.buffer), 3
# Success
hist.append({'inp': '/bin/ls', 'rtn': 0})
yield assert_equal, len(hist.buffer), 4
yield assert_equal, '/bin/ls', hist.buffer[-1]['inp']
yield assert_equal, 0, hist.buffer[-1]['rtn']
# Error
hist.append({'inp': 'ls bazz', 'rtn': 1})
yield assert_equal, len(hist.buffer), 4
yield assert_equal, '/bin/ls', hist.buffer[-1]['inp']
yield assert_equal, 0, hist.buffer[-1]['rtn']
# Error
hist.append({'inp': 'ls bazz', 'rtn': -1})
yield assert_equal, len(hist.buffer), 4
yield assert_equal, '/bin/ls', hist.buffer[-1]['inp']
yield assert_equal, 0, hist.buffer[-1]['rtn']
os.remove(FNAME)
示例7: test_eval_recursive_callable_partial
def test_eval_recursive_callable_partial():
if ON_WINDOWS:
raise SkipTest
built_ins.ENV = Env(HOME=os.path.expanduser('~'))
with mock_xonsh_env(built_ins.ENV):
assert_equal(ALIASES.get('indirect_cd')(['arg2', 'arg3']),
['..', 'arg2', 'arg3'])
示例8: test_man_completion
def test_man_completion():
if ON_WINDOWS:
raise SkipTest
with mock_xonsh_env({}):
man_completer = ManCompleter()
completions = man_completer.option_complete('--', 'yes')
assert_true('--version' in completions)
assert_true('--help' in completions)
示例9: check_load_static_config
def check_load_static_config(s, exp, loaded):
env = {'XONSH_SHOW_TRACEBACK': False}
with tempfile.NamedTemporaryFile() as f, mock_xonsh_env(env):
f.write(s)
f.flush()
conf = load_static_config(env, f.name)
assert_equal(exp, conf)
assert_equal(env['LOADED_CONFIG'], loaded)
示例10: test_repath_home_contents
def test_repath_home_contents():
home = os.path.expanduser('~')
env = Env(HOME=home)
with mock_xonsh_env(env):
exp = os.listdir(home)
exp = {os.path.join(home, p) for p in exp}
obs = set(pathsearch(regexsearch, '~/.*'))
assert exp == obs
示例11: test_man_completion
def test_man_completion():
if ON_WINDOWS:
raise SkipTest
with tempfile.TemporaryDirectory() as tempdir:
with mock_xonsh_env({'XONSH_DATA_DIR': tempdir}):
completions = complete_from_man('--', 'yes --', 4, 6, __xonsh_env__)
assert_true('--version' in completions)
assert_true('--help' in completions)
示例12: check_xonsh_ast
def check_xonsh_ast(xenv, inp, run=True, mode='eval'):
with mock_xonsh_env(xenv):
obs = PARSER.parse(inp, debug_level=DEBUG_LEVEL)
if obs is None:
return # comment only
bytecode = compile(obs, '<test-xonsh-ast>', mode)
if run:
exec(bytecode)
示例13: test_repath_home_var
def test_repath_home_var():
if ON_WINDOWS:
raise SkipTest
exp = os.path.expanduser('~')
built_ins.ENV = Env(HOME=exp)
with mock_xonsh_env(built_ins.ENV):
obs = pathsearch(regexsearch, '$HOME')
assert_equal(1, len(obs))
assert_equal(exp, obs[0])
示例14: test_repath_backslash
def test_repath_backslash():
home = os.path.expanduser('~')
env = Env(HOME=home)
with mock_xonsh_env(env):
exp = os.listdir(home)
exp = {p for p in exp if re.match(r'\w\w.*', p)}
exp = {os.path.join(home, p) for p in exp}
obs = set(pathsearch(regexsearch, r'~/\w\w.*'))
assert exp == obs
示例15: test_repath_home_var_brace
def test_repath_home_var_brace():
if ON_WINDOWS:
raise SkipTest
exp = os.path.expanduser('~')
built_ins.ENV = Env(HOME=exp)
with mock_xonsh_env(built_ins.ENV):
obs = regexpath('${"HOME"}')
assert_equal(1, len(obs))
assert_equal(exp, obs[0])