const DOMParser = require('xmldom').DOMParser;
const XMLSerializer = require('xmldom').XMLSerializer;
const fs = require('fs');
const path = require('path');
const settings = require('./settings');
var xmlTestProfile = undefined;
const testHomePath = fs.mkdtempSync(".m2");
const settingsPath = path.join(testHomePath, '.m2', 'settings.xml');
beforeEach(() => {
xmlTestProfile = new DOMParser().parseFromString(`
_properties_
false
_sonatype-snapshots_
false
`);
});
afterEach(() => {
for (key in process.env) {
if (key.match(/^INPUT_/)) {
delete process.env[key];
}
}
try {
fs.unlinkSync(settingsPath);
} catch (error) {
}
});
afterAll(() => {
try {
fs.rmdirSync(path.dirname(settingsPath));
fs.rmdirSync(testHomePath);
} catch (error) {
}
});
test('template should be read', () => {
const template = settings.getSettingsTemplate();
expect(template).toBeDefined();
});
test('xml should be write', () => {
if (!fs.existsSync(testHomePath)) {
fs.mkdirSync(testHomePath);
}
settings.writeSettings(settingsPath, xmlTestProfile);
const settingsStatus = fs.lstatSync(settingsPath);
expect(settingsStatus.isFile()).toBeTruthy();
expect(settingsStatus.size).toBeGreaterThan(0);
});
test('fillServers do nothing if no params', () => {
const xml = new DOMParser().parseFromString("");
settings.fillServers(xml);
const xmlStr = new XMLSerializer().serializeToString(xml);
expect(xmlStr).toBe("");
});
test('fillServers one server', () => {
const xml = new DOMParser().parseFromString("");
process.env['INPUT_SERVERS'] = '[{"id": "id1", "username": "username1", "password":"password1"}]';
settings.fillServers(xml);
const xmlStr = new XMLSerializer().serializeToString(xml);
expect(xmlStr).toBe("" +
"id1username1password1" +
"");
});
test('fillServers two servers', () => {
const xml = new DOMParser().parseFromString("");
process.env['INPUT_SERVERS'] = '[{"id": "id1", "username": "username1", "password":"password1"},\
{"id": "id2", "username": "username2", "password":"password2"}]';
settings.fillServers(xml);
const xmlStr = new XMLSerializer().serializeToString(xml);
expect(xmlStr).toBe("" +
"id1username1password1" +
"id2username2password2" +
"");
});
test('addSonatypeSnapshots activate', () => {
process.env['INPUT_SONATYPESNAPSHOTS'] = "true";
settings.addSonatypeSnapshots(xmlTestProfile);
const xmlStr = new XMLSerializer().serializeToString(xmlTestProfile);
expect(xmlStr).toBe(`
_properties_
false
_sonatype-snapshots_
true
`);
});
test('fillProperties', () => {
process.env['INPUT_PROPERTIES'] = '[{"propertyName1": "propertyValue1"}, {"propertyName2": "propertyValue2"}]';
settings.fillProperties(xmlTestProfile);
const xmlStr = new XMLSerializer().serializeToString(xmlTestProfile);
expect(xmlStr).toBe(`
_properties_
true
propertyValue1propertyValue2
_sonatype-snapshots_
false
`);
})
test('fillProperties do nothing if no params', () => {
settings.fillProperties(xmlTestProfile);
const xmlStr = new XMLSerializer().serializeToString(xmlTestProfile);
expect(xmlStr).toBe(`
_properties_
false
_sonatype-snapshots_
false
`);
})