tests/run-tests.py: Ensure correct cwd for mpy tests.

Previously when using --via-mpy, the file was compiled to tests/<tmp>.mpy
and then run using `micropython -m <tmp>` in the current cwd
(usually tests/).  This meant that an import in the test would be resolved
relative to tests/.

This is different to regular (non-via-mpy) tests, where we run (for
example) `micropython basics/test.py` which means that an import would be
resolved relative to basics/.

Now --via-mpy matches the .py behavior.  This is important because:
a) It makes it so import tests do the right thing.
b) There are directory names in tests/ that match built-in module names.

Furthermore, it always ensures the cwd (for both micropython and cpython)
is the test directory (e.g. basics/) rather than being left unset.  This
also makes it clearer inside the test that e.g. file access is relative to
the Python file.

Updated tests with file paths to match.

This work was funded through GitHub Sponsors.

Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
pull/11468/head
Jim Mussared 2023-05-11 14:28:35 +10:00 zatwierdzone przez Damien George
rodzic ab3f9ecb59
commit 17127bbee5
14 zmienionych plików z 38 dodań i 34 usunięć

Wyświetl plik

@ -1,20 +1,20 @@
f = open("io/data/file1")
f = open("data/file1")
print(f.read(5))
print(f.readline())
print(f.read())
f = open("io/data/file1")
f = open("data/file1")
print(f.readlines())
f = open("io/data/file1", "r")
f = open("data/file1", "r")
print(f.readlines())
f = open("io/data/file1", "rb")
f = open("data/file1", "rb")
print(f.readlines())
f = open("io/data/file1", mode="r")
f = open("data/file1", mode="r")
print(f.readlines())
f = open("io/data/file1", mode="rb")
f = open("data/file1", mode="rb")
print(f.readlines())
# write() error
f = open("io/data/file1", "r")
f = open("data/file1", "r")
try:
f.write("x")
except OSError:
@ -22,7 +22,7 @@ except OSError:
f.close()
# read(n) error on binary file
f = open("io/data/file1", "ab")
f = open("data/file1", "ab")
try:
f.read(1)
except OSError:
@ -30,7 +30,7 @@ except OSError:
f.close()
# read(n) error on text file
f = open("io/data/file1", "at")
f = open("data/file1", "at")
try:
f.read(1)
except OSError:
@ -38,7 +38,7 @@ except OSError:
f.close()
# read() w/o args error
f = open("io/data/file1", "ab")
f = open("data/file1", "ab")
try:
f.read()
except OSError:

Wyświetl plik

@ -1,3 +1,3 @@
f = open("io/data/file1")
f = open("data/file1")
for l in f:
print(l)

Wyświetl plik

@ -1,3 +1,3 @@
f = open("io/data/file1")
f = open("data/file1")
b = f.read(100)
print(len(b))

Wyświetl plik

@ -1,4 +1,4 @@
f = open("io/data/bigfile1")
f = open("data/bigfile1")
b = f.read()
print(len(b))
print(b)

Wyświetl plik

@ -1,4 +1,4 @@
f = open("io/data/bigfile1", "rb")
f = open("data/bigfile1", "rb")
b = f.read(512)
print(len(b))
print(b)

Wyświetl plik

@ -1,13 +1,13 @@
b = bytearray(30)
f = open("io/data/file1", "rb")
f = open("data/file1", "rb")
print(f.readinto(b))
print(b)
f = open("io/data/file2", "rb")
f = open("data/file2", "rb")
print(f.readinto(b))
print(b)
# readinto() on writable file
f = open("io/data/file1", "ab")
f = open("data/file1", "ab")
try:
f.readinto(bytearray(4))
except OSError:

Wyświetl plik

@ -1,10 +1,10 @@
b = bytearray(30)
f = open("io/data/file1", "rb")
f = open("data/file1", "rb")
# 2nd arg (length to read) is extension to CPython
print(f.readinto(b, 8))
print(b)
b = bytearray(4)
f = open("io/data/file1", "rb")
f = open("data/file1", "rb")
print(f.readinto(b, 8))
print(b)

Wyświetl plik

@ -1,4 +1,4 @@
f = open("io/data/file1")
f = open("data/file1")
print(f.readline())
print(f.readline(3))
print(f.readline(4))
@ -6,7 +6,7 @@ print(f.readline(5))
print(f.readline())
# readline() on writable file
f = open("io/data/file1", "ab")
f = open("data/file1", "ab")
try:
f.readline()
except OSError:

Wyświetl plik

@ -1,4 +1,4 @@
f = open("io/data/file1", "rb")
f = open("data/file1", "rb")
print(f.seek(6))
print(f.read(5))
print(f.tell())
@ -18,14 +18,14 @@ print(f.tell())
f.close()
# test text mode
f = open("io/data/file1", "rt")
f = open("data/file1", "rt")
print(f.seek(6))
print(f.read(5))
print(f.tell())
f.close()
# seek closed file
f = open("io/data/file1", "r")
f = open("data/file1", "r")
f.close()
try:
f.seek(1)

Wyświetl plik

@ -1,4 +1,4 @@
f = open("io/data/file1")
f = open("data/file1")
with f as f2:
print(f2.read())

Wyświetl plik

@ -261,29 +261,31 @@ def run_micropython(pyb, args, test_file, is_special=False):
# a standard test run on PC
# create system command
cmdlist = [MICROPYTHON, "-X", "emit=" + args.emit]
cmdlist = [os.path.abspath(MICROPYTHON), "-X", "emit=" + args.emit]
if args.heapsize is not None:
cmdlist.extend(["-X", "heapsize=" + args.heapsize])
if sys.platform == "darwin":
cmdlist.extend(["-X", "realtime"])
cwd = os.path.dirname(test_file)
# if running via .mpy, first compile the .py file
if args.via_mpy:
mpy_modname = tempfile.mktemp(dir="")
mpy_filename = mpy_modname + ".mpy"
mpy_filename = tempfile.mktemp(dir=cwd, suffix=".mpy")
subprocess.check_output(
[MPYCROSS]
+ args.mpy_cross_flags.split()
+ ["-o", mpy_filename, "-X", "emit=" + args.emit, test_file]
)
mpy_modname = os.path.splitext(os.path.basename(mpy_filename))[0]
cmdlist.extend(["-m", mpy_modname])
else:
cmdlist.append(test_file)
cmdlist.append(os.path.abspath(test_file))
# run the actual test
try:
output_mupy = subprocess.check_output(
cmdlist, stderr=subprocess.STDOUT, timeout=TEST_TIMEOUT
cmdlist, stderr=subprocess.STDOUT, timeout=TEST_TIMEOUT, cwd=cwd
)
except subprocess.CalledProcessError as er:
had_crash = True
@ -707,7 +709,9 @@ def run_tests(pyb, tests, args, result_dir, num_threads=1):
else:
# run CPython to work out expected output
try:
output_expected = subprocess.check_output(CPYTHON3_CMD + [test_file])
output_expected = subprocess.check_output(
CPYTHON3_CMD + [os.path.abspath(test_file)], cwd=os.path.dirname(test_file)
)
if args.write_exp:
with open(test_file_expected, "wb") as f:
f.write(output_expected)

Wyświetl plik

@ -1,4 +1,4 @@
f = open("unicode/data/utf-8_1.txt", encoding="utf-8")
f = open("data/utf-8_1.txt", encoding="utf-8")
l = f.readline()
print(l)
print(len(l))

Wyświetl plik

@ -6,7 +6,7 @@ def do(mode):
enc = None
else:
enc = "utf-8"
f = open("unicode/data/utf-8_2.txt", mode=mode, encoding=enc)
f = open("data/utf-8_2.txt", mode=mode, encoding=enc)
print(f.read(1))
print(f.read(1))
print(f.read(2))

Wyświetl plik

@ -1,5 +1,5 @@
try:
f = open("unicode/data/utf-8_invalid.txt", encoding="utf-8")
f = open("data/utf-8_invalid.txt", encoding="utf-8")
f.read()
except UnicodeError:
print("UnicodeError")