@@ -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.
4950test_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+
6073test_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