1
1
mirror of https://github.com/theoludwig/programming-challenges.git synced 2025-05-18 12:02:53 +02:00

feat: usage of ESM modules imports (instead of CommonJS) (#14)

This commit is contained in:
Divlo
2022-04-23 18:41:14 +02:00
committed by GitHub
parent c502c17e7c
commit 104c61935a
44 changed files with 6623 additions and 5171 deletions

View File

@ -1,43 +1,39 @@
import fs from 'node:fs'
import tap from 'tap'
import fsMock from 'mock-fs'
import { copyDirectory } from '../copyDirectory.js'
describe('utils/copyDirectory', () => {
afterEach(() => {
await tap.test('utils/copyDirectory', async (t) => {
t.afterEach(() => {
fsMock.restore()
})
it('copy the files', async () => {
await t.test('copy the files', async (t) => {
fsMock({
'/source': {
'default.png': '',
'index.ts': '',
'.npmignore': ''
'index.ts': ''
},
'/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)
t.equal(destinationDirectoryContent.length, 0)
t.equal(sourceDirectoryContent.length, 2)
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'])
)
t.equal(destinationDirectoryContent.length, 2)
t.equal(sourceDirectoryContent.length, 2)
t.strictSame(destinationDirectoryContent, ['default.png', 'index.ts'])
t.strictSame(sourceDirectoryContent, ['default.png', 'index.ts'])
})
it('copy the files and folders recursively', async () => {
await t.test('copy the files and folders recursively', async (t) => {
fsMock({
'/source': {
'random-folder': {
@ -46,11 +42,10 @@ describe('utils/copyDirectory', () => {
'mycode.ts': ''
}
},
'index.ts': '',
'.npmignore': ''
'index.ts': ''
},
'/destination': {}
}, { createCwd: false })
})
let destinationDirectoryContent = await fs.promises.readdir('/destination')
let sourceDirectoryContent = await fs.promises.readdir('/source')
@ -58,33 +53,27 @@ describe('utils/copyDirectory', () => {
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)
t.equal(randomFolderContent.length, 2)
t.equal(secondRandomFolderContent.length, 1)
t.equal(destinationDirectoryContent.length, 0)
t.equal(sourceDirectoryContent.length, 2)
await copyDirectory('/source', '/destination')
destinationDirectoryContent = await fs.promises.readdir('/destination')
sourceDirectoryContent = await fs.promises.readdir('/source')
randomFolderContent = await fs.promises.readdir('/destination/random-folder')
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'])
)
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'])
})
})

View File

@ -1,6 +1,7 @@
import fs from 'node:fs'
import fsMock from 'mock-fs'
import tap from 'tap'
import {
TEMPORARY_PATH,
@ -8,20 +9,30 @@ import {
} from '../createTemporaryEmptyFolder.js'
import { isExistingPath } from '../isExistingPath.js'
describe('utils/createTemporaryEmptyFolder', () => {
afterEach(() => {
await tap.test('utils/createTemporaryEmptyFolder', async (t) => {
t.afterEach(() => {
fsMock.restore()
})
it('should remove and create again the temporary folder', async () => {
fsMock({
[TEMPORARY_PATH]: {
'file.txt': ''
}
}, { createCwd: false })
expect(await isExistingPath(TEMPORARY_PATH)).toBeTruthy()
expect((await fs.promises.readdir(TEMPORARY_PATH)).length).toEqual(1)
await t.test('should create the temporary folder', async (t) => {
fsMock({})
t.equal(await isExistingPath(TEMPORARY_PATH), false)
await createTemporaryEmptyFolder()
expect((await fs.promises.readdir(TEMPORARY_PATH)).length).toEqual(0)
t.equal(await isExistingPath(TEMPORARY_PATH), true)
})
await t.test(
'should remove and create again the temporary folder',
async (t) => {
fsMock({
[TEMPORARY_PATH]: {
'file.txt': ''
}
})
t.equal(await isExistingPath(TEMPORARY_PATH), true)
t.equal((await fs.promises.readdir(TEMPORARY_PATH)).length, 1)
await createTemporaryEmptyFolder()
t.equal((await fs.promises.readdir(TEMPORARY_PATH)).length, 0)
}
)
})

View File

@ -1,23 +1,24 @@
import fsMock from 'mock-fs'
import tap from 'tap'
import { isExistingPath } from '../isExistingPath.js'
describe('utils/isExistingFile', () => {
afterEach(() => {
await tap.test('utils/isExistingPath', async (t) => {
t.afterEach(() => {
fsMock.restore()
})
it('should return true if the file exists', async () => {
await t.test('should return true if the file exists', async () => {
fsMock({
'/file.txt': ''
}, { createCwd: false })
expect(await isExistingPath('/file.txt')).toBeTruthy()
})
t.equal(await isExistingPath('/file.txt'), true)
})
it("should return false if the file doesn't exists", async () => {
await t.test("should return false if the file doesn't exists", async () => {
fsMock({
'/file.txt': ''
}, { createCwd: false })
expect(await isExistingPath('/randomfile.txt')).toBeFalsy()
})
t.equal(await isExistingPath('/randomfile.txt'), false)
})
})