Debian Codegraph @

tests/test_alias.py

raw
1 from IPython.utils.capture import capture_output
2
3 import pytest
4
5
6 def test_alias_lifecycle():
7 name = "test_alias1"
8 cmd = 'echo "Hello"'
9 am = _ip.alias_manager
10 am.clear_aliases()
11 am.define_alias(name, cmd)
12 assert am.is_alias(name)
13 assert am.retrieve_alias(name) == cmd
14 assert (name, cmd) in am.aliases
15
16 # Test running the alias
17 orig_system = _ip.system
18 result = []
19 _ip.system = result.append
20 try:
21 _ip.run_cell("%{}".format(name))
22 result = [c.strip() for c in result]
23 assert result == [cmd]
24 finally:
25 _ip.system = orig_system
26
27 # Test removing the alias
28 am.undefine_alias(name)
29 assert not am.is_alias(name)
30 with pytest.raises(ValueError):
31 am.retrieve_alias(name)
32 assert (name, cmd) not in am.aliases
33
34
35 def test_alias_args_error():
36 """Error expanding with wrong number of arguments"""
37 _ip.alias_manager.define_alias("parts", "echo first %s second %s")
38 # capture stderr:
39 with capture_output() as cap:
40 _ip.run_cell("parts 1")
41
42 assert cap.stderr.split(":")[0] == "UsageError"
43
44
45 def test_alias_args_commented():
46 """Check that alias correctly ignores 'commented out' args"""
47 _ip.run_line_magic("alias", "commentarg echo this is %%s a commented out arg")
48
49 with capture_output() as cap:
50 _ip.run_cell("commentarg")
51
52 # strip() is for pytest compat; testing via iptest patch IPython shell
53 # in testing.globalipapp and replace the system call which messed up the
54 # \r\n
55 assert cap.stdout.strip() == "this is %s a commented out arg"
56
57
58 def test_alias_args_commented_nargs():
59 """Check that alias correctly counts args, excluding those commented out"""
60 am = _ip.alias_manager
61 alias_name = "comargcount"
62 cmd = "echo this is %%s a commented out arg and this is not %s"
63
64 am.define_alias(alias_name, cmd)
65 assert am.is_alias(alias_name)
66
67 thealias = am.get_alias(alias_name)
68 assert thealias.nargs == 1