xonsh/tests/test_environ.py

196 lines
6.1 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
2015-05-14 18:18:46 -05:00
"""Tests the xonsh environment."""
from __future__ import unicode_literals, print_function
import os
2016-05-16 00:43:27 -04:00
import tempfile
import builtins
from tempfile import TemporaryDirectory
from xonsh.tools import ON_WINDOWS
import pytest
from xonsh.environ import (Env, load_static_config,
locate_binary, partial_format_prompt)
2015-05-14 18:18:46 -05:00
from tools import skip_if_on_unix
2016-05-16 02:02:24 -04:00
2015-05-14 18:18:46 -05:00
def test_env_normal():
env = Env(VAR='wakka')
2016-06-22 17:14:27 -04:00
assert 'wakka' == env['VAR']
2015-05-14 18:18:46 -05:00
2016-07-01 12:42:07 +03:00
@pytest.mark.parametrize('path', [['/home/wakka'], ['wakka']])
def test_env_path_list(path):
env = Env(MYPATH=path)
assert path == env['MYPATH'].paths
@pytest.mark.parametrize('path', [
['/home/wakka' + os.pathsep + '/home/jawaka'],
['wakka' + os.pathsep + 'jawaka']
])
def test_env_path_str(path):
env = Env(MYPATH=path)
assert path == env['MYPATH'].paths
2015-05-14 18:18:46 -05:00
def test_env_detype():
env = Env(MYPATH=['wakka', 'jawaka'])
2016-06-22 17:14:27 -04:00
assert 'wakka' + os.pathsep + 'jawaka' == env.detype()['MYPATH']
2015-05-14 18:18:46 -05:00
2016-07-01 12:42:07 +03:00
@pytest.mark.parametrize('path1, path2',[
(['/home/wakka', '/home/jawaka'], '/home/woah'),
(['wakka', 'jawaka'], 'woah')
])
def test_env_detype_mutable_access_clear(path1, path2):
env = Env(MYPATH=path1)
assert path1[0] + os.pathsep + path1[1] == env.detype()['MYPATH']
env['MYPATH'][0] = path2
2016-06-22 17:14:27 -04:00
assert env._detyped is None
2016-07-01 12:42:07 +03:00
assert path2 + os.pathsep + path1[1] == env.detype()['MYPATH']
2015-05-14 18:18:46 -05:00
def test_env_detype_no_dict():
env = Env(YO={'hey': 42})
det = env.detype()
2016-06-22 17:14:27 -04:00
assert 'YO' not in det
2015-05-14 18:18:46 -05:00
2016-07-02 03:36:32 +03:00
@pytest.mark.parametrize('formatter_dict',[{
2016-07-01 12:42:07 +03:00
'a_string': 'cat',
'none': (lambda: None),
'f': (lambda: 'wakka'),
2016-07-02 03:36:32 +03:00
}])
2016-07-01 12:42:07 +03:00
@pytest.mark.parametrize('inp, exp', [
('my {a_string}', 'my cat'),
('my {none}{a_string}', 'my cat'),
('{f} jawaka', 'wakka jawaka'),
])
2016-07-02 03:36:32 +03:00
def test_format_prompt(inp, exp, formatter_dict):
obs = partial_format_prompt(template=inp, formatter_dict=formatter_dict)
2016-07-01 12:42:07 +03:00
assert exp == obs
2016-07-02 03:36:32 +03:00
@pytest.mark.parametrize('formatter_dict',[{
'a_string': 'cats',
'a_number': 7,
'empty': '',
'current_job': (lambda: 'sleep'),
'none': (lambda: None),
2016-07-02 03:36:32 +03:00
}])
@pytest.mark.parametrize('inp, exp', [
('{a_number:{0:^3}}cats', ' 7 cats'),
( '{current_job:{} | }xonsh', 'sleep | xonsh'),
( '{none:{} | }{a_string}{empty:!}', 'cats!'),
( '{none:{}}', ''),
( '{{{a_string:{{{}}}}}}', '{{cats}}'),
( '{{{none:{{{}}}}}}', '{}'),
])
2016-07-02 03:36:32 +03:00
def test_format_prompt_with_format_spec(inp, exp, formatter_dict):
2016-07-01 12:42:07 +03:00
obs = partial_format_prompt(template=inp, formatter_dict=formatter_dict)
assert exp == obs
def test_format_prompt_with_broken_template():
for p in ('{user', '{user}{hostname'):
2016-06-22 17:14:27 -04:00
assert partial_format_prompt(p) == p
# '{{user' will be parsed to '{user'
for p in ('{{user}', '{{user'):
2016-06-22 17:14:27 -04:00
assert 'user' in partial_format_prompt(p)
def test_format_prompt_with_broken_template_in_func():
for p in (
lambda: '{user',
lambda: '{{user',
lambda: '{{user}',
lambda: '{user}{hostname',
):
# '{{user' will be parsed to '{user'
2016-06-22 17:14:27 -04:00
assert 'user' in partial_format_prompt(p)
2015-05-14 18:18:46 -05:00
2016-06-09 11:44:50 +08:00
def test_format_prompt_with_invalid_func():
def p():
foo = bar # raises exception
return '{user}'
2016-06-22 17:14:27 -04:00
assert isinstance(partial_format_prompt(p), str)
2016-06-09 11:44:50 +08:00
2016-07-03 22:03:06 +03:00
def test_histcontrol_none():
2015-11-28 16:12:41 -07:00
env = Env(HISTCONTROL=None)
2016-06-22 17:14:27 -04:00
assert isinstance(env['HISTCONTROL'], set)
assert len(env['HISTCONTROL']) == 0
2015-11-28 16:12:41 -07:00
2016-07-01 12:42:07 +03:00
def test_HISTCONTROL_empty():
env = Env(HISTCONTROL='')
2016-06-22 17:14:27 -04:00
assert isinstance(env['HISTCONTROL'], set)
assert len(env['HISTCONTROL']) == 0
2015-11-28 16:12:41 -07:00
2016-07-03 22:03:06 +03:00
def test_histcontrol_ignoredups():
env = Env(HISTCONTROL='ignoredups')
2016-06-22 17:14:27 -04:00
assert isinstance(env['HISTCONTROL'], set)
assert len(env['HISTCONTROL']) == 1
assert ('ignoredups' in env['HISTCONTROL'])
assert ('ignoreerr' not in env['HISTCONTROL'])
2015-11-28 16:12:41 -07:00
2016-07-03 22:03:06 +03:00
def test_histcontrol_ignoreerr_ignoredups():
env = Env(HISTCONTROL='ignoreerr,ignoredups,ignoreerr')
2016-06-22 17:14:27 -04:00
assert len(env['HISTCONTROL']) == 2
assert ('ignoreerr' in env['HISTCONTROL'])
assert ('ignoredups' in env['HISTCONTROL'])
2015-11-28 16:12:41 -07:00
def test_swap():
env = Env(VAR='wakka')
2016-06-22 17:14:27 -04:00
assert env['VAR'] == 'wakka'
# positional arg
with env.swap({'VAR': 'foo'}):
2016-06-22 17:14:27 -04:00
assert env['VAR'] == 'foo'
# make sure the environment goes back outside the context manager
2016-06-22 17:14:27 -04:00
assert env['VAR'] == 'wakka'
# kwargs only
with env.swap(VAR1='foo', VAR2='bar'):
2016-06-22 17:14:27 -04:00
assert env['VAR1'] == 'foo'
assert env['VAR2'] == 'bar'
# positional and kwargs
with env.swap({'VAR3': 'baz'}, VAR1='foo', VAR2='bar'):
2016-06-22 17:14:27 -04:00
assert env['VAR1'] == 'foo'
assert env['VAR2'] == 'bar'
assert env['VAR3'] == 'baz'
# make sure the environment goes back outside the context manager
2016-06-22 17:14:27 -04:00
assert env['VAR'] == 'wakka'
assert 'VAR1' not in env
assert 'VAR2' not in env
assert 'VAR3' not in env
@pytest.mark.parametrize('s, exp, loaded',[
(b'{"best": "awash"}', {'best': 'awash'}, True), # works
(b'["best", "awash"]', {}, False), # fail
(b'{"best": "awash"', {}, False) # json fail
])
def test_load_static_config(s, exp, loaded, tmpdir, xonsh_builtins):
2016-05-23 08:42:09 +02:00
env = Env({'XONSH_SHOW_TRACEBACK': False})
xonsh_builtins.__xonsh_env__ = env
f = tmpdir.join('test_static_config')
f.write(s)
conf = load_static_config(env, str(f))
2016-06-22 17:14:27 -04:00
assert exp == conf
assert env['LOADED_CONFIG'] == loaded
2016-05-16 02:02:24 -04:00
@skip_if_on_unix
2016-07-01 12:42:07 +03:00
def test_locate_binary_on_windows(xonsh_builtins):
files = ('file1.exe', 'FILE2.BAT', 'file3.txt')
with TemporaryDirectory() as tmpdir:
for fname in files:
fpath = os.path.join(tmpdir, fname)
with open(fpath, 'w') as f:
f.write(fpath)
xonsh_builtins.__xonsh_env__.update({
'PATH': [tmpdir],
'PATHEXT': ['.COM', '.EXE', '.BAT'],
})
2016-07-03 22:03:06 +03:00
assert locate_binary('file1') == os.path.join(tmpdir,'file1.exe')
assert locate_binary('file1.exe') == os.path.join(tmpdir,'file1.exe')
assert locate_binary('file2') == os.path.join(tmpdir,'FILE2.BAT')
assert locate_binary('file2.bat') == os.path.join(tmpdir,'FILE2.BAT')
assert locate_binary('file3') is None