From 17127bbee52f2c674c5c79b675fabacea7bc2751 Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Thu, 11 May 2023 14:28:35 +1000 Subject: [PATCH] tests/run-tests.py: Ensure correct cwd for mpy tests. Previously when using --via-mpy, the file was compiled to tests/.mpy and then run using `micropython -m ` 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 --- tests/io/file1.py | 20 ++++++++++---------- tests/io/file_iter.py | 2 +- tests/io/file_long_read.py | 2 +- tests/io/file_long_read2.py | 2 +- tests/io/file_long_read3.py | 2 +- tests/io/file_readinto.py | 6 +++--- tests/io/file_readinto_len.py | 4 ++-- tests/io/file_readline.py | 4 ++-- tests/io/file_seek.py | 6 +++--- tests/io/file_with.py | 2 +- tests/run-tests.py | 16 ++++++++++------ tests/unicode/file1.py | 2 +- tests/unicode/file2.py | 2 +- tests/unicode/file_invalid.py | 2 +- 14 files changed, 38 insertions(+), 34 deletions(-) diff --git a/tests/io/file1.py b/tests/io/file1.py index de30045d31..1274a653d1 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 48e8739966..26e82b9b1a 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 8bdd484504..3f57d5594d 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 337a5fba96..ea87f91f10 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 d8b0cce550..1ea47e1853 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 1f3702a217..f9004013d9 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 84cc8cf5e1..d6eb1dfc41 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 86d010eaf6..3d270db514 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 2fe57692c6..3990df8409 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 899c0f9287..d5217dfe96 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 03269a9f1c..498db8b404 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 08b7d25041..6508b5f716 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 1a5b933c89..c35c18789c 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 3f7f184062..5c42302519 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")