|
| 1 | +import { resolve, relative } from 'path' |
| 2 | +import { strictEqual, stringifyEqual } from '@dr-js/core/module/common/verify' |
| 3 | +import { readFileAsync, writeFileAsync } from '@dr-js/core/module/node/file/function' |
| 4 | +import { PATH_TYPE, toPosixPath } from '@dr-js/core/module/node/file/Path' |
| 5 | +import { createDirectory, getDirectoryInfoTree } from '@dr-js/core/module/node/file/Directory' |
| 6 | +import { modifyDelete } from '@dr-js/core/module/node/file/Modify' |
| 7 | +import { resetDirectory } from '@dr-js/dev/module/node/file' |
| 8 | + |
| 9 | +const TEST_ROOT = resolve(__dirname, 'test-root-gitignore/') |
| 10 | +const fromRoot = (...args) => resolve(TEST_ROOT, ...args) |
| 11 | + |
| 12 | +const SOURCE_FILE = fromRoot('source-file') |
| 13 | +const SOURCE_DIRECTORY = fromRoot('source-directory/') |
| 14 | + |
| 15 | +const EXPECT_FILE_CONTENT = 'console.log([ { 1: 2 } ])\n'.repeat(64) |
| 16 | +const EXPECT_INFO_LIST = [ |
| 17 | + [ '', [ { 'name': '1', 'type': 'Directory', 'size': null }, { 'name': 'file-empty', 'type': 'File', 'size': 0 }, { 'name': 'file.js', 'type': 'File', 'size': Buffer.byteLength(EXPECT_FILE_CONTENT) } ] ], |
| 18 | + [ '1', [ { 'name': '2', 'type': 'Directory', 'size': null } ] ], |
| 19 | + [ '1/2', [ { 'name': '3', 'type': 'Directory', 'size': null } ] ], |
| 20 | + [ '1/2/3', [ { 'name': '4', 'type': 'Directory', 'size': null } ] ], |
| 21 | + [ '1/2/3/4', [ { 'name': '5', 'type': 'Directory', 'size': null } ] ], |
| 22 | + [ '1/2/3/4/5', [] ] |
| 23 | +] |
| 24 | + |
| 25 | +const setupRoot = async () => { |
| 26 | + await resetDirectory(SOURCE_DIRECTORY) |
| 27 | + |
| 28 | + await writeFileAsync(SOURCE_FILE, EXPECT_FILE_CONTENT) |
| 29 | + |
| 30 | + await createDirectory(fromRoot(SOURCE_DIRECTORY, '1/2/3/4/5')) |
| 31 | + await writeFileAsync(fromRoot(SOURCE_DIRECTORY, 'file-empty'), '') |
| 32 | + await writeFileAsync(fromRoot(SOURCE_DIRECTORY, 'file.js'), EXPECT_FILE_CONTENT) |
| 33 | +} |
| 34 | + |
| 35 | +const clearRoot = async () => { |
| 36 | + await modifyDelete(TEST_ROOT) |
| 37 | +} |
| 38 | + |
| 39 | +const verifyOutputFile = async (path) => { |
| 40 | + strictEqual(String(await readFileAsync(path)), EXPECT_FILE_CONTENT) |
| 41 | +} |
| 42 | + |
| 43 | +const verifyOutputDirectory = async (path) => { |
| 44 | + const infoTree = await getDirectoryInfoTree(path) |
| 45 | + const infoList = Object.entries(infoTree.subInfoListMap).map(([ key, subInfoList ]) => [ toPosixPath(relative(path, key)), subInfoList.map(({ name, type, stat }) => ({ name, type, size: type === PATH_TYPE.Directory ? null : stat.size })) ]) |
| 46 | + // console.log('verifyOutputDirectory', infoList.map((v) => JSON.stringify(v))) |
| 47 | + // console.log('EXPECT_INFO_LIST', EXPECT_INFO_LIST.map((v) => JSON.stringify(v))) |
| 48 | + stringifyEqual(infoList, EXPECT_INFO_LIST) |
| 49 | +} |
| 50 | + |
| 51 | +export { |
| 52 | + fromRoot, setupRoot, clearRoot, |
| 53 | + SOURCE_FILE, SOURCE_DIRECTORY, |
| 54 | + verifyOutputFile, verifyOutputDirectory |
| 55 | +} |
0 commit comments