cleanup and overide

This commit is contained in:
Slawomir Jaranowski
2020-01-23 22:41:53 +01:00
parent cfeadc6038
commit e385db4b9a
6 changed files with 223 additions and 41 deletions

View File

@ -21,16 +21,16 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE. THE SOFTWARE.
*/ */
const core = require('@actions/core');
const os = require('os');
const path = require('path');
const core = require('@actions/core');
const settings = require('./settings'); const settings = require('./settings');
async function run() { async function run() {
try {
settings.cleanup(); settings.cleanup();
} catch (error) {
core.setFailed(error.message);
}
} }
run(); run();
module.exports = { run };

View File

@ -56,6 +56,5 @@ afterAll(() => {
test('run with default values', () => { test('run with default values', () => {
const out = cp.execSync(`node ${cleanupPath}`, { env: process.env }).toString(); cp.execSync(`node ${cleanupPath}`, { env: process.env }).toString();
console.log(out);
}) })

View File

@ -1,3 +1,27 @@
/*
The MIT License (MIT)
Copyright (c) 2020 Slawomir Jaranowski and contributors
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
const core = require('@actions/core'); const core = require('@actions/core');
const settings = require('./settings'); const settings = require('./settings');
@ -7,7 +31,6 @@ async function run() {
settings.generate(); settings.generate();
} catch (error) { } catch (error) {
core.setFailed(error.message); core.setFailed(error.message);
console.error(error);
} }
} }

View File

@ -21,6 +21,7 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE. THE SOFTWARE.
*/ */
const process = require('process'); const process = require('process');
const cp = require('child_process'); const cp = require('child_process');
const path = require('path'); const path = require('path');
@ -60,35 +61,20 @@ afterAll(() => {
}); });
function assertSetingsFile() {
const settingsStatus = fs.lstatSync(settingsPath);
expect(settingsStatus.isFile()).toBeTruthy();
expect(settingsStatus.size).toBeGreaterThan(0);
const settingsBody = fs.readFileSync(settingsPath).toString();
expect(settingsBody).toMatch('<settings>');
}
test('run with default values', () => {
const out = cp.execSync(`node ${indexPath}`, { env: process.env }).toString();
console.log(out);
assertSetingsFile();
})
test('run with all feature', () => { test('run with all feature', () => {
process.env['INPUT_SERVERS'] = '[{"id": "serverId", "username": "username", "password": "password"}]'; process.env['INPUT_SERVERS'] = '[{"id": "serverId", "username": "username", "password": "password"}]';
process.env['INPUT_PROPERTIES'] = '[{"prop1": "value1"}, {"prop2": "value2"}]' process.env['INPUT_PROPERTIES'] = '[{"prop1": "value1"}, {"prop2": "value2"}]'
process.env['INPUT_SONATYPESNAPSHOT'] = true; process.env['INPUT_SONATYPESNAPSHOT'] = true;
const out = cp.execSync(`node ${indexPath}`, { env: process.env }).toString(); cp.execSync(`node ${indexPath}`, { env: process.env }).toString();
console.log(out);
assertSetingsFile(); const settingsStatus = fs.lstatSync(settingsPath);
expect(settingsStatus.isFile()).toBeTruthy();
expect(settingsStatus.size).toBeGreaterThan(0);
const settingsBody = fs.readFileSync(settingsPath).toString(); const settingsBody = fs.readFileSync(settingsPath).toString();
expect(settingsBody).toMatch('<settings>');
expect(settingsBody).toMatch('<servers><server><id>serverId</id><username>username</username><password>password</password></server></servers>'); expect(settingsBody).toMatch('<servers><server><id>serverId</id><username>username</username><password>password</password></server></servers>');
expect(settingsBody).toMatch('prop1'); expect(settingsBody).toMatch('prop1');
}) })

View File

@ -1,3 +1,27 @@
/*
The MIT License (MIT)
Copyright (c) 2020 Slawomir Jaranowski and contributors
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
const core = require('@actions/core'); const core = require('@actions/core');
const os = require('os'); const os = require('os');
const path = require('path'); const path = require('path');
@ -7,7 +31,9 @@ const XMLSerializer = require('xmldom').XMLSerializer;
const xpath = require('xpath'); const xpath = require('xpath');
const settingsPath = path.join(os.homedir(), '.m2', 'settings.xml'); function getSettingsPath() {
return path.join(os.homedir(), '.m2', 'settings.xml');
}
function getSettingsTemplate() { function getSettingsTemplate() {
const templatePath = path.join(__dirname, 'templates', 'settings.xml'); const templatePath = path.join(__dirname, 'templates', 'settings.xml');
@ -87,12 +113,20 @@ function addSonatypeSnapshots(template) {
} }
function generate() { function generate() {
const settingsPath = getSettingsPath();
core.info('Prepare maven setings: ' + settingsPath); core.info('Prepare maven setings: ' + settingsPath);
if (fs.existsSync(settingsPath)) { if (fs.existsSync(settingsPath)) {
const val = core.getInput("override");
if (val && val.toLocaleLowerCase() == 'true') {
core.info('maven settings.xml already exists - override');
} else {
core.warning('maven settings.xml already exists - skip'); core.warning('maven settings.xml already exists - skip');
return; return;
} }
}
const templateXml = getSettingsTemplate(); const templateXml = getSettingsTemplate();
fillServers(templateXml); fillServers(templateXml);
@ -103,8 +137,19 @@ function generate() {
} }
function cleanup() { function cleanup() {
const mavenSettingsState = core.getState('maven-settings'); const mavenSettingsState = core.getState('maven-settings');
core.info('Cleanup maven setings: ' + settingsPath + " state: " + mavenSettingsState); const settingsPath = getSettingsPath();
if (mavenSettingsState == 'ok') {
if (fs.existsSync(settingsPath)) {
fs.unlinkSync(settingsPath);
core.info('Cleanup maven setings: ' + settingsPath + ' - file was removed');
} else {
core.warning('Cleanup maven setings: ' + settingsPath + ' - file not exist');
}
} else {
core.info('Cleanup maven setings: ' + settingsPath + ' - file wasn\'t generated by action');
}
} }
module.exports = { module.exports = {

View File

@ -1,4 +1,29 @@
/*
The MIT License (MIT)
Copyright (c) 2020 Slawomir Jaranowski and contributors
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
const os = require('os');
const process = require('process');
const DOMParser = require('xmldom').DOMParser; const DOMParser = require('xmldom').DOMParser;
const XMLSerializer = require('xmldom').XMLSerializer; const XMLSerializer = require('xmldom').XMLSerializer;
const fs = require('fs'); const fs = require('fs');
@ -11,6 +36,20 @@ var xmlTestProfile = undefined;
const testHomePath = fs.mkdtempSync(".m2"); const testHomePath = fs.mkdtempSync(".m2");
const settingsPath = path.join(testHomePath, '.m2', 'settings.xml'); const settingsPath = path.join(testHomePath, '.m2', 'settings.xml');
var consoleOutput = [];
beforeAll(() => {
if (!fs.existsSync(testHomePath)) {
fs.mkdirSync(testHomePath);
}
process.env['HOME'] = testHomePath;
process.env['USERPROFILE'] = testHomePath;
os.homedir = () => testHomePath;
process.stdout.write = output => consoleOutput.push(output);
});
beforeEach(() => { beforeEach(() => {
xmlTestProfile = new DOMParser().parseFromString(`<settings> xmlTestProfile = new DOMParser().parseFromString(`<settings>
<profiles> <profiles>
@ -30,6 +69,19 @@ beforeEach(() => {
</profiles> </profiles>
</settings>`); </settings>`);
consoleOutput = [];
});
afterAll(() => {
try {
fs.rmdirSync(path.dirname(settingsPath));
} catch (error) {
}
try {
fs.rmdirSync(testHomePath);
} catch (error) {
}
}); });
afterEach(() => { afterEach(() => {
@ -46,14 +98,6 @@ afterEach(() => {
} }
}); });
afterAll(() => {
try {
fs.rmdirSync(path.dirname(settingsPath));
fs.rmdirSync(testHomePath);
} catch (error) {
}
});
test('template should be read', () => { test('template should be read', () => {
const template = settings.getSettingsTemplate(); const template = settings.getSettingsTemplate();
@ -193,3 +237,88 @@ test('fillProperties do nothing if no params', () => {
</profiles> </profiles>
</settings>`); </settings>`);
}) })
test('cleanup - not generated', () => {
settings.cleanup();
expect(consoleOutput).toEqual(
expect.arrayContaining([
expect.stringMatching(/Cleanup maven setings: \..+\/\.m2\/settings.xml - file wasn\'t generated by action/)
])
);
})
test('cleanup - not exist', () => {
process.env['STATE_maven-settings'] = 'ok';
settings.cleanup();
expect(consoleOutput).toEqual(
expect.arrayContaining([
expect.stringMatching(/::warning::Cleanup maven setings: \..+\/\.m2\/settings.xml - file not exist/)
])
);
})
test('cleanup - ok', () => {
process.env['STATE_maven-settings'] = 'ok';
fs.closeSync(fs.openSync(settingsPath, 'w'));
settings.cleanup();
expect(consoleOutput).toEqual(
expect.arrayContaining([
expect.stringMatching(/Cleanup maven setings: \..+\/\.m2\/settings.xml - file was removed/)
])
);
expect(fs.existsSync(settingsPath)).not.toBeTruthy();
})
test('genereate', () => {
process.env['INPUT_SERVERS'] = '[{"id": "serverId", "username": "username", "password": "password"}]';
process.env['INPUT_PROPERTIES'] = '[{"prop1": "value1"}, {"prop2": "value2"}]'
process.env['INPUT_SONATYPESNAPSHOT'] = true;
settings.generate();
expect(consoleOutput).toEqual(
expect.arrayContaining([
expect.stringMatching(/Prepare maven setings: \..+\/.m2\/settings.xml/),
expect.stringMatching(/:save-state name=maven-settings::ok/)
])
);
})
test('genereate - skip', () => {
fs.closeSync(fs.openSync(settingsPath, 'w'));
settings.generate();
expect(consoleOutput).toEqual(
expect.arrayContaining([
expect.stringMatching(/Prepare maven setings: \..+\/.m2\/settings.xml/),
expect.stringMatching(/::warning::maven settings.xml already exists - skip/)
])
);
})
test('genereate - override', () => {
fs.closeSync(fs.openSync(settingsPath, 'w'));
process.env['INPUT_OVERRIDE'] = 'true';
settings.generate();
expect(consoleOutput).toEqual(
expect.arrayContaining([
expect.stringMatching(/Prepare maven setings: \..+\/.m2\/settings.xml/),
expect.stringMatching(/maven settings.xml already exists - override/),
expect.stringMatching(/:save-state name=maven-settings::ok/)
])
);
})