Skip to content

Commit bd091f5

Browse files
upy-fs-hex: Add more tests for import files from Universal Hex.
1 parent 5cfb37e commit bd091f5

File tree

2 files changed

+67
-10
lines changed

2 files changed

+67
-10
lines changed

src/__tests__/micropython-fs-hex.spec.ts

Lines changed: 57 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1016,6 +1016,21 @@ describe('Test importing files from Intel Hex.', () => {
10161016
});
10171017
});
10181018

1019+
describe('Test importing files from Universal Hex.', () => {
1020+
// TODO: These tests
1021+
// it('Correctly read files from a Universal Hex.', () => {});
1022+
// it('Throws an error if it is not a Universal hex.', () => {});
1023+
// it('Throws an error if there are no files to import.', () => {});
1024+
// it('Throws an error if files in the individual hexes do not match.', () => {});
1025+
// it('Disabled overwrite flag throws an error when file exists.', () => {});
1026+
// it('Enabled overwrite flag replaces the file.', () => {});
1027+
// it('By default it throws an error if a filename already exists.', () => {});
1028+
// it('Constructor hex file with files to import throws an error.', () => {});
1029+
// it('Enabling formatFirst flag erases the previous files.', () => {});
1030+
// it('Enabling formatFirst flag only formats if there are files to import.', () => {});
1031+
// it('Disabling formatFirst flag, and by default, keeps old files.', () => {});
1032+
});
1033+
10191034
describe('Test MicroPython hex filesystem size.', () => {
10201035
it('Get how much available fs space there is in a MicroPython hex file.', () => {
10211036
const micropythonFs = new MicropythonFsHex(uPy1HexFile);
@@ -1047,16 +1062,13 @@ describe('Test MicroPython hex filesystem size.', () => {
10471062
});
10481063

10491064
describe('End-to-end loop around.', () => {
1050-
it('Create some files, export hex, and import in a new instance', () => {
1065+
it('Create some files, export Intel hex, and import in a new instance', () => {
10511066
const microbitFs = new MicropythonFsHex(uPy1HexFile);
10521067
microbitFs.write('a.txt', 'This is some content');
10531068
microbitFs.write('main.py', 'print("This is my code")');
1054-
10551069
const iHexWithFiles = microbitFs.getIntelHex();
1056-
const microbitFsImported = new MicropythonFsHex([
1057-
{ hex: uPy1HexFile, boardId: 0x9900 },
1058-
{ hex: uPy2HexFile, boardId: 0x9903 },
1059-
]);
1070+
1071+
const microbitFsImported = new MicropythonFsHex(uPy2HexFile);
10601072
const importedFiles = microbitFsImported.importFilesFromIntelHex(
10611073
iHexWithFiles
10621074
);
@@ -1077,8 +1089,46 @@ describe('End-to-end loop around.', () => {
10771089
expect(microbitFsImported.getStorageUsed()).toEqual(
10781090
microbitFs.getStorageUsed()
10791091
);
1092+
});
1093+
1094+
it('Create some files, export Universal hex, and import in a new instance', () => {
1095+
const microbitFs = new MicropythonFsHex([
1096+
{ hex: uPy1HexFile, boardId: microbitBoardId.V1 },
1097+
{ hex: uPy2HexFile, boardId: microbitBoardId.V2 },
1098+
]);
1099+
microbitFs.write('a.txt', 'This is some content');
1100+
microbitFs.write('main.py', 'print("This is my code")');
1101+
const uHexWithFiles = microbitFs.getUniversalHex();
1102+
1103+
const microbitFsImported = new MicropythonFsHex([
1104+
{ hex: uPy1HexFile, boardId: microbitBoardId.V1 },
1105+
{ hex: uPy2HexFile, boardId: microbitBoardId.V2 },
1106+
]);
1107+
const importedFiles = microbitFsImported.importFilesFromHex(uHexWithFiles);
1108+
1109+
expect(microbitFs.ls()).toEqual(importedFiles);
1110+
expect(microbitFs.ls()).toEqual(['a.txt', 'main.py']);
1111+
expect(microbitFsImported.ls()).toEqual(microbitFs.ls());
1112+
1113+
expect(microbitFs.read('a.txt')).toEqual('This is some content');
1114+
expect(microbitFsImported.read('a.txt')).toEqual(microbitFs.read('a.txt'));
1115+
1116+
expect(microbitFsImported.readBytes('a.txt')).toEqual(
1117+
microbitFs.readBytes('a.txt')
1118+
);
1119+
1120+
expect(microbitFsImported.size('a.txt')).toEqual(microbitFs.size('a.txt'));
1121+
1122+
expect(microbitFsImported.getStorageUsed()).toEqual(
1123+
microbitFs.getStorageUsed()
1124+
);
10801125

1081-
expect(microbitFsImported.getIntelHex(0x9900)).toEqual(iHexWithFiles);
1126+
expect(microbitFsImported.getIntelHex(microbitBoardId.V1)).toEqual(
1127+
microbitFs.getIntelHex(microbitBoardId.V1)
1128+
);
1129+
expect(microbitFsImported.getIntelHex(microbitBoardId.V2)).toEqual(
1130+
microbitFs.getIntelHex(microbitBoardId.V2)
1131+
);
10821132
});
10831133
});
10841134

src/hex-mem-info.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* .
2+
* Retrieves the device information stored inside a MicroPython hex file.
33
*
44
* (c) 2020 Micro:bit Educational Foundation and the microbit-fs contributors.
55
* SPDX-License-Identifier: MIT
@@ -10,6 +10,13 @@ import { DeviceMemInfo, DeviceVersion } from './device-mem-info';
1010
import * as flashRegions from './flash-regions';
1111
import * as uicr from './uicr';
1212

13+
/**
14+
* Attempts to retrieve the device memory data from an MicroPython Intel Hex
15+
* memory map.
16+
*
17+
* @param {MemoryMap} intelHexMap MicroPython Intel Hex memory map to scan.
18+
* @returns {DeviceMemInfo} Device data.
19+
*/
1320
function getHexMapDeviceMemInfo(intelHexMap: MemoryMap): DeviceMemInfo {
1421
let errorMsg = '';
1522
try {
@@ -25,10 +32,10 @@ function getHexMapDeviceMemInfo(intelHexMap: MemoryMap): DeviceMemInfo {
2532
}
2633

2734
/**
28-
* .
35+
* Attempts to retrieve the device memory data from an MicroPython Intel Hex.
2936
*
3037
* @param intelHex - MicroPython Intel Hex string.
31-
* @returns .
38+
* @returns {DeviceMemInfo} Device data.
3239
*/
3340
function getIntelHexDeviceMemInfo(intelHex: string): DeviceMemInfo {
3441
return getHexMapDeviceMemInfo(MemoryMap.fromHex(intelHex));

0 commit comments

Comments
 (0)