Skip to content

Commit 312c417

Browse files
committed
tests: Fixes for Windows.
conftest.py: Use os.replace() instead of os.rename() to work on Windows. test_fs.py: Remove leading / on lfs filenames. Move mock data sets back into tests/data/ Change the layout of the mock-fs folder.
1 parent 6053e33 commit 312c417

File tree

9 files changed

+60
-49
lines changed

9 files changed

+60
-49
lines changed

tests/conftest.py

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,16 @@
1212
from pathlib import Path
1313
from typing import Any
1414

15-
import mp_image_tool_esp32
1615
import pytest
16+
import requests
1717
from _pytest.config import Config
18+
19+
import mp_image_tool_esp32
1820
from mp_image_tool_esp32 import data_table
1921

2022
rootdir: Path = Path(__file__).parent.parent # The root directory of the project
21-
datadir: Path = Path(__file__).parent / "data" # Location for firmware files
23+
testsdir: Path = Path(__file__).parent # Location for test files
24+
datadir: Path = testsdir / "data" # Location for firmware files
2225

2326
mpi_prog: Path = rootdir / "mp-image-tool-esp32" # Location of the tool to test
2427

@@ -45,9 +48,9 @@
4548
firmware_baseurl = "https://micropython.org/resources/firmware/"
4649
firmware_files = [
4750
f"ESP32_GENERIC-{firmware_version}.bin",
48-
f"ESP32_GENERIC_S2-{firmware_version}.bin",
49-
f"ESP32_GENERIC_S3-FLASH_4M-{firmware_version}.bin",
50-
f"ESP32_GENERIC_C3-{firmware_version}.bin",
51+
# f"ESP32_GENERIC_S2-{firmware_version}.bin",
52+
# f"ESP32_GENERIC_S3-FLASH_4M-{firmware_version}.bin",
53+
# f"ESP32_GENERIC_C3-{firmware_version}.bin",
5154
]
5255
firmware_file: Path = datadir / firmware_files[0]
5356
mpi_last_output: str = ""
@@ -110,10 +113,10 @@ def download_firmware_files():
110113
for f in firmware_files:
111114
if not (datadir / f).exists():
112115
print(f"Downloading firmware file: {f}")
113-
subprocess.run(
114-
["wget", f"{firmware_baseurl}{f}", "-O", f"{datadir / f}"],
115-
check=True,
116-
)
116+
r: requests.Response = requests.get(f"{firmware_baseurl}{f}")
117+
if r.status_code == 200:
118+
with open(datadir / f, "wb") as fw:
119+
fw.write(r.content)
117120

118121

119122
## pytest_configure is called after command line options have been parsed
@@ -158,7 +161,7 @@ def do_mpi_run(firmware: Path, *args: str, output: Path | None = None) -> None:
158161
# subprocess.run(cmd)
159162
if not output and outputfile.exists() and firmware.parent == Path("."):
160163
# Replace the firmware file with the output file
161-
os.rename(outputfile, firmware)
164+
os.replace(outputfile, firmware)
162165

163166

164167
def mpi_run(firmware: Path, *args: str, output: Path | None = None) -> str:
@@ -304,7 +307,7 @@ def firmware(device: Path | None, firmwarefile: Path) -> Path:
304307
def bootloader(firmwarefile: Path) -> bytes:
305308
"""A fixture to extract the bootloader from a firmware file.
306309
The bootloader image is returned as a bytes object."""
307-
return firmwarefile.read_bytes()[:BOOTLOADER_SIZE].rstrip(b"\xFF")
310+
return firmwarefile.read_bytes()[:BOOTLOADER_SIZE].rstrip(b"\xff")
308311

309312

310313
@pytest.fixture()
@@ -319,4 +322,4 @@ def app_image(firmwarefile: Path) -> bytes:
319322
offset = FACTORY_OFFSET # Start of app image within firmware file
320323
if chip_name in ("esp32", "esp32s2"):
321324
offset -= 0x1000 # ESP32 and ESP32S2 firmwares start at offset 0x1000
322-
return firmwarefile.read_bytes()[offset:].rstrip(b"\xFF")
325+
return firmwarefile.read_bytes()[offset:].rstrip(b"\xff")
1.65 MB
Binary file not shown.

tests/data/mock-fs/boot.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# This file is executed on every boot (including wake-boot from deepsleep)
2+
#import esp
3+
#esp.osdebug(None)
4+
#import webrepl
5+
#webrepl.start()
1.98 KB
Binary file not shown.
375 Bytes
Binary file not shown.

tests/data/mock-fs/ota/status.mpy

2.53 KB
Binary file not shown.

tests/data/mock-fs/ota/update.mpy

2 KB
Binary file not shown.

tests/test_fs.py

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,11 @@ def test_mkdir(mock_device: Path) -> None:
4646

4747

4848
def test_rm_file(mock_device: Path) -> None:
49-
mpi_check_ls(mock_device, "-q --fs rm lib/ota/blockdev_writer.mpy")
49+
mpi_check_ls(mock_device, "-q --fs rm ota/blockdev_writer.mpy")
5050

5151

5252
def test_rm_directory(mock_device: Path) -> None:
53-
mpi_check_ls(mock_device, "-q --fs rm /lib")
53+
mpi_check_ls(mock_device, "-q --fs rm ota")
5454

5555

5656
def test_mkfs(mock_device: Path) -> None:
@@ -70,7 +70,7 @@ def test_grow(mock_device: Path) -> None:
7070

7171
def test_get(mock_device: Path) -> None:
7272
"""Download a file and compare to the original."""
73-
mpi_run(mock_device, "-q --fs get /boot.py")
73+
mpi_run(mock_device, "-q --fs get boot.py")
7474
assert Path("boot.py").exists()
7575
assert Path("boot.py").read_text() == (mockfs_dir / "boot.py").read_text()
7676

@@ -82,7 +82,7 @@ def test_put(mock_device: Path) -> None:
8282
input.write_bytes(data)
8383
assert input.stat().st_size == 256 * 16
8484
mpi_run(mock_device, f"-q --fs put {input}")
85-
mpi_run(mock_device, f"-q --fs get /{input} {output}")
85+
mpi_run(mock_device, f"-q --fs get {input} {output}")
8686
assert output.exists()
8787
assert output.read_bytes() == input.read_bytes()
8888

@@ -92,10 +92,16 @@ def test_get_directory(mock_device: Path) -> None:
9292
original."""
9393
rootfs = Path("rootfs")
9494
assert not rootfs.exists()
95-
mpi_run(mock_device, "-q --fs get / rootfs")
95+
try:
96+
mpi_run(mock_device, "-q --fs get . rootfs")
97+
except Exception as e:
98+
for f in rootfs.rglob("*"):
99+
print(f, f.is_file(), f.stat())
100+
raise e
96101
assert rootfs.is_dir()
97102
new_files = list(rootfs.rglob("*"))
98103
orig_files = list(mockfs_dir.rglob("*"))
104+
assert len(orig_files) > 3 # We expect 6 files in the mockfs
99105
assert len(new_files) == len(orig_files)
100106
for new, orig in zip(new_files, orig_files):
101107
assert new.relative_to(rootfs) == orig.relative_to(mockfs_dir)
@@ -111,10 +117,11 @@ def test_put_directory(mock_device: Path) -> None:
111117
"""Upload the entire filesystem to a directory and compare to the
112118
original."""
113119
rootfs = Path("rootfs")
114-
assert not rootfs.exists()
115-
mpi_run(mock_device, "-q --fs mkdir /rootfs")
116-
mpi_run(mock_device, f"-q --fs put {mockfs_dir} /rootfs")
117-
mpi_run(mock_device, f"-q --fs get /rootfs {rootfs}")
120+
if rootfs.exists():
121+
shutil.rmtree(rootfs)
122+
mpi_run(mock_device, "-q --fs mkdir rootfs")
123+
mpi_run(mock_device, f"-q --fs put '{mockfs_dir.as_posix()}' rootfs")
124+
mpi_run(mock_device, f"-q --fs get rootfs {rootfs}")
118125
assert rootfs.is_dir()
119126
new_dir = rootfs / mockfs_dir.name
120127
new_files = list(new_dir.rglob("*"))

tests/test_outputs.yaml

Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,11 @@
8181

8282
-q --fs ls: |
8383
boot.py
84-
lib/
85-
lib/ota/
86-
lib/ota/blockdev_writer.mpy
87-
lib/ota/rollback.mpy
88-
lib/ota/status.mpy
89-
lib/ota/update.mpy
84+
ota/
85+
ota/blockdev_writer.mpy
86+
ota/rollback.mpy
87+
ota/status.mpy
88+
ota/update.mpy
9089
9190
-q --fs cat boot.py: |
9291
# This file is executed on every boot (including wake-boot from deepsleep)
@@ -97,41 +96,38 @@
9796
9897
-q --fs rename boot.py boot.bak: |
9998
boot.bak
100-
lib/
101-
lib/ota/
102-
lib/ota/blockdev_writer.mpy
103-
lib/ota/rollback.mpy
104-
lib/ota/status.mpy
105-
lib/ota/update.mpy
99+
ota/
100+
ota/blockdev_writer.mpy
101+
ota/rollback.mpy
102+
ota/status.mpy
103+
ota/update.mpy
106104
107105
-q --fs mkdir data: |
108106
boot.py
109107
data/
110-
lib/
111-
lib/ota/
112-
lib/ota/blockdev_writer.mpy
113-
lib/ota/rollback.mpy
114-
lib/ota/status.mpy
115-
lib/ota/update.mpy
116-
117-
-q --fs rm lib/ota/blockdev_writer.mpy: |
108+
ota/
109+
ota/blockdev_writer.mpy
110+
ota/rollback.mpy
111+
ota/status.mpy
112+
ota/update.mpy
113+
114+
-q --fs rm ota/blockdev_writer.mpy: |
118115
boot.py
119-
lib/
120-
lib/ota/
121-
lib/ota/rollback.mpy
122-
lib/ota/status.mpy
123-
lib/ota/update.mpy
116+
ota/
117+
ota/rollback.mpy
118+
ota/status.mpy
119+
ota/update.mpy
124120
125-
-q --fs rm /lib: |
121+
-q --fs rm ota: |
126122
boot.py
127123
128124
-q --fs mkfs vfs: |
129125
boot.py
130126
131127
-q --fs df vfs: |
132128
# Partition Total kB Used kB Free kB Used%
133-
vfs 2048 36 2012 2%
129+
vfs 2048 28 2020 1%
134130
135131
-q -f 6M --resize vfs=0 --fs grow --fs df: |
136132
# Partition Total kB Used kB Free kB Used%
137-
vfs 4096 36 4060 1%
133+
vfs 4096 28 4068 1%

0 commit comments

Comments
 (0)