Skip to content

Commit c4356ac

Browse files
committed
fixup! esp32: Add low level SD card block device NIFs
Signed-off-by: Davide Bettio <davide@uninstall.it>
1 parent 7028706 commit c4356ac

1 file changed

Lines changed: 17 additions & 4 deletions

File tree

src/platforms/esp32/test/main/test_erl_sources/test_sdcard.erl

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,18 +45,31 @@ test_read(SDCard) ->
4545
ok.
4646

4747
%% Read the last sector, overwrite it with a known pattern, read it back and
48-
%% finally restore the original contents so the card is left unchanged.
48+
%% restore the original contents, so that the card is left unchanged even when
49+
%% an assertion fails part way through.
4950
test_write_roundtrip(SDCard) ->
5051
{ok, #{sector_size := SectorSize, sector_count := SectorCount}} = esp:sdcard_info(SDCard),
5152
LastSector = SectorCount - 1,
5253
{ok, Original} = esp:sdcard_read(SDCard, LastSector),
5354
Pattern = make_pattern(SectorSize),
54-
ok = esp:sdcard_write(SDCard, LastSector, Pattern),
55-
{ok, Pattern} = esp:sdcard_read(SDCard, LastSector),
56-
ok = esp:sdcard_write(SDCard, LastSector, Original),
55+
try
56+
ok = esp:sdcard_write(SDCard, LastSector, Pattern),
57+
{ok, Pattern} = esp:sdcard_read(SDCard, LastSector)
58+
after
59+
restore_sector(SDCard, LastSector, Original)
60+
end,
5761
{ok, Original} = esp:sdcard_read(SDCard, LastSector),
5862
ok.
5963

64+
%% Best effort, so that a failing restore does not replace the original error.
65+
%% The caller asserts separately that the original contents came back.
66+
restore_sector(SDCard, Sector, Data) ->
67+
try
68+
esp:sdcard_write(SDCard, Sector, Data)
69+
catch
70+
_:_ -> ok
71+
end.
72+
6073
test_bad_args(SDCard) ->
6174
{ok, #{sector_size := SectorSize, sector_count := SectorCount}} = esp:sdcard_info(SDCard),
6275
%% Data that is not exactly one sector long is rejected.

0 commit comments

Comments
 (0)