-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbash2python_test.py
executable file
·213 lines (185 loc) · 9.09 KB
/
bash2python_test.py
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#!/usr/bin/env pytest
## TODO (have the script invoke pytest: see mezcla/tests/test_system.py)
#!/usr/bin/env python
#
# Simple test suite for bash2python.py. oming up with general tests will be difficult,
# given the open-ended nature of the task. So just concentrate on the conversion of
# simple examples that should be converted for each of the main supported features
# (e.g., assignments, loops, and environment support).
#
# Notes:
# - Use 'pytest bash2python_test.py -k "not skip"' to skip the tests that cover functions not yet implemented.
#
## Temp: Tom's notes (TODO: delete when reviewed)
## - Add comments as tests are creatred because it is harder to add after the face. You just need to cover
## the intention of the test not the specifics.
## - Add helper function for following sequence (n.b., in general avoid significant duplication of code):
## subprocess.check_output(["python3", "bash2python.py", "--script", bash_script])
## .decode()
## .strip()
## == expected_python_script
## - Please avoid putting function-level comments before the function, as this disrupts quickly
## scanning the file. Instead, put them after the docstring as with my revision to test_for_loop.
## - Please run pylint for anything I expressed interest in modifying, including tests. You can filter out
## nitpicking ones as in the the the python-lint alias(es) from tomohara-aliases.bash. (See my
## python-lint example at bottom in case you have trouble sourcing it.)
## - Put tests in ./tests subdirectory as with tests/test_check_errors.py (n.b., in general follow the
## convention of the repo).
##
"""Tests for bash2python.py"""
import subprocess
import pytest
def test_variable_substitution():
"""TODO add brief high-level comment"""
bash_script = "echo $foo"
expected_python_script = "run(f'echo {foo}')"
assert (
subprocess.check_output(["python3", "bash2python.py", "--script", bash_script])
.decode()
.strip()
== expected_python_script
)
def test_operator_substitution():
"""TODO add brief high-level comment"""
bash_script = "if [ 1 -gt 0 -lt 2]; then echo Hello; fi"
expected_python_script = "if 1 > 0 < 2:\n run('echo Hello')"
assert (
subprocess.check_output(["python3", "bash2python.py", "--script", bash_script])
.decode()
.strip()
== expected_python_script
)
def test_comments_1():
"""TODO add brief high-level comment"""
bash_script = "# This is a comment"
expected_python_script = "# This is a comment"
assert (
subprocess.check_output(["python3", "bash2python.py", "--script", bash_script])
.decode()
.strip()
== expected_python_script
)
def test_while_loop():
"""TODO add brief high-level comment"""
bash_script = "while [ $i -le 5 ]; do echo $i; done"
expected_python_script = "while {i} <= 5 :\n run(f'echo {i}')"
assert (
subprocess.check_output(["python3", "bash2python.py", "--script", bash_script])
.decode()
.strip()
== expected_python_script
)
def test_command_substitution():
"""TODO add brief high-level comment"""
bash_script = "echo $(ls)"
expected_python_script = "run('echo $(ls)', skip_print=False)"
assert (
subprocess.check_output(["python3", "bash2python.py", "--script", bash_script])
.decode()
.strip()
== expected_python_script
)
def test_command_pipe():
"""TODO add brief high-level comment"""
bash_script = "ls | grep test | wc -l"
expected_python_script = "run('ls | grep test | wc -l')"
assert (
subprocess.check_output(["python3", "bash2python.py", "--script", bash_script])
.decode()
.strip()
== expected_python_script
)
def test_comments_2():
"""TODO add brief high-level comment"""
bash_script = "echo $foo # Another comment"
expected_python_script = "run(f'echo {foo} ') # Another comment"
assert (
subprocess.check_output(["python3", "bash2python.py", "--script", bash_script])
.decode()
.strip()
== expected_python_script
)
def test_variable_assignment():
"""TODO add brief high-level comment"""
bash_script = "name='John Doe'; echo $name"
expected_python_script = "name = 'John Doe'\n run(f'echo {name}')"
assert (
subprocess.check_output(["python3", "bash2python.py", "--script", bash_script])
.decode()
.strip()
== expected_python_script
)
def test_arithmetic_expression():
"""TODO add brief high-level comment"""
bash_script = "echo $((2+3*4))"
expected_python_script = "run('echo $((2+3*4))', skip_print=False)"
assert (
subprocess.check_output(["python3", "bash2python.py", "--script", bash_script])
.decode()
.strip()
== expected_python_script
)
def test_double_quotes():
"""Make sure double quotes escaped inside run calls"""
bash_script = 'echo "Hello world"'
expected_python_script = "run('echo \"Hello world\"')"
assert (
subprocess.check_output(["python3", "bash2python.py", "--script", bash_script])
.decode()
.strip()
== expected_python_script
)
@pytest.mark.skip
def test_for_loop():
"""TODO flesh out: test problematic for loop"""
## NOTE: This 2 tests will be fail with the actual code because they respond to non-implemented functions yet
bash_script = "for i in {1..5}; do echo $i; done"
expected_python_script = "for i in range(1, 6):\n run(f'echo {i}')"
assert (
subprocess.check_output(["python3", "bash2python.py", "--script", bash_script])
.decode()
.strip()
== expected_python_script
)
@pytest.mark.skip
def test_if_else_condition():
"""TODO flesh out: test if/else"""
bash_script = "if [ $a -eq $b ]; then echo 'Equal'; else echo 'Not Equal'; fi"
expected_python_script = (
"if a == b:\n run('echo 'Equal'')\nelse:\n run('echo 'Not Equal'')"
)
assert (
subprocess.check_output(["python3", "bash2python.py", "--script", bash_script])
.decode()
.strip()
== expected_python_script
)
def test_embedded_for_not_supported():
"""Make sure that embedded FOR issues warning about not being supported"""
assert(False)
##---------------------------------------------------------------------------------
##TEMP
## Example of Tom's python-lint alias
## $ cmd-trace python-lint backup/bash2python_test.py.~1~ 2>&1 | egrep 'grep|bash2python_test'
## + eval 'python-lint backup/bash2python_test.py.~1~'
## ++ python-lint backup/bash2python_test.py.~1~
## ++ python-lint-work backup/bash2python_test.py.~1~
## ++ command grep --perl-regexp -v '(Exactly one space required)|\((bad-continuation|bad-whitespace|bad-indentation|bare-except|c-extension-no-member|consider-using-enumerate|consider-using-f-string|consider-using-with|global-statement|global-variable-not-assigned|keyword-arg-before-vararg|len-as-condition|line-too-long|logging-not-lazy|misplaced-comparison-constant|missing-final-newline|redefined-variable-type|redundant-keyword-arg|superfluous-parens|too-many-arguments|too-many-instance-attributes|trailing-newlines|useless-\S+|wrong-import-order|wrong-import-position)\)'
## ++ python-lint-full backup/bash2python_test.py.~1~
## ++ command grep --perl-regexp -v '\((bad-continuation|bad-option-value|fixme|invalid-name|locally-disabled|too-few-public-methods|too-many-\S+|trailing-whitespace|star-args|unnecessary-pass)\)'
## ++ command grep --perl-regexp -v '^(([A-Z]:[0-9]+)|(Your code has been rated)|(No config file found)|(PYLINTHOME is now)|(\-\-\-\-\-))'
## ++ nice -19 pylint backup/bash2python_test.py.~1~
## ************* Module bash2python_test.py
## backup/bash2python_test.py.~1~:1:0: C0114: Missing module docstring (missing-module-docstring)
## backup/bash2python_test.py.~1~:5:0: C0116: Missing function or method docstring (missing-function-docstring)
## backup/bash2python_test.py.~1~:16:0: C0116: Missing function or method docstring (missing-function-docstring)
## backup/bash2python_test.py.~1~:27:0: C0116: Missing function or method docstring (missing-function-docstring)
## backup/bash2python_test.py.~1~:38:0: C0116: Missing function or method docstring (missing-function-docstring)
## backup/bash2python_test.py.~1~:49:0: C0116: Missing function or method docstring (missing-function-docstring)
## backup/bash2python_test.py.~1~:60:0: C0116: Missing function or method docstring (missing-function-docstring)
## backup/bash2python_test.py.~1~:71:0: C0116: Missing function or method docstring (missing-function-docstring)
## backup/bash2python_test.py.~1~:82:0: C0116: Missing function or method docstring (missing-function-docstring)
## backup/bash2python_test.py.~1~:93:0: C0116: Missing function or method docstring (missing-function-docstring)
## backup/bash2python_test.py.~1~:104:0: C0116: Missing function or method docstring (missing-function-docstring)
## backup/bash2python_test.py.~1~:118:0: C0116: Missing function or method docstring (missing-function-docstring)
## backup/bash2python_test.py.~1~:130:0: C0116: Missing function or method docstring (missing-function-docstring)