1
1
mirror of https://github.com/theoludwig/programming-challenges.git synced 2024-12-08 00:45:29 +01:00
programming-challenges/cli/utils/__test__/copyDirectory.test.ts

91 lines
3.1 KiB
TypeScript
Raw Normal View History

import fs from 'node:fs'
import fsMock from 'mock-fs'
import { copyDirectory } from '../copyDirectory'
describe('utils/copyDirectory', () => {
afterEach(async () => {
fsMock.restore()
})
it('copy the files', async () => {
fsMock({
'/source': {
'default.png': '',
'index.ts': '',
'.npmignore': ''
},
'/destination': {}
})
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': {}
})
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'])
)
})
})