Skip to content

Commit

Permalink
Merge pull request #56 from snyk/fix/version-parsing-and-project-name
Browse files Browse the repository at this point in the history
fix: version parsing and project name
  • Loading branch information
dkontorovskyy committed Jul 30, 2019
2 parents 6e61e47 + 4c042a2 commit aa46958
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 9 deletions.
8 changes: 5 additions & 3 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ async function pluginInspect(root: string, targetFile: string, options: any): Pr
const sbtArgs = buildArgs(options.args, false, true);
const sbtVersion = getSbtVersion(root, targetFile);
const sbtPluginPath = generateSbtPluginPath(sbtVersion);
const packageName = path.basename(root);
const packageVersion = '1.0.0';

try {
// We could be running from a bundled CLI generated by `pkg`.
Expand All @@ -140,7 +142,7 @@ async function pluginInspect(root: string, targetFile: string, options: any): Pr
name: 'snyk:sbt',
runtime: 'unknown',
},
package: parser.parseSbtPluginResults(stdout),
package: parser.parseSbtPluginResults(stdout, packageName, packageVersion),
};
} catch (error) {
debug('Failed to produce dependency tree with custom snyk plugin due to error: ' + error.message);
Expand All @@ -152,8 +154,8 @@ function getSbtVersion(root: string, targetFile: string): string {
const buildPropsPath = path.join(root, path.dirname(targetFile), 'project/build.properties');
return fs.readFileSync(buildPropsPath, 'utf-8')
.split('\n') // split into lines
.find((line) => line.startsWith('sbt.version='))! // locate version line
.split('=')[1]; // return only the version
.find((line) => !!line.match(/sbt\.version\s*=/))! // locate version line
.split(/=\s*/)[1].trim(); // return only the version
}

// guess whether we have the couriser plugin by looking for sbt-coursier
Expand Down
6 changes: 3 additions & 3 deletions lib/parse-sbt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ function parse(text, name, version, isCoursier): DepTree {
return createSnykTree(rootTree, name, version);
}

function parseSbtPluginResults(sbtOutput: string): DepTree {
function parseSbtPluginResults(sbtOutput: string, packageName: string, packageVersion: string): DepTree {
// remove all other output
const outputStart = 'Snyk Output Start';
const outputEnd = 'Snyk Output End';
Expand All @@ -204,8 +204,8 @@ function parseSbtPluginResults(sbtOutput: string): DepTree {
}

const depTree = {
name: '.',
version: '1.0.0',
name: packageName,
version: packageVersion,
dependencies: {},
};

Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/testproj-1.2.8/project/build.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
sbt.version=1.2.8
sbt.version = 1.2.8
4 changes: 2 additions & 2 deletions test/functional/parse-sbt.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ test('parse `sbt dependencies` output: plugin 1.2.8', async (t) => {
__dirname, '..', 'fixtures',
'sbt-plugin-1.2.8-output.txt'),
'utf8');
const depTree = parser.parseSbtPluginResults(sbtOutput);
const depTree = parser.parseSbtPluginResults(sbtOutput, 'com.example:hello_2.12', '1.0.0');

t.equal(depTree.name, 'com.example:hello_2.12');
t.equal(depTree.version, '0.1.0-SNAPSHOT');
Expand All @@ -121,7 +121,7 @@ test('parse `sbt dependencies` output: plugin 0.13', async (t) => {
__dirname, '..', 'fixtures',
'sbt-plugin-0.13-output.txt'),
'utf8');
const depTree = parser.parseSbtPluginResults(sbtOutput);
const depTree = parser.parseSbtPluginResults(sbtOutput, 'com.example:hello_2.12', '1.0.0');

t.equal(depTree.name, 'com.example:hello_2.12');
t.equal(depTree.version, '0.1.0-SNAPSHOT');
Expand Down

0 comments on commit aa46958

Please sign in to comment.