Skip to content

Commit f9f78b5

Browse files
upy-fs-hex: Add formatFirst flag to importFilesFromIntelHex.
It erases the filesystem before importing the new files.
1 parent fb8b051 commit f9f78b5

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,45 @@ describe('Test importing files from hex.', () => {
482482

483483
expect(failCase).toThrow(Error);
484484
});
485+
486+
it('Enabling formatFirst flag erases the previous files.', () => {
487+
const micropythonFs = new MicropythonFsHex(uPyHexFile);
488+
micropythonFs.write('old_file.py', 'Some content.');
489+
490+
const fileList = micropythonFs.importFilesFromIntelHex(
491+
hexStrWithFiles,
492+
false,
493+
true
494+
);
495+
496+
Object.keys(extraFiles).forEach((filename) => {
497+
expect(fileList).toContain(filename);
498+
expect(micropythonFs.read(filename)).toEqual(extraFiles[filename]);
499+
});
500+
expect(micropythonFs.ls()).not.toContain('old_file.py');
501+
});
502+
503+
it('Disabling formatFirst flag, and by default, keeps old files.', () => {
504+
const micropythonFs = new MicropythonFsHex(uPyHexFile);
505+
micropythonFs.write('old_file.py', 'Some content.');
506+
507+
const fileList1 = micropythonFs.importFilesFromIntelHex(
508+
hexStrWithFiles,
509+
false
510+
);
511+
const fileList2 = micropythonFs.importFilesFromIntelHex(
512+
hexStrWithFiles,
513+
true,
514+
false
515+
);
516+
517+
Object.keys(extraFiles).forEach((filename) => {
518+
expect(fileList1).toContain(filename);
519+
expect(fileList2).toContain(filename);
520+
expect(micropythonFs.read(filename)).toEqual(extraFiles[filename]);
521+
});
522+
expect(micropythonFs.ls()).toContain('old_file.py');
523+
});
485524
});
486525

487526
/*

src/micropython-fs-hex.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,10 +152,20 @@ export class MicropythonFsHex implements FsInterface {
152152
*
153153
* @param intelHex - MicroPython hex string with files.
154154
* @param overwrite - Flag to overwrite existing files in this instance.
155+
* @param formatFirst - Erase all the previous files before importing. It only
156+
* erases the files after there are no error during hex file parsing.
155157
* @returns A filename list of added files.
156158
*/
157-
importFilesFromIntelHex(intelHex: string, overwrite?: boolean): string[] {
159+
importFilesFromIntelHex(
160+
intelHex: string,
161+
overwrite?: boolean,
162+
formatFirst?: boolean
163+
): string[] {
158164
const files = getIntelHexFiles(intelHex);
165+
if (formatFirst) {
166+
delete this._files;
167+
this._files = {};
168+
}
159169
const existingFiles: string[] = [];
160170
Object.keys(files).forEach((filename) => {
161171
if (!overwrite && this.exists(filename)) {

0 commit comments

Comments
 (0)