@@ -22,6 +22,7 @@ import {
22
22
} from '../micropython-fs-builder' ;
23
23
24
24
const uPyHexFile = fs . readFileSync ( './src/__tests__/upy-v1.0.1.hex' , 'utf8' ) ;
25
+ const uPyHexMap = MemoryMap . fromHex ( uPyHexFile ) ;
25
26
const makecodeHexFile = fs . readFileSync ( './src/__tests__/makecode.hex' , 'utf8' ) ;
26
27
const randContent = strToBytes ( 'Some random content.' ) ;
27
28
@@ -209,6 +210,21 @@ describe('Writing files to the filesystem.', () => {
209
210
expect ( file1data ) . toEqual ( files [ 1 ] . bytes ( ) ) ;
210
211
} ) ;
211
212
213
+ it ( 'Input to addIntelHexFiles() as a Intel Hex string and Memory Map generate compatible data' , ( ) => {
214
+ const fsFiles = {
215
+ [ files [ 0 ] . fileName ] : strToBytes ( files [ 0 ] . fileStr ) ,
216
+ [ files [ 1 ] . fileName ] : strToBytes ( files [ 1 ] . fileStr ) ,
217
+ } ;
218
+ for ( let i = 0 ; i < 32 ; i ++ ) {
219
+ fsFiles [ 'file_' + i + '.txt' ] = strToBytes ( "Content doesn't matter " + i ) ;
220
+ }
221
+
222
+ const hexFromIntelHex = addIntelHexFiles ( uPyHexFile , fsFiles ) as string ;
223
+ const hexFromMemoryMap = addIntelHexFiles ( uPyHexMap , fsFiles ) as string ;
224
+
225
+ expect ( hexFromIntelHex ) . toEqual ( hexFromMemoryMap ) ;
226
+ } ) ;
227
+
212
228
it ( 'Both addIntelHexFiles() and generateHexWithFiles() generate compatible data' , ( ) => {
213
229
const fsFiles = {
214
230
[ files [ 0 ] . fileName ] : strToBytes ( files [ 0 ] . fileStr ) ,
@@ -315,7 +331,7 @@ describe('Writing files to the filesystem.', () => {
315
331
} ;
316
332
317
333
it ( 'Can generate a full chunk that also consumes the next one.' , ( ) => {
318
- const fwWithFsOther = addIntelHexFiles ( uPyHexFile , {
334
+ const fwWithFsOther = addIntelHexFiles ( uPyHexMap , {
319
335
[ fullChunkPlus . fileName ] : strToBytes ( fullChunkPlus . fileStr ) ,
320
336
} ) ;
321
337
@@ -327,7 +343,7 @@ describe('Writing files to the filesystem.', () => {
327
343
} ) ;
328
344
329
345
it ( 'Correctly generate an almost full chunk (not using last byte).' , ( ) => {
330
- const fwWithFsOther = addIntelHexFiles ( uPyHexFile , {
346
+ const fwWithFsOther = addIntelHexFiles ( uPyHexMap , {
331
347
[ fullChunkMinus . fileName ] : strToBytes ( fullChunkMinus . fileStr ) ,
332
348
} ) ;
333
349
@@ -339,7 +355,7 @@ describe('Writing files to the filesystem.', () => {
339
355
} ) ;
340
356
341
357
it ( 'Correctly generate just over a full chunk.' , ( ) => {
342
- const fwWithFsOther = addIntelHexFiles ( uPyHexFile , {
358
+ const fwWithFsOther = addIntelHexFiles ( uPyHexMap , {
343
359
[ twoChunks . fileName ] : strToBytes ( twoChunks . fileStr ) ,
344
360
} ) ;
345
361
@@ -351,21 +367,21 @@ describe('Writing files to the filesystem.', () => {
351
367
} ) ;
352
368
353
369
it ( 'Empty file name throws an error.' , ( ) => {
354
- const failCase = ( ) => addIntelHexFiles ( uPyHexFile , { '' : randContent } ) ;
370
+ const failCase = ( ) => addIntelHexFiles ( uPyHexMap , { '' : randContent } ) ;
355
371
356
372
expect ( failCase ) . toThrow ( 'File has to have a file name' ) ;
357
373
} ) ;
358
374
359
375
it ( 'Empty file data throw an error.' , ( ) => {
360
376
const failCase = ( ) =>
361
- addIntelHexFiles ( uPyHexFile , { 'my_file.txt' : new Uint8Array ( 0 ) } ) ;
377
+ addIntelHexFiles ( uPyHexMap , { 'my_file.txt' : new Uint8Array ( 0 ) } ) ;
362
378
363
379
expect ( failCase ) . toThrow ( 'has to contain data' ) ;
364
380
} ) ;
365
381
366
382
it ( 'Large file that does not fit throws error.' , ( ) => {
367
383
const failCase = ( ) => {
368
- addIntelHexFiles ( uPyHexFile , {
384
+ addIntelHexFiles ( uPyHexMap , {
369
385
'my_file.txt' : new Uint8Array ( 50 * 1024 ) . fill ( 0x55 ) ,
370
386
} ) ;
371
387
} ;
@@ -402,15 +418,14 @@ describe('Writing files to the filesystem.', () => {
402
418
403
419
it ( 'Add a group of files that do not fit.' , ( ) => {
404
420
// The MicroPython hex has about 29 KBs
405
- const hexWithFs = uPyHexFile ;
406
421
// Use 4 KB blocks per file (each chunk is 128 B)
407
422
const fakeBigFileData = new Uint8Array ( 4000 ) . fill ( 0x55 ) ;
408
423
const tooManyBigFiles : { [ filename : string ] : Uint8Array } = { } ;
409
424
for ( let i = 0 ; i < 8 ; i ++ ) {
410
425
tooManyBigFiles [ 'file_' + i + '.txt' ] = fakeBigFileData ;
411
426
}
412
427
413
- const addingAllFiles = ( ) => addIntelHexFiles ( uPyHexFile , tooManyBigFiles ) ;
428
+ const addingAllFiles = ( ) => addIntelHexFiles ( uPyHexMap , tooManyBigFiles ) ;
414
429
415
430
expect ( addingAllFiles ) . toThrow ( 'Not enough space' ) ;
416
431
} ) ;
@@ -420,7 +435,7 @@ describe('Writing files to the filesystem.', () => {
420
435
const largeName = 'a' . repeat ( maxLength ) ;
421
436
422
437
const workingCase = ( ) =>
423
- addIntelHexFiles ( uPyHexFile , { [ largeName ] : randContent } ) ;
438
+ addIntelHexFiles ( uPyHexMap , { [ largeName ] : randContent } ) ;
424
439
425
440
expect ( workingCase ) . not . toThrow ( Error ) ;
426
441
} ) ;
@@ -430,7 +445,7 @@ describe('Writing files to the filesystem.', () => {
430
445
const largeName = 'a' . repeat ( maxLength + 1 ) ;
431
446
432
447
const failCase = ( ) =>
433
- addIntelHexFiles ( uPyHexFile , { [ largeName ] : randContent } ) ;
448
+ addIntelHexFiles ( uPyHexMap , { [ largeName ] : randContent } ) ;
434
449
435
450
expect ( failCase ) . toThrow ( 'File name' ) ;
436
451
} ) ;
@@ -618,16 +633,18 @@ describe('Reading files from the filesystem.', () => {
618
633
hexMap . set ( index , value ) ;
619
634
} ) ;
620
635
} ;
621
- const fullUpyFsMemMap = MemoryMap . fromHex ( uPyHexFile ) ;
636
+ const fullUpyFsMemMap = uPyHexMap . clone ( ) ;
622
637
addHexToMap ( fullUpyFsMemMap , afirstHex ) ;
623
638
addHexToMap ( fullUpyFsMemMap , alastHex ) ;
624
639
addHexToMap ( fullUpyFsMemMap , mainHex ) ;
625
640
626
- const foundFiles = getIntelHexFiles ( fullUpyFsMemMap . asHexString ( ) ) ;
641
+ const foundFilesFromHex = getIntelHexFiles ( fullUpyFsMemMap . asHexString ( ) ) ;
642
+ const foundFilesFromMap = getIntelHexFiles ( fullUpyFsMemMap ) ;
627
643
628
- expect ( foundFiles ) . toHaveProperty ( [ afirstFilename ] , afirstContent ) ;
629
- expect ( foundFiles ) . toHaveProperty ( [ alastFilename ] , alastContent ) ;
630
- expect ( foundFiles ) . toHaveProperty ( [ mainFilename ] , mainContent ) ;
644
+ expect ( foundFilesFromHex ) . toEqual ( foundFilesFromMap ) ;
645
+ expect ( foundFilesFromHex ) . toHaveProperty ( [ afirstFilename ] , afirstContent ) ;
646
+ expect ( foundFilesFromHex ) . toHaveProperty ( [ alastFilename ] , alastContent ) ;
647
+ expect ( foundFilesFromHex ) . toHaveProperty ( [ mainFilename ] , mainContent ) ;
631
648
} ) ;
632
649
633
650
// When MicroPython saves a file that takes full chunk it still utilises
@@ -656,15 +673,17 @@ describe('Reading files from the filesystem.', () => {
656
673
// In the one_chunk_plus.py example the data inside the file would take
657
674
// exactly 128 Bytes, or one chunk. However, MicroPython also "takes" or
658
675
// "links" the next chunk and doesn't put any data into it.
659
- const fullUpyFsMemMap = MemoryMap . fromHex ( uPyHexFile ) ;
676
+ const fullUpyFsMemMap = uPyHexMap . clone ( ) ;
660
677
const oneChunkPlusMemMap = MemoryMap . fromHex ( oneChunkPlusHex ) ;
661
678
oneChunkPlusMemMap . forEach ( ( value : Uint8Array , index : number ) => {
662
679
fullUpyFsMemMap . set ( index , value ) ;
663
680
} ) ;
664
681
665
- const foundFiles = getIntelHexFiles ( fullUpyFsMemMap . asHexString ( ) ) ;
682
+ const foundFilesFromHex = getIntelHexFiles ( fullUpyFsMemMap . asHexString ( ) ) ;
683
+ const foundFilesFromMap = getIntelHexFiles ( fullUpyFsMemMap ) ;
666
684
667
- expect ( foundFiles ) . toHaveProperty (
685
+ expect ( foundFilesFromHex ) . toEqual ( foundFilesFromMap ) ;
686
+ expect ( foundFilesFromHex ) . toHaveProperty (
668
687
[ oneChunkPlusFilename ] ,
669
688
strToBytes ( oneChunkPlusContent )
670
689
) ;
@@ -692,15 +711,17 @@ describe('Reading files from the filesystem.', () => {
692
711
it ( 'Can read a file that occupies almost a full chunk.' , ( ) => {
693
712
// In contrast to the one_chunk_plus.py example, this one fills the chunk
694
713
// minus 1 Byte (The second 0xFF at the end is the chunk tail).
695
- const fullUpyFsMemMap = MemoryMap . fromHex ( uPyHexFile ) ;
714
+ const fullUpyFsMemMap = uPyHexMap . clone ( ) ;
696
715
const oneChunkMinusMemMap = MemoryMap . fromHex ( oneChunkMinusHex ) ;
697
716
oneChunkMinusMemMap . forEach ( ( value : Uint8Array , index : number ) => {
698
717
fullUpyFsMemMap . set ( index , value ) ;
699
718
} ) ;
700
719
701
- const foundFiles = getIntelHexFiles ( fullUpyFsMemMap . asHexString ( ) ) ;
720
+ const foundFilesFromHex = getIntelHexFiles ( fullUpyFsMemMap . asHexString ( ) ) ;
721
+ const foundFilesFromMap = getIntelHexFiles ( fullUpyFsMemMap ) ;
702
722
703
- expect ( foundFiles ) . toHaveProperty (
723
+ expect ( foundFilesFromHex ) . toEqual ( foundFilesFromMap ) ;
724
+ expect ( foundFilesFromHex ) . toHaveProperty (
704
725
[ oneChunkMinusFilename ] ,
705
726
strToBytes ( oneChunkMinusContent )
706
727
) ;
@@ -722,7 +743,7 @@ describe('Reading files from the filesystem.', () => {
722
743
it ( 'Duplicate file names throws an error.' , ( ) => {
723
744
// In contrast to the one_chunk_plus.py example, this one fills the chunk
724
745
// minus 1 Byte (The second 0xFF at the end is the chunk tail).
725
- const fullUpyFsMemMap = MemoryMap . fromHex ( uPyHexFile ) ;
746
+ const fullUpyFsMemMap = uPyHexMap . clone ( ) ;
726
747
const oneFileCopyMemMap = MemoryMap . fromHex ( oneFileCopyHex ) ;
727
748
oneFileCopyMemMap . forEach ( ( value : Uint8Array , index : number ) => {
728
749
fullUpyFsMemMap . set ( index , value ) ;
@@ -738,7 +759,7 @@ describe('Reading files from the filesystem.', () => {
738
759
} ) ;
739
760
740
761
it ( 'Reading files from empty MicroPython hex returns empty list.' , ( ) => {
741
- const foundFiles = getIntelHexFiles ( uPyHexFile ) ;
762
+ const foundFiles = getIntelHexFiles ( uPyHexMap ) ;
742
763
743
764
expect ( foundFiles ) . toEqual ( { } ) ;
744
765
} ) ;
@@ -757,7 +778,7 @@ describe('Reading files from the filesystem.', () => {
757
778
758
779
describe ( 'Calculate sizes.' , ( ) => {
759
780
it ( 'Get how much available fs space there is in a MicroPython hex file.' , ( ) => {
760
- const totalSize = getMemMapFsSize ( MemoryMap . fromHex ( uPyHexFile ) ) ;
781
+ const totalSize = getMemMapFsSize ( uPyHexMap ) ;
761
782
762
783
// Calculated by hand from the uPyHexFile v1.0.1 release.
763
784
expect ( totalSize ) . toEqual ( 27 * 1024 ) ;
0 commit comments