Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { extname } from 'path';
import { retrieveSimilarSbomPackages } from './sbom.js';
import { getPomSpringBootVersion, getXMLFromFile, retrieveSimilarPomPackages, retrieveSimilarPomProperties } from './pom.js';
import {
getPomSpringBootVersion,
getXMLFromFile,
retrieveSimilarPomPackages,
retrieveSimilarPomProperties,
} from './pom.js';

export const checkDependencies = async () => {
const start = Date.now();
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "spring-boot-dependency-checker",
"version": "0.0.9",
"version": "0.1.0",
"description": "Spring Boot Dependency Checker - validate that you're using the versions Spring Boot has approved with your project.",
"keywords": [
"spring boot",
Expand Down
4 changes: 2 additions & 2 deletions pom.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export const getXMLFromFile = async (filename) => {
}
};

const getPomProperties = async (parsedPom) => {
export const getPomProperties = async (parsedPom) => {
const properties = parsedPom.project?.properties;
if (properties) {
return Object.keys(properties);
Expand All @@ -26,7 +26,7 @@ const getSpringBootProperties = async (filename) => {
return getJsonFromFile(`${cachePath}/properties_${filename}.json`);
};

const getPomDependenciesWithVersions = async (parsedPom) => {
export const getPomDependenciesWithVersions = async (parsedPom) => {
// if it's not an array, a single dependency has been declared and it doesn't apply
if (Array.isArray(parsedPom?.project?.dependencies?.dependency)) {
return parsedPom.project.dependencies.dependency.filter(dep => dep.version);
Expand Down
173 changes: 173 additions & 0 deletions test/test-pom.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
import { strictEqual } from 'node:assert';
import { writeFileSync } from 'fs';
import { unlink } from 'node:fs';
import {
getPomDependenciesWithVersions,
getPomProperties,
getPomSpringBootVersion,
getXMLFromFile,
retrieveSimilarPomPackages,
} from '../pom.js';

describe('test pom parsing', () => {
const filename = 'pom.xml';

it('should read a properly formatted XML file', async () => {
const testFile = `<project>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.1.0</version>
</parent>
<properties>
<java.version>1.8</java.version>
<jackson.version>2.10.2</jackson.version>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</dependency>
<dependency>
<groupId>org.java-websocket</groupId>
<artifactId>Java-WebSocket</artifactId>
<version>2.3.1</version>
</dependency>
</dependencies>
</project>`;
await writeFileSync(filename, testFile);

const xmlData = await getXMLFromFile(filename);

strictEqual(xmlData.project.parent.artifactId, 'spring-boot-starter-parent');
strictEqual(xmlData.project.parent.groupId, 'org.springframework.boot');
strictEqual(xmlData.project.parent.version, '3.1.0');

strictEqual(xmlData.project.properties['java.version'], 1.8);
strictEqual(xmlData.project.properties['jackson.version'], '2.10.2');

strictEqual(xmlData.project.dependencies.dependency.length, 2);
strictEqual(xmlData.project.dependencies.dependency[0].groupId, 'org.apache.httpcomponents');
strictEqual(xmlData.project.dependencies.dependency[0].artifactId, 'httpclient');
strictEqual(xmlData.project.dependencies.dependency[1].groupId, 'org.java-websocket');
strictEqual(xmlData.project.dependencies.dependency[1].artifactId, 'Java-WebSocket');
strictEqual(xmlData.project.dependencies.dependency[1].version, '2.3.1');
});

it('should return an array of pom properties when they exist', async () => {
const parsedPom = {
project: {
properties: {
'jackson.version': '2.1.0',
'snakeyaml.version': '3.0.0',
},
},
};

const pomProperties = await getPomProperties(parsedPom);

strictEqual(pomProperties.length, 2);
strictEqual(pomProperties[0], 'jackson.version');
strictEqual(pomProperties[1], 'snakeyaml.version');
});

it('should return an empty array of pom properties when they do not exist', async () => {
const parsedPom = {
project: {},
};

const pomProperties = await getPomProperties(parsedPom);

strictEqual(pomProperties.length, 0);
});

it('should return an array of pom dependencies when they exist', async () => {
const parsedPom = {
project: {
dependencies: {
dependency: [
{ groupId: 'org.apache.httpcomponents', artifactId: 'httpclient' },
{
groupId: 'org.java-websocket',
artifactId: 'Java-WebSocket',
version: '2.3.1',
}],
},
},
};

const pomDependenciesWithVersions = await getPomDependenciesWithVersions(parsedPom);

strictEqual(pomDependenciesWithVersions.length, 1);
strictEqual(pomDependenciesWithVersions[0].artifactId, 'Java-WebSocket');
strictEqual(pomDependenciesWithVersions[0].groupId, 'org.java-websocket');
strictEqual(pomDependenciesWithVersions[0].version, '2.3.1');
});

it('should return an array of pom dependencies when they exist', async () => {
const parsedPom = {
project: {},
};

const pomDependenciesWithVersions = await getPomDependenciesWithVersions(parsedPom);

strictEqual(pomDependenciesWithVersions.length, 0);
});

it('should get spring boot version from pom when it exists', async () => {
const parsedPom = {
project: {
parent: {
groupId: 'org.springframework.boot',
artifactId: 'spring-boot-starter-parent',
version: '2.1.0',
},
},
};

const pomSpringBootVersion = await getPomSpringBootVersion(parsedPom);

strictEqual(pomSpringBootVersion, '2.1.0');
});

it('should return a value for spring boot version from pom when it doesn\'t exists', async () => {
const parsedPom = {
project: {
parent: {},
},
};

const pomSpringBootVersion = await getPomSpringBootVersion(parsedPom);

strictEqual(pomSpringBootVersion, '');
});

it('should output mismatched packages', async () => {
const parsedPom = {
project: {
parent: {
groupId: 'org.springframework.boot',
artifactId: 'spring-boot-starter-parent',
version: '2.1.0',
},
'dependencies': {
'dependency': [
{ groupId: 'org.apache.httpcomponents', artifactId: 'httpclient' },
{
groupId: 'org.java-websocket',
artifactId: 'Java-WebSocket',
version: '2.3.1',
}],
},
},
};

await retrieveSimilarPomPackages(parsedPom);
});

after(() => {
unlink(filename, (err) => {
if (err) throw err;
});
});
});