Completed the attribute support for servers. (#95)

* Completed the attribute support for servers, and password is optional. Resolves #92.

* Updated oracleServers.xml template to match new servers.xml template.

* Revised tests to match code and template changes.

* Added documentation for server attributes.

* Added additional use cases to exercise server attribute handling.
This commit is contained in:
Anthony Whitford
2021-02-16 13:46:27 -08:00
committed by GitHub
parent abf1253b29
commit cd16d8406c
6 changed files with 139 additions and 76 deletions

View File

@ -38,6 +38,14 @@ const settingsPath = path.join(testHomePath, '.m2', 'settings.xml');
var consoleOutput = [];
function stringAsXml(str) {
return new DOMParser().parseFromString(str, 'text/xml');
}
function xmlAsString(xml) {
return new XMLSerializer().serializeToString(xml).replace(/^\s*$(?:\r\n?|\n)/gm, '');
}
beforeAll(() => {
if (!fs.existsSync(testHomePath)) {
fs.mkdirSync(testHomePath);
@ -51,7 +59,7 @@ beforeAll(() => {
});
beforeEach(() => {
xmlTestProfile = new DOMParser().parseFromString(`<settings>
xmlTestProfile = stringAsXml(`<settings>
<profiles>
<profile>
<id>_properties_</id>
@ -119,7 +127,7 @@ test('xml should be write', () => {
test('fillServers do nothing if no params', () => {
const xml = new DOMParser().parseFromString("<servers/>");
const xml = stringAsXml("<servers/>");
settings.fillServers(xml, 'servers');
@ -130,54 +138,46 @@ test('fillServers do nothing if no params', () => {
test('fillServers one server', () => {
const xml = new DOMParser().parseFromString("<servers/>");
const xml = stringAsXml("<servers/>");
process.env['INPUT_SERVERS'] = '[{"id": "id1", "username": "username1", "password":"password1"}]';
settings.fillServers(xml, 'servers');
const xmlStr = new XMLSerializer().serializeToString(xml).replace(/^ $/mg, '');
expect(xmlStr).toBe(`<servers>
expect(xmlAsString(xml)).toBe(`<servers>
<server>
<id>id1</id>
<username>username1</username>
<password>password1</password>
</server></servers>`);
});
test('fillServers with username and configuration', () => {
const xml = new DOMParser().parseFromString("<servers/>");
const xml = stringAsXml("<servers/>");
process.env['INPUT_SERVERS'] = '[{"id": "id1", "username": "username", "configuration": {"prop1": "prop1Value", "prop2": "prop2Value"}}]';
settings.fillServers(xml, 'servers');
const xmlStr = new XMLSerializer().serializeToString(xml).replace(/^ $/mg, '')
expect(xmlStr).toBe(`<servers>
expect(xmlAsString(xml)).toBe(`<servers>
<server>
<id>id1</id>
<username>username</username>
<configuration><prop1>prop1Value</prop1><prop2>prop2Value</prop2></configuration>
</server></servers>`);
});
test('fillServers with username, password and configuration', () => {
const xml = new DOMParser().parseFromString("<servers/>");
const xml = stringAsXml("<servers/>");
process.env['INPUT_SERVERS'] = '[{"id": "id1", "username": "username", "password": "password", "configuration": {"prop1": "prop1Value", "prop2": "prop2Value"}}]';
settings.fillServers(xml, 'servers');
const xmlStr = new XMLSerializer().serializeToString(xml).replace(/^ $/mg, '')
expect(xmlStr).toBe(`<servers>
expect(xmlAsString(xml)).toBe(`<servers>
<server>
<id>id1</id>
<username>username</username>
@ -186,73 +186,115 @@ test('fillServers with username, password and configuration', () => {
</server></servers>`);
});
test('fillServers with username, privateKey', () => {
const xml = stringAsXml("<servers/>");
process.env['INPUT_SERVERS'] = '[{"id": "id1", "username": "username", "privateKey": "${user.home}/.ssh/id_rsa"}]';
settings.fillServers(xml, 'servers');
expect(xmlAsString(xml)).toBe(`<servers>
<server>
<id>id1</id>
<username>username</username>
<privateKey>\${user.home}/.ssh/id_rsa</privateKey>
</server></servers>`);
});
test('fillServers with username, privateKey, and passphrase', () => {
const xml = stringAsXml("<servers/>");
process.env['INPUT_SERVERS'] = '[{"id": "id1", "username": "username", "privateKey": "${user.home}/.ssh/id_rsa", "passphrase": "secret"}]';
settings.fillServers(xml, 'servers');
expect(xmlAsString(xml)).toBe(`<servers>
<server>
<id>id1</id>
<username>username</username>
<privateKey>\${user.home}/.ssh/id_rsa</privateKey>
<passphrase>secret</passphrase>
</server></servers>`);
});
test('fillServers with all attributes', () => {
const xml = stringAsXml("<servers/>");
process.env['INPUT_SERVERS'] = '[{"id": "server001", "username": "my_login", "password": "my_password", "privateKey": "${user.home}/.ssh/id_dsa", "passphrase": "some_passphrase", "filePermissions": "664", "directoryPermissions": "775", "configuration": {"prop1": "prop1Value", "prop2": "prop2Value"} }]';
settings.fillServers(xml, 'servers');
expect(xmlAsString(xml)).toBe(`<servers>
<server>
<id>server001</id>
<username>my_login</username>
<password>my_password</password>
<privateKey>\${user.home}/.ssh/id_dsa</privateKey>
<passphrase>some_passphrase</passphrase>
<filePermissions>664</filePermissions>
<directoryPermissions>775</directoryPermissions>
<configuration><prop1>prop1Value</prop1><prop2>prop2Value</prop2></configuration>
</server></servers>`);
});
test('fillServers with configuration', () => {
const xml = new DOMParser().parseFromString("<servers/>");
const xml = stringAsXml("<servers/>");
process.env['INPUT_SERVERS'] = '[{"id": "id1", "configuration": {"prop1": "prop1Value", "prop2": "prop2Value"}}]';
settings.fillServers(xml, 'servers');
const xmlStr = new XMLSerializer().serializeToString(xml).replace(/^ $/mg, '')
expect(xmlStr).toBe(`<servers>
expect(xmlAsString(xml)).toBe(`<servers>
<server>
<id>id1</id>
<configuration><prop1>prop1Value</prop1><prop2>prop2Value</prop2></configuration>
</server></servers>`);
});
test('fillServers with configuration subLevel', () => {
const xml = new DOMParser().parseFromString("<servers/>");
const xml = stringAsXml("<servers/>");
process.env['INPUT_SERVERS'] = '[{"id": "id1", "configuration": {"prop1": {"prop11": "value11", "prop12": "value12"}, "prop2": "value2"}}]';
settings.fillServers(xml, 'servers');
const xmlStr = new XMLSerializer().serializeToString(xml).replace(/^ $/mg, '')
expect(xmlStr).toBe(`<servers>
expect(xmlAsString(xml)).toBe(`<servers>
<server>
<id>id1</id>
<configuration><prop1><prop11>value11</prop11><prop12>value12</prop12></prop1><prop2>value2</prop2></configuration>
</server></servers>`);
});
test('fillServers two servers', () => {
const xml = new DOMParser().parseFromString("<servers/>");
const xml = stringAsXml("<servers/>");
process.env['INPUT_SERVERS'] = '[{"id": "id1", "username": "username1", "password":"password1"},\
{"id": "id2", "username": "username2", "password":"password2"}]';
settings.fillServers(xml, 'servers');
const xmlStr = new XMLSerializer().serializeToString(xml).replace(/^ $/mg, '');
expect(xmlStr).toBe(`<servers>
expect(xmlAsString(xml)).toBe(`<servers>
<server>
<id>id1</id>
<username>username1</username>
<password>password1</password>
</server>
<server>
<id>id2</id>
<username>username2</username>
<password>password2</password>
</server></servers>`);
});
test('fill servers incorrect fields', () => {
const xml = new DOMParser().parseFromString("<servers/>");
const xml = stringAsXml("<servers/>");
process.env['INPUT_SERVERS'] = '[{"idx": "id1"}]';
@ -263,22 +305,20 @@ test('fill servers incorrect fields', () => {
expect(xmlStr).toBe('<servers/>');
expect(consoleOutput).toEqual(
expect.arrayContaining([
expect.stringMatching(/::error::servers must contain id, \(username and password\) or configuration/)
expect.stringMatching(/::error::servers must contain id, and username or configuration/)
])
);
});
test('fill oracleServers', () => {
const xml = new DOMParser().parseFromString("<servers/>");
const xml = stringAsXml("<servers/>");
process.env['INPUT_ORACLESERVERS'] = '[{"id": "id1", "username": "username1", "password":"password1"}]';
settings.fillServers(xml, 'oracleServers');
const xmlStr = new XMLSerializer().serializeToString(xml);
expect(xmlStr).toBe(`<servers>
expect(xmlAsString(xml)).toBe(`<servers>
<server>
<id>id1</id>
<username>username1</username>
@ -305,27 +345,24 @@ test('fill oracleServers', () => {
test('fillServers github', () => {
const xml = new DOMParser().parseFromString("<servers/>");
const xml = stringAsXml("<servers/>");
process.env['INPUT_GITHUBSERVER'] = 'true';
settings.fillServerForGithub(xml);
const xmlStr = new XMLSerializer().serializeToString(xml).replace(/^ $/mg, '');
expect(xmlStr).toBe(`<servers>
expect(xmlAsString(xml)).toBe(`<servers>
<server>
<id>github</id>
<username>\${env.GITHUB_ACTOR}</username>
<password>\${env.GITHUB_TOKEN}</password>
</server></servers>`);
expect(consoleOutput).toEqual([]);
});
test('fillMirrors do nothing if no params', () => {
const xml = new DOMParser().parseFromString("<mirrors/>");
const xml = stringAsXml("<mirrors/>");
settings.fillMirrors(xml);
@ -337,7 +374,7 @@ test('fillMirrors do nothing if no params', () => {
test('fillMirrors one mirror', () => {
const xml = new DOMParser().parseFromString("<mirrors/>");
const xml = stringAsXml("<mirrors/>");
process.env['INPUT_MIRRORS'] = '[{"id": "id1", "name": "name", "mirrorOf":"mirrorOf", "url":"url"}]';
@ -357,7 +394,7 @@ test('fillMirrors one mirror', () => {
test('fillMirrors two mirrors', () => {
const xml = new DOMParser().parseFromString("<mirrors/>");
const xml = stringAsXml("<mirrors/>");
process.env['INPUT_MIRRORS'] = '[{"id": "id1", "name": "name1", "mirrorOf":"mirrorOf1", "url":"url1"},{"id": "id2", "name": "name2", "mirrorOf":"mirrorOf2", "url":"url2"}]';
@ -383,7 +420,7 @@ test('fillMirrors two mirrors', () => {
test('fillMirrors incorrect fields', () => {
const xml = new DOMParser().parseFromString("<mirrors/>");
const xml = stringAsXml("<mirrors/>");
process.env['INPUT_MIRRORS'] = '[{"idx": "id1"}]';
@ -403,7 +440,7 @@ test('addApacheSnapshots', () => {
process.env['INPUT_APACHESNAPSHOTS'] = "true";
const xml = new DOMParser().parseFromString('<profiles/>');
const xml = stringAsXml('<profiles/>');
settings.addApacheSnapshots(xml);
@ -445,7 +482,7 @@ test('addSonatypeSnapshots', () => {
process.env['INPUT_SONATYPESNAPSHOTS'] = "true";
const xml = new DOMParser().parseFromString('<profiles/>');
const xml = stringAsXml('<profiles/>');
settings.addSonatypeSnapshots(xml);
@ -487,7 +524,7 @@ test('addOracleRepo', () => {
process.env['INPUT_ORACLEREPO'] = "true";
const xml = new DOMParser().parseFromString('<profiles/>');
const xml = stringAsXml('<profiles/>');
settings.addOracleRepo(xml);
@ -529,7 +566,7 @@ test('fillProperties', () => {
process.env['INPUT_PROPERTIES'] = '[{"propertyName1": "propertyValue1"}, {"propertyName2": "propertyValue2"}]';
const xml = new DOMParser().parseFromString('<profiles/>');
const xml = stringAsXml('<profiles/>');
settings.fillProperties(xml);