Skip to content

Commit

Permalink
test(quote_readline): use @pytest.mark.bashcomp(temp_cwd=True)
Browse files Browse the repository at this point in the history
  • Loading branch information
akinomyoga committed May 21, 2021
1 parent dea1716 commit a08b0c7
Showing 1 changed file with 11 additions and 19 deletions.
30 changes: 11 additions & 19 deletions test/t/unit/test_unit_quote_readline.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from conftest import assert_bash_exec


@pytest.mark.bashcomp(cmd=None)
@pytest.mark.bashcomp(cmd=None, temp_cwd=True)
class TestUnitQuoteReadline:
def test_exec(self, bash):
assert_bash_exec(bash, "quote_readline '' >/dev/null")
Expand All @@ -17,13 +17,6 @@ def test_env_non_pollution(self, bash):
bash, "foo() { quote_readline meh >/dev/null; }; foo; unset foo"
)

def quote_bash_word(self, s):
return "$'" + re.sub(
r'[\\\'\000-\037]',
lambda m: r'\\' if m.group(0) == '\\'
else r'\'' if m.group(0) == "'"
else r'\%03o' % ord(m.group(0)), s) + "'"

def test_github_issue_189_1(self, bash):
"""Test error messages on a certain command line
Expand Down Expand Up @@ -51,9 +44,8 @@ def test_github_issue_492_1(self, bash):
$ echo '$(touch file.txt)[TAB]
"""
with tempfile.TemporaryDirectory() as tmpdir:
assert_bash_exec(bash, "(cd %s; quote_readline $'\\'$(touch 1.txt)' >/dev/null)" % self.quote_bash_word(tmpdir))
assert not os.path.exists(os.path.join(tmpdir, "1.txt"))
assert_bash_exec(bash, "quote_readline $'\\'$(touch 1.txt)' >/dev/null")
assert not os.path.exists("./1.txt")

def test_github_issue_492_2(self, bash):
"""Test the file clear by unintended redirection on a certain command line
Expand All @@ -66,9 +58,8 @@ def test_github_issue_492_2(self, bash):
$ awk '$1 > 1.0[TAB]
"""
with tempfile.TemporaryDirectory() as tmpdir:
assert_bash_exec(bash, "(cd %s; quote_readline $'\\'$1 > 1.0' >/dev/null)" % self.quote_bash_word(tmpdir))
assert not os.path.exists(os.path.join(tmpdir, "1.0"))
assert_bash_exec(bash, "quote_readline $'\\'$1 > 1.0' >/dev/null")
assert not os.path.exists("./1.0")

def test_github_issue_492_3(self, bash):
"""Test code execution through unintended pathname expansions
Expand All @@ -80,9 +71,11 @@ def test_github_issue_492_3(self, bash):
$ echo '$*[TAB]
"""
with tempfile.TemporaryDirectory() as tmpdir:
os.mkdir(os.path.join(tmpdir, "ret=$(echo injected >&2)"))
assert_bash_exec(bash, "(cd %s; quote_readline $'\\'$*' >/dev/null)" % self.quote_bash_word(tmpdir))
os.mkdir("./ret=$(echo injected >&2)")
assert_bash_exec(bash, "quote_readline $'\\'$*' >/dev/null")

@pytest.mark.bashcomp(cmd=None, temp_cwd=True, pre_cmds=("shopt -s failglob",))
class TestUnitQuoteReadlineWithFailglob:

def test_github_issue_492_4(self, bash):
"""Test error messages through unintended pathname expansions
Expand All @@ -95,5 +88,4 @@ def test_github_issue_492_4(self, bash):
$ echo a\\ b*[TAB]
"""
with tempfile.TemporaryDirectory() as tmpdir:
assert_bash_exec(bash, "(cd %s; shopt -s failglob; quote_readline $'a\\\\\\tb*' >/dev/null)" % self.quote_bash_word(tmpdir))
assert_bash_exec(bash, "quote_readline $'a\\\\\\tb*' >/dev/null")

0 comments on commit a08b0c7

Please sign in to comment.