|
| 1 | +import { DirInfo } from '@/Dir'; |
| 2 | +import { describe, expect, it } from 'vitest'; |
| 3 | +import { autoClearCacheDir, getTempCacheDirFn } from './helper'; |
| 4 | +import fs from 'fs-extra'; |
| 5 | +import { FileInfo } from '@/File'; |
| 6 | + |
| 7 | +const getTempCacheDir = getTempCacheDirFn('dir'); |
| 8 | + |
| 9 | +describe('Build DirInfo', () => { |
| 10 | + autoClearCacheDir('dir'); |
| 11 | + |
| 12 | + it('Should work correctly when using unexist paths', () => { |
| 13 | + const pathname = getTempCacheDir(); |
| 14 | + expect(() => DirInfo.build({ pathname })).not.toThrowError(); |
| 15 | + }); |
| 16 | + |
| 17 | + it('Should work correctly when using exist paths', () => { |
| 18 | + const pathname = getTempCacheDir(true); |
| 19 | + fs.ensureDirSync(path.join(pathname, '/subDir')); |
| 20 | + fs.ensureFileSync(path.join(pathname, '/subDir/content.txt')); |
| 21 | + expect(() => DirInfo.build({ pathname })).not.toThrowError(); |
| 22 | + }); |
| 23 | + |
| 24 | + it('Should work correctly with cache', () => { |
| 25 | + const pathname = getTempCacheDir(); |
| 26 | + const infoA = DirInfo.build({ pathname }); |
| 27 | + const infoB = DirInfo.build({ pathname }); |
| 28 | + expect(infoA === infoB).toBeTruthy(); |
| 29 | + }); |
| 30 | +}); |
| 31 | + |
| 32 | +describe('Get or check item in DirInfo', () => { |
| 33 | + autoClearCacheDir('dir'); |
| 34 | + |
| 35 | + it('Should has not un-ensure file or dir', () => { |
| 36 | + const pathname = getTempCacheDir(); |
| 37 | + const dir = DirInfo.build({ pathname }); |
| 38 | + |
| 39 | + expect(dir.exist).not.toBeTruthy(); |
| 40 | + expect(dir.has('/path/no/exist')).not.toBeTruthy(); |
| 41 | + expect(dir.get('/path/no/exist')).not.toBeTruthy(); |
| 42 | + }); |
| 43 | + |
| 44 | + it('Should has exist file or dir', () => { |
| 45 | + const pathname = getTempCacheDir(true); |
| 46 | + fs.ensureDirSync(path.join(pathname, '/path/exist')); |
| 47 | + const dir = DirInfo.build({ pathname }); |
| 48 | + |
| 49 | + expect(dir.exist).toBeTruthy(); |
| 50 | + expect(dir.has('/path/exist')).toBeTruthy(); |
| 51 | + expect(dir.get('/path/exist')).toBeTruthy(); |
| 52 | + }); |
| 53 | + |
| 54 | + it('Should get item with correct type', () => { |
| 55 | + const pathname = getTempCacheDir(); |
| 56 | + const dir = DirInfo.build({ pathname }); |
| 57 | + |
| 58 | + dir.ensure('/dir', { type: 'dir' }); |
| 59 | + expect(dir.get('/dir') instanceof DirInfo).toBeTruthy(); |
| 60 | + |
| 61 | + dir.ensure('/file', { type: 'file' }); |
| 62 | + expect(dir.get('/file') instanceof FileInfo).toBeTruthy(); |
| 63 | + }); |
| 64 | +}); |
| 65 | + |
| 66 | +describe('Ensure item in DirInfo', () => { |
| 67 | + autoClearCacheDir('dir'); |
| 68 | + |
| 69 | + it('Should ensure virtual file or dir', () => { |
| 70 | + const pathname = getTempCacheDir(); |
| 71 | + const dir = DirInfo.build({ pathname }); |
| 72 | + const subPathname = '/path/no/exist'; |
| 73 | + |
| 74 | + dir.ensure(subPathname, { type: 'dir' }); |
| 75 | + |
| 76 | + expect(dir.has(subPathname)).toBeTruthy(); |
| 77 | + expect(dir.get(subPathname)?.exist).not.toBeTruthy(); |
| 78 | + }); |
| 79 | + |
| 80 | + it('Should get error when ensure duplicate name at the end', () => { |
| 81 | + const pathname = getTempCacheDir(); |
| 82 | + const dir = DirInfo.build({ pathname }); |
| 83 | + |
| 84 | + dir.ensure('/dir', { type: 'dir' }); |
| 85 | + expect(() => dir.ensure('/dir', { type: 'file' })).toThrowError(); |
| 86 | + }); |
| 87 | + |
| 88 | + it('Should work correctly when ensure same item at the end', () => { |
| 89 | + const pathname = getTempCacheDir(); |
| 90 | + const dir = DirInfo.build({ pathname }); |
| 91 | + |
| 92 | + dir.ensure('/dir', { type: 'dir' }); |
| 93 | + expect(() => dir.ensure('/dir', { type: 'dir' })).not.toThrowError(); |
| 94 | + }); |
| 95 | + |
| 96 | + it('Should get error when there is an existing file before the end', () => { |
| 97 | + const pathname = getTempCacheDir(); |
| 98 | + const dir = DirInfo.build({ pathname }); |
| 99 | + |
| 100 | + dir.ensure('/file', { type: 'file' }); |
| 101 | + expect(() => dir.ensure('/file/file', { type: 'file' })).toThrowError(); |
| 102 | + }); |
| 103 | + |
| 104 | + it('Should work correctly when there is an existing folder before the end', () => { |
| 105 | + const pathname = getTempCacheDir(); |
| 106 | + const dir = DirInfo.build({ pathname }); |
| 107 | + |
| 108 | + dir.ensure('/dir', { type: 'dir' }); |
| 109 | + expect(() => dir.ensure('/dir/file', { type: 'file' })).not.toThrowError(); |
| 110 | + }); |
| 111 | +}); |
0 commit comments