2015-11-16 14:04:32 -08:00
|
|
|
# -*- coding: utf-8 -*-
|
2015-05-14 18:18:46 -05:00
|
|
|
"""Tests the xonsh environment."""
|
|
|
|
from __future__ import unicode_literals, print_function
|
|
|
|
import os
|
2017-03-15 09:09:08 -04:00
|
|
|
import itertools
|
2016-06-07 15:49:23 +02:00
|
|
|
from tempfile import TemporaryDirectory
|
2020-05-05 06:42:28 -04:00
|
|
|
from xonsh.tools import always_true
|
2016-06-07 15:49:23 +02:00
|
|
|
|
2016-06-27 18:39:10 +03:00
|
|
|
import pytest
|
|
|
|
|
2016-09-25 15:54:31 -04:00
|
|
|
from xonsh.commands_cache import CommandsCache
|
2018-08-30 09:18:49 -05:00
|
|
|
from xonsh.environ import (
|
|
|
|
Env,
|
|
|
|
locate_binary,
|
Environment variable registration, deregistration (addresses #3227) (#3377)
* First attempt at register/deregister machinery for envvars
* Added detailed docstring, simplified ensurers
Also added some type checking that became clear from docstring writing.
* Changes in response to @scopatz review
Simplified kwarg names.
* defaultval -> default
* Created new Var namedtuple, as well as DEFAULT_VARS
We should now delete DEFAULT_ENSURERS, DEFAULT_VALUES, DEFAULT_DOCS, and
refactor Env to use the new single namedtuple and the DEFAULT_VARS dict
* Removed DEFAULT_ENSURERS, DEFAULT_VALUES, DEFAULT_DOCS
Now need to edit Env to use new DEFAULT_VARS, Var namedtuple
* Finished updating Env object to use new combined Var
Also made corresponding changes elsewhere ensurer was used
* Working on test failures
* More fixes in light of test failures
* Set default values for Var in register.
There's a bit of duplication here, but makes for a cleaner function.
* Black reformatting on environ.py
* Removed history replay
* Added register tests
* Added addtional deregistration test
* Removed all replay references, in docs too
* Added news item for env-reg-dereg
* trigger rebuild
* doc fix
* more doc fixes
* again
* attr names
* reorder imports
* fix flake error
Co-authored-by: Anthony Scopatz <scopatz@gmail.com>
2020-08-05 07:39:11 -07:00
|
|
|
DEFAULT_VARS,
|
2018-08-30 09:18:49 -05:00
|
|
|
default_env,
|
|
|
|
make_args_env,
|
2019-08-06 14:39:11 -04:00
|
|
|
LsColors,
|
2018-08-30 09:18:49 -05:00
|
|
|
)
|
2015-05-14 18:18:46 -05:00
|
|
|
|
2016-06-27 18:39:10 +03:00
|
|
|
from tools import skip_if_on_unix
|
2016-05-16 02:02:24 -04:00
|
|
|
|
2018-08-30 09:18:49 -05:00
|
|
|
|
2015-05-14 18:18:46 -05:00
|
|
|
def test_env_normal():
|
2018-08-30 09:18:49 -05:00
|
|
|
env = Env(VAR="wakka")
|
|
|
|
assert "wakka" == env["VAR"]
|
|
|
|
|
2015-05-14 18:18:46 -05:00
|
|
|
|
2016-07-04 20:09:55 +03:00
|
|
|
def test_env_contains():
|
2018-08-30 09:18:49 -05:00
|
|
|
env = Env(VAR="wakka")
|
|
|
|
assert "VAR" in env
|
2016-07-04 20:09:55 +03:00
|
|
|
|
2018-08-30 09:18:49 -05:00
|
|
|
|
|
|
|
@pytest.mark.parametrize("path", [["/home/wakka"], ["wakka"]])
|
2016-07-01 12:42:07 +03:00
|
|
|
def test_env_path_list(path):
|
|
|
|
env = Env(MYPATH=path)
|
2018-08-30 09:18:49 -05:00
|
|
|
assert path == env["MYPATH"].paths
|
|
|
|
|
2016-07-01 12:42:07 +03:00
|
|
|
|
2018-08-30 09:18:49 -05:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"path",
|
|
|
|
[["/home/wakka" + os.pathsep + "/home/jawaka"], ["wakka" + os.pathsep + "jawaka"]],
|
|
|
|
)
|
2016-07-01 12:42:07 +03:00
|
|
|
def test_env_path_str(path):
|
|
|
|
env = Env(MYPATH=path)
|
2018-08-30 09:18:49 -05:00
|
|
|
assert path == env["MYPATH"].paths
|
|
|
|
|
2015-05-14 18:18:46 -05:00
|
|
|
|
|
|
|
def test_env_detype():
|
2018-08-30 09:18:49 -05:00
|
|
|
env = Env(MYPATH=["wakka", "jawaka"])
|
|
|
|
assert "wakka" + os.pathsep + "jawaka" == env.detype()["MYPATH"]
|
|
|
|
|
2015-05-14 18:18:46 -05:00
|
|
|
|
2018-08-30 09:18:49 -05:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"path1, path2",
|
|
|
|
[(["/home/wakka", "/home/jawaka"], "/home/woah"), (["wakka", "jawaka"], "woah")],
|
|
|
|
)
|
2016-07-01 12:42:07 +03:00
|
|
|
def test_env_detype_mutable_access_clear(path1, path2):
|
|
|
|
env = Env(MYPATH=path1)
|
2018-08-30 09:18:49 -05:00
|
|
|
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
|
2018-08-30 09:18:49 -05:00
|
|
|
assert path2 + os.pathsep + path1[1] == env.detype()["MYPATH"]
|
|
|
|
|
2015-05-14 18:18:46 -05:00
|
|
|
|
|
|
|
def test_env_detype_no_dict():
|
2018-08-30 09:18:49 -05:00
|
|
|
env = Env(YO={"hey": 42})
|
Environment variable registration, deregistration (addresses #3227) (#3377)
* First attempt at register/deregister machinery for envvars
* Added detailed docstring, simplified ensurers
Also added some type checking that became clear from docstring writing.
* Changes in response to @scopatz review
Simplified kwarg names.
* defaultval -> default
* Created new Var namedtuple, as well as DEFAULT_VARS
We should now delete DEFAULT_ENSURERS, DEFAULT_VALUES, DEFAULT_DOCS, and
refactor Env to use the new single namedtuple and the DEFAULT_VARS dict
* Removed DEFAULT_ENSURERS, DEFAULT_VALUES, DEFAULT_DOCS
Now need to edit Env to use new DEFAULT_VARS, Var namedtuple
* Finished updating Env object to use new combined Var
Also made corresponding changes elsewhere ensurer was used
* Working on test failures
* More fixes in light of test failures
* Set default values for Var in register.
There's a bit of duplication here, but makes for a cleaner function.
* Black reformatting on environ.py
* Removed history replay
* Added register tests
* Added addtional deregistration test
* Removed all replay references, in docs too
* Added news item for env-reg-dereg
* trigger rebuild
* doc fix
* more doc fixes
* again
* attr names
* reorder imports
* fix flake error
Co-authored-by: Anthony Scopatz <scopatz@gmail.com>
2020-08-05 07:39:11 -07:00
|
|
|
env.register("YO", validate=always_true, convert=None, detype=None)
|
2015-05-14 18:18:46 -05:00
|
|
|
det = env.detype()
|
2018-08-30 09:18:49 -05:00
|
|
|
assert "YO" not in det
|
|
|
|
|
2015-05-14 18:18:46 -05: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)
|
2018-08-30 09:18:49 -05: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():
|
2018-08-30 09:18:49 -05:00
|
|
|
env = Env(HISTCONTROL="")
|
|
|
|
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():
|
2018-08-30 09:18:49 -05:00
|
|
|
env = Env(HISTCONTROL="ignoredups")
|
|
|
|
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():
|
2018-08-30 09:18:49 -05:00
|
|
|
env = Env(HISTCONTROL="ignoreerr,ignoredups,ignoreerr")
|
|
|
|
assert len(env["HISTCONTROL"]) == 2
|
|
|
|
assert "ignoreerr" in env["HISTCONTROL"]
|
|
|
|
assert "ignoredups" in env["HISTCONTROL"]
|
|
|
|
|
2015-11-28 16:12:41 -07:00
|
|
|
|
2020-03-15 20:12:21 +05:30
|
|
|
def test_histcontrol_ignoreerr_ignoredups_erase_dups():
|
|
|
|
env = Env(HISTCONTROL="ignoreerr,ignoredups,ignoreerr,erasedups")
|
|
|
|
assert len(env["HISTCONTROL"]) == 3
|
|
|
|
assert "ignoreerr" in env["HISTCONTROL"]
|
|
|
|
assert "ignoredups" in env["HISTCONTROL"]
|
|
|
|
assert "erasedups" in env["HISTCONTROL"]
|
|
|
|
|
|
|
|
|
2016-04-01 01:41:23 -04:00
|
|
|
def test_swap():
|
2018-08-30 09:18:49 -05:00
|
|
|
env = Env(VAR="wakka")
|
|
|
|
assert env["VAR"] == "wakka"
|
2016-04-01 01:41:23 -04:00
|
|
|
|
|
|
|
# positional arg
|
2018-08-30 09:18:49 -05:00
|
|
|
with env.swap({"VAR": "foo"}):
|
|
|
|
assert env["VAR"] == "foo"
|
2016-04-01 01:41:23 -04:00
|
|
|
|
|
|
|
# make sure the environment goes back outside the context manager
|
2018-08-30 09:18:49 -05:00
|
|
|
assert env["VAR"] == "wakka"
|
2016-04-01 01:41:23 -04:00
|
|
|
|
|
|
|
# kwargs only
|
2018-08-30 09:18:49 -05:00
|
|
|
with env.swap(VAR1="foo", VAR2="bar"):
|
|
|
|
assert env["VAR1"] == "foo"
|
|
|
|
assert env["VAR2"] == "bar"
|
2016-04-01 01:41:23 -04:00
|
|
|
|
|
|
|
# positional and kwargs
|
2018-08-30 09:18:49 -05:00
|
|
|
with env.swap({"VAR3": "baz"}, VAR1="foo", VAR2="bar"):
|
|
|
|
assert env["VAR1"] == "foo"
|
|
|
|
assert env["VAR2"] == "bar"
|
|
|
|
assert env["VAR3"] == "baz"
|
2016-04-01 01:41:23 -04:00
|
|
|
|
|
|
|
# make sure the environment goes back outside the context manager
|
2018-08-30 09:18:49 -05:00
|
|
|
assert env["VAR"] == "wakka"
|
|
|
|
assert "VAR1" not in env
|
|
|
|
assert "VAR2" not in env
|
|
|
|
assert "VAR3" not in env
|
2016-04-01 01:41:23 -04:00
|
|
|
|
2016-06-27 18:39:10 +03:00
|
|
|
|
2018-08-21 15:21:46 -04:00
|
|
|
def test_swap_exception_replacement():
|
2018-08-30 09:18:49 -05:00
|
|
|
env = Env(VAR="original value")
|
2018-08-21 15:21:46 -04:00
|
|
|
try:
|
2018-08-30 09:18:49 -05:00
|
|
|
with env.swap(VAR="inner value"):
|
|
|
|
assert env["VAR"] == "inner value"
|
2018-08-21 15:21:46 -04:00
|
|
|
raise Exception()
|
|
|
|
except Exception:
|
2018-08-30 09:18:49 -05:00
|
|
|
assert env["VAR"] == "original value"
|
|
|
|
assert env["VAR"] == "original value"
|
2018-08-21 15:21:46 -04:00
|
|
|
|
|
|
|
|
2016-06-27 18:39:10 +03:00
|
|
|
@skip_if_on_unix
|
2016-07-01 12:42:07 +03:00
|
|
|
def test_locate_binary_on_windows(xonsh_builtins):
|
2018-08-30 09:18:49 -05:00
|
|
|
files = ("file1.exe", "FILE2.BAT", "file3.txt")
|
2016-06-27 18:39:10 +03:00
|
|
|
with TemporaryDirectory() as tmpdir:
|
|
|
|
for fname in files:
|
|
|
|
fpath = os.path.join(tmpdir, fname)
|
2018-08-30 09:18:49 -05:00
|
|
|
with open(fpath, "w") as f:
|
2016-06-27 18:39:10 +03:00
|
|
|
f.write(fpath)
|
2018-09-13 14:03:35 -04:00
|
|
|
xonsh_builtins.__xonsh__.env.update(
|
2018-08-30 09:18:49 -05:00
|
|
|
{"PATH": [tmpdir], "PATHEXT": [".COM", ".EXE", ".BAT"]}
|
|
|
|
)
|
2018-09-13 14:03:35 -04:00
|
|
|
xonsh_builtins.__xonsh__.commands_cache = CommandsCache()
|
2018-08-30 09:18:49 -05: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
|
2017-01-30 14:45:35 +02:00
|
|
|
|
|
|
|
|
2017-01-31 10:44:26 +02:00
|
|
|
def test_event_on_envvar_change(xonsh_builtins):
|
2017-01-30 14:45:35 +02:00
|
|
|
env = Env(TEST=0)
|
2018-09-13 14:03:35 -04:00
|
|
|
xonsh_builtins.__xonsh__.env = env
|
2017-01-30 14:45:35 +02:00
|
|
|
share = []
|
|
|
|
# register
|
2020-06-13 01:36:22 -04:00
|
|
|
|
2017-01-31 10:44:26 +02:00
|
|
|
@xonsh_builtins.events.on_envvar_change
|
2017-01-30 14:45:35 +02:00
|
|
|
def handler(name, oldvalue, newvalue, **kwargs):
|
2017-01-31 10:44:26 +02:00
|
|
|
share.extend((name, oldvalue, newvalue))
|
2017-01-30 14:45:35 +02:00
|
|
|
|
|
|
|
# trigger
|
2018-08-30 09:18:49 -05:00
|
|
|
env["TEST"] = 1
|
2017-01-30 14:45:35 +02:00
|
|
|
|
2018-08-30 09:18:49 -05:00
|
|
|
assert share == ["TEST", 0, 1]
|
2017-01-30 15:41:48 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_event_on_envvar_new(xonsh_builtins):
|
|
|
|
env = Env()
|
2018-09-13 14:03:35 -04:00
|
|
|
xonsh_builtins.__xonsh__.env = env
|
2017-01-30 15:41:48 +02:00
|
|
|
share = []
|
|
|
|
# register
|
2020-06-13 01:36:22 -04:00
|
|
|
|
2017-01-31 10:44:26 +02:00
|
|
|
@xonsh_builtins.events.on_envvar_new
|
|
|
|
def handler(name, value, **kwargs):
|
|
|
|
share.extend((name, value))
|
|
|
|
|
|
|
|
# trigger
|
2018-08-30 09:18:49 -05:00
|
|
|
env["TEST"] = 1
|
|
|
|
|
|
|
|
assert share == ["TEST", 1]
|
2017-01-31 10:44:26 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_event_on_envvar_change_from_none_value(xonsh_builtins):
|
|
|
|
env = Env(TEST=None)
|
2018-09-13 14:03:35 -04:00
|
|
|
xonsh_builtins.__xonsh__.env = env
|
2017-01-31 10:44:26 +02:00
|
|
|
share = []
|
|
|
|
# register
|
2020-06-13 01:36:22 -04:00
|
|
|
|
2017-01-31 10:44:26 +02:00
|
|
|
@xonsh_builtins.events.on_envvar_change
|
2017-01-30 15:41:48 +02:00
|
|
|
def handler(name, oldvalue, newvalue, **kwargs):
|
2017-01-31 10:44:26 +02:00
|
|
|
share.extend((name, oldvalue, newvalue))
|
2017-01-30 15:41:48 +02:00
|
|
|
|
|
|
|
# trigger
|
2018-08-30 09:18:49 -05:00
|
|
|
env["TEST"] = 1
|
2017-01-30 15:41:48 +02:00
|
|
|
|
2018-08-30 09:18:49 -05:00
|
|
|
assert share == ["TEST", None, 1]
|
2017-01-31 12:26:50 +02:00
|
|
|
|
|
|
|
|
2018-08-30 09:18:49 -05:00
|
|
|
@pytest.mark.parametrize("val", [1, None, True, "ok"])
|
2017-01-31 12:26:50 +02:00
|
|
|
def test_event_on_envvar_change_no_fire_when_value_is_same(val, xonsh_builtins):
|
|
|
|
env = Env(TEST=val)
|
2018-09-13 14:03:35 -04:00
|
|
|
xonsh_builtins.__xonsh__.env = env
|
2017-01-31 12:26:50 +02:00
|
|
|
share = []
|
|
|
|
# register
|
2020-06-13 01:36:22 -04:00
|
|
|
|
2017-01-31 12:26:50 +02:00
|
|
|
@xonsh_builtins.events.on_envvar_change
|
|
|
|
def handler(name, oldvalue, newvalue, **kwargs):
|
|
|
|
share.extend((name, oldvalue, newvalue))
|
|
|
|
|
|
|
|
# trigger
|
2018-08-30 09:18:49 -05:00
|
|
|
env["TEST"] = val
|
2017-01-31 12:26:50 +02:00
|
|
|
|
|
|
|
assert share == []
|
2017-01-31 12:51:26 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_events_on_envvar_called_in_right_order(xonsh_builtins):
|
|
|
|
env = Env()
|
2018-09-13 14:03:35 -04:00
|
|
|
xonsh_builtins.__xonsh__.env = env
|
2017-01-31 12:51:26 +02:00
|
|
|
share = []
|
|
|
|
# register
|
2020-06-13 01:36:22 -04:00
|
|
|
|
2017-01-31 12:51:26 +02:00
|
|
|
@xonsh_builtins.events.on_envvar_new
|
|
|
|
def handler(name, value, **kwargs):
|
2018-08-30 09:18:49 -05:00
|
|
|
share[:] = ["new"]
|
2017-01-31 12:51:26 +02:00
|
|
|
|
|
|
|
@xonsh_builtins.events.on_envvar_change
|
2020-01-04 13:56:02 -05:00
|
|
|
def handler1(name, oldvalue, newvalue, **kwargs):
|
2018-08-30 09:18:49 -05:00
|
|
|
share[:] = ["change"]
|
2017-01-31 12:51:26 +02:00
|
|
|
|
|
|
|
# trigger new
|
2018-08-30 09:18:49 -05:00
|
|
|
env["TEST"] = 1
|
2017-01-31 12:51:26 +02:00
|
|
|
|
2018-08-30 09:18:49 -05:00
|
|
|
assert share == ["new"]
|
2017-01-31 12:51:26 +02:00
|
|
|
|
|
|
|
# trigger change
|
2018-08-30 09:18:49 -05:00
|
|
|
env["TEST"] = 2
|
2017-01-31 12:51:26 +02:00
|
|
|
|
2018-08-30 09:18:49 -05:00
|
|
|
assert share == ["change"]
|
2017-01-31 12:51:26 +02:00
|
|
|
|
2017-03-15 09:09:08 -04:00
|
|
|
|
2017-11-29 13:51:13 -05:00
|
|
|
def test_no_lines_columns():
|
2018-08-30 09:18:49 -05:00
|
|
|
os.environ["LINES"] = "spam"
|
|
|
|
os.environ["COLUMNS"] = "eggs"
|
2017-11-29 13:51:13 -05:00
|
|
|
try:
|
|
|
|
env = default_env()
|
2018-08-30 09:18:49 -05:00
|
|
|
assert "LINES" not in env
|
|
|
|
assert "COLUMNS" not in env
|
2017-11-29 13:51:13 -05:00
|
|
|
finally:
|
2018-08-30 09:18:49 -05:00
|
|
|
del os.environ["LINES"]
|
|
|
|
del os.environ["COLUMNS"]
|
2018-07-20 20:41:00 -04:00
|
|
|
|
|
|
|
|
|
|
|
def test_make_args_env():
|
2018-08-30 09:18:49 -05:00
|
|
|
obs = make_args_env(["script", "1", "2", "3"])
|
2018-07-20 20:41:00 -04:00
|
|
|
exp = {
|
2018-08-30 09:18:49 -05:00
|
|
|
"ARGS": ["script", "1", "2", "3"],
|
|
|
|
"ARG0": "script",
|
|
|
|
"ARG1": "1",
|
|
|
|
"ARG2": "2",
|
|
|
|
"ARG3": "3",
|
2018-07-20 20:41:00 -04:00
|
|
|
}
|
|
|
|
assert exp == obs
|
2019-04-26 20:27:45 +02:00
|
|
|
|
2019-05-15 23:23:49 +02:00
|
|
|
|
2019-04-26 20:27:45 +02:00
|
|
|
def test_delitem():
|
|
|
|
env = Env(VAR="a value")
|
2019-05-15 23:23:49 +02:00
|
|
|
assert env["VAR"] == "a value"
|
|
|
|
del env["VAR"]
|
2019-04-26 20:27:45 +02:00
|
|
|
with pytest.raises(Exception):
|
2020-05-05 06:42:28 -04:00
|
|
|
env["VAR"]
|
2019-05-15 23:23:49 +02:00
|
|
|
|
2019-04-26 20:27:45 +02:00
|
|
|
|
|
|
|
def test_delitem_default():
|
|
|
|
env = Env()
|
2019-05-15 23:23:49 +02:00
|
|
|
a_key, a_value = next(
|
Environment variable registration, deregistration (addresses #3227) (#3377)
* First attempt at register/deregister machinery for envvars
* Added detailed docstring, simplified ensurers
Also added some type checking that became clear from docstring writing.
* Changes in response to @scopatz review
Simplified kwarg names.
* defaultval -> default
* Created new Var namedtuple, as well as DEFAULT_VARS
We should now delete DEFAULT_ENSURERS, DEFAULT_VALUES, DEFAULT_DOCS, and
refactor Env to use the new single namedtuple and the DEFAULT_VARS dict
* Removed DEFAULT_ENSURERS, DEFAULT_VALUES, DEFAULT_DOCS
Now need to edit Env to use new DEFAULT_VARS, Var namedtuple
* Finished updating Env object to use new combined Var
Also made corresponding changes elsewhere ensurer was used
* Working on test failures
* More fixes in light of test failures
* Set default values for Var in register.
There's a bit of duplication here, but makes for a cleaner function.
* Black reformatting on environ.py
* Removed history replay
* Added register tests
* Added addtional deregistration test
* Removed all replay references, in docs too
* Added news item for env-reg-dereg
* trigger rebuild
* doc fix
* more doc fixes
* again
* attr names
* reorder imports
* fix flake error
Co-authored-by: Anthony Scopatz <scopatz@gmail.com>
2020-08-05 07:39:11 -07:00
|
|
|
(k, v.default) for (k, v) in env._vars.items() if isinstance(v.default, str)
|
2019-05-15 23:23:49 +02:00
|
|
|
)
|
2019-04-26 20:27:45 +02:00
|
|
|
del env[a_key]
|
|
|
|
assert env[a_key] == a_value
|
|
|
|
del env[a_key]
|
|
|
|
assert env[a_key] == a_value
|
2019-08-06 14:39:11 -04:00
|
|
|
|
|
|
|
|
2020-06-25 19:49:04 -04:00
|
|
|
def test_lscolors_target(xonsh_builtins):
|
2019-08-06 14:39:11 -04:00
|
|
|
lsc = LsColors.fromstring("ln=target")
|
2020-06-13 01:36:22 -04:00
|
|
|
assert lsc["ln"] == ("NO_COLOR",)
|
|
|
|
assert lsc.is_target("ln")
|
2019-08-06 14:39:11 -04:00
|
|
|
assert lsc.detype() == "ln=target"
|
2020-06-13 01:36:22 -04:00
|
|
|
assert not (lsc.is_target("mi"))
|
2019-12-26 05:51:48 -05:00
|
|
|
|
2020-01-04 13:56:02 -05:00
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"key_in,old_in,new_in,test",
|
|
|
|
[
|
2020-06-09 21:47:59 -04:00
|
|
|
("fi", ("NO_COLOR",), ("BLUE",), "existing key, change value"),
|
|
|
|
("fi", ("NO_COLOR",), ("NO_COLOR",), "existing key, no change in value"),
|
2020-01-04 13:56:02 -05:00
|
|
|
("tw", None, ("NO_COLOR",), "create new key"),
|
|
|
|
("pi", ("BACKGROUND_BLACK", "YELLOW"), None, "delete existing key"),
|
2019-12-26 05:51:48 -05:00
|
|
|
],
|
|
|
|
)
|
|
|
|
def test_lscolors_events(key_in, old_in, new_in, test, xonsh_builtins):
|
2020-06-09 21:47:59 -04:00
|
|
|
lsc = LsColors.fromstring("fi=0:di=01;34:pi=40;33")
|
2019-12-26 05:51:48 -05:00
|
|
|
# corresponding colors: [('NO_COLOR',), ('BOLD_CYAN',), ('BOLD_CYAN',), ('BACKGROUND_BLACK', 'YELLOW')]
|
|
|
|
|
|
|
|
event_fired = False
|
2020-01-04 13:56:02 -05:00
|
|
|
|
2019-12-26 05:51:48 -05:00
|
|
|
@xonsh_builtins.events.on_lscolors_change
|
2020-01-04 13:56:02 -05:00
|
|
|
def handler(key, oldvalue, newvalue, **kwargs):
|
2019-12-26 05:51:48 -05:00
|
|
|
nonlocal old_in, new_in, key_in, event_fired
|
2020-01-04 13:56:02 -05:00
|
|
|
assert (
|
|
|
|
key == key_in and oldvalue == old_in and newvalue == new_in
|
|
|
|
), "Old and new event values match"
|
2019-12-26 05:51:48 -05:00
|
|
|
event_fired = True
|
|
|
|
|
|
|
|
xonsh_builtins.__xonsh__.env["LS_COLORS"] = lsc
|
|
|
|
|
|
|
|
if new_in is None:
|
2020-05-05 06:42:28 -04:00
|
|
|
lsc.pop(key_in, "argle")
|
2019-12-26 05:51:48 -05:00
|
|
|
else:
|
|
|
|
lsc[key_in] = new_in
|
|
|
|
|
|
|
|
if old_in == new_in:
|
|
|
|
assert not event_fired, "No event if value doesn't change"
|
|
|
|
else:
|
|
|
|
assert event_fired
|
Environment variable registration, deregistration (addresses #3227) (#3377)
* First attempt at register/deregister machinery for envvars
* Added detailed docstring, simplified ensurers
Also added some type checking that became clear from docstring writing.
* Changes in response to @scopatz review
Simplified kwarg names.
* defaultval -> default
* Created new Var namedtuple, as well as DEFAULT_VARS
We should now delete DEFAULT_ENSURERS, DEFAULT_VALUES, DEFAULT_DOCS, and
refactor Env to use the new single namedtuple and the DEFAULT_VARS dict
* Removed DEFAULT_ENSURERS, DEFAULT_VALUES, DEFAULT_DOCS
Now need to edit Env to use new DEFAULT_VARS, Var namedtuple
* Finished updating Env object to use new combined Var
Also made corresponding changes elsewhere ensurer was used
* Working on test failures
* More fixes in light of test failures
* Set default values for Var in register.
There's a bit of duplication here, but makes for a cleaner function.
* Black reformatting on environ.py
* Removed history replay
* Added register tests
* Added addtional deregistration test
* Removed all replay references, in docs too
* Added news item for env-reg-dereg
* trigger rebuild
* doc fix
* more doc fixes
* again
* attr names
* reorder imports
* fix flake error
Co-authored-by: Anthony Scopatz <scopatz@gmail.com>
2020-08-05 07:39:11 -07:00
|
|
|
|
|
|
|
|
|
|
|
def test_register_custom_var_generic():
|
|
|
|
"""Test that a registered envvar without any type is treated
|
|
|
|
permissively.
|
|
|
|
|
|
|
|
"""
|
|
|
|
env = Env()
|
|
|
|
|
|
|
|
assert "MY_SPECIAL_VAR" not in env
|
|
|
|
env.register("MY_SPECIAL_VAR")
|
|
|
|
assert "MY_SPECIAL_VAR" in env
|
|
|
|
|
|
|
|
env["MY_SPECIAL_VAR"] = 32
|
|
|
|
assert env["MY_SPECIAL_VAR"] == 32
|
|
|
|
|
|
|
|
env["MY_SPECIAL_VAR"] = True
|
|
|
|
assert env["MY_SPECIAL_VAR"] == True
|
|
|
|
|
|
|
|
|
|
|
|
def test_register_custom_var_int():
|
|
|
|
env = Env()
|
|
|
|
env.register("MY_SPECIAL_VAR", type='int')
|
|
|
|
|
|
|
|
env["MY_SPECIAL_VAR"] = "32"
|
|
|
|
assert env["MY_SPECIAL_VAR"] == 32
|
|
|
|
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
env["MY_SPECIAL_VAR"] = "wakka"
|
|
|
|
|
|
|
|
|
|
|
|
def test_register_custom_var_float():
|
|
|
|
env = Env()
|
|
|
|
env.register("MY_SPECIAL_VAR", type='float')
|
|
|
|
|
|
|
|
env["MY_SPECIAL_VAR"] = "27"
|
|
|
|
assert env["MY_SPECIAL_VAR"] == 27.0
|
|
|
|
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
env["MY_SPECIAL_VAR"] = "wakka"
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("val,converted",
|
|
|
|
[
|
|
|
|
(True, True),
|
|
|
|
(32, True),
|
|
|
|
(0, False),
|
|
|
|
(27.0, True),
|
|
|
|
(None, False),
|
|
|
|
("lol", True),
|
|
|
|
("false", False),
|
|
|
|
("no", False),
|
|
|
|
])
|
|
|
|
def test_register_custom_var_bool(val, converted):
|
|
|
|
env = Env()
|
|
|
|
env.register("MY_SPECIAL_VAR", type='bool')
|
|
|
|
|
|
|
|
env["MY_SPECIAL_VAR"] = val
|
|
|
|
assert env["MY_SPECIAL_VAR"] == converted
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("val,converted",
|
|
|
|
[
|
|
|
|
(32, "32"),
|
|
|
|
(0, "0"),
|
|
|
|
(27.0, "27.0"),
|
|
|
|
(None, "None"),
|
|
|
|
("lol", "lol"),
|
|
|
|
("false", "false"),
|
|
|
|
("no", "no"),
|
|
|
|
])
|
|
|
|
def test_register_custom_var_str(val, converted):
|
|
|
|
env = Env()
|
|
|
|
env.register("MY_SPECIAL_VAR", type='str')
|
|
|
|
|
|
|
|
env["MY_SPECIAL_VAR"] = val
|
|
|
|
assert env["MY_SPECIAL_VAR"] == converted
|
|
|
|
|
|
|
|
|
|
|
|
def test_register_custom_var_path():
|
|
|
|
env = Env()
|
|
|
|
env.register("MY_SPECIAL_VAR", type='path')
|
|
|
|
|
|
|
|
paths = ["/home/wakka", "/home/wakka/bin"]
|
|
|
|
env["MY_SPECIAL_VAR"] = paths
|
|
|
|
|
|
|
|
assert hasattr(env['MY_SPECIAL_VAR'], 'paths')
|
|
|
|
assert env["MY_SPECIAL_VAR"].paths == paths
|
|
|
|
|
|
|
|
with pytest.raises(TypeError):
|
|
|
|
env["MY_SPECIAL_VAR"] = 32
|
|
|
|
|
|
|
|
|
|
|
|
def test_deregister_custom_var():
|
|
|
|
env = Env()
|
|
|
|
|
|
|
|
env.register("MY_SPECIAL_VAR", type='path')
|
|
|
|
env.deregister("MY_SPECIAL_VAR")
|
|
|
|
assert "MY_SPECIAL_VAR" not in env
|
|
|
|
|
|
|
|
env.register("MY_SPECIAL_VAR", type='path')
|
|
|
|
paths = ["/home/wakka", "/home/wakka/bin"]
|
|
|
|
env["MY_SPECIAL_VAR"] = paths
|
|
|
|
env.deregister("MY_SPECIAL_VAR")
|
|
|
|
|
|
|
|
# deregistering a variable that has a value set doesn't
|
|
|
|
# remove it from env;
|
|
|
|
# the existing variable also maintains its type validation, conversion
|
|
|
|
assert "MY_SPECIAL_VAR" in env
|
|
|
|
with pytest.raises(TypeError):
|
|
|
|
env["MY_SPECIAL_VAR"] = 32
|
|
|
|
|
|
|
|
# removing, then re-adding the variable without registering
|
|
|
|
# gives it only default permissive validation, conversion
|
|
|
|
del env["MY_SPECIAL_VAR"]
|
|
|
|
env["MY_SPECIAL_VAR"] = 32
|