Skip to content

Commit

Permalink
chore: move isSupported and getRequiredData for sharing
Browse files Browse the repository at this point in the history
  • Loading branch information
lili2311 committed Apr 16, 2021
1 parent 69c6f81 commit 84b314f
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 28 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { EntityToFix, WithUserMessage } from '../../../../types';
import { EntityToFix, WithUserMessage } from '../../../types';

interface Supported {
supported: true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ import {
} from '../../../../types';
import { FixedCache, PluginFixResponse } from '../../../types';
import { updateDependencies } from './update-dependencies';
import { MissingRemediationDataError } from '../../../../lib/errors/missing-remediation-data';
import { MissingFileNameError } from '../../../../lib/errors/missing-file-name';
import { partitionByFixable } from './is-supported';
import { partitionByFixable } from './../is-supported';
import { NoFixesCouldBeAppliedError } from '../../../../lib/errors/no-fixes-applied';
import { extractProvenance } from './extract-version-provenance';
import {
Expand All @@ -24,6 +22,7 @@ import {
} from './update-dependencies/requirements-file-parser';
import { standardizePackageName } from './update-dependencies/standardize-package-name';
import { containsRequireDirective } from './contains-require-directive';
import { validateRequiredData } from '../valdidate-required-data';

const debug = debugLib('snyk-fix:python:requirements.txt');

Expand Down Expand Up @@ -62,28 +61,6 @@ export async function pipRequirementsTxt(
return handlerResult;
}

export function getRequiredData(
entity: EntityToFix,
): {
remediation: RemediationChanges;
targetFile: string;
workspace: Workspace;
} {
const { remediation } = entity.testResult;
if (!remediation) {
throw new MissingRemediationDataError();
}
const { targetFile } = entity.scanResult.identity;
if (!targetFile) {
throw new MissingFileNameError();
}
const { workspace } = entity;
if (!workspace) {
throw new NoFixesCouldBeAppliedError();
}
return { targetFile, remediation, workspace };
}

async function fixAll(
entities: EntityToFix[],
options: FixOptions,
Expand Down Expand Up @@ -189,7 +166,7 @@ export async function applyAllFixes(
changes: FixChangesSummary[];
fixedMeta: { [filePath: string]: FixChangesSummary[] };
}> {
const { remediation, targetFile: entryFileName, workspace } = getRequiredData(
const { remediation, targetFile: entryFileName, workspace } = validateRequiredData(
entity,
);
const fixedMeta: {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { MissingRemediationDataError } from '../../../lib/errors/missing-remediation-data';
import { MissingFileNameError } from '../../../lib/errors/missing-file-name';
import { NoFixesCouldBeAppliedError } from '../../../lib/errors/no-fixes-applied';
import { EntityToFix, RemediationChanges, Workspace } from '../../../types';

export function validateRequiredData(
entity: EntityToFix,
): {
remediation: RemediationChanges;
targetFile: string;
workspace: Workspace;
} {
const { remediation } = entity.testResult;
if (!remediation) {
throw new MissingRemediationDataError();
}
const { targetFile } = entity.scanResult.identity;
if (!targetFile) {
throw new MissingFileNameError();
}
const { workspace } = entity;
if (!workspace) {
throw new NoFixesCouldBeAppliedError();
}
return { targetFile, remediation, workspace };
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isSupported } from '../../../../src/plugins/python/handlers/pip-requirements/is-supported';
import { isSupported } from '../../../../src/plugins/python/handlers/is-supported';
import { generateEntityToFix } from '../../../helpers/generate-entity-to-fix';

describe('isSupported', () => {
Expand Down

0 comments on commit 84b314f

Please sign in to comment.