|
| 1 | +import * as fs from 'fs'; |
| 2 | + |
| 3 | +import { FileSystem } from '../file-manager'; |
| 4 | + |
| 5 | +describe('Test', () => { |
| 6 | + const uPyHexFile = fs.readFileSync('./src/__tests__/upy-v1.0.1.hex', 'utf8'); |
| 7 | + |
| 8 | + it('Write and Read files', () => { |
| 9 | + const content1 = "from microbit import display\r\ndisplay.show('x')"; |
| 10 | + const content2 = "from microbit import display\r\ndisplay.show('y')"; |
| 11 | + |
| 12 | + const microbitFs = new FileSystem(uPyHexFile); |
| 13 | + microbitFs.write('test.py', content1); |
| 14 | + microbitFs.write('test2.py', content2); |
| 15 | + const file1 = microbitFs.read('test.py'); |
| 16 | + const file2 = microbitFs.read('test2.py'); |
| 17 | + |
| 18 | + const files = microbitFs.ls(); |
| 19 | + expect(files).toContain('test.py'); |
| 20 | + expect(files).toContain('test2.py'); |
| 21 | + expect(file1).toEqual(content1); |
| 22 | + expect(file2).toEqual(content2); |
| 23 | + }); |
| 24 | + |
| 25 | + it('Write and Read files bytes', () => { |
| 26 | + const content1 = new Uint8Array([1, 2, 3]); |
| 27 | + const content2 = new Uint8Array([4, 5, 6]); |
| 28 | + |
| 29 | + const microbitFs = new FileSystem(uPyHexFile); |
| 30 | + microbitFs.write('test.py', content1); |
| 31 | + microbitFs.write('test2.py', content2); |
| 32 | + const file1 = microbitFs.readBytes('test.py'); |
| 33 | + const file2 = microbitFs.readBytes('test2.py'); |
| 34 | + |
| 35 | + const files = microbitFs.ls(); |
| 36 | + expect(files).toContain('test.py'); |
| 37 | + expect(files).toContain('test2.py'); |
| 38 | + expect(file1).toEqual(content1); |
| 39 | + expect(file2).toEqual(content2); |
| 40 | + }); |
| 41 | + |
| 42 | + it('Delete files', () => { |
| 43 | + const content = "from microbit import display\r\ndisplay.show('x')"; |
| 44 | + |
| 45 | + const microbitFs = new FileSystem(uPyHexFile); |
| 46 | + microbitFs.write('test.py', content); |
| 47 | + microbitFs.write('test2.py', content); |
| 48 | + const lsBefore = microbitFs.ls(); |
| 49 | + microbitFs.remove('test.py'); |
| 50 | + const lsAfter = microbitFs.ls(); |
| 51 | + |
| 52 | + expect(lsBefore).toContain('test.py'); |
| 53 | + expect(lsAfter).not.toContain('test.py'); |
| 54 | + }); |
| 55 | + |
| 56 | + it('Get Intel Hex', () => { |
| 57 | + // This test is a bit useless at the moment, need to replace as soon |
| 58 | + // as this method is properly implemented |
| 59 | + const microbitFs = new FileSystem(uPyHexFile); |
| 60 | + const returnedIntelHex = microbitFs.getIntelHex(); |
| 61 | + |
| 62 | + expect(returnedIntelHex).toEqual(uPyHexFile); |
| 63 | + }); |
| 64 | +}); |
0 commit comments