mirror of
https://github.com/theoludwig/programming-challenges.git
synced 2024-11-09 22:08:58 +01:00
91 lines
3.2 KiB
TypeScript
91 lines
3.2 KiB
TypeScript
import fs from 'node:fs'
|
|
|
|
import fsMock from 'mock-fs'
|
|
|
|
import { copyDirectory } from '../copyDirectory.js'
|
|
|
|
describe('utils/copyDirectory', () => {
|
|
afterEach(() => {
|
|
fsMock.restore()
|
|
})
|
|
|
|
it('copy the files', async () => {
|
|
fsMock({
|
|
'/source': {
|
|
'default.png': '',
|
|
'index.ts': '',
|
|
'.npmignore': ''
|
|
},
|
|
'/destination': {}
|
|
}, { createCwd: false })
|
|
|
|
let destinationDirectoryContent = await fs.promises.readdir('/destination')
|
|
let sourceDirectoryContent = await fs.promises.readdir('/source')
|
|
expect(destinationDirectoryContent.length).toEqual(0)
|
|
expect(sourceDirectoryContent.length).toEqual(3)
|
|
|
|
await copyDirectory('/source', '/destination')
|
|
destinationDirectoryContent = await fs.promises.readdir('/destination')
|
|
sourceDirectoryContent = await fs.promises.readdir('/source')
|
|
expect(destinationDirectoryContent.length).toEqual(3)
|
|
expect(sourceDirectoryContent.length).toEqual(3)
|
|
expect(destinationDirectoryContent).toEqual(
|
|
expect.arrayContaining(['default.png', 'index.ts', '.npmignore'])
|
|
)
|
|
expect(sourceDirectoryContent).toEqual(
|
|
expect.arrayContaining(['default.png', 'index.ts', '.npmignore'])
|
|
)
|
|
})
|
|
|
|
it('copy the files and folders recursively', async () => {
|
|
fsMock({
|
|
'/source': {
|
|
'random-folder': {
|
|
'default.png': '',
|
|
'second-random-folder': {
|
|
'mycode.ts': ''
|
|
}
|
|
},
|
|
'index.ts': '',
|
|
'.npmignore': ''
|
|
},
|
|
'/destination': {}
|
|
}, { createCwd: false })
|
|
|
|
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'
|
|
)
|
|
expect(randomFolderContent.length).toEqual(2)
|
|
expect(secondRandomFolderContent.length).toEqual(1)
|
|
expect(destinationDirectoryContent.length).toEqual(0)
|
|
expect(sourceDirectoryContent.length).toEqual(3)
|
|
|
|
await copyDirectory('/source', '/destination')
|
|
destinationDirectoryContent = await fs.promises.readdir('/destination')
|
|
sourceDirectoryContent = await fs.promises.readdir('/source')
|
|
randomFolderContent = await fs.promises.readdir('/destination/random-folder')
|
|
secondRandomFolderContent = await fs.promises.readdir(
|
|
'/destination/random-folder/second-random-folder'
|
|
)
|
|
expect(destinationDirectoryContent.length).toEqual(3)
|
|
expect(sourceDirectoryContent.length).toEqual(3)
|
|
expect(destinationDirectoryContent).toEqual(
|
|
expect.arrayContaining(['random-folder', 'index.ts', '.npmignore'])
|
|
)
|
|
expect(sourceDirectoryContent).toEqual(
|
|
expect.arrayContaining(['random-folder', 'index.ts', '.npmignore'])
|
|
)
|
|
expect(randomFolderContent.length).toEqual(2)
|
|
expect(secondRandomFolderContent.length).toEqual(1)
|
|
expect(randomFolderContent).toEqual(
|
|
expect.arrayContaining(['default.png', 'second-random-folder'])
|
|
)
|
|
expect(secondRandomFolderContent).toEqual(
|
|
expect.arrayContaining(['mycode.ts'])
|
|
)
|
|
})
|
|
})
|