Skip to content

Commit

Permalink
refactor: use async generator
Browse files Browse the repository at this point in the history
  • Loading branch information
scolladon committed Nov 8, 2024
1 parent d37205b commit e523e24
Show file tree
Hide file tree
Showing 6 changed files with 162 additions and 148 deletions.
16 changes: 13 additions & 3 deletions __tests__/unit/lib/adapter/GitAdapter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,10 @@ describe('GitAdapter', () => {
mockedShowBuffer.mockResolvedValue(Buffer.from(content) as never)

// Act
const result = await gitAdapter.getFilesFrom('directory/path')
const result: any = []
for await (const file of gitAdapter.getFilesFrom('directory/path')) {
result.push(file)
}

// Assert

Expand All @@ -291,7 +294,10 @@ describe('GitAdapter', () => {
mockedCatFile.mockResolvedValue(Buffer.from(content) as never)

// Act
const result = await gitAdapter.getFilesFrom('directory/path')
const result: any = []
for await (const file of gitAdapter.getFilesFrom('directory/path')) {
result.push(file)
}

// Assert

Expand All @@ -311,7 +317,11 @@ describe('GitAdapter', () => {

// Act

const result = await gitAdapter.getFilesFrom('wrong/path')
const result: any = []
for await (const file of gitAdapter.getFilesFrom('directory/path')) {
result.push(file)
}

expect(result).toEqual([])
})
})
Expand Down
271 changes: 137 additions & 134 deletions __tests__/unit/lib/utils/fsHelper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,14 +126,12 @@ describe('copyFile', () => {
it('should copy file', async () => {
// Arrange
const sourcePath = 'source/copyFile'
mockGetFilesFrom.mockImplementation(() =>
Promise.resolve([
{
path: sourcePath,
content: Buffer.from(''),
},
])
)
mockGetFilesFrom.mockImplementation(async function* () {
yield await {
path: sourcePath,
content: Buffer.from(''),
}
})

// Act
await copyFiles(work.config, sourcePath)
Expand Down Expand Up @@ -166,39 +164,44 @@ describe('copyFile', () => {
describe('when source location is empty', () => {
it('should copy file', async () => {
// Arrange
mockGetFilesFrom.mockImplementation(() =>
Promise.resolve([
{
path: 'source/emptyFile',
content: Buffer.from(''),
},
])
)
mockGetFilesFrom.mockImplementation(async function* () {
yield await {
path: 'source/emptyFile',
content: Buffer.from(''),
}
yield await {
path: 'source/anotherEmptyFile',
content: Buffer.from(''),
}
})

// Act
await copyFiles(work.config, 'source/emptyFile')

// Assert
expect(mockGetFilesFrom).toBeCalled()
expect(outputFile).toBeCalledTimes(2)
expect(outputFile).toBeCalledWith(
'output/source/emptyFile',
Buffer.from('')
)
expect(outputFile).toBeCalledWith(
'output/source/anotherEmptyFile',
Buffer.from('')
)
})
})

describe('when source location is not empty', () => {
describe('when content is a folder', () => {
it('should copy the folder', async () => {
// Arrange
mockGetFilesFrom.mockImplementation(() =>
Promise.resolve([
{
path: 'copyDir/copyFile',
content: Buffer.from('content'),
},
])
)
mockGetFilesFrom.mockImplementation(async function* () {
yield await {
path: 'copyDir/copyFile',
content: Buffer.from('content'),
}
})

// Act
await copyFiles(work.config, 'source/copyDir')
Expand All @@ -216,11 +219,10 @@ describe('copyFile', () => {
describe('when content is not a git location', () => {
it('should ignore this path', async () => {
// Arrange
const sourcePath = 'source/warning'
mockGetFilesFrom.mockImplementation(() => Promise.reject())
mockGetFilesFrom.mockImplementation(async function* () {})

// Act
await copyFiles(work.config, sourcePath)
await copyFiles(work.config, 'source/warning')

// Assert
expect(mockGetFilesFrom).toBeCalled()
Expand All @@ -230,11 +232,12 @@ describe('copyFile', () => {
describe('when content is a file', () => {
beforeEach(async () => {
// Arrange
mockGetFilesFrom.mockImplementation(() =>
Promise.resolve([
{ path: 'source/copyFile', content: Buffer.from('content') },
])
)
mockGetFilesFrom.mockImplementation(async function* () {
yield await {
path: 'source/copyFile',
content: Buffer.from('content'),
}
})
})
it('should copy the file', async () => {
// Act
Expand All @@ -251,130 +254,130 @@ describe('copyFile', () => {
})
})
})
})

describe('readDir', () => {
describe('when path exist', () => {
const dir = 'dir/'
const file = 'test.js'
beforeEach(() => {
// Arrange
mockGetFilesPath.mockImplementation(() =>
Promise.resolve([`${dir}${file}`])
)
})
it('should return the file', async () => {
// Act
const dirContent = await readDir(dir, work.config)

// Assert
expect(dirContent).toEqual(expect.arrayContaining([`${dir}${file}`]))
expect(mockGetFilesPath).toHaveBeenCalled()
})
describe('readDir', () => {
describe('when path exist', () => {
const dir = 'dir/'
const file = 'test.js'
beforeEach(() => {
// Arrange
mockGetFilesPath.mockImplementation(() =>
Promise.resolve([`${dir}${file}`])
)
})
it('should return the file', async () => {
// Act
const dirContent = await readDir(dir, work.config)

describe('when path does not exist', () => {
beforeEach(() => {
// Arrange
mockGetFilesPath.mockImplementation(() =>
Promise.reject(new Error('test'))
)
})
it('should throw', async () => {
// Act
try {
await readDir('path', work.config)
} catch (err) {
// Assert
expect(err).toBeTruthy()
expect(mockGetFilesPath).toHaveBeenCalled()
}
})
// Assert
expect(dirContent).toEqual(expect.arrayContaining([`${dir}${file}`]))
expect(mockGetFilesPath).toHaveBeenCalled()
})
})

describe('pathExists', () => {
it('returns true when path is folder', async () => {
describe('when path does not exist', () => {
beforeEach(() => {
// Arrange
mockPathExists.mockImplementation(() => Promise.resolve(true))

mockGetFilesPath.mockImplementation(() =>
Promise.reject(new Error('test'))
)
})
it('should throw', async () => {
// Act
const result = await pathExists('path', work.config)

// Assert
expect(result).toBe(true)
try {
await readDir('path', work.config)
} catch (err) {
// Assert
expect(err).toBeTruthy()
expect(mockGetFilesPath).toHaveBeenCalled()
}
})
it('returns true when path is file', async () => {
// Arrange
mockPathExists.mockImplementation(() => Promise.resolve(true))
})
})

// Act
const result = await pathExists('path', work.config)
describe('pathExists', () => {
it('returns true when path is folder', async () => {
// Arrange
mockPathExists.mockImplementation(() => Promise.resolve(true))

// Assert
expect(result).toBe(true)
})
it('returns false when path does not exist', async () => {
// Arrange
mockPathExists.mockImplementation(() => Promise.resolve(false))
// Act
const result = await pathExists('path', work.config)

// Act
const result = await pathExists('path', work.config)
// Assert
expect(result).toBe(true)
})
it('returns true when path is file', async () => {
// Arrange
mockPathExists.mockImplementation(() => Promise.resolve(true))

// Assert
expect(result).toBe(false)
})
// Act
const result = await pathExists('path', work.config)

// Assert
expect(result).toBe(true)
})
it('returns false when path does not exist', async () => {
// Arrange
mockPathExists.mockImplementation(() => Promise.resolve(false))

describe('writeFile', () => {
beforeEach(() => {
mockBuildIgnoreHelper.mockResolvedValue({
globalIgnore: {
ignores: () => false,
} as unknown as Ignore,
} as unknown as IgnoreHelper)
})
it('write the content to the file system', async () => {
// Arrange
const path = 'folder/file'
const config: Config = work.config
config.output = 'root'
const content = 'content'
// Act
const result = await pathExists('path', work.config)

// Act
await writeFile(path, content, config)
// Assert
expect(result).toBe(false)
})
})

// Assert
expect(outputFile).toHaveBeenCalledWith('root/folder/file', content)
})
describe('writeFile', () => {
beforeEach(() => {
mockBuildIgnoreHelper.mockResolvedValue({
globalIgnore: {
ignores: () => false,
} as unknown as Ignore,
} as unknown as IgnoreHelper)
})
it('write the content to the file system', async () => {
// Arrange
const path = 'folder/file'
const config: Config = work.config
config.output = 'root'
const content = 'content'

// Act
await writeFile(path, content, config)

// Assert
expect(outputFile).toHaveBeenCalledWith('root/folder/file', content)
})

it('call only once for the same path', async () => {
// Arrange
const config: Config = work.config
config.output = 'root'
const content = 'content'
const path = 'other/path/file'
await writeFile(path, content, config)
it('call only once for the same path', async () => {
// Arrange
const config: Config = work.config
config.output = 'root'
const content = 'content'
const path = 'other/path/file'
await writeFile(path, content, config)

// Act
await writeFile(path, content, config)
// Act
await writeFile(path, content, config)

// Assert
expect(outputFile).toBeCalledTimes(1)
})
// Assert
expect(outputFile).toBeCalledTimes(1)
})

it('should not copy ignored path', async () => {
// Arrange
mockBuildIgnoreHelper.mockResolvedValue({
globalIgnore: {
ignores: () => true,
} as unknown as Ignore,
} as unknown as IgnoreHelper)
it('should not copy ignored path', async () => {
// Arrange
mockBuildIgnoreHelper.mockResolvedValue({
globalIgnore: {
ignores: () => true,
} as unknown as Ignore,
} as unknown as IgnoreHelper)

// Act
await writeFile('', '', {} as Config)
// Act
await writeFile('', '', {} as Config)

// Assert
expect(outputFile).not.toBeCalled()
})
// Assert
expect(outputFile).not.toBeCalled()
})
})
Loading

0 comments on commit e523e24

Please sign in to comment.