Skip to content

Commit 143c341

Browse files
committed
tests: Add ability to test uPy cmdline executable.
This allows to test options passed to cmdline executable, as well as the behaviour of the REPL.
1 parent af43565 commit 143c341

5 files changed

Lines changed: 75 additions & 5 deletions

File tree

tests/cmdline/cmd_verbose.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# cmdline: -v -v
2+
# test verbose output
3+
print(1)

tests/cmdline/cmd_verbose.py.exp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
1
2+
File cmdline/cmd_verbose.py, code block '<module>' (descriptor: ######
3+
Raw bytecode (code_info_size=8, bytecode_size=13):
4+
08 82 23 83 45 43 00 00 02 00 00 1c 81 13 00 81
5+
64 01 32 11 5b
6+
arg names:
7+
(N_STATE 2)
8+
(N_EXC_STACK 0)
9+
(NUM_LOCAL 0)
10+
bc=-3 line=1
11+
bc=0 line=3
12+
00 LOAD_NAME print (cache=0)
13+
04 LOAD_CONST_SMALL_INT 1
14+
05 CALL_FUNCTION n=1 nkw=0
15+
07 POP_TOP
16+
08 LOAD_CONST_NONE
17+
09 RETURN_VALUE
18+
mem: total=######
19+
stack: ######
20+
GC: total: ######
21+
No. of 1-blocks: ######

tests/cmdline/repl_basic.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# basic REPL tests
2+
print(1)
3+
OA

tests/cmdline/repl_basic.py.exp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Micro Python ######
2+
>>> # basic REPL tests
3+
>>> print(1)
4+
1
5+
>>> print(1)
6+
1
7+
>>>

tests/run-tests

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,46 @@ def rm_f(fname):
2828
def run_micropython(pyb, args, test_file):
2929
if pyb is None:
3030
# run on PC
31-
try:
32-
output_mupy = subprocess.check_output([MICROPYTHON, '-X', 'emit=' + args.emit, test_file])
33-
except subprocess.CalledProcessError:
34-
output_mupy = b'CRASH'
31+
if test_file.startswith('cmdline/'):
32+
# special handling for tests of the unix cmdline program
33+
34+
# check for any cmdline options needed for this test
35+
args = [MICROPYTHON]
36+
with open(test_file, 'rb') as f:
37+
line = f.readline()
38+
if line.startswith(b'# cmdline:'):
39+
args += line[10:].strip().split()
40+
41+
# run the test, possibly with redirected input
42+
try:
43+
if test_file.startswith('cmdline/repl_'):
44+
f = open(test_file, 'rb')
45+
output_mupy = subprocess.check_output(args, stdin=f)
46+
f.close()
47+
else:
48+
output_mupy = subprocess.check_output(args + [test_file])
49+
except subprocess.CalledProcessError:
50+
output_mupy = b'CRASH'
51+
52+
# erase parts of the output that are not stable across runs
53+
with open(test_file + '.exp', 'rb') as f:
54+
lines_exp = f.readlines()
55+
lines_mupy = [line + b'\n' for line in output_mupy.split(b'\n')]
56+
if output_mupy.endswith(b'\n'):
57+
lines_mupy = lines_mupy[:-1] # remove erroneous last empty line
58+
if len(lines_mupy) == len(lines_exp):
59+
for i in range(len(lines_mupy)):
60+
pos = lines_exp[i].find(b'######')
61+
if pos != -1 and len(lines_mupy[i]) >= pos:
62+
lines_mupy[i] = lines_mupy[i][:pos] + b'######\n'
63+
output_mupy = b''.join(lines_mupy)
64+
65+
else:
66+
# a standard test
67+
try:
68+
output_mupy = subprocess.check_output([MICROPYTHON, '-X', 'emit=' + args.emit, test_file])
69+
except subprocess.CalledProcessError:
70+
output_mupy = b'CRASH'
3571
else:
3672
# run on pyboard
3773
import pyboard
@@ -186,7 +222,7 @@ def main():
186222
if args.test_dirs is None:
187223
if pyb is None:
188224
# run PC tests
189-
test_dirs = ('basics', 'micropython', 'float', 'import', 'io', 'misc', 'unicode', 'extmod', 'unix')
225+
test_dirs = ('basics', 'micropython', 'float', 'import', 'io', 'misc', 'unicode', 'extmod', 'unix', 'cmdline')
190226
else:
191227
# run pyboard tests
192228
test_dirs = ('basics', 'micropython', 'float', 'misc', 'extmod', 'pyb', 'pybnative', 'inlineasm')

0 commit comments

Comments
 (0)