From 41f025740b3d307e221decc5e969ddaf87430eba Mon Sep 17 00:00:00 2001 From: Matt Busche Date: Mon, 22 Apr 2024 21:48:25 -0500 Subject: [PATCH] Add more tests --- index.js | 7 +- package-lock.json | 4 +- package.json | 2 +- pom.js | 4 +- test/test-pom.spec.js | 173 ++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 184 insertions(+), 6 deletions(-) create mode 100644 test/test-pom.spec.js diff --git a/index.js b/index.js index 39be290..3764850 100644 --- a/index.js +++ b/index.js @@ -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(); diff --git a/package-lock.json b/package-lock.json index 48eb85f..c6b5a7f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "spring-boot-dependency-checker", - "version": "0.0.9", + "version": "0.1.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "spring-boot-dependency-checker", - "version": "0.0.9", + "version": "0.1.0", "license": "\tGPL-3.0-or-later", "dependencies": { "fast-xml-parser": "^4.3.6", diff --git a/package.json b/package.json index 16f2ac0..ffa4000 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/pom.js b/pom.js index 27fe5c2..281ced3 100644 --- a/pom.js +++ b/pom.js @@ -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); @@ -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); diff --git a/test/test-pom.spec.js b/test/test-pom.spec.js new file mode 100644 index 0000000..b98181d --- /dev/null +++ b/test/test-pom.spec.js @@ -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 = ` + + org.springframework.boot + spring-boot-starter-parent + 3.1.0 + + + 1.8 + 2.10.2 + + + + org.apache.httpcomponents + httpclient + + + org.java-websocket + Java-WebSocket + 2.3.1 + + + `; + 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; + }); + }); +}); \ No newline at end of file