Skip to content

Commit

Permalink
Merge pull request #15 from snyk/fix/support_quotes_in_toml
Browse files Browse the repository at this point in the history
fix: support quoted keys in inline tables
  • Loading branch information
admons authored Aug 16, 2021
2 parents 791ca5f + 34ed9ac commit 12d7ab2
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 9 deletions.
5 changes: 2 additions & 3 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ export function buildDepGraph(
includeDevDependencies,
);
const pkgDetails: PkgInfo = manifest.pkgInfoFrom(manifestFileContents);
const pkgSpecs: PoetryLockFileDependency[] = lockFile.packageSpecsFrom(
lockFileContents,
);
const pkgSpecs: PoetryLockFileDependency[] =
lockFile.packageSpecsFrom(lockFileContents);
return poetryDepGraphBuilder.build(pkgDetails, dependencyNames, pkgSpecs);
}
5 changes: 3 additions & 2 deletions lib/lock-file-parser.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import * as toml from 'toml';
import * as toml from '@iarna/toml';

export function packageSpecsFrom(
lockFileContents: string,
): PoetryLockFileDependency[] {
const lockFile: PoetryLockFile = toml.parse(lockFileContents);
const lockFile = toml.parse(lockFileContents) as unknown as PoetryLockFile;

if (!lockFile.package) {
throw new LockFileNotValid();
}
Expand Down
10 changes: 7 additions & 3 deletions lib/manifest-parser.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import * as toml from 'toml';
import * as toml from '@iarna/toml';

export function pkgInfoFrom(manifestFileContents: string) {
let manifest: PoetryManifestType;
try {
manifest = toml.parse(manifestFileContents);
manifest = toml.parse(
manifestFileContents,
) as unknown as PoetryManifestType;
return {
name: manifest.tool.poetry.name,
version: manifest.tool.poetry.version,
Expand All @@ -17,7 +19,9 @@ export function getDependencyNamesFrom(
manifestFileContents: string,
includeDevDependencies: boolean,
): string[] {
const manifest: PoetryManifestType = toml.parse(manifestFileContents);
const manifest = toml.parse(
manifestFileContents,
) as unknown as PoetryManifestType;
if (!manifest.tool?.poetry) {
throw new ManifestFileNotValid();
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@
"prepare": "npm run build"
},
"dependencies": {
"@iarna/toml": "^2.2.5",
"@snyk/cli-interface": "^2.9.2",
"@snyk/dep-graph": "^1.23.0",
"debug": "^4.2.0",
"toml": "^3.0.0",
"tslib": "^2.0.0"
},
"devDependencies": {
Expand Down
8 changes: 8 additions & 0 deletions test/unit/lib/manifest-parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,5 +80,13 @@ describe('when loading manifest files', () => {
const poetryDependencies = getDependencyNamesFrom('[tool.poetry]', false);
expect(poetryDependencies.length).toBe(0);
});

it('should handle quoted keys in inline tables', () => {
const fileContents = `[tool.poetry.dependencies]
pkg_a = {"version" = "^1.0"}`;
const poetryDependencies = getDependencyNamesFrom(fileContents, false);
expect(poetryDependencies.length).toBe(1);
expect(poetryDependencies.includes('pkg_a')).toBe(true);
});
});
});

0 comments on commit 12d7ab2

Please sign in to comment.