Skip to content

Commit

Permalink
fix(v2): Update maven dependency parsing to exclude dependencies with…
Browse files Browse the repository at this point in the history
… test scope. (#124)
  • Loading branch information
dvandra authored Sep 3, 2020
1 parent 34212f2 commit d8dd2ac
Show file tree
Hide file tree
Showing 2 changed files with 165 additions and 2 deletions.
3 changes: 1 addition & 2 deletions src/collector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,7 @@ class NaivePomXmlSaxParser {

parser.on("object", function (name, obj) {
if (obj.hasOwnProperty("groupId") && obj.hasOwnProperty("artifactId") && obj.hasOwnProperty("version") &&
(!obj.hasOwnProperty("scope") || (obj.hasOwnProperty("scope") && obj["scope"] === "compile") ||
(obj.hasOwnProperty("scope") && obj["scope"] === "runtime"))) {
(!obj.hasOwnProperty("scope") || (obj.hasOwnProperty("scope") && obj["scope"] != "test"))) {
let ga = `${obj["groupId"]}:${obj["artifactId"]}`;
let entry: IKeyValueEntry = new KeyValueEntry(ga, {line: 0, column: 0});
entry.value = new Variant(ValueType.String, obj["version"]);
Expand Down
164 changes: 164 additions & 0 deletions test/maven.collector.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
import { expect } from 'chai';
import { PomXmlDependencyCollector } from '../src/collector';

describe('Maven pom.xml parser test', () => {
const collector:PomXmlDependencyCollector = new PomXmlDependencyCollector();

it('tests valid pom.xml', async () => {
const deps = await collector.collect(
`<dependencyManagement>
<dependencies>
<!-- Dependency with scope as runtime -->
<dependency>
<groupId>{a.groupId}</groupId>
<artifactId>bc</artifactId>
<version>{a.version}</version>
</dependency>
<!-- Dependency with scope as runtime -->
<dependency>
<groupId>b</groupId>
<artifactId>c-d</artifactId>
<version>1.2.3</version>
<scope>runtime</scope>
</dependency>
<!-- Dependency with scope as compile -->
<dependency>
<groupId>c</groupId>
<artifactId>ab-cd</artifactId>
<version>2.3</version>
<scope>compile</scope>
<optional>true</optional>
</dependency>
<!-- Dependency with scope as test -->
<dependency>
<groupId>d</groupId>
<artifactId>AB-CD</artifactId>
<version>3.4.5.6</version>
<scope>test</scope>
<optional>true</optional>
</dependency>
</dependencies>
</dependencyManagement>
`);
expect(deps.length).equal(3);
expect(deps[0]).is.eql({
name: {value: '{a.groupId}:bc', position: {line: 0, column: 0}},
version: {value: '{a.version}', position: {line: 7, column: 30}}
});
expect(deps[1]).is.eql({
name: {value: 'b:c-d', position: {line: 0, column: 0}},
version: {value: '1.2.3', position: {line: 14, column: 30}}
});
expect(deps[2]).is.eql({
name: {value: 'c:ab-cd', position: {line: 0, column: 0}},
version: {value: '2.3', position: {line: 22, column: 30}}
});
});

it('tests pom.xml without any scope', async () => {
const deps = await collector.collect(
`<dependencyManagement>
<dependencies>
<!-- Dependency with scope as runtime -->
<dependency>
<groupId>{a.groupId}</groupId>
<artifactId>bc</artifactId>
<version>{a.version}</version>
</dependency>
<!-- Dependency with scope as runtime -->
<dependency>
<groupId>b</groupId>
<artifactId>c-d</artifactId>
<version>1.2.3</version>
</dependency>
<!-- Dependency with scope as compile -->
<dependency>
<groupId>c</groupId>
<artifactId>ab-cd</artifactId>
<version>2.3</version>
<optional>true</optional>
</dependency>
</dependencies>
</dependencyManagement>
`);
expect(deps.length).equal(3);
expect(deps[0]).is.eql({
name: {value: '{a.groupId}:bc', position: {line: 0, column: 0}},
version: {value: '{a.version}', position: {line: 7, column: 30}}
});
expect(deps[1]).is.eql({
name: {value: 'b:c-d', position: {line: 0, column: 0}},
version: {value: '1.2.3', position: {line: 14, column: 30}}
});
expect(deps[2]).is.eql({
name: {value: 'c:ab-cd', position: {line: 0, column: 0}},
version: {value: '2.3', position: {line: 21, column: 30}}
});
});

it('tests pom.xml with only test scope', async () => {
const deps = await collector.collect(
`<dependencyManagement>
<dependencies>
<!-- Dependency with scope as runtime -->
<dependency>
<groupId>{a.groupId}</groupId>
<artifactId>bc</artifactId>
<version>{a.version}</version>
<scope>test</scope>
</dependency>
<!-- Dependency with scope as runtime -->
<dependency>
<groupId>b</groupId>
<artifactId>c-d</artifactId>
<version>1.2.3</version>
<scope>test</scope>
</dependency>
<!-- Dependency with scope as compile -->
<dependency>
<groupId>c</groupId>
<artifactId>ab-cd</artifactId>
<version>2.3</version>
<scope>test</scope>
<optional>true</optional>
</dependency>
</dependencies>
</dependencyManagement>
`);
expect(deps.length).equal(0);
});

it('tests pom.xml with empty string', async () => {
const deps = await collector.collect(
`
`);
expect(deps.length).equal(0);
});

it('tests pom.xml with empty dependencyManagement', async () => {
const deps = await collector.collect(
`<dependencyManagement>
</dependencyManagement>
`);
expect(deps.length).equal(0);
});

it('tests pom.xml with empty dependencyManagement + dependencies', async () => {
const deps = await collector.collect(
`<dependencyManagement>
<dependencies>
</dependencies>
</dependencyManagement>
`);
expect(deps.length).equal(0);
});
});

0 comments on commit d8dd2ac

Please sign in to comment.