Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 38 additions & 36 deletions packages/cli-kit/src/public/node/fs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,8 @@ import {
fileRealPath,
} from './fs.js'
import {joinPath, normalizePath} from './path.js'
import {takeRandomFromArray} from '../common/array.js'
import {beforeEach, describe, expect, test, vi} from 'vitest'
import FastGlob from 'fast-glob'

import * as os from 'os'

vi.mock('../common/array.js')
vi.mock('fast-glob')
vi.mock('os', async (importOriginal) => {
const actual = await importOriginal<typeof import('os')>()
return {...actual, EOL: actual.EOL}
})
import * as array from '../common/array.js'
import {describe, expect, test, vi} from 'vitest'

describe('inTemporaryDirectory', () => {
test('ties the lifecycle of the temporary directory to the lifecycle of the callback', async () => {
Expand Down Expand Up @@ -223,10 +213,11 @@ describe('makeDirectoryWithRandomName', () => {
test('rerolls the name if a directory exists with the same name', async () => {
await inTemporaryDirectory(async (tmpDir) => {
// Given
vi.mocked(takeRandomFromArray).mockReturnValueOnce('taken')
vi.mocked(takeRandomFromArray).mockReturnValueOnce('directory')
vi.mocked(takeRandomFromArray).mockReturnValueOnce('free')
vi.mocked(takeRandomFromArray).mockReturnValueOnce('directory')
const takeRandomFromArraySpy = vi.spyOn(array, 'takeRandomFromArray')
takeRandomFromArraySpy.mockReturnValueOnce('taken')
takeRandomFromArraySpy.mockReturnValueOnce('directory')
takeRandomFromArraySpy.mockReturnValueOnce('free')
takeRandomFromArraySpy.mockReturnValueOnce('directory')

const content = 'test'
const filePath = joinPath(tmpDir, 'taken-directory-app')
Expand All @@ -237,7 +228,8 @@ describe('makeDirectoryWithRandomName', () => {

// Then
expect(got).toEqual('free-directory-app')
expect(takeRandomFromArray).toHaveBeenCalledTimes(4)
expect(takeRandomFromArraySpy).toHaveBeenCalledTimes(4)
takeRandomFromArraySpy.mockRestore()
})
})
})
Expand Down Expand Up @@ -292,20 +284,38 @@ describe('createFileReadStream', () => {
})

describe('glob', () => {
test('calls fastGlob with dot:true if no dot option is passed', async () => {
// When
await glob('pattern')
test('returns the files that match the pattern', async () => {
await inTemporaryDirectory(async (tmpDir) => {
// Given
const filePath = joinPath(tmpDir, 'test-file')
const dotFilePath = joinPath(tmpDir, '.dot-file')
await touchFile(filePath)
await touchFile(dotFilePath)

// Then
expect(FastGlob).toBeCalledWith('pattern', {dot: true})
// When
const got = await glob(joinPath(tmpDir, '*'))

// Then
expect(got.map((path) => normalizePath(path))).toContain(normalizePath(filePath))
expect(got.map((path) => normalizePath(path))).toContain(normalizePath(dotFilePath))
})
})

test('calls fastGlob with dot option if passed', async () => {
// When
await glob('pattern', {dot: false})
test('returns the files that match the pattern excluding dot files', async () => {
await inTemporaryDirectory(async (tmpDir) => {
// Given
const filePath = joinPath(tmpDir, 'test-file')
const dotFilePath = joinPath(tmpDir, '.dot-file')
await touchFile(filePath)
await touchFile(dotFilePath)

// Then
expect(FastGlob).toBeCalledWith('pattern', {dot: false})
// When
const got = await glob(joinPath(tmpDir, '*'), {dot: false})

// Then
expect(got.map((path) => normalizePath(path))).toContain(normalizePath(filePath))
expect(got.map((path) => normalizePath(path))).not.toContain(normalizePath(dotFilePath))
})
})
})

Expand Down Expand Up @@ -335,24 +345,16 @@ describe('detectEOL', () => {
test('returns the default EOL if no EOL is found', async () => {
// Given
const fileContent = 'testcontent'
vi.mocked(os).EOL = '\n'

// When
const eol = detectEOL(fileContent)

// Then
expect(eol).toEqual('\n')
expect(eol).toMatch(/^(\r\n|\n)$/)
})
})

describe('copyDirectoryContents', () => {
beforeEach(() => {
// restore fast-glob to its original implementation for the tests
vi.doMock('fast-glob', async () => {
return vi.importActual('fast-glob')
})
})

test('copies the contents of source directory to destination directory', async () => {
// Given
await inTemporaryDirectory(async (tmpDir) => {
Expand Down
Loading