8000 tools/mpremote: Add initial regression tests for mpremote. · anight/micropython@6461ffd · GitHub
[go: up one dir, main page]

Skip to content

Commit 6461ffd

Browse files
jimmodpgeorge
authored andcommitted
tools/mpremote: Add initial regression tests for mpremote.
These tests are specifically for the command-line interface and cover: - resume/soft-reset/connect/disconnect - mount - fs cp,touch,mkdir,cat,sha256sum,rm,rmdir - eval/exec/run This work was funded through GitHub Sponsors. Signed-off-by: Jim Mussared <jim.mussared@gmail.com> Signed-off-by: Damien George <damien@micropython.org>
1 parent dd6f78f commit 6461ffd

12 files changed

+654
-0
lines changed

tools/mpremote/tests/README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Tests for mpremote
2+
3+
This directory contains a set of tests for `mpremote`.
4+
5+
Requirements:
6+
- A device running MicroPython connected to a serial port on the host.
7+
- Python 3.x, `bash` and various Unix tools such as `find`, `mktemp`, `sed`, `sort`, `tr`.
8+
9+
To run the tests do:
10+
11+
$ ./run-mpremote-tests.sh
12+
13+
Each test should print "OK" if it passed. Otherwise it will print "CRASH", or "FAIL"
14+
and a diff of the expected and actual test output.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/bin/bash
2+
set -e
3+
4+
TEST_DIR=$(dirname $0)
5+
MPREMOTE=${TEST_DIR}/../mpremote.py
6+
7+
if [ -z "$1" ]; then
8+
# Find tests matching test_*.sh
9+
TESTS=${TEST_DIR}/test_*.sh
10+
else
11+
# Specific test path from the command line.
12+
TESTS="$1"
13+
fi
14+
15+
for t in $TESTS; do
16+
TMP=$(mktemp -d)
17+
echo -n "${t}: "
18+
# Strip CR and replace the random temp dir with a token.
19+
if env MPREMOTE=${MPREMOTE} TMP="${TMP}" "${t}" | tr -d '\r' | sed "s,${TMP},"'${TMP},g' > "${t}.out"; then
20+
if diff "${t}.out" "${t}.exp" > /dev/null; then
21+
echo "OK"
22+
else
23+
echo "FAIL"
24+
diff "${t}.out" "${t}.exp" || true
25+
fi
26+
else
27+
echo "CRASH"
28+
fi
29+
rm -r "${TMP}"
30+
done
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/bin/bash
2+
set -e
3+
4+
$MPREMOTE exec "print('mpremote')"
5+
6+
$MPREMOTE exec "print('before sleep'); import time; time.sleep(0.1); print('after sleep')"
7+
$MPREMOTE exec --no-follow "print('before sleep'); import time; time.sleep(0.1); print('after sleep')"
8+
sleep 0.3
9+
10+
$MPREMOTE eval "1+2"
11+
$MPREMOTE eval "[{'a': 'b'}, (1,2,3,), True]"
12+
13+
cat << EOF > /tmp/run.py
14+
print("run")
15+
EOF
16+
17+
$MPREMOTE run /tmp/run.py
18+
19+
cat << EOF > /tmp/run.py
20+
import time
21+
for i in range(3):
22+
time.sleep(0.1)
23+
print("run")
24+
EOF
25+
$MPREMOTE run --no-follow /tmp/run.py
26+
sleep 0.5
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
mpremote
2+
before sleep
3+
after sleep
4+
3
5+
[{'a': 'b'}, (1, 2, 3), True]
6+
run
Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
#!/bin/bash
2+
set -e
3+
4+
# Creates a RAM disk big enough to hold two copies of the test directory
5+
# structure.
6+
cat << EOF > "${TMP}/ramdisk.py"
7+
class RAMBlockDev:
8+
def __init__(self, block_size, num_blocks):
9+
self.block_size = block_size
10+
self.data = bytearray(block_size * num_blocks)
11+
12+
def readblocks(self, block_num, buf):
13+
for i in range(len(buf)):
14+
buf[i] = self.data[block_num * self.block_size + i]
15+
16+
def writeblocks(self, block_num, buf):
17+
for i in range(len(buf)):
18+
self.data[block_num * self.block_size + i] = buf[i]
19+
20+
def ioctl(self, op, arg):
21+
if op == 4: # get number of blocks
22+
return len(self.data) // self.block_size
23+
if op == 5: # get block size
24+
return self.block_size
25+
26+
import os
27+
28+
bdev = RAMBlockDev(512, 50)
29+
os.VfsFat.mkfs(bdev)
30+
os.mount(bdev, '/ramdisk')
31+
os.chdir('/ramdisk')
32+
EOF
33+
34+
35+
echo -----
36+
$MPREMOTE run "${TMP}/ramdisk.py"
37+
$MPREMOTE resume ls
38+
39+
echo -----
40+
$MPREMOTE resume touch a.py
41+
$MPREMOTE resume touch :b.py
42+
$MPREMOTE resume ls :
43+
$MPREMOTE resume cat a.py
44+
$MPREMOTE resume cat :b.py
45+
$MPREMOTE resume sha256sum a.py
46+
echo -n "" | sha256sum
47+
48+
echo -----
49+
cat << EOF > "${TMP}/a.py"
50+
print("Hello")
51+
print("World")
52+
EOF
53+
$MPREMOTE resume cp "${TMP}/a.py" :
54+
$MPREMOTE resume cp "${TMP}/a.py" :b.py
55+
$MPREMOTE resume cp "${TMP}/a.py" :c.py
56+
$MPREMOTE resume cp :a.py :d.py
57+
$MPREMOTE resume ls
58+
$MPREMOTE resume exec "import a; import b; import c"
59+
$MPREMOTE resume sha256sum a.py
60+
cat "${TMP}/a.py" | sha256sum
61+
62+
echo -----
63+
$MPREMOTE resume mkdir aaa
64+
$MPREMOTE resume mkdir :bbb
65+
$MPREMOTE resume cp "${TMP}/a.py" :aaa
66+
$MPREMOTE resume cp "${TMP}/a.py" :bbb/b.py
67+
$MPREMOTE resume cat :aaa/a.py bbb/b.py
68+
69+
echo -----
70+
$MPREMOTE resume rm :b.py c.py
71+
$MPREMOTE resume ls
72+
$MPREMOTE resume rm :aaa/a.py bbb/b.py
73+
$MPREMOTE resume rmdir aaa :bbb
74+
$MPREMOTE resume ls
75+
76+
echo -----
77+
env EDITOR="sed -i s/Hello/Goodbye/" $MPREMOTE resume edit d.py
78+
$MPREMOTE resume sha256sum :d.py
79+
$MPREMOTE resume exec "import d"
80+
81+
82+
# Create a local directory structure and copy it to `:` on the device.
83+
echo -----
84+
mkdir -p "${TMP}/package"
85+
mkdir -p "${TMP}/package/subpackage"
86+
cat << EOF > "${TMP}/package/__init__.py"
87+
from .x import x
88+
from .subpackage import y
89+
EOF
90+
cat << EOF > "${TMP}/package/x.py"
91+
def x():
92+
print("x")
93+
EOF
94+
cat << EOF > "${TMP}/package/subpackage/__init__.py"
95+
from .y import y
96+
EOF
97+
cat << EOF > "${TMP}/package/subpackage/y.py"
98+
def y():
99+
print("y")
100+
EOF
101+
$MPREMOTE run "${TMP}/ramdisk.py"
102+
$MPREMOTE resume cp -r "${TMP}/package" :
103+
$MPREMOTE resume ls : :package :package/subpackage
104+
$MPREMOTE resume exec "import package; package.x(); package.y()"
105+
106+
107+
# Same thing except with a destination directory name.
108+
echo -----
109+
$MPREMOTE run "${TMP}/ramdisk.py"
110+
$MPREMOTE resume cp -r "${TMP}/package" :package2
111+
$MPREMOTE resume ls : :package2 :package2/subpackage
112+
$MPREMOTE resume exec "import package2; package2.x(); package2.y()"
113+
114+
115+
# Copy to an existing directory, it will be copied inside.
116+
echo -----
117+
$MPREMOTE run "${TMP}/ramdisk.py"
118+
$MPREMOTE resume mkdir :test
119+
$MPREMOTE resume cp -r "${TMP}/package" :test
120+
$MPREMOTE resume ls :test :test/package :test/package/subpackage
121+
122+
# Copy to non-existing sub-directory.
123+
echo -----
124+
$MPREMOTE resume cp -r "${TMP}/package" :test/package2
125+
$MPREMOTE resume ls :test :test/package2 :test/package2/subpackage
126+
127+
# Copy from the device back to local.
128+
echo -----
129+
mkdir "${TMP}/copy"
130+
$MPREMOTE resume cp -r :test/package "${TMP}/copy"
131+
ls "${TMP}/copy" "${TMP}/copy/package" "${TMP}/copy/package/subpackage"
132+
133+
# Copy from the device back to local with destination directory name.
134+
echo -----
135+
$MPREMOTE resume cp -r :test/package "${TMP}/copy/package2"
136+
ls "${TMP}/copy" "${TMP}/copy/package2" "${TMP}/copy/package2/subpackage"
137+
138+
139+
# Copy from device to another location on the device with destination directory name.
140+
echo -----
141+
$MPREMOTE run "${TMP}/ramdisk.py"
142+
$MPREMOTE resume cp -r "${TMP}/package" :
143+
$MPREMOTE resume cp -r :package :package3
144+
$MPREMOTE resume ls : :package3 :package3/subpackage
145+
146+
# Copy from device to another location on the device into an existing directory.
147+
echo -----
148+
$MPREMOTE run "${TMP}/ramdisk.py"
149+
$MPREMOTE resume cp -r "${TMP}/package" :
150+
$MPREMOTE resume mkdir :package4
151+
$MPREMOTE resume cp -r :package :package4
152+
$MPREMOTE resume ls : :package4 :package4/package :package4/package/subpackage
153+
154+
# Repeat an existing copy with one file modified.
155+
echo -----
156+
cat << EOF > "${TMP}/package/subpackage/y.py"
157+
def y():
158+
print("y2")
159+
EOF
160+
$MPREMOTE resume cp -r "${TMP}/package" :
161+
$MPREMOTE resume ls : :package :package/subpackage
162+
$MPREMOTE resume exec "import package; package.x(); package.y()"

0 commit comments

Comments
 (0)
0