Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2022 from snyk/feat/generic-tool-version-check
feat(@snyk/fix): make tool version check generic
- Loading branch information
Showing
3 changed files
with
86 additions
and
38 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import * as chalk from 'chalk'; | ||
|
||
import * as pipenvPipfileFix from '@snyk/fix-pipenv-pipfile'; | ||
|
||
import * as ora from 'ora'; | ||
|
||
import { FixOptions } from '../types'; | ||
|
||
const supportFunc = { | ||
pipenv: { | ||
isInstalled: () => pipenvPipfileFix.isPipenvInstalled(), | ||
isSupportedVersion: (version) => | ||
pipenvPipfileFix.isPipenvSupportedVersion(version), | ||
}, | ||
}; | ||
|
||
export async function checkPackageToolSupported( | ||
packageManager: 'pipenv', | ||
options: FixOptions, | ||
): Promise<void> { | ||
const { version } = await supportFunc[packageManager].isInstalled(); | ||
|
||
const spinner = ora({ isSilent: options.quiet, stream: process.stdout }); | ||
spinner.clear(); | ||
spinner.text = `Checking ${packageManager} version`; | ||
spinner.indent = 2; | ||
spinner.start(); | ||
|
||
if (!version) { | ||
spinner.stopAndPersist({ | ||
text: chalk.hex('#EDD55E')( | ||
`Could not detect ${packageManager} version, proceeding anyway. Some operations may fail.`, | ||
), | ||
symbol: chalk.hex('#EDD55E')('⚠️'), | ||
}); | ||
return; | ||
} | ||
|
||
const { supported, versions } = supportFunc[ | ||
packageManager | ||
].isSupportedVersion(version); | ||
if (!supported) { | ||
const spinnerMessage = ` ${version} ${packageManager} version detected. Currently the following ${packageManager} versions are supported: ${versions.join( | ||
',', | ||
)}`; | ||
spinner.stopAndPersist({ | ||
text: chalk.hex('#EDD55E')(spinnerMessage), | ||
symbol: chalk.hex('#EDD55E')('⚠️'), | ||
}); | ||
} else { | ||
spinner.stop(); | ||
} | ||
} |
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
31 changes: 31 additions & 0 deletions
31
packages/snyk-fix/test/acceptance/plugins/package-tool-supported.spec.ts
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,31 @@ | ||
import * as pipenvPipfileFix from '@snyk/fix-pipenv-pipfile'; | ||
import { checkPackageToolSupported } from '../../../src/plugins/package-tool-supported'; | ||
|
||
jest.mock('@snyk/fix-pipenv-pipfile'); | ||
|
||
describe('checkPackageToolSupported', () => { | ||
it('pipenv fix package called with correct data', async () => { | ||
// Arrange | ||
const isPipenvSupportedVersionSpy = jest | ||
.spyOn(pipenvPipfileFix, 'isPipenvSupportedVersion') | ||
.mockReturnValue({ | ||
supported: true, | ||
versions: ['123.123.123'], | ||
}); | ||
const isPipenvInstalledSpy = jest | ||
.spyOn(pipenvPipfileFix, 'isPipenvInstalled') | ||
.mockResolvedValue({ | ||
version: '123.123.123', | ||
}); | ||
|
||
// Act | ||
await checkPackageToolSupported('pipenv', {}); | ||
|
||
// Assert | ||
expect(isPipenvInstalledSpy).toHaveBeenCalled(); | ||
expect(isPipenvInstalledSpy).toHaveBeenNthCalledWith(1); | ||
|
||
expect(isPipenvSupportedVersionSpy).toHaveBeenCalled(); | ||
expect(isPipenvSupportedVersionSpy).toHaveBeenCalledWith('123.123.123'); | ||
}); | ||
}); |