Skip to content

Commit 9738a5b

Browse files
upy-fs-builder: Remove addIntelHexFile() and improve tests.
Having addIntelHexFiles() makes addIntelHexFile() unnecesary and now all the tests target the same entry point function. Tests now also check for specific error messages, instead of just any error.
1 parent f0502fe commit 9738a5b

File tree

2 files changed

+56
-81
lines changed

2 files changed

+56
-81
lines changed

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

Lines changed: 55 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,8 @@ import * as fs from 'fs';
1111

1212
import MemoryMap from 'nrf-intel-hex';
1313

14-
import { bytesToStr, strToBytes } from '../common';
14+
import { strToBytes } from '../common';
1515
import {
16-
addIntelHexFile,
1716
addIntelHexFiles,
1817
calculateFileSize,
1918
getIntelHexFiles,
@@ -286,12 +285,10 @@ describe('Writing files to the filesystem.', () => {
286285
},
287286
};
288287

289-
it('Can generate a full chunk that also uses the next one.', () => {
290-
const fwWithFsOther = addIntelHexFile(
291-
uPyHexFile,
292-
fullChunkPlus.fileName,
293-
strToBytes(fullChunkPlus.fileStr)
294-
);
288+
it('Can generate a full chunk that also consumes the next one.', () => {
289+
const fwWithFsOther = addIntelHexFiles(uPyHexFile, {
290+
[fullChunkPlus.fileName]: strToBytes(fullChunkPlus.fileStr),
291+
});
295292

296293
const opMap = MemoryMap.fromHex(fwWithFsOther);
297294
const readFileData = opMap
@@ -301,11 +298,9 @@ describe('Writing files to the filesystem.', () => {
301298
});
302299

303300
it('Correctly generate an almost full chunk (not using last byte).', () => {
304-
const fwWithFsOther = addIntelHexFile(
305-
uPyHexFile,
306-
fullChunkMinus.fileName,
307-
strToBytes(fullChunkMinus.fileStr)
308-
);
301+
const fwWithFsOther = addIntelHexFiles(uPyHexFile, {
302+
[fullChunkMinus.fileName]: strToBytes(fullChunkMinus.fileStr),
303+
});
309304

310305
const opMap = MemoryMap.fromHex(fwWithFsOther);
311306
const readFileData = opMap
@@ -314,12 +309,10 @@ describe('Writing files to the filesystem.', () => {
314309
expect(readFileData).toEqual(fullChunkMinus.bytes());
315310
});
316311

317-
it('Correctlty generate just over a full chunk.', () => {
318-
const fwWithFsOther = addIntelHexFile(
319-
uPyHexFile,
320-
twoChunks.fileName,
321-
strToBytes(twoChunks.fileStr)
322-
);
312+
it('Correctly generate just over a full chunk.', () => {
313+
const fwWithFsOther = addIntelHexFiles(uPyHexFile, {
314+
[twoChunks.fileName]: strToBytes(twoChunks.fileStr),
315+
});
323316

324317
const opMap = MemoryMap.fromHex(fwWithFsOther);
325318
const readFileData = opMap
@@ -329,31 +322,25 @@ describe('Writing files to the filesystem.', () => {
329322
});
330323

331324
it('Empty file name throws an error.', () => {
332-
const failCase = () => addIntelHexFile(uPyHexFile, '', randContent);
325+
const failCase = () => addIntelHexFiles(uPyHexFile, { '': randContent });
333326

334-
expect(failCase).toThrow(Error);
327+
expect(failCase).toThrow('File has to have a file name');
335328
});
336329

337330
it('Empty file data throw an error.', () => {
338-
const failCase = () => {
339-
const hexWithFs = addIntelHexFile(
340-
uPyHexFile,
341-
'my_file.txt',
342-
new Uint8Array(0)
343-
);
344-
};
345-
expect(failCase).toThrow(Error);
331+
const failCase = () =>
332+
addIntelHexFiles(uPyHexFile, { 'my_file.txt': new Uint8Array(0) });
333+
334+
expect(failCase).toThrow('has to contain data');
346335
});
347336

348337
it('Large file that does not fit throws error.', () => {
349338
const failCase = () => {
350-
const hexWithFs = addIntelHexFile(
351-
uPyHexFile,
352-
'my_file.txt',
353-
new Uint8Array(50 * 1024).fill(0x55)
354-
);
339+
addIntelHexFiles(uPyHexFile, {
340+
'my_file.txt': new Uint8Array(50 * 1024).fill(0x55),
341+
});
355342
};
356-
expect(failCase).toThrow(Error);
343+
expect(failCase).toThrow('Not enough space');
357344
});
358345

359346
it('Add files until no more fit.', () => {
@@ -362,36 +349,49 @@ describe('Writing files to the filesystem.', () => {
362349
// Use 4 KB blocks per file (each chunk is 128 B)
363350
const fakeBigFileData = new Uint8Array(4000).fill(0x55);
364351
const fakeSingleChunkData = new Uint8Array([0x55, 0x55]);
352+
365353
const addLargeFiles = () => {
366354
// At 4Kbs we only fit 7 files
367355
for (let i = 0; i < 15; i++) {
368-
hexWithFs = addIntelHexFile(
369-
hexWithFs,
370-
'file_' + i + '.txt',
371-
fakeBigFileData
372-
);
356+
hexWithFs = addIntelHexFiles(hexWithFs, {
357+
['file_' + i + '.txt']: fakeBigFileData,
358+
}) as string;
373359
}
374360
};
375361
const completeFsFilling = () => {
376362
// At a maximum of 4 Kbs left, it would fit 32 chunks max
377363
for (let i = 100; i < 132; i++) {
378-
hexWithFs = addIntelHexFile(
379-
hexWithFs,
380-
'file_' + i + '.txt',
381-
fakeSingleChunkData
382-
);
364+
hexWithFs = addIntelHexFiles(hexWithFs, {
365+
['file_' + i + '.txt']: fakeSingleChunkData,
366+
}) as string;
383367
}
384368
};
385-
expect(addLargeFiles).toThrow(Error);
386-
expect(completeFsFilling).toThrow(Error);
369+
370+
expect(addLargeFiles).toThrow('Not enough space');
371+
expect(completeFsFilling).toThrow('no storage space left');
372+
});
373+
374+
it('Add a group of files that do not fit.', () => {
375+
// The MicroPython hex has about 29 KBs
376+
const hexWithFs = uPyHexFile;
377+
// Use 4 KB blocks per file (each chunk is 128 B)
378+
const fakeBigFileData = new Uint8Array(4000).fill(0x55);
379+
const tooManyBigFiles: { [filename: string]: Uint8Array } = {};
380+
for (let i = 0; i < 8; i++) {
381+
tooManyBigFiles['file_' + i + '.txt'] = fakeBigFileData;
382+
}
383+
384+
const addingAllFiles = () => addIntelHexFiles(uPyHexFile, tooManyBigFiles);
385+
386+
expect(addingAllFiles).toThrow('Not enough space');
387387
});
388388

389389
it('Max filename length works.', () => {
390390
const maxLength = 120;
391391
const largeName = 'a'.repeat(maxLength);
392392

393393
const workingCase = () =>
394-
addIntelHexFile(uPyHexFile, largeName, randContent);
394+
addIntelHexFiles(uPyHexFile, { [largeName]: randContent });
395395

396396
expect(workingCase).not.toThrow(Error);
397397
});
@@ -400,20 +400,20 @@ describe('Writing files to the filesystem.', () => {
400400
const maxLength = 120;
401401
const largeName = 'a'.repeat(maxLength + 1);
402402

403-
const failCase = () => addIntelHexFile(uPyHexFile, largeName, randContent);
403+
const failCase = () =>
404+
addIntelHexFiles(uPyHexFile, { [largeName]: randContent });
404405

405-
expect(failCase).toThrow(Error);
406+
expect(failCase).toThrow('File name');
406407
});
407408

408409
it('Adding files to non-MicroPython hex fails.', () => {
409410
const failCase = () =>
410-
addIntelHexFile(makecodeHexFile, 'a.py', randContent);
411+
addIntelHexFiles(makecodeHexFile, { 'a.py': randContent });
411412

412-
expect(failCase).toThrow(Error);
413+
expect(failCase).toThrow('Could not find valid MicroPython UICR');
413414
});
414415

415416
// TODO: Hex file with persistent page marker doesn't get two markers
416-
// TODO: Hex file with injection string (:::...) still works
417417
});
418418

419419
describe('Reading files from the filesystem.', () => {
@@ -704,7 +704,7 @@ describe('Reading files from the filesystem.', () => {
704704

705705
const failCase = () => getIntelHexFiles(fullUpyFsMemMap.asHexString());
706706

707-
expect(failCase).toThrow(Error);
707+
expect(failCase).toThrow('Found multiple files named');
708708
});
709709

710710
it('Reading files from empty MicroPython hex returns empty list.', () => {
@@ -716,7 +716,7 @@ describe('Reading files from the filesystem.', () => {
716716
it('Reading files from non-MicroPython hex fails.', () => {
717717
const failCase = () => getIntelHexFiles(makecodeHexFile);
718718

719-
expect(failCase).toThrow(Error);
719+
expect(failCase).toThrow('Could not find valid MicroPython UICR');
720720
});
721721

722722
// TODO: Read tests with a file that has chunks in non-continuous order

src/micropython-fs-builder.ts

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -344,36 +344,12 @@ function addMemMapFile(
344344
return intelHexMap;
345345
}
346346

347-
/**
348-
* Adds a byte array as a file in the MicroPython filesystem.
349-
*
350-
* @throws {Error} When the invalid file name is given.
351-
* @throws {Error} When the the file doesn't have any data.
352-
* @throws {Error} When there are issues calculating the file system boundaries.
353-
* @throws {Error} When there is no space left for the file.
354-
*
355-
* @param intelHex - MicroPython Intel Hex string.
356-
* @param filename - Name for the file.
357-
* @param data - Byte array for the file data.
358-
* @returns MicroPython Intel Hex string with the file in the filesystem.
359-
*/
360-
function addIntelHexFile(
361-
intelHex: string,
362-
filename: string,
363-
data: Uint8Array
364-
): string {
365-
// filename and data checked in addMemMapFile
366-
let intelHexMap: MemoryMap = MemoryMap.fromHex(intelHex);
367-
intelHexMap = addMemMapFile(intelHexMap, filename, data);
368-
return intelHexMap.asHexString() + '\n';
369-
}
370-
371347
/**
372348
* Adds a hash table of filenames and byte arrays as files to the MicroPython
373349
* filesystem.
374350
*
375351
* @throws {Error} When the an invalid file name is given.
376-
* @throws {Error} When the a file doesn't have any data.
352+
* @throws {Error} When a file doesn't have any data.
377353
* @throws {Error} When there are issues calculating the file system boundaries.
378354
* @throws {Error} When there is no space left for a file.
379355
*
@@ -518,7 +494,6 @@ function getIntelHexFsSize(intelHex: string): number {
518494
}
519495

520496
export {
521-
addIntelHexFile,
522497
addIntelHexFiles,
523498
calculateFileSize,
524499
getIntelHexFiles,

0 commit comments

Comments
 (0)