2023-07-20 22:00:11 +02:00
|
|
|
import test from 'node:test'
|
|
|
|
import assert from 'node:assert/strict'
|
2021-07-03 18:40:17 +02:00
|
|
|
import fs from 'node:fs'
|
|
|
|
|
2021-06-09 20:31:45 +02:00
|
|
|
import fsMock from 'mock-fs'
|
|
|
|
|
2022-02-19 18:30:29 +01:00
|
|
|
import { copyDirectory } from '../copyDirectory.js'
|
2021-06-09 20:31:45 +02:00
|
|
|
|
2023-07-20 22:00:11 +02:00
|
|
|
await test('utils/copyDirectory', async (t) => {
|
2022-04-23 18:41:14 +02:00
|
|
|
t.afterEach(() => {
|
2021-06-09 20:31:45 +02:00
|
|
|
fsMock.restore()
|
|
|
|
})
|
|
|
|
|
2023-07-20 22:00:11 +02:00
|
|
|
await t.test('copy the files', async () => {
|
2021-06-09 20:31:45 +02:00
|
|
|
fsMock({
|
|
|
|
'/source': {
|
|
|
|
'default.png': '',
|
2022-04-23 18:41:14 +02:00
|
|
|
'index.ts': ''
|
2021-06-09 20:31:45 +02:00
|
|
|
},
|
|
|
|
'/destination': {}
|
2022-04-23 18:41:14 +02:00
|
|
|
})
|
2021-06-09 20:31:45 +02:00
|
|
|
|
|
|
|
let destinationDirectoryContent = await fs.promises.readdir('/destination')
|
|
|
|
let sourceDirectoryContent = await fs.promises.readdir('/source')
|
2023-07-20 22:00:11 +02:00
|
|
|
assert.strictEqual(destinationDirectoryContent.length, 0)
|
|
|
|
assert.strictEqual(sourceDirectoryContent.length, 2)
|
2021-06-09 20:31:45 +02:00
|
|
|
|
|
|
|
await copyDirectory('/source', '/destination')
|
|
|
|
destinationDirectoryContent = await fs.promises.readdir('/destination')
|
|
|
|
sourceDirectoryContent = await fs.promises.readdir('/source')
|
2023-07-20 22:00:11 +02:00
|
|
|
assert.strictEqual(destinationDirectoryContent.length, 2)
|
|
|
|
assert.strictEqual(sourceDirectoryContent.length, 2)
|
|
|
|
assert.deepStrictEqual(destinationDirectoryContent, [
|
|
|
|
'default.png',
|
|
|
|
'index.ts'
|
|
|
|
])
|
|
|
|
assert.deepStrictEqual(sourceDirectoryContent, ['default.png', 'index.ts'])
|
2021-06-09 20:31:45 +02:00
|
|
|
})
|
2021-08-27 13:03:41 +02:00
|
|
|
|
2023-07-20 22:00:11 +02:00
|
|
|
await t.test('copy the files and folders recursively', async () => {
|
2021-08-27 13:03:41 +02:00
|
|
|
fsMock({
|
|
|
|
'/source': {
|
|
|
|
'random-folder': {
|
|
|
|
'default.png': '',
|
|
|
|
'second-random-folder': {
|
|
|
|
'mycode.ts': ''
|
|
|
|
}
|
|
|
|
},
|
2022-04-23 18:41:14 +02:00
|
|
|
'index.ts': ''
|
2021-08-27 13:03:41 +02:00
|
|
|
},
|
|
|
|
'/destination': {}
|
2022-04-23 18:41:14 +02:00
|
|
|
})
|
2021-08-27 13:03:41 +02:00
|
|
|
|
|
|
|
let destinationDirectoryContent = await fs.promises.readdir('/destination')
|
|
|
|
let sourceDirectoryContent = await fs.promises.readdir('/source')
|
|
|
|
let randomFolderContent = await fs.promises.readdir('/source/random-folder')
|
|
|
|
let secondRandomFolderContent = await fs.promises.readdir(
|
|
|
|
'/source/random-folder/second-random-folder'
|
|
|
|
)
|
2023-07-20 22:00:11 +02:00
|
|
|
assert.strictEqual(randomFolderContent.length, 2)
|
|
|
|
assert.strictEqual(secondRandomFolderContent.length, 1)
|
|
|
|
assert.strictEqual(destinationDirectoryContent.length, 0)
|
|
|
|
assert.strictEqual(sourceDirectoryContent.length, 2)
|
2021-08-27 13:03:41 +02:00
|
|
|
|
|
|
|
await copyDirectory('/source', '/destination')
|
|
|
|
destinationDirectoryContent = await fs.promises.readdir('/destination')
|
|
|
|
sourceDirectoryContent = await fs.promises.readdir('/source')
|
2022-04-23 18:41:14 +02:00
|
|
|
randomFolderContent = await fs.promises.readdir(
|
|
|
|
'/destination/random-folder'
|
|
|
|
)
|
2021-08-27 13:03:41 +02:00
|
|
|
secondRandomFolderContent = await fs.promises.readdir(
|
|
|
|
'/destination/random-folder/second-random-folder'
|
|
|
|
)
|
2023-07-20 22:00:11 +02:00
|
|
|
assert.strictEqual(destinationDirectoryContent.length, 2)
|
|
|
|
assert.strictEqual(sourceDirectoryContent.length, 2)
|
|
|
|
assert.deepStrictEqual(destinationDirectoryContent, [
|
|
|
|
'index.ts',
|
|
|
|
'random-folder'
|
|
|
|
])
|
|
|
|
assert.deepStrictEqual(sourceDirectoryContent, [
|
|
|
|
'index.ts',
|
|
|
|
'random-folder'
|
|
|
|
])
|
|
|
|
assert.strictEqual(randomFolderContent.length, 2)
|
|
|
|
assert.strictEqual(secondRandomFolderContent.length, 1)
|
|
|
|
assert.deepStrictEqual(randomFolderContent, [
|
|
|
|
'default.png',
|
|
|
|
'second-random-folder'
|
|
|
|
])
|
|
|
|
assert.deepStrictEqual(secondRandomFolderContent, ['mycode.ts'])
|
2021-08-27 13:03:41 +02:00
|
|
|
})
|
2021-06-09 20:31:45 +02:00
|
|
|
})
|