xonsh/tests/test_history_dummy.py
Blake Ramsdell b5c42d6716
Add XONSH_HISTORY_IGNORE_REGEX support (#4953)
* Add XONSH_HISTORY_IGNORE_REGEX support

* Add changelog text

* Add cleanup notes for XONSH_HISTORY_IGNORE_REGEX

* Rename should_append to is_ignored

* Change is_ignored to a History method

* Remove unused re imports

* Docstring formatting cleanup, Black compliance

* Move XONSH_HISTORY_IGNORE_REGEX to History class

* Clarify with parenthesis

* Formatting compliance

* Add validate_ignore_regex check

* Remove validation comment

* Remove XONSH_HISTORY_IGNORE_REGEX test comments

* Add __init__ chaining to DummyHistory

* Add broken test for XONSH_IGNORE_REGEX

* Remove extra test variants

* Fix unit test environment variable name

* Add test for bad regex error output

* Remove unneeded __init__ chaining

* Add test_is_ignore

* Add is_regex and associated tests

* Add doc for XONSH_HISTORY_IGNORE_REGEX

* Change validate_ignore_regex to use is_regex

* Compile regex once and use a cached property

* Let cached_property handle saving regex
2022-10-05 00:16:42 +05:30

41 lines
1.4 KiB
Python

"""Tests the dummy history backend."""
import pytest
from xonsh.history.dummy import DummyHistory
from xonsh.history.main import construct_history
@pytest.mark.parametrize("backend", ["dummy", DummyHistory, DummyHistory()])
def test_construct_history_str(xession, backend):
xession.env["XONSH_HISTORY_BACKEND"] = backend
assert isinstance(construct_history(), DummyHistory)
def test_ignore_regex_invalid(xession, capsys):
xession.env["XONSH_HISTORY_BACKEND"] = "dummy"
xession.env["XONSH_HISTORY_IGNORE_REGEX"] = "**"
history = construct_history()
captured = capsys.readouterr()
assert (
"XONSH_HISTORY_IGNORE_REGEX is not a valid regular expression and will be ignored"
in captured.err
)
assert not history.is_ignored({"inp": "history"})
def test_is_ignore(xession):
xession.env["XONSH_HISTORY_BACKEND"] = "dummy"
xession.env["XONSH_HISTORY_IGNORE_REGEX"] = "(ls|cat)"
history = construct_history()
assert history.is_ignored({"inp": "cat foo.txt"})
assert not history.is_ignored({"inp": "history"})
assert history.is_ignored({"inp": "ls bar"})
def test_is_ignore_no_regex(xession):
xession.env["XONSH_HISTORY_BACKEND"] = "dummy"
history = construct_history()
assert not history.is_ignored({"inp": "cat foo.txt"})
assert not history.is_ignored({"inp": "history"})
assert not history.is_ignored({"inp": "ls bar"})