Skip to content

Commit

Permalink
feat(@snyk/fix): make tool version check generic
Browse files Browse the repository at this point in the history
  • Loading branch information
lili2311 committed Jun 11, 2021
1 parent 25acaff commit c639350
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 38 deletions.
53 changes: 53 additions & 0 deletions packages/snyk-fix/src/plugins/package-tool-supported.ts
@@ -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();
}
}
@@ -1,9 +1,8 @@
import * as debugLib from 'debug';
import * as ora from 'ora';
import * as chalk from 'chalk';
import * as pipenvPipfileFix from '@snyk/fix-pipenv-pipfile';

import { EntityToFix, FixOptions } from '../../../../types';
import { checkPackageToolSupported } from '../../../package-tool-supported';
import { PluginFixResponse } from '../../../types';
import { updateDependencies } from './update-dependencies';

Expand All @@ -20,7 +19,7 @@ export async function pipenvPipfile(
skipped: [],
};

await checkPipenvSupport(options);
await checkPackageToolSupported('pipenv', options);
for (const [index, entity] of fixable.entries()) {
const spinner = ora({ isSilent: options.quiet, stream: process.stdout });
const spinnerMessage = `Fixing Pipfile ${index + 1}/${fixable.length}`;
Expand All @@ -39,38 +38,3 @@ export async function pipenvPipfile(

return handlerResult;
}

async function checkPipenvSupport(options: FixOptions): Promise<void> {
const { version } = await pipenvPipfileFix.isPipenvInstalled();

const spinner = ora({ isSilent: options.quiet, stream: process.stdout });
spinner.clear();
spinner.text = 'Checking pipenv version';
spinner.indent = 2;
spinner.start();

if (!version) {
spinner.stopAndPersist({
text: chalk.hex('#EDD55E')(
'Could not detect pipenv version, proceeding anyway. Some operations may fail.',
),
symbol: chalk.hex('#EDD55E')('⚠️'),
});
return;
}

const { supported, versions } = pipenvPipfileFix.isPipenvSupportedVersion(
version,
);
if (!supported) {
const spinnerMessage = ` ${version} pipenv version detected. Currently the following pipenv versions are supported: ${versions.join(
',',
)}`;
spinner.stopAndPersist({
text: chalk.hex('#EDD55E')(spinnerMessage),
symbol: chalk.hex('#EDD55E')('⚠️'),
});
} else {
spinner.stop();
}
}
@@ -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');
});
});

0 comments on commit c639350

Please sign in to comment.