Skip to content

Commit fb8b051

Browse files
upy-fs-hex: Detect and throw error if constructor hex has files.
1 parent 8d2a57f commit fb8b051

File tree

2 files changed

+22
-12
lines changed

2 files changed

+22
-12
lines changed

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

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -411,20 +411,20 @@ describe('Test importing files from hex.', () => {
411411
addIntelHexFileSpy.mockReset();
412412
});
413413

414-
const hexStrWithFiles = (): string => {
414+
const createHexStrWithFiles = (): string => {
415415
const fullUpyFsMemMap = MemoryMap.fromHex(uPyHexFile);
416416
const filesMap = MemoryMap.fromHex(extraFilesHex);
417417
filesMap.forEach((value: Uint8Array, index: number) => {
418418
fullUpyFsMemMap.set(index, value);
419419
});
420420
return fullUpyFsMemMap.asHexString();
421421
};
422+
const hexStrWithFiles: string = createHexStrWithFiles();
422423

423424
it('Correctly read files from a hex.', () => {
424-
const hexWithFiles = hexStrWithFiles();
425425
const micropythonFs = new MicropythonFsHex(uPyHexFile);
426426

427-
const fileList = micropythonFs.importFilesFromIntelHex(hexWithFiles);
427+
const fileList = micropythonFs.importFilesFromIntelHex(hexStrWithFiles);
428428

429429
Object.keys(extraFiles).forEach((filename) => {
430430
expect(fileList).toContain(filename);
@@ -433,53 +433,55 @@ describe('Test importing files from hex.', () => {
433433
});
434434

435435
it('Disabled overwrite flag throws an error when file exists.', () => {
436-
const hexWithFiles = hexStrWithFiles();
437436
const micropythonFs = new MicropythonFsHex(uPyHexFile);
438437
const originalFileContent = 'Original file content.';
439438
micropythonFs.write('a.py', originalFileContent);
440439

441440
const failCase = () => {
442-
micropythonFs.importFilesFromIntelHex(hexWithFiles, false);
441+
micropythonFs.importFilesFromIntelHex(hexStrWithFiles, false);
443442
};
444443

445444
expect(failCase).toThrow(Error);
446445
expect(micropythonFs.read('a.py')).toEqual(originalFileContent);
447446
});
448447

449448
it('Enabled overwrite flag replaces the file.', () => {
450-
const hexWithFiles = hexStrWithFiles();
451449
const micropythonFs = new MicropythonFsHex(uPyHexFile);
452450
const originalFileContent = 'Original file content.';
453451
micropythonFs.write('a.py', originalFileContent);
454452

455-
micropythonFs.importFilesFromIntelHex(hexWithFiles, true);
453+
micropythonFs.importFilesFromIntelHex(hexStrWithFiles, true);
456454

457455
expect(micropythonFs.read('a.py')).not.toEqual(originalFileContent);
458456
expect(micropythonFs.read('a.py')).toEqual(extraFiles['a.py']);
459457
});
460458

461459
it('By default it throws an error if a filename already exists.', () => {
462-
const hexWithFiles = hexStrWithFiles();
463460
const micropythonFs = new MicropythonFsHex(uPyHexFile);
464461
micropythonFs.write('a.py', 'some content');
465462

466463
const failCase = () => {
467-
micropythonFs.importFilesFromIntelHex(hexWithFiles);
464+
micropythonFs.importFilesFromIntelHex(hexStrWithFiles);
468465
};
469466

470467
expect(failCase).toThrow(Error);
471468
});
472469

473470
it('When files are imported it still uses the constructor hex file.', () => {
474-
const hexWithFiles = hexStrWithFiles();
475471
const micropythonFs = new MicropythonFsHex(uPyHexFile);
476472

477-
micropythonFs.importFilesFromIntelHex(hexWithFiles);
473+
micropythonFs.importFilesFromIntelHex(hexStrWithFiles);
478474
const returnedIntelHex = micropythonFs.getIntelHex();
479475

480476
expect(addIntelHexFileSpy.mock.calls.length).toEqual(3);
481477
expect(addIntelHexFileSpy.mock.calls[0][0]).toBe(uPyHexFile);
482478
});
479+
480+
it('Constructor hex file with files to import thorws an error.', () => {
481+
const failCase = () => new MicropythonFsHex(hexStrWithFiles);
482+
483+
expect(failCase).toThrow(Error);
484+
});
483485
});
484486

485487
/*

src/micropython-fs-hex.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,20 @@ export class MicropythonFsHex implements FsInterface {
99

1010
/**
1111
* File System manager constructor.
12+
* At the moment it needs a MicroPython hex string without a files included.
13+
*
14+
* TODO: If files are already in input hex file, deal with them somehow.
1215
*
1316
* @param intelHex - MicroPython Intel Hex string.
1417
*/
1518
constructor(intelHex: string) {
1619
this._intelHex = intelHex;
17-
// TODO: Read present file system in Intel Hex and populate files here
20+
this.importFilesFromIntelHex(this._intelHex);
21+
if (this.ls().length) {
22+
throw new Error(
23+
'There are files in the MicropythonFsHex constructor hex file input.'
24+
);
25+
}
1826
}
1927

2028
/**

0 commit comments

Comments
 (0)