Skip to content

Commit df9e2bf

Browse files
upy-fs: On import throw error if there aren't file in the hex to import.
1 parent 391f425 commit df9e2bf

File tree

2 files changed

+30
-9
lines changed

2 files changed

+30
-9
lines changed

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

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,14 @@ describe('Test importing files from hex.', () => {
536536
});
537537
});
538538

539+
it('Throws and error if there are no files to import.', () => {
540+
const micropythonFs = new MicropythonFsHex(uPyHexFile);
541+
542+
const failCase = () => micropythonFs.importFilesFromIntelHex(uPyHexFile);
543+
544+
expect(failCase).toThrow('Hex does not have any files to import');
545+
});
546+
539547
it('Disabled overwrite flag throws an error when file exists.', () => {
540548
const micropythonFs = new MicropythonFsHex(uPyHexFile);
541549
const originalFileContent = 'Original file content.';
@@ -573,15 +581,6 @@ describe('Test importing files from hex.', () => {
573581
expect(failCase).toThrow(Error);
574582
});
575583

576-
it('When files are imported it still uses the constructor hex file.', () => {
577-
const micropythonFs = new MicropythonFsHex(uPyHexFile);
578-
579-
micropythonFs.importFilesFromIntelHex(hexStrWithFiles);
580-
const returnedIntelHex = micropythonFs.getIntelHex();
581-
582-
expect(generateHexWithFilesSpy.mock.calls.length).toEqual(1);
583-
});
584-
585584
it('Constructor hex file with files to import throws an error.', () => {
586585
const failCase = () => new MicropythonFsHex(hexStrWithFiles);
587586

@@ -606,6 +605,23 @@ describe('Test importing files from hex.', () => {
606605
expect(micropythonFs.ls()).not.toContain('old_file.py');
607606
});
608607

608+
it('Enabling formatFirst flag only formats if there are files to import.', () => {
609+
const micropythonFs = new MicropythonFsHex(uPyHexFile);
610+
micropythonFs.write('old_file.py', 'Some content.');
611+
612+
try {
613+
const fileList = micropythonFs.importFilesFromIntelHex(uPyHexFile, {
614+
overwrite: false,
615+
formatFirst: true,
616+
});
617+
} catch (e) {
618+
// Not having files to import should raise an error
619+
}
620+
621+
expect(micropythonFs.ls()).toContain('old_file.py');
622+
expect(micropythonFs.read('old_file.py')).toContain('Some content.');
623+
});
624+
609625
it('Disabling formatFirst flag, and by default, keeps old files.', () => {
610626
const micropythonFs = new MicropythonFsHex(uPyHexFile);
611627
micropythonFs.write('old_file.py', 'Some content.');

src/micropython-fs-hex.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,7 @@ export class MicropythonFsHex implements FsInterface {
235235
* Read the files included in a MicroPython hex string and add them to this
236236
* instance.
237237
*
238+
* @throws {Error} When there are no files to import in the hex.
238239
* @throws {Error} When there is a problem reading the files from the hex.
239240
* @throws {Error} When a filename already exists in this instance (all other
240241
* files are still imported).
@@ -253,6 +254,10 @@ export class MicropythonFsHex implements FsInterface {
253254
}: { overwrite?: boolean; formatFirst?: boolean } = {}
254255
): string[] {
255256
const files = getIntelHexFiles(intelHex);
257+
if (!Object.keys(files).length) {
258+
throw new Error('Hex does not have any files to import');
259+
}
260+
256261
if (formatFirst) {
257262
delete this._files;
258263
this._files = {};

0 commit comments

Comments
 (0)