2021-07-03 18:40:17 +02:00
|
|
|
import fs from 'node:fs'
|
|
|
|
|
2022-04-23 18:41:14 +02:00
|
|
|
import tap from 'tap'
|
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
|
|
|
|
2022-04-23 18:41:14 +02:00
|
|
|
await tap.test('utils/copyDirectory', async (t) => {
|
|
|
|
t.afterEach(() => {
|
2021-06-09 20:31:45 +02:00
|
|
|
fsMock.restore()
|
|
|
|
})
|
|
|
|
|
2022-04-23 18:41:14 +02:00
|
|
|
await t.test('copy the files', async (t) => {
|
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')
|
2022-04-23 18:41:14 +02:00
|
|
|
t.equal(destinationDirectoryContent.length, 0)
|
|
|
|
t.equal(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')
|
2022-04-23 18:41:14 +02:00
|
|
|
t.equal(destinationDirectoryContent.length, 2)
|
|
|
|
t.equal(sourceDirectoryContent.length, 2)
|
|
|
|
t.strictSame(destinationDirectoryContent, ['default.png', 'index.ts'])
|
|
|
|
t.strictSame(sourceDirectoryContent, ['default.png', 'index.ts'])
|
2021-06-09 20:31:45 +02:00
|
|
|
})
|
2021-08-27 13:03:41 +02:00
|
|
|
|
2022-04-23 18:41:14 +02:00
|
|
|
await t.test('copy the files and folders recursively', async (t) => {
|
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'
|
|
|
|
)
|
2022-04-23 18:41:14 +02:00
|
|
|
t.equal(randomFolderContent.length, 2)
|
|
|
|
t.equal(secondRandomFolderContent.length, 1)
|
|
|
|
t.equal(destinationDirectoryContent.length, 0)
|
|
|
|
t.equal(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'
|
|
|
|
)
|
2022-04-23 18:41:14 +02:00
|
|
|
t.equal(destinationDirectoryContent.length, 2)
|
|
|
|
t.equal(sourceDirectoryContent.length, 2)
|
|
|
|
t.strictSame(destinationDirectoryContent, ['index.ts', 'random-folder'])
|
|
|
|
t.strictSame(sourceDirectoryContent, ['index.ts', 'random-folder'])
|
|
|
|
t.equal(randomFolderContent.length, 2)
|
|
|
|
t.equal(secondRandomFolderContent.length, 1)
|
|
|
|
t.strictSame(randomFolderContent, ['default.png', 'second-random-folder'])
|
|
|
|
t.strictSame(secondRandomFolderContent, ['mycode.ts'])
|
2021-08-27 13:03:41 +02:00
|
|
|
})
|
2021-06-09 20:31:45 +02:00
|
|
|
})
|