Skip to content

Commit e5f50fe

Browse files
author
Michele Mesiti
committed
Adding some tests for the exit code checker
Using pytest: - test for the ordinary case - test for the conda case, mocking effect of conda env on prompt
1 parent e7b3565 commit e5f50fe

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed

tests/test_exit_code_checker.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import os
2+
import pexpect
3+
from bash_kernel.kernel import IREPLWrapper
4+
from bash_kernel.exit_code_checker import get_last_exit_code
5+
import pytest
6+
7+
8+
def test_get_last_exit_code_command_not_found(bashwrapper):
9+
bashwrapper.run_command("this-command-does-not-exist")
10+
assert get_last_exit_code(bashwrapper) == 127
11+
12+
13+
def test_get_last_exit_code_success(bashwrapper):
14+
bashwrapper.run_command("echo")
15+
assert get_last_exit_code(bashwrapper) == 0
16+
17+
18+
def test_get_last_exit_code_command_not_found_conda(mock_conda_bashwrapper):
19+
mock_conda_bashwrapper.run_command("this-command-does-not-exist")
20+
assert get_last_exit_code(mock_conda_bashwrapper) == 127
21+
22+
23+
def test_get_last_exit_code_success_conda(mock_conda_bashwrapper):
24+
mock_conda_bashwrapper.run_command("echo")
25+
assert get_last_exit_code(mock_conda_bashwrapper) == 0
26+
27+
28+
@pytest.fixture(scope="session")
29+
def mock_conda_bashwrapper(bashwrapper):
30+
class FakeCondaWrapper:
31+
def __init__(self):
32+
self.child = bashwrapper
33+
34+
def run_command(self, cmd):
35+
out = self.child.run_command(cmd)
36+
# Simulating the effect of a conda env
37+
# on the output of a wrapper
38+
return out + "(condaenv)"
39+
40+
return FakeCondaWrapper()
41+
42+
43+
# From BashKernel._start_bash
44+
@pytest.fixture(scope="session")
45+
def bashwrapper():
46+
unique_prompt = "PROMPT_ASDFASFAS"
47+
bashrc = os.path.join(os.path.dirname(pexpect.__file__), "bashrc.sh")
48+
child = pexpect.spawn(
49+
"bash",
50+
["--rcfile", bashrc],
51+
echo=False,
52+
encoding="utf-8",
53+
codec_errors="replace",
54+
)
55+
56+
ps1 = unique_prompt + "\[\]" + ">"
57+
ps2 = unique_prompt + "\[\]" + "+"
58+
prompt_change = "PS1='{0}' PS2='{1}' PROMPT_COMMAND=''".format(ps1, ps2)
59+
# Using IREPLWrapper to get incremental output
60+
bashwrapper = IREPLWrapper(
61+
child, "\$", prompt_change, unique_prompt, extra_init_cmd="export PAGER=cat"
62+
)
63+
return bashwrapper

0 commit comments

Comments
 (0)