Skip to content

Commit

Permalink
feat: -c directive is supported
Browse files Browse the repository at this point in the history
No longer skip fixing pip manifests that include
`-c somefile.txt` to reference the constraints.
  • Loading branch information
lili2311 committed Apr 1, 2021
1 parent bf8f901 commit 120485b
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 34 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import * as chalk from 'chalk';

import { EntityToFix, WithUserMessage } from '../../../../types';
import { containsRequireDirective } from './contains-require-directive';

interface Supported {
supported: true;
Expand Down Expand Up @@ -33,30 +30,6 @@ export async function isSupported(
};
}

// TODO: fix the non null assertion here
let requirementsTxt;
try {
const fileName = entity.scanResult.identity.targetFile!;
requirementsTxt = await entity.workspace.readFile(fileName);
} catch (e) {
return {
supported: false,
reason: e.message,
};
}

const { containsRequire, matches } = await containsRequireDirective(
requirementsTxt,
);
if (containsRequire && matches.some((m) => m.includes('c'))) {
return {
supported: false,
reason: `Requirements with ${chalk.bold(
'-c',
)} directive are not yet supported`,
};
}

return { supported: true };
}

Expand Down
10 changes: 5 additions & 5 deletions packages/snyk-fix/test/unit/fix.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ describe('Snyk fix', () => {
);
jest
.spyOn(txtDevProjectTestResult.workspace, 'readFile')
.mockImplementationOnce(() => {
.mockImplementation(() => {
throw new Error('Test Error: Invalid encoding');
});
const pipfileProjectTestResult = generateEntityToFix(
Expand All @@ -150,17 +150,17 @@ describe('Snyk fix', () => {
expect(Object.keys(res.results)).toHaveLength(1);
expect(Object.keys(res.results)[0]).toEqual('python');
// skipped unsupported
expect(res.results.python.skipped).toHaveLength(2);
expect(res.results.python.skipped).toHaveLength(1);
expect(res.results.python.skipped[0]).toEqual({
userMessage: 'Pipfile is not supported',
original: pipfileProjectTestResult,
});
expect(res.results.python.skipped[1]).toEqual({
userMessage: 'Test Error: Invalid encoding',
expect(res.results.python.failed[0]).toEqual({
error: new Error('Test Error: Invalid encoding'),
original: txtDevProjectTestResult,
});

expect(res.results.python.failed).toHaveLength(0);
expect(res.results.python.failed).toHaveLength(1);
expect(res.results.python.succeeded).toHaveLength(1);

expect(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ describe('isSupported', () => {
const res = await isSupported(entity);
expect(res.supported).toBeTruthy();
});
it('with -c directive in the manifest not supported', async () => {
it('with -c directive in the manifest is supported', async () => {
const entity = generateEntityToFix(
'pip',
'requirements.txt',
'-c constraints.txt',
);
const res = await isSupported(entity);
expect(res.supported).toBeFalsy();
expect(res.supported).toBeTruthy();
});
it('with -e directive in the manifest is supported', async () => {
const entity = generateEntityToFix('pip', 'requirements.txt', '-e .');
Expand Down

0 comments on commit 120485b

Please sign in to comment.