|
| 1 | +import { |
| 2 | + assert, assertEquals, assertThrows, assertNotEquals |
| 3 | +} from 'https://deno.land/[email protected]/testing/asserts.ts' |
| 4 | +import { SEP } from "https://deno.land/[email protected]/path/separator.ts" |
| 5 | +import { |
| 6 | + existsDir, existsDirSync, existsFile, existsFileSync, |
| 7 | + ensureTextFile, lazyRemove |
| 8 | +} from './fs.ts' |
| 9 | + |
| 10 | + |
| 11 | +Deno.test(`fs existsDirSync`, () => { |
| 12 | + // true test cases |
| 13 | + const dir = Deno.makeTempDirSync() |
| 14 | + assert(existsDirSync(dir)) |
| 15 | + assert(existsDirSync(Deno.realPathSync(getStandardFolder()))) |
| 16 | + // false test cases |
| 17 | + assertEquals(existsDirSync(`${dir}${SEP}foobar`), false) |
| 18 | + const file = Deno.makeTempFileSync() |
| 19 | + assertEquals(existsDirSync(file), false) |
| 20 | + // error test cases |
| 21 | + assertThrows(() => existsDirSync({} as string), Error) |
| 22 | +}) |
| 23 | + |
| 24 | + |
| 25 | +Deno.test(`fs async existsDir`, async () => { |
| 26 | + // true test cases |
| 27 | + assert(await existsDir(await Deno.realPath(getStandardFolder()))) |
| 28 | + const dir = await Deno.makeTempDir() |
| 29 | + assertEquals(await existsDir(dir), true) |
| 30 | + // false test cases |
| 31 | + assertEquals(await existsDir(`${dir}${SEP}foobar`), false) |
| 32 | + const file = await Deno.makeTempFile() |
| 33 | + assertEquals(await existsDir(file), false) |
| 34 | + // error test cases |
| 35 | + existsDir({} as string).then(err => { |
| 36 | + assert(err instanceof Error) |
| 37 | + }).catch(e => console.error(e)) |
| 38 | +}) |
| 39 | + |
| 40 | +Deno.test(`fs existsFileSync`, () => { |
| 41 | + // true test cases |
| 42 | + const file = Deno.makeTempFileSync() |
| 43 | + assert(existsFileSync(file)) |
| 44 | + // false test cases |
| 45 | + const dir = Deno.makeTempDirSync() |
| 46 | + assert(!existsFileSync(`${dir}`)) |
| 47 | + assert(!existsFileSync(`${dir}${SEP}llksdafzxc.ts`)) |
| 48 | + // error test cases |
| 49 | + assertThrows(() => existsDirSync({} as string), Error) |
| 50 | +}) |
| 51 | + |
| 52 | +Deno.test(`fs async existsFile`, async () => { |
| 53 | + // true test cases |
| 54 | + const file = await Deno.makeTempFile() |
| 55 | + assert(await existsFile(file)) |
| 56 | + // false test cases |
| 57 | + const dir = Deno.makeTempDirSync() |
| 58 | + assertEquals(await existsFile(dir), false) |
| 59 | + assertEquals(await existsFileSync(`${dir}${SEP}llksdafzxc.ts`), false) |
| 60 | + // error test cases |
| 61 | + existsFile({} as string).then(err => { |
| 62 | + assert(err instanceof Error) |
| 63 | + }).catch(e => console.error(e)) |
| 64 | +}) |
| 65 | + |
| 66 | +Deno.test('ensureTextFile', async () => { |
| 67 | + // true test case |
| 68 | + const dirPath = await Deno.makeTempDir() |
| 69 | + const textFilePath = `${dirPath}${SEP}test.txt` |
| 70 | + const content = 'This is a test' |
| 71 | + await ensureTextFile(textFilePath, content) |
| 72 | + assert(await existsFile(textFilePath)) |
| 73 | + const testContent = await Deno.readTextFile(textFilePath) |
| 74 | + assertEquals(testContent, content) |
| 75 | + // false test case |
| 76 | + // illegal folder name |
| 77 | + const textFilePath2 = `${SEP}test2.txt` |
| 78 | + let testContent2 = '' |
| 79 | + try { |
| 80 | + await ensureTextFile(textFilePath2, content) |
| 81 | + testContent2 = await Deno.readTextFile(textFilePath2) |
| 82 | + } catch (error) { |
| 83 | + assertNotEquals(testContent2, content) |
| 84 | + } |
| 85 | +}) |
| 86 | + |
| 87 | +Deno.test('lazyRemove', async () => { |
| 88 | + // true test case |
| 89 | + const filePath = await Deno.makeTempFile() |
| 90 | + await lazyRemove(filePath) |
| 91 | + assertEquals(existsFileSync(filePath), false) |
| 92 | + // false test case |
| 93 | + const dirPath = await Deno.makeTempDir() |
| 94 | + await lazyRemove(`${dirPath}${SEP}asdfsdf.txt`) |
| 95 | + assert(await existsDir(dirPath)) |
| 96 | + // error test |
| 97 | + lazyRemove({} as string).then(err => { |
| 98 | + assert(err instanceof Error) |
| 99 | + }).catch(e => console.error(e)) |
| 100 | + |
| 101 | +}) |
| 102 | + |
| 103 | + |
| 104 | +/** |
| 105 | + * Returns an operating system-specific |
| 106 | + * example folder. |
| 107 | + * @returns 'C:\Windows' for Windows or |
| 108 | + * '/tmp' for unix-based operating systems |
| 109 | + */ |
| 110 | +const getStandardFolder = () => { |
| 111 | + return Deno.build.os === 'windows' ? "C:\\Windows" : '/tmp' |
| 112 | +} |
0 commit comments