-
Notifications
You must be signed in to change notification settings - Fork 574
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 #1738 from snyk/feat/extract-python-provenance
@snyk/fix: Extract requirements.txt version provenance (-r, -c directives)
- Loading branch information
Showing
13 changed files
with
215 additions
and
4 deletions.
There are no files selected for viewing
51 changes: 51 additions & 0 deletions
51
packages/snyk-fix/src/plugins/python/handlers/pip-requirements/extract-version-provenance.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,51 @@ | ||
import * as path from 'path'; | ||
import * as debugLib from 'debug'; | ||
|
||
import { containsRequireDirective } from '.'; | ||
import { | ||
parseRequirementsFile, | ||
Requirement, | ||
} from './update-dependencies/requirements-file-parser'; | ||
import { Workspace } from '../../../../types'; | ||
|
||
interface PythonProvenance { | ||
[fileName: string]: Requirement[]; | ||
} | ||
|
||
const debug = debugLib('snyk-fix:python:extract-version-provenance'); | ||
|
||
export async function extractProvenance( | ||
workspace: Workspace, | ||
dir: string, | ||
fileName: string, | ||
provenance: PythonProvenance = {}, | ||
): Promise<PythonProvenance> { | ||
const requirementsTxt = await workspace.readFile(path.join(dir, fileName)); | ||
provenance = { | ||
...provenance, | ||
[fileName]: parseRequirementsFile(requirementsTxt), | ||
}; | ||
const { containsRequire, matches } = await containsRequireDirective( | ||
requirementsTxt, | ||
); | ||
if (containsRequire) { | ||
for (const match of matches) { | ||
const requiredFilePath = match[2]; | ||
if (provenance[requiredFilePath]) { | ||
debug('Detected recursive require directive, skipping'); | ||
continue; | ||
} | ||
|
||
provenance = { | ||
...provenance, | ||
...(await extractProvenance( | ||
workspace, | ||
dir, | ||
requiredFilePath, | ||
provenance, | ||
)), | ||
}; | ||
} | ||
} | ||
return provenance; | ||
} |
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
143 changes: 143 additions & 0 deletions
143
...es/snyk-fix/test/acceptance/plugins/python/update-dependencies/extract-provenance.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,143 @@ | ||
import * as fs from 'fs'; | ||
import * as pathLib from 'path'; | ||
import { extractProvenance } from '../../../../../src/plugins/python/handlers/pip-requirements/extract-version-provenance'; | ||
import { parseRequirementsFile } from '../../../../../src/plugins/python/handlers/pip-requirements/update-dependencies/requirements-file-parser'; | ||
|
||
describe('extractProvenance', () => { | ||
const workspacesPath = pathLib.resolve(__dirname, 'workspaces'); | ||
it('can extract and parse 1 required files', async () => { | ||
// Arrange | ||
const targetFile = pathLib.resolve(workspacesPath, 'with-require/dev.txt'); | ||
|
||
const workspace = { | ||
readFile: async (path: string) => { | ||
return fs.readFileSync(pathLib.resolve(workspacesPath, path), 'utf-8'); | ||
}, | ||
writeFile: async () => { | ||
return; | ||
}, | ||
}; | ||
const { dir, base } = pathLib.parse(targetFile); | ||
|
||
// Act | ||
const result = await extractProvenance(workspace, dir, base); | ||
// Assert | ||
const baseTxt = fs.readFileSync( | ||
pathLib.resolve(workspacesPath, 'with-require/base.txt'), | ||
'utf-8', | ||
); | ||
const devTxt = fs.readFileSync(targetFile, 'utf-8'); | ||
|
||
expect(result['dev.txt']).toEqual(parseRequirementsFile(devTxt)); | ||
expect(result['base.txt']).toEqual(parseRequirementsFile(baseTxt)); | ||
}); | ||
|
||
it('can extract and parse 1 required files', async () => { | ||
// Arrange | ||
const targetFile = pathLib.resolve( | ||
workspacesPath, | ||
'with-require-folder-up/reqs/requirements.txt', | ||
); | ||
|
||
const workspace = { | ||
readFile: async (path: string) => { | ||
return fs.readFileSync(pathLib.resolve(workspacesPath, path), 'utf-8'); | ||
}, | ||
writeFile: async () => { | ||
return; | ||
}, | ||
}; | ||
const { dir, base } = pathLib.parse(targetFile); | ||
|
||
// Act | ||
const result = await extractProvenance(workspace, dir, base); | ||
// Assert | ||
const baseTxt = fs.readFileSync( | ||
pathLib.resolve(workspacesPath, 'with-require-folder-up/base.txt'), | ||
'utf-8', | ||
); | ||
const requirementsTxt = fs.readFileSync(targetFile, 'utf-8'); | ||
|
||
expect(result['requirements.txt']).toEqual( | ||
parseRequirementsFile(requirementsTxt), | ||
); | ||
expect(result['../base.txt']).toEqual(parseRequirementsFile(baseTxt)); | ||
}); | ||
it('can extract and parse all required files with both -r and -c', async () => { | ||
// Arrange | ||
const folder = 'with-multiple-requires'; | ||
const targetFile = pathLib.resolve(workspacesPath, `${folder}/dev.txt`); | ||
|
||
const workspace = { | ||
readFile: async (path: string) => { | ||
return fs.readFileSync(pathLib.resolve(workspacesPath, path), 'utf-8'); | ||
}, | ||
writeFile: async () => { | ||
return; | ||
}, | ||
}; | ||
const { dir, base } = pathLib.parse(targetFile); | ||
|
||
// Act | ||
const result = await extractProvenance(workspace, dir, base); | ||
// Assert | ||
const baseTxt = fs.readFileSync( | ||
pathLib.resolve(workspacesPath, pathLib.join(folder, 'base.txt')), | ||
'utf-8', | ||
); | ||
const reqsBaseTxt = fs.readFileSync( | ||
pathLib.resolve(workspacesPath, pathLib.join(folder, 'reqs', 'base.txt')), | ||
'utf-8', | ||
); | ||
const devTxt = fs.readFileSync(targetFile, 'utf-8'); | ||
const constraintsTxt = fs.readFileSync( | ||
pathLib.resolve( | ||
workspacesPath, | ||
pathLib.join(folder, 'reqs', 'constraints.txt'), | ||
), | ||
'utf-8', | ||
); | ||
|
||
expect(result['dev.txt']).toEqual(parseRequirementsFile(devTxt)); | ||
expect(result['base.txt']).toEqual(parseRequirementsFile(baseTxt)); | ||
expect(result['reqs/base.txt']).toEqual(parseRequirementsFile(reqsBaseTxt)); | ||
expect(result['reqs/constraints.txt']).toEqual( | ||
parseRequirementsFile(constraintsTxt), | ||
); | ||
}); | ||
|
||
it('can extract and parse all required files when -r is recursive', async () => { | ||
// Arrange | ||
const folder = 'with-recursive-requires'; | ||
const targetFile = pathLib.resolve(workspacesPath, `${folder}/dev.txt`); | ||
|
||
const workspace = { | ||
readFile: async (path: string) => { | ||
return fs.readFileSync(pathLib.resolve(workspacesPath, path), 'utf-8'); | ||
}, | ||
writeFile: async () => { | ||
return; | ||
}, | ||
}; | ||
const { dir, base } = pathLib.parse(targetFile); | ||
|
||
// Act | ||
const result = await extractProvenance(workspace, dir, base); | ||
// Assert | ||
const baseTxt = fs.readFileSync( | ||
pathLib.resolve(workspacesPath, `${folder}/base.txt`), | ||
'utf-8', | ||
); | ||
const devTxt = fs.readFileSync(targetFile, 'utf-8'); | ||
const constraintsTxt = fs.readFileSync( | ||
pathLib.resolve(workspacesPath, `${folder}/constraints.txt`), | ||
'utf-8', | ||
); | ||
|
||
expect(result['dev.txt']).toEqual(parseRequirementsFile(devTxt)); | ||
expect(result['base.txt']).toEqual(parseRequirementsFile(baseTxt)); | ||
expect(result['constraints.txt']).toEqual( | ||
parseRequirementsFile(constraintsTxt), | ||
); | ||
}); | ||
}); |
1 change: 1 addition & 0 deletions
1
.../acceptance/plugins/python/update-dependencies/workspaces/with-multiple-requires/base.txt
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 @@ | ||
Jinja2==2.7.2 |
4 changes: 4 additions & 0 deletions
4
...t/acceptance/plugins/python/update-dependencies/workspaces/with-multiple-requires/dev.txt
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,4 @@ | ||
-r reqs/base.txt | ||
-r base.txt | ||
-c reqs/constraints.txt | ||
Django==1.6.1 |
1 change: 1 addition & 0 deletions
1
...ptance/plugins/python/update-dependencies/workspaces/with-multiple-requires/reqs/base.txt
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 @@ | ||
click>7.0 |
1 change: 1 addition & 0 deletions
1
...plugins/python/update-dependencies/workspaces/with-multiple-requires/reqs/constraints.txt
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 @@ | ||
Django==1.6.7 |
3 changes: 3 additions & 0 deletions
3
...acceptance/plugins/python/update-dependencies/workspaces/with-recursive-requires/base.txt
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,3 @@ | ||
click>7.0 | ||
# recursive require! | ||
-r dev.txt |
1 change: 1 addition & 0 deletions
1
...nce/plugins/python/update-dependencies/workspaces/with-recursive-requires/constraints.txt
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 @@ | ||
Django==1.6.7 |
3 changes: 3 additions & 0 deletions
3
.../acceptance/plugins/python/update-dependencies/workspaces/with-recursive-requires/dev.txt
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,3 @@ | ||
-r base.txt | ||
-c constraints.txt | ||
Django==1.6.1 |
Empty file.
1 change: 1 addition & 0 deletions
1
...lugins/python/update-dependencies/workspaces/with-require-folder-up/reqs/requirements.txt
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 @@ | ||
-r ../base.txt |
1 change: 1 addition & 0 deletions
1
...k-fix/test/acceptance/plugins/python/update-dependencies/workspaces/with-require/base.txt
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 @@ | ||
click>7.0 |