generated from fabasoad/typescript-setup-action
-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Remove setup-stack step * Remove glob
- Loading branch information
Showing
7 changed files
with
59 additions
and
122 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
Large diffs are not rendered by default.
Oops, something went wrong.
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
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 |
---|---|---|
@@ -1,33 +1,33 @@ | ||
import { sync } from 'glob' | ||
import path from 'path' | ||
import { readdirSync, statSync } from 'fs' | ||
import { Logger } from 'winston' | ||
import CliExeNameProvider from './CliExeNameProvider' | ||
import { KITTEN_CLI_NAME } from './consts' | ||
import LoggerFactory from './LoggerFactory' | ||
|
||
export default class ExecutableFileFinder implements IExecutableFileFinder { | ||
private log: Logger | ||
private provider: ICliExeNameProvider | ||
private readonly log: Logger = LoggerFactory.create(ExecutableFileFinder.name) | ||
private readonly provider: ICliExeNameProvider | ||
|
||
constructor( | ||
cliName: string, | ||
provider: ICliExeNameProvider = new CliExeNameProvider(cliName)) { | ||
this.provider = provider | ||
this.log = LoggerFactory.create('ExecutableFileFinder') | ||
} | ||
|
||
find(folderPath: string, cliName: string): string { | ||
const pattern: string = | ||
`${folderPath}${path.sep}**${path.sep}${cliName}*` | ||
const files: string[] = sync(pattern) | ||
.filter((f: string) => f.endsWith(this.provider.getExeFileName())) | ||
if (files.length === 0) { | ||
throw new Error('Execution file has not been found under ' + | ||
`${folderPath} folder using ${pattern} pattern`) | ||
} else if (files.length > 1) { | ||
throw new Error('There are more than 1 execution file has been found ' + | ||
`under ${folderPath} folder using ${pattern} pattern: ${files}`) | ||
find(dirPath: string): string { | ||
const files: string[] = [dirPath] | ||
while (files.length > 0) { | ||
const filePath: string = files.pop() || '' | ||
if (statSync(filePath).isDirectory()) { | ||
readdirSync(filePath) | ||
.forEach((f: string) => files.push(`${filePath}${path.sep}${f}`)) | ||
} else if (filePath.endsWith(this.provider.getExeFileName())) { | ||
this.log.info(`${KITTEN_CLI_NAME} path is ${filePath}`) | ||
return filePath | ||
} | ||
} | ||
this.log.info(`${cliName} path is ${files[0]}`) | ||
return files[0] | ||
throw new Error('Execution file has not been found under ' + | ||
`${dirPath} folder`) | ||
} | ||
} |
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 |
---|---|---|
@@ -1,60 +1,46 @@ | ||
import { sync } from 'glob' | ||
import itParam from 'mocha-param' | ||
import path from 'path' | ||
import ExecutableFileFinder from '../ExecutableFileFinder' | ||
import fs from 'fs'; | ||
import itParam from 'mocha-param'; | ||
|
||
jest.mock('glob', () => ({ sync: jest.fn() })) | ||
const TEST_EXE = 'file.exe' | ||
|
||
interface INegativeTestFixture { | ||
message: string | ||
suffix: string | ||
} | ||
jest.mock('fs', () => ({ | ||
readdirSync: jest.fn((dirPath: string) => { | ||
switch (dirPath) { | ||
case 'folder1': return ['file.txt', 'folder2'] | ||
case 'folder1/folder2': return [TEST_EXE] | ||
default: return [] | ||
} | ||
}), | ||
statSync: jest.fn((dirPath: string) => ({ | ||
isDirectory: jest.fn(() => dirPath.endsWith('folder1') || | ||
dirPath.endsWith('folder2')) | ||
})) | ||
})) | ||
|
||
describe('ExecutableFileFinder', () => { | ||
const SUFFIX: string = '3ttg37ne' | ||
const items: INegativeTestFixture[] = [{ | ||
message: 'There are more than 1 execution file has been found', | ||
suffix: SUFFIX | ||
}, { | ||
message: 'Execution file has not been found', | ||
suffix: 'u4h0t03e' | ||
}] | ||
describe('ExecutableFileFinder::find', () => { | ||
let finder: ExecutableFileFinder | ||
|
||
it('should find successfully', () => { | ||
const folderPath: string = '4se2ov6f' | ||
const cliName: string = '1clx8w43' | ||
const files: string[] = [cliName + SUFFIX, cliName]; | ||
(sync as jest.Mock).mockImplementation(() => files) | ||
const finder: ExecutableFileFinder = new ExecutableFileFinder(cliName, { | ||
getExeFileName: (): string => SUFFIX | ||
beforeEach(() => { | ||
(fs.readdirSync as jest.Mock).mockClear(); | ||
(fs.statSync as jest.Mock).mockClear() | ||
const cliName = '1clx8w43' | ||
finder = new ExecutableFileFinder(cliName, { | ||
getExeFileName: () => TEST_EXE | ||
}) | ||
const actual: string = finder.find(folderPath, cliName) | ||
expect((sync as jest.Mock).mock.calls.length).toBe(1) | ||
expect(sync).toHaveBeenCalledWith( | ||
`${folderPath}${path.sep}**${path.sep}${cliName}*`) | ||
expect(actual).toBe(files[0]) | ||
}) | ||
|
||
itParam('should throw error (${value.message})', | ||
items, (item: INegativeTestFixture) => { | ||
const folderPath: string = '4se2ov6f' | ||
const cliName: string = '1clx8w43' | ||
const files: string[] = [cliName + SUFFIX, `gt11c1zr${SUFFIX}`]; | ||
(sync as jest.Mock).mockImplementation(() => files) | ||
const finder: ExecutableFileFinder = new ExecutableFileFinder(cliName, { | ||
getExeFileName: (): string => item.suffix | ||
}) | ||
try { | ||
finder.find(folderPath, cliName) | ||
} catch (e) { | ||
expect((<Error>e).message).toContain(item.message) | ||
expect((sync as jest.Mock).mock.calls.length).toBe(1) | ||
expect(sync).toHaveBeenCalledWith( | ||
`${folderPath}${path.sep}**${path.sep}${cliName}*`) | ||
return | ||
} | ||
fail() | ||
}) | ||
it('should find successfully', () => { | ||
const dirPath = 'folder1' | ||
const actual: string = finder.find(dirPath) | ||
expect(actual).toBe(`folder1/folder2/${TEST_EXE}`) | ||
}) | ||
|
||
afterEach(() => (sync as jest.Mock).mockClear()) | ||
itParam( | ||
'should not find successfully (${value})', | ||
['folder3', null], | ||
(dirPath: string) => { | ||
expect(() => finder.find(dirPath)).toThrow( | ||
`Execution file has not been found under ${dirPath} folder`) | ||
}) | ||
}) |
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