-
Notifications
You must be signed in to change notification settings - Fork 207
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2f899b4
commit d2369f7
Showing
5 changed files
with
119 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import * as fs from 'fs'; | ||
import * as path from 'path'; | ||
import { Reader } from './reader'; | ||
|
||
export class FileSystemReader implements Reader { | ||
constructor(private readonly directory: string) {} | ||
|
||
public list(): Promise<string[]> { | ||
return fs.promises.readdir(this.directory); | ||
} | ||
|
||
public read(name: string): Promise<string> { | ||
return fs.promises.readFile(path.join(this.directory, name), 'utf8'); | ||
} | ||
|
||
public readSync(name: string): string { | ||
return fs.readFileSync(path.join(this.directory, name), 'utf8'); | ||
} | ||
|
||
public async readAnyOf(filenames: string[]): Promise<string | undefined> { | ||
try { | ||
for (const file of filenames) { | ||
return await this.read(file); | ||
} | ||
} catch (err) { | ||
return filenames.length > 0 | ||
? await this.readAnyOf(filenames.slice(1, filenames.length)) | ||
: undefined; | ||
} | ||
} | ||
|
||
public readSyncAnyOf(filenames: string[]): string | undefined { | ||
try { | ||
for (const file of filenames) { | ||
return this.readSync(file); | ||
} | ||
} catch (err) { | ||
return filenames.length > 0 | ||
? this.readSyncAnyOf(filenames.slice(1, filenames.length)) | ||
: undefined; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './reader'; | ||
export * from './file-system.reader'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export interface Reader { | ||
list(): string[] | Promise<string[]>; | ||
read(name: string): string | Promise<string>; | ||
readSync(name: string): string; | ||
readAnyOf(filenames: string[]): string | Promise<string | undefined>; | ||
readSyncAnyOf(filenames: string[]): string | undefined; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import * as fs from 'fs'; | ||
import { FileSystemReader, Reader } from '../../../src/lib/readers'; | ||
|
||
jest.mock('fs', () => ({ | ||
readFileSync: jest.fn().mockReturnValue('content'), | ||
promises: { | ||
readdir: jest.fn().mockResolvedValue([]), | ||
readFile: jest.fn().mockResolvedValue('content'), | ||
}, | ||
})); | ||
|
||
const dir: string = process.cwd(); | ||
const reader: Reader = new FileSystemReader(dir); | ||
|
||
describe('File System Reader', () => { | ||
afterAll(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
it('should use fs.promises.readdir when list', async () => { | ||
await reader.list(); | ||
expect(fs.promises.readdir).toHaveBeenCalled(); | ||
}); | ||
it('should use fs.promises.readFile when read', async () => { | ||
await reader.read('filename'); | ||
expect(fs.promises.readFile).toHaveBeenCalled(); | ||
}); | ||
|
||
describe('readAnyOf tests', () => { | ||
it('should call readFile when running readAnyOf fn', async () => { | ||
const filenames: string[] = ['file1', 'file2', 'file3']; | ||
await reader.readAnyOf(filenames); | ||
|
||
expect(fs.promises.readFile).toHaveBeenCalled(); | ||
}); | ||
|
||
it('should return undefined when no file is passed', async () => { | ||
const content = await reader.readAnyOf([]); | ||
expect(content).toEqual(undefined); | ||
}); | ||
}); | ||
|
||
describe('readSyncAnyOf tests', () => { | ||
it('should call readFileSync when running readSyncAnyOf fn', async () => { | ||
const filenames: string[] = ['file1', 'file2', 'file3']; | ||
reader.readSyncAnyOf(filenames); | ||
|
||
expect(fs.readFileSync).toHaveBeenCalled(); | ||
}); | ||
|
||
it('should return undefined when no file is passed', async () => { | ||
const content = reader.readSyncAnyOf([]); | ||
expect(content).toEqual(undefined); | ||
}); | ||
}); | ||
}); |