1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
|
# ############################################################################ #
# #
# ::: :::::::: #
# test_hooks.py :+: :+: :+: #
# +:+ +:+ +:+ #
# By: cacharle <me@cacharle.xyz> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2021/02/27 20:03:52 by cacharle #+# #+# #
# Updated: 2021/03/02 17:45:15 by cacharle ### ########.fr #
# #
# ############################################################################ #
from minishell_test.config import Config
from minishell_test.hooks import (
sort_lines,
error_line0,
discard,
export_singleton,
replace_double,
platform_status,
linux_replace,
error_eof_to_expected_token,
linux_discard,
should_not_be,
DISCARDED_TEXT,
)
from tests.helpers import config_context
Config.init([])
def test_sort_lines():
assert "a\nb\nc" == sort_lines("a\nb\nc")
assert "a\nb\nc" == sort_lines("c\nb\na")
assert "a\nb\nc" == sort_lines("b\na\nc")
assert """\
EDITOR=vim
GNUPGHOME=/home/cacharle/.local/share/gnupg
JUPYTER_CONFIG_DIR=/home/cacharle/.config/jupyter
LESSHISTFILE=-
LESS_TERMCAP_se=[0m
LESS_TERMCAP_so=[01;33m
SHELL=/usr/bin/zsh
XDG_CONFIG_HOME=/home/cacharle/.config
XDG_DATA_HOME=/home/cacharle/.local/share
XMONAD_CONFIG_HOME=/home/cacharle/.config/xmonad
XMONAD_DATA_HOME=/home/cacharle/.local/share/xmonad
_=/usr/bin/env\
""" == sort_lines("""\
SHELL=/usr/bin/zsh
LESSHISTFILE=-
JUPYTER_CONFIG_DIR=/home/cacharle/.config/jupyter
XMONAD_CONFIG_HOME=/home/cacharle/.config/xmonad
XMONAD_DATA_HOME=/home/cacharle/.local/share/xmonad
LESS_TERMCAP_se=[0m
LESS_TERMCAP_so=[01;33m
XDG_DATA_HOME=/home/cacharle/.local/share
XDG_CONFIG_HOME=/home/cacharle/.config
GNUPGHOME=/home/cacharle/.local/share/gnupg
EDITOR=vim
_=/usr/bin/env\
""")
def test_error_line0():
assert "" == error_line0("")
assert "foo" == error_line0("foo")
assert "a\nb" == error_line0("a\nb")
assert "a\nb\nc" == error_line0("a\nb\nc")
assert "minishell: bonjour\n" == error_line0(Config.shell_reference_prefix + "-c: bonjour\nfoo\n")
assert "minishell: \n" == error_line0(Config.shell_reference_prefix + "-c: \nfoo\n")
assert Config.shell_reference_prefix + "-c:asdf\nfoo\n" == error_line0(Config.shell_reference_prefix + "-c:asdf\nfoo\n")
def test_discard():
assert DISCARDED_TEXT == discard("")
assert DISCARDED_TEXT == discard("foo")
def test_export_singleton():
assert "" == export_singleton("declare -x IGOTNUMBERS42")
assert "" == export_singleton("declare -x IGOTUNDERSCORE__")
assert "" == export_singleton("declare -x I")
assert "" == export_singleton("declare -x _")
assert """\
declare -x XDG_SESSION_TYPE="tty"
declare -x XDG_VTNR="1"
declare -x XINITRC="/home/cacharle/.config/x11/xinitrc"
declare -x XMONAD_CACHE_HOME="/home/cacharle/.cache/xmonad"
declare -x XMONAD_DATA_HOME="/home/cacharle/.local/share/xmonad"
declare -x YSU_MESSAGE_POSITION="after"
declare -x YSU_VERSION="1.7.3"
declare -x ZDOTDIR="/home/cacharle/.config/zsh"\
""" == export_singleton("""\
declare -x XDG_SESSION_ID
declare -x XDG_SESSION_TYPE="tty"
declare -x XDG_VTNR="1"
declare -x XINITRC="/home/cacharle/.config/x11/xinitrc"
declare -x XMONAD_CACHE_HOME="/home/cacharle/.cache/xmonad"
declare -x XMONAD_CONFIG_HOME
declare -x XMONAD_DATA_HOME="/home/cacharle/.local/share/xmonad"
declare -x YSU_MESSAGE_POSITION="after"
declare -x YSU_VERSION="1.7.3"
declare -x ZDOTDIR="/home/cacharle/.config/zsh"\
""")
with config_context(shell_reference_args=["--posix"]):
assert "" == export_singleton("export IGOTNUMBERS42")
assert "" == export_singleton("export IGOTUNDERSCORE__")
assert "" == export_singleton("export I")
assert "" == export_singleton("export _")
assert """\
export XDG_SESSION_TYPE="tty"
export XDG_VTNR="1"
export XINITRC="/home/cacharle/.config/x11/xinitrc"
export XMONAD_CACHE_HOME="/home/cacharle/.cache/xmonad"
export XMONAD_DATA_HOME="/home/cacharle/.local/share/xmonad"
export YSU_MESSAGE_POSITION="after"
export YSU_VERSION="1.7.3"
export ZDOTDIR="/home/cacharle/.config/zsh"\
""" == export_singleton("""\
export XDG_SESSION_ID
export XDG_SESSION_TYPE="tty"
export XDG_VTNR="1"
export XINITRC="/home/cacharle/.config/x11/xinitrc"
export XMONAD_CACHE_HOME="/home/cacharle/.cache/xmonad"
export XMONAD_CONFIG_HOME
export XMONAD_DATA_HOME="/home/cacharle/.local/share/xmonad"
export YSU_MESSAGE_POSITION="after"
export YSU_VERSION="1.7.3"
export ZDOTDIR="/home/cacharle/.config/zsh"\
""")
def test_replace_double():
assert "/" == replace_double("/")("//")
assert "//" == replace_double("/")("////")
assert "////" == replace_double("/")("////////")
assert ";" == replace_double(";")(";;")
assert ";;" == replace_double(";")(";;;;")
assert ";;;;" == replace_double(";")(";;;;;;;;")
def test_error_eof_to_expected_token():
assert "syntax error expected token" == error_eof_to_expected_token("-c: line 1: syntax error: unexpected end of file")
def test_should_not_be():
assert "OUTPUT SHOULD NOT BE " == should_not_be("")("")
assert "OUTPUT SHOULD NOT BE bonjour" == should_not_be("bonjour")("bonjour")
assert DISCARDED_TEXT == should_not_be("foo")("")
assert DISCARDED_TEXT == should_not_be("bar")("bonjour")
def test_platform_status():
with config_context(platform='darwin'):
assert 0 == platform_status(0, 1)(0)
assert 1 == platform_status(42, 42)(1)
with config_context(platform='linux'):
assert 0 == platform_status(0, 1)(1)
assert 42 == platform_status(0, 1)(42)
with config_context(platform='foo'):
assert 0 == platform_status(42, 42)(0)
def test_linux_replace():
with config_context(platform='darwin'):
assert "Is a directory" == linux_replace("Is a directory", "is a directory")("Is a directory")
assert "SHLVL=0" == linux_replace("SHLVL=0", "SHLVL=1")("SHLVL=0")
assert "\\" == linux_replace("\\", "")("\\")
with config_context(platform='linux'):
assert "is a directory" == linux_replace("Is a directory", "is a directory")("Is a directory")
assert "SHLVL=1" == linux_replace("SHLVL=0", "SHLVL=1")("SHLVL=0")
assert "" == linux_replace("\\", "")("\\")
def test_linux_discard():
with config_context(platform='darwin'):
assert "" == linux_discard("")
assert "foo" == linux_discard("foo")
with config_context(platform='linux'):
assert DISCARDED_TEXT == linux_discard("")
assert DISCARDED_TEXT == linux_discard("foo")
|