Skip to content

Commit 5f6d4cf

Browse files
committed
tests: Update tests to use the home directory set by connect().
Use the home directory set by `MPRemotePath.connect()` in tests to ensure that the home directory is consistent across different test runs. We cant assume that "/" is usable on the unix port (as it is read-only for users).
1 parent 0bd787d commit 5f6d4cf

File tree

4 files changed

+46
-39
lines changed

4 files changed

+46
-39
lines changed

tests/conftest.py

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,26 @@
11
import argparse
22
import logging.config
33
import os
4-
import shutil
5-
from contextlib import suppress
64
from pathlib import Path
75
from typing import Generator
86

97
import pytest
108
import unix_port
119
import yaml
12-
from mpremote.transport_serial import TransportError
1310

1411
from mpremote_path import MPRemotePath as MPath
1512

1613
# Set the default serial port for the micropython board to use the unix port.
1714
default_port = "unix"
1815
default_baud_rate = 115200
1916

17+
# Location of the tests and data directories on the local filesystem.
2018
tests_dir = Path(__file__).parent # Base directory for the tests.
2119
data_dir = tests_dir / "_data" # Local directory containing test data files.
2220
logging_config = tests_dir / "logging.yaml" # Logging configuration file.
2321

24-
board_test_dir = "/_tests" # Directory to create for tests on the micropython board.
22+
# Directory to create for tests on the micropython board.
23+
board_test_dir = "_mpremote_path_tests"
2524

2625
logging.config.dictConfig(yaml.safe_load(logging_config.read_text()))
2726

@@ -64,6 +63,7 @@ def pytest_addoption(parser: argparse.Namespace) -> None:
6463
)
6564

6665

66+
# TODO: Make this a function on the board.
6767
def rm_recursive(path: Path) -> None:
6868
"""Remove a directory and all it's contents recursively."""
6969
if path.is_file():
@@ -99,12 +99,12 @@ def serial_port(
9999
tests_dir = Path(__file__).parent # Base directory for the tests.
100100
unix_dir = tests_dir / "unix-micropython"
101101
micropython_path = unix_dir / "micropython"
102-
command = str(micropython_path) + " -i -m boot"
102+
command = str(micropython_path) + "" # " -i -m boot"
103103

104104
working_dir = tmp_path_factory.mktemp("unix_micropython")
105105
unix_fs = working_dir / "fs"
106106
unix_fs.mkdir() # Create the filesystem directory
107-
shutil.copy(unix_dir / "boot.py", unix_fs) # Copy boot.py to the fs
107+
# shutil.copy(unix_dir / "boot.py", unix_fs) # Copy boot.py to the fs
108108

109109
with unix_port.run_pty_bridge(
110110
command, cwd=unix_fs, use_socat=use_socat
@@ -130,36 +130,37 @@ def root(
130130
# entering and exiting the REPL for every command.
131131
with MPath.board.raw_repl():
132132
rm_recursive(MPath(board_test_dir)) # Clean up after previous test runs
133-
path, pwd = MPath("/"), MPath.cwd()
134-
path.chdir()
135-
yield path
136-
pwd.chdir()
133+
cwd = MPath.cwd()
134+
try:
135+
yield cwd
136+
finally:
137+
cwd.chdir()
138+
MPath.disconnect() # Disconnect from the board
137139

138140

139141
@pytest.fixture()
140142
def testdir(root: MPath) -> Generator[MPath, None, None]:
141143
path, pwd = MPath(board_test_dir), MPath.cwd()
142-
yield path
143-
if pwd: # Restore the previous working directory and cleanup
144+
try:
145+
yield path
146+
finally:
144147
pwd.chdir()
145148

146149

147150
@pytest.fixture()
148151
def testfolder(root: MPath) -> Generator[MPath, None, None]:
149152
"Create a test folder on the board and cd into it."
150-
path, pwd = MPath(board_test_dir), MPath.cwd()
151-
if path in (pwd, *pwd.parents):
152-
MPath("/").chdir()
153+
pwd, path = MPath("~").expanduser(), MPath(board_test_dir)
154+
pwd.chdir()
153155
rm_recursive(path)
154-
with suppress(TransportError, OSError):
155-
path.mkdir()
156+
path = path.resolve()
157+
path.mkdir()
156158
path.chdir()
157159
try:
158160
yield path
159161
finally:
160162
pwd.chdir()
161-
with suppress(TransportError, OSError):
162-
rm_recursive(path)
163+
# rm_recursive(path)
163164

164165

165166
@pytest.fixture()

tests/test_base.py

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
def test_root_folder(root: MPath) -> None:
2121
"""Test the RemotePath class."""
22-
p = MPath("/")
22+
p = root
2323
assert p.exists() is True
2424
assert len(list(p.stat())) == 10
2525
assert stat.S_ISDIR(p.stat().st_mode) is True
@@ -31,10 +31,10 @@ def test_root_folder(root: MPath) -> None:
3131
assert p.is_char_device() is False
3232
assert p.is_fifo() is False
3333
assert p.is_socket() is False
34-
assert p.cwd().as_posix() == "/"
35-
assert p.home().as_posix() == "/"
36-
assert MPath("~").expanduser().as_posix() == "/"
37-
assert MPath("~/boot.py").expanduser().as_posix() == "/boot.py"
34+
assert MPath.cwd().as_posix() == p.as_posix()
35+
assert p.home().as_posix() == p.as_posix()
36+
assert MPath("~").expanduser().as_posix() == p.as_posix()
37+
assert MPath("~/boot.py").expanduser().as_posix() == (p / "boot.py").as_posix()
3838

3939

4040
def test_mkdir(testdir: MPath) -> None:
@@ -74,13 +74,14 @@ def test_mkdir_parents(testdir: MPath) -> None:
7474
def test_cd(testdir: MPath) -> None:
7575
"Test changing directories"
7676
p = testdir
77+
resolved = p.resolve()
7778
assert p.exists() is False
7879
p.mkdir()
7980
assert p.exists() is True
8081
p.chdir()
81-
assert p.cwd().as_posix() == testdir.as_posix()
82-
MPath("/").chdir()
83-
assert p.cwd().as_posix() == "/"
82+
assert p.cwd().as_posix() == resolved.as_posix()
83+
MPath("~").expanduser().chdir()
84+
assert MPath.cwd().as_posix() == p.parent.resolve().as_posix()
8485
p.rmdir()
8586
assert p.exists() is False
8687

@@ -142,11 +143,16 @@ def test_read_write_text(testfolder: MPath) -> None:
142143

143144
def test_resolve_samefile(root: MPath) -> None:
144145
"Test resolving paths: absolute(), and resolve()"
145-
q = MPath("./lib/mpy")
146-
q = q / "../.././main.py"
146+
q = MPath("./lib/mpy") / "../.././main.py"
147147
assert q.as_posix() == "lib/mpy/../../main.py"
148-
assert q.absolute().as_posix() == "/lib/mpy/../../main.py"
149-
assert q.absolute().resolve().as_posix() == "/main.py"
148+
assert (
149+
q.absolute().as_posix()
150+
== MPath("~").expanduser().as_posix().rstrip("/") + "/lib/mpy/../../main.py"
151+
)
152+
assert (
153+
q.absolute().resolve().as_posix()
154+
== MPath("~").expanduser().as_posix().rstrip("/") + "/main.py"
155+
)
150156
assert q.samefile(q.absolute()) is True
151157
assert q.samefile(q.absolute().resolve()) is True
152158

@@ -161,7 +167,7 @@ def test_copy_copyfile(testfolder: MPath) -> None:
161167
q = p.copyfile("test2.touch")
162168
assert q.read_text() == msg
163169
q.unlink()
164-
d1 = testfolder / "dir1"
170+
d1 = MPath("dir1")
165171
d1.mkdir()
166172
q = p.copy(d1)
167173
assert str(q) == str(d1 / "test1.touch")

tests/unix_port.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ def pty_bridge_python(
122122
# Run micropython and forward data to/from `serial_port_name`.
123123
run_pty_bridge_process(command, bridge_pty)
124124
log.debug("Bridge process: Exiting.")
125-
sys.exit() # Exit the subprocesses.
125+
os._exit(0) # Exit the subprocesses.
126126

127127
# Parent process - the main process
128128
try:

uv.lock

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)