mirror of
https://github.com/theoludwig/programming-challenges.git
synced 2025-05-18 12:02:53 +02:00
feat: rewrite programming-challenges CLI (#3)
This commit is contained in:
38
cli/utils/__test__/copyDirectory.test.ts
Normal file
38
cli/utils/__test__/copyDirectory.test.ts
Normal file
@ -0,0 +1,38 @@
|
||||
import fsMock from 'mock-fs'
|
||||
import fs from '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'])
|
||||
)
|
||||
})
|
||||
})
|
33
cli/utils/__test__/createTemporaryEmptyFolder.test.ts
Normal file
33
cli/utils/__test__/createTemporaryEmptyFolder.test.ts
Normal file
@ -0,0 +1,33 @@
|
||||
import fsMock from 'mock-fs'
|
||||
import fs from 'fs'
|
||||
|
||||
import {
|
||||
TEMPORARY_PATH,
|
||||
createTemporaryEmptyFolder
|
||||
} from '../createTemporaryEmptyFolder'
|
||||
import { isExistingPath } from '../isExistingPath'
|
||||
|
||||
describe('utils/createTemporaryEmptyFolder', () => {
|
||||
afterEach(async () => {
|
||||
fsMock.restore()
|
||||
})
|
||||
|
||||
it('should create the temporary folder', async () => {
|
||||
fsMock({})
|
||||
expect(await isExistingPath(TEMPORARY_PATH)).toBeFalsy()
|
||||
await createTemporaryEmptyFolder()
|
||||
expect(await isExistingPath(TEMPORARY_PATH)).toBeTruthy()
|
||||
})
|
||||
|
||||
it('should remove and create again the temporary folder', async () => {
|
||||
fsMock({
|
||||
[TEMPORARY_PATH]: {
|
||||
'file.txt': ''
|
||||
}
|
||||
})
|
||||
expect(await isExistingPath(TEMPORARY_PATH)).toBeTruthy()
|
||||
expect((await fs.promises.readdir(TEMPORARY_PATH)).length).toEqual(1)
|
||||
await createTemporaryEmptyFolder()
|
||||
expect((await fs.promises.readdir(TEMPORARY_PATH)).length).toEqual(0)
|
||||
})
|
||||
})
|
23
cli/utils/__test__/isExistingPath.test.ts
Normal file
23
cli/utils/__test__/isExistingPath.test.ts
Normal file
@ -0,0 +1,23 @@
|
||||
import fsMock from 'mock-fs'
|
||||
|
||||
import { isExistingPath } from '../isExistingPath'
|
||||
|
||||
describe('utils/isExistingFile', () => {
|
||||
afterEach(async () => {
|
||||
fsMock.restore()
|
||||
})
|
||||
|
||||
it('should return true if the file exists', async () => {
|
||||
fsMock({
|
||||
'/file.txt': ''
|
||||
})
|
||||
expect(await isExistingPath('/file.txt')).toBeTruthy()
|
||||
})
|
||||
|
||||
it("should return false if the file doesn't exists", async () => {
|
||||
fsMock({
|
||||
'/file.txt': ''
|
||||
})
|
||||
expect(await isExistingPath('/randomfile.txt')).toBeFalsy()
|
||||
})
|
||||
})
|
Reference in New Issue
Block a user