Skip to content

Commit

Permalink
Merge pull request #41 from terrestris/tests
Browse files Browse the repository at this point in the history
Tests
  • Loading branch information
simonseyock authored Jul 15, 2024
2 parents fb9a6aa + 2ff6162 commit f3e3ebc
Show file tree
Hide file tree
Showing 13 changed files with 7,517 additions and 2,912 deletions.
10,197 changes: 7,302 additions & 2,895 deletions package-lock.json

Large diffs are not rendered by default.

18 changes: 14 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
"description": "a very simple maven plugin for semantic release",
"main": "index.js",
"scripts": {
"release": "release-it",
"check": "npm run typecheck && npm test",
"doc": "jsdoc2md src/plugin-config.js | ./replace-docs.sh",
"check": "npm run typecheck",
"typecheck": "tsc --pretty --noEmit true --emitDeclarationOnly false"
"test": "jest",
"typecheck": "tsc --pretty --noEmit true --emitDeclarationOnly false",
"release": "release-it"
},
"repository": {
"type": "git",
Expand All @@ -26,13 +27,22 @@
"glob": "^9.2.1"
},
"devDependencies": {
"@types/jest": "^29.5.12",
"@types/node": "^20.14.10",
"@types/semantic-release__error": "^3.0.3",
"jest": "^29.7.0",
"jsdoc-to-markdown": "^8.0.0",
"release-it": "^16.1.5",
"typescript": "^5.5.3"
},
"publishConfig": {
"access": "public"
}
},
"files": [
"src/**/*",
"LICENSE",
"README.md",
"index.js",
"package.json"
]
}
1 change: 1 addition & 0 deletions src/exec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const execa = require('execa');
/**
* @param {string} file
* @param {string[]} options
* @private
*/
function exec(file, options) {
const childProcess = execa(file, options);
Expand Down
15 changes: 10 additions & 5 deletions src/maven.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const { exec } = require('./exec');
/**
* @param {string|undefined} settingsPath
* @returns {string[]}
* @private
*/
function settingsOption(settingsPath) {
if (settingsPath) {
Expand All @@ -29,6 +30,7 @@ function settingsOption(settingsPath) {
* @param {boolean} processAllModules
* @param {boolean} debug
* @returns {Promise<void>}
* @private
*/
async function updateVersion(logger, mvnw, versionStr, settingsPath, processAllModules, debug) {
logger.log(`Updating pom.xml to version ${versionStr}`);
Expand All @@ -42,11 +44,11 @@ async function updateVersion(logger, mvnw, versionStr, settingsPath, processAllM
command,
[
'versions:set',
...settingsOption(settingsPath),
...debugOption,
'--batch-mode',
'--no-transfer-progress',
'-DgenerateBackupPoms=false',
...settingsOption(settingsPath),
`-DnewVersion=${versionStr}`,
...processAllModulesOption
]
Expand All @@ -65,9 +67,10 @@ async function updateVersion(logger, mvnw, versionStr, settingsPath, processAllM
* @param {boolean} processAllModules
* @param {boolean} debug
* @returns {Promise<void>}
* @private
*/
async function updateSnapshotVersion(logger, mvnw, settingsPath, processAllModules, debug) {
logger.log(`Update pom.xml to next snapshot version`);
logger.log('Update pom.xml to next snapshot version');

const command = mvnw ? './mvnw' : 'mvn';
const processAllModulesOption = processAllModules ? ['-DprocessAllModules'] : [];
Expand All @@ -78,11 +81,11 @@ async function updateSnapshotVersion(logger, mvnw, settingsPath, processAllModul
command,
[
'versions:set',
...settingsOption(settingsPath),
...debugOption,
'--batch-mode',
'--no-transfer-progress',
'-DnextSnapshot=true',
...settingsOption(settingsPath),
'-DgenerateBackupPoms=false',
...processAllModulesOption
]
Expand All @@ -103,6 +106,7 @@ async function updateSnapshotVersion(logger, mvnw, settingsPath, processAllModul
* @param {boolean} clean
* @param {boolean} debug
* @returns {Promise<void>}
* @private
*/
async function deploy(logger, mvnw, nextVersion, mavenTarget, settingsPath, clean, debug) {
logger.log(`Deploying version ${nextVersion} with maven`);
Expand All @@ -117,11 +121,11 @@ async function deploy(logger, mvnw, nextVersion, mavenTarget, settingsPath, clea
[
...cleanOption,
...mavenTarget.split(' '),
...settingsOption(settingsPath),
...debugOption,
'--batch-mode',
'--no-transfer-progress',
'-DskipTests',
...settingsOption(settingsPath)
'-DskipTests'
]
);
} catch (e) {
Expand All @@ -135,6 +139,7 @@ async function deploy(logger, mvnw, nextVersion, mavenTarget, settingsPath, clea
* @param {Logger} logger
* @param {boolean} mvnw
* @returns {Promise<void>}
* @private
*/
async function testMvn(logger, mvnw) {
logger.log('Testing if mvn exists');
Expand Down
1 change: 1 addition & 0 deletions src/plugin-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const SemanticReleaseError = require("@semantic-release/error");
/**
* @param {Partial<PluginConfig>} config
* @returns {PluginConfig}
* @private
*/
function evaluateConfig(config) {
const withDefaults = Object.assign({
Expand Down
1 change: 1 addition & 0 deletions src/prepare.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const {
const {
evaluateConfig
} = require('./plugin-config');

const SemanticReleaseError = require("@semantic-release/error");

/**
Expand Down
1 change: 1 addition & 0 deletions src/publish.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const {
const {
evaluateConfig
} = require('./plugin-config');

const SemanticReleaseError = require("@semantic-release/error");

/**
Expand Down
3 changes: 1 addition & 2 deletions src/success.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ const {
} = require('@semantic-release/git/lib/git');

const { glob } = require("glob");
const SemanticReleaseError = require("@semantic-release/error");

/**
* @param {import("./plugin-config").PluginConfig} pluginConfig
Expand Down Expand Up @@ -42,7 +41,7 @@ module.exports = async function success(pluginConfig, {

await updateSnapshotVersion(logger, mvnw, settingsPath, processAllModules, debug);
if (!options?.repositoryUrl) {
logger.log('No git repository url configured. No files are commited.');
logger.error('No git repository url configured. No files are commited.');
return;
}

Expand Down
5 changes: 0 additions & 5 deletions src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,3 @@
* @property {(msg: string) => void} log
* @property {(msg: string|Error) => void} error
*/

/**
* @typedef {Object} Release
* @property {string} version
*/
2 changes: 1 addition & 1 deletion src/verify-conditions.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const {

/**
* @param {import("./plugin-config").PluginConfig} pluginConfig
* @param {{ logger: Logger}} params
* @param {{ logger: Logger }} params
* @returns {Promise<void>}
*/
module.exports = async function verifyConditions(pluginConfig, {
Expand Down
3 changes: 3 additions & 0 deletions test/__mocks__/exec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
exec: jest.fn()
};
164 changes: 164 additions & 0 deletions test/maven.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
const {
exec
} = require('../src/exec');
const {updateVersion, updateSnapshotVersion, deploy, testMvn} = require("../src/maven");

jest.mock('../src/exec');

describe('maven', () => {
const logger = { log: jest.fn(), error: jest.fn() };

afterEach(() => {
logger.log.mockClear();
logger.error.mockClear();
});

test('updateVersion with all options off', () => {
updateVersion(logger, false, '1.1.1', undefined, false, false);
expect(exec).toBeCalledWith(
'mvn',
[
'versions:set',
'--batch-mode',
'--no-transfer-progress',
'-DgenerateBackupPoms=false',
'-DnewVersion=1.1.1'
]
);

expect(logger.log).toBeCalledTimes(1);
expect(logger.log).toBeCalledWith(`Updating pom.xml to version 1.1.1`);
expect(logger.error).toBeCalledTimes(0);
});

test('updateVersion with all options on', () => {
updateVersion(logger, true, '1.1.2', 'some/path', true, true);
expect(exec).toBeCalledWith(
'./mvnw',
[
'versions:set',
'--settings',
'some/path',
'-X',
'--batch-mode',
'--no-transfer-progress',
'-DgenerateBackupPoms=false',
'-DnewVersion=1.1.2',
'-DprocessAllModules'
]
);

expect(logger.log).toBeCalledTimes(1);
expect(logger.log).toBeCalledWith(`Updating pom.xml to version 1.1.2`);
expect(logger.error).toBeCalledTimes(0);
});

test('updateSnapshotVersion with all options off', () => {
updateSnapshotVersion(logger, false, undefined, false, false);

expect(exec).toBeCalledWith(
'mvn',
[
'versions:set',
'--batch-mode',
'--no-transfer-progress',
'-DnextSnapshot=true',
'-DgenerateBackupPoms=false'
]
);

expect(logger.log).toBeCalledTimes(1);
expect(logger.log).toBeCalledWith('Update pom.xml to next snapshot version');
expect(logger.error).toBeCalledTimes(0);
});

test('updateSnapshotVersion with all options on', () => {
updateSnapshotVersion(logger, true, 'some/path', true, true);

expect(exec).toBeCalledWith(
'./mvnw',
[
'versions:set',
'--settings',
'some/path',
'-X',
'--batch-mode',
'--no-transfer-progress',
'-DnextSnapshot=true',
'-DgenerateBackupPoms=false',
'-DprocessAllModules'
]
);

expect(logger.log).toBeCalledTimes(1);
expect(logger.log).toBeCalledWith('Update pom.xml to next snapshot version');
expect(logger.error).toBeCalledTimes(0);
});

test('deploy with all options off', () => {
deploy(logger, false, '1.1.3', 'deploy', undefined, false, false);

expect(exec).toBeCalledWith(
'mvn',
[
'deploy',
'--batch-mode',
'--no-transfer-progress',
'-DskipTests',
]
);

expect(logger.log).toBeCalledTimes(1);
expect(logger.log).toBeCalledWith(`Deploying version 1.1.3 with maven`);
expect(logger.error).toBeCalledTimes(0);
});

test('deploy with all options on', () => {
deploy(logger, true, '1.1.4', 'deploy jib:build', 'some/path', true, true);

expect(exec).toBeCalledWith(
'./mvnw',
[
'clean',
'deploy',
'jib:build',
'--settings',
'some/path',
'-X',
'--batch-mode',
'--no-transfer-progress',
'-DskipTests'
]
);

expect(logger.log).toBeCalledTimes(1);
expect(logger.log).toBeCalledWith(`Deploying version 1.1.4 with maven`);
expect(logger.error).toBeCalledTimes(0);
});

test('testMvn with all options off', () => {
testMvn(logger, false);

expect(exec).toBeCalledWith(
'mvn',
['-v']
);

expect(logger.log).toBeCalledTimes(1);
expect(logger.log).toBeCalledWith('Testing if mvn exists');
expect(logger.error).toBeCalledTimes(0);
});

test('testMvn with all options on', () => {
testMvn(logger, true);

expect(exec).toBeCalledWith(
'./mvnw',
['-v']
);

expect(logger.log).toBeCalledTimes(1);
expect(logger.log).toBeCalledWith('Testing if mvn exists');
expect(logger.error).toBeCalledTimes(0);
});
});
18 changes: 18 additions & 0 deletions test/plugin-config.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const {evaluateConfig} = require("../src/plugin-config");

describe('evaluateConfig', () => {
test('will reject settings path with illegal characters', () => {
expect(() => {
evaluateConfig({ settingsPath: '; echo "test"' });
}).toThrow('Config settingsPath contains disallowed characters')
});

test('will reject unknown maven target', () => {
expect(() => {
evaluateConfig({
// @ts-ignore
mavenTarget: 'unknown-target'
});
}).toThrow('Unrecognized maven target unknown-target')
})
});

0 comments on commit f3e3ebc

Please sign in to comment.