|
| 1 | +import { FsFile } from '@chunkd/source-file'; |
| 2 | +import o from 'ospec'; |
| 3 | +import { FileSystemAbstraction } from '../fs.abstraction.js'; |
| 4 | + |
| 5 | +export class FakeSystem extends FsFile { |
| 6 | + constructor(protocol = 'fake') { |
| 7 | + super(); |
| 8 | + this.protocol = protocol; |
| 9 | + } |
| 10 | +} |
| 11 | + |
| 12 | +o.spec('FileSystemAbstraction', () => { |
| 13 | + o('should find file systems', () => { |
| 14 | + const fsa = new FileSystemAbstraction(); |
| 15 | + |
| 16 | + o(fsa.get('/foo').protocol).equals('file'); |
| 17 | + o(fsa.get('/').protocol).equals('file'); |
| 18 | + o(fsa.get('./').protocol).equals('file'); |
| 19 | + }); |
| 20 | + |
| 21 | + o('should register new file systems', () => { |
| 22 | + const fsa = new FileSystemAbstraction(); |
| 23 | + |
| 24 | + const fakeLocal = new FakeSystem('fake'); |
| 25 | + fsa.register('fake://', fakeLocal); |
| 26 | + |
| 27 | + o(fsa.get('/').protocol).equals('file'); |
| 28 | + o(fsa.get('//').protocol).equals('file'); |
| 29 | + |
| 30 | + o(fsa.get('fake://foo').protocol).equals('fake'); |
| 31 | + o(fsa.get('fake:/foo').protocol).equals('file'); |
| 32 | + o(fsa.get('fake//foo').protocol).equals('file'); |
| 33 | + o(fsa.get('fake').protocol).equals('file'); |
| 34 | + }); |
| 35 | + |
| 36 | + o('should find file systems in order they were registered', () => { |
| 37 | + const fakeA = new FakeSystem('fake'); |
| 38 | + const fakeB = new FakeSystem('fakeSpecific'); |
| 39 | + const fsa = new FileSystemAbstraction(); |
| 40 | + |
| 41 | + fsa.register('fake://', fakeA); |
| 42 | + fsa.register('fake://some-prefix-string/', fakeB); |
| 43 | + |
| 44 | + o(fsa.get('fake://foo').protocol).equals('fake'); |
| 45 | + o(fsa.get('fake://some-prefix-string/').protocol).equals('fakeSpecific'); |
| 46 | + o(fsa.get('fake://some-prefix-string/some-key').protocol).equals('fakeSpecific'); |
| 47 | + }); |
| 48 | + |
| 49 | + o('should order file systems by length', () => { |
| 50 | + const fakeA = new FakeSystem('fake'); |
| 51 | + const fakeB = new FakeSystem('fakeSpecific'); |
| 52 | + const fsa = new FileSystemAbstraction(); |
| 53 | + |
| 54 | + fsa.register('fake://some-prefix-string/', fakeB); |
| 55 | + fsa.register('fake://', fakeA); |
| 56 | + |
| 57 | + o(fsa.get('fake://foo').protocol).equals('fake'); |
| 58 | + o(fsa.get('fake://some-prefix-string/').protocol).equals('fakeSpecific'); |
| 59 | + o(fsa.get('fake://some-prefix-string/some-key').protocol).equals('fakeSpecific'); |
| 60 | + }); |
| 61 | +}); |
0 commit comments