summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJim Mussared <jim.mussared@gmail.com>2023-05-11 14:28:35 +1000
committerDamien George <damien@micropython.org>2023-05-18 13:48:21 +1000
commit17127bbee52f2c674c5c79b675fabacea7bc2751 (patch)
treef776a6511f071eb3a6dff24673d8606bbaf97b45
parentab3f9ecb595bf114c744e253cdbd6d1e42951d2f (diff)
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>
-rw-r--r--tests/io/file1.py20
-rw-r--r--tests/io/file_iter.py2
-rw-r--r--tests/io/file_long_read.py2
-rw-r--r--tests/io/file_long_read2.py2
-rw-r--r--tests/io/file_long_read3.py2
-rw-r--r--tests/io/file_readinto.py6
-rw-r--r--tests/io/file_readinto_len.py4
-rw-r--r--tests/io/file_readline.py4
-rw-r--r--tests/io/file_seek.py6
-rw-r--r--tests/io/file_with.py2
-rwxr-xr-xtests/run-tests.py16
-rw-r--r--tests/unicode/file1.py2
-rw-r--r--tests/unicode/file2.py2
-rw-r--r--tests/unicode/file_invalid.py2
14 files changed, 38 insertions, 34 deletions
diff --git a/tests/io/file1.py b/tests/io/file1.py
index de30045d3..1274a653d 100644
--- a/tests/io/file1.py
+++ b/tests/io/file1.py
@@ -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:
diff --git a/tests/io/file_iter.py b/tests/io/file_iter.py
index 48e873996..26e82b9b1 100644
--- a/tests/io/file_iter.py
+++ b/tests/io/file_iter.py
@@ -1,3 +1,3 @@
-f = open("io/data/file1")
+f = open("data/file1")
for l in f:
print(l)
diff --git a/tests/io/file_long_read.py b/tests/io/file_long_read.py
index 8bdd48450..3f57d5594 100644
--- a/tests/io/file_long_read.py
+++ b/tests/io/file_long_read.py
@@ -1,3 +1,3 @@
-f = open("io/data/file1")
+f = open("data/file1")
b = f.read(100)
print(len(b))
diff --git a/tests/io/file_long_read2.py b/tests/io/file_long_read2.py
index 337a5fba9..ea87f91f1 100644
--- a/tests/io/file_long_read2.py
+++ b/tests/io/file_long_read2.py
@@ -1,4 +1,4 @@
-f = open("io/data/bigfile1")
+f = open("data/bigfile1")
b = f.read()
print(len(b))
print(b)
diff --git a/tests/io/file_long_read3.py b/tests/io/file_long_read3.py
index d8b0cce55..1ea47e185 100644
--- a/tests/io/file_long_read3.py
+++ b/tests/io/file_long_read3.py
@@ -1,4 +1,4 @@
-f = open("io/data/bigfile1", "rb")
+f = open("data/bigfile1", "rb")
b = f.read(512)
print(len(b))
print(b)
diff --git a/tests/io/file_readinto.py b/tests/io/file_readinto.py
index 1f3702a21..f9004013d 100644
--- a/tests/io/file_readinto.py
+++ b/tests/io/file_readinto.py
@@ -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:
diff --git a/tests/io/file_readinto_len.py b/tests/io/file_readinto_len.py
index 84cc8cf5e..d6eb1dfc4 100644
--- a/tests/io/file_readinto_len.py
+++ b/tests/io/file_readinto_len.py
@@ -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)
diff --git a/tests/io/file_readline.py b/tests/io/file_readline.py
index 86d010eaf..3d270db51 100644
--- a/tests/io/file_readline.py
+++ b/tests/io/file_readline.py
@@ -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:
diff --git a/tests/io/file_seek.py b/tests/io/file_seek.py
index 2fe57692c..3990df840 100644
--- a/tests/io/file_seek.py
+++ b/tests/io/file_seek.py
@@ -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)
diff --git a/tests/io/file_with.py b/tests/io/file_with.py
index 899c0f928..d5217dfe9 100644
--- a/tests/io/file_with.py
+++ b/tests/io/file_with.py
@@ -1,4 +1,4 @@
-f = open("io/data/file1")
+f = open("data/file1")
with f as f2:
print(f2.read())
diff --git a/tests/run-tests.py b/tests/run-tests.py
index 03269a9f1..498db8b40 100755
--- a/tests/run-tests.py
+++ b/tests/run-tests.py
@@ -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)
diff --git a/tests/unicode/file1.py b/tests/unicode/file1.py
index 08b7d2504..6508b5f71 100644
--- a/tests/unicode/file1.py
+++ b/tests/unicode/file1.py
@@ -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))
diff --git a/tests/unicode/file2.py b/tests/unicode/file2.py
index 1a5b933c8..c35c18789 100644
--- a/tests/unicode/file2.py
+++ b/tests/unicode/file2.py
@@ -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))
diff --git a/tests/unicode/file_invalid.py b/tests/unicode/file_invalid.py
index 3f7f18406..5c4230251 100644
--- a/tests/unicode/file_invalid.py
+++ b/tests/unicode/file_invalid.py
@@ -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")