add test coverage

This commit is contained in:
Slawomir Jaranowski
2019-10-02 21:31:26 +02:00
parent b3d9019152
commit f803851b19
3 changed files with 67 additions and 7 deletions

View File

@ -3,7 +3,7 @@ const cp = require('child_process');
const path = require('path'); const path = require('path');
const fs = require('fs'); const fs = require('fs');
const testHomePath = path.join(__dirname, 'temp'); const testHomePath = fs.mkdtempSync(".m2");
const settingsPath = path.join(testHomePath, '.m2', 'settings.xml'); const settingsPath = path.join(testHomePath, '.m2', 'settings.xml');
const indexPath = path.join(__dirname, 'index.js'); const indexPath = path.join(__dirname, 'index.js');
@ -19,16 +19,14 @@ afterEach(() => {
try { try {
fs.unlinkSync(settingsPath); fs.unlinkSync(settingsPath);
} catch (error) { } catch (error) {
console.error(error.message);
} }
}); });
afterAll(() => { afterAll(() => {
try { try {
fs.rmdirSync(path.join(testHomePath, ".m2")); fs.rmdirSync(path.dirname(settingsPath));
fs.rmdirSync(testHomePath); fs.rmdirSync(testHomePath);
} catch (error) { } catch (error) {
console.error(error.message);
} }
}); });

View File

@ -5,7 +5,7 @@
"main": "index.js", "main": "index.js",
"scripts": { "scripts": {
"lint": "eslint index.js settings.js", "lint": "eslint index.js settings.js",
"test": "eslint index.js settings.js && jest" "test": "eslint index.js settings.js && jest --coverage"
}, },
"repository": { "repository": {
"type": "git", "type": "git",

View File

@ -1,11 +1,16 @@
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 path = require('path');
const settings = require('./settings'); const settings = require('./settings');
var xmlTestProfile = undefined; var xmlTestProfile = undefined;
const testHomePath = fs.mkdtempSync(".m2");
const settingsPath = path.join(testHomePath, '.m2', 'settings.xml');
beforeEach(() => { beforeEach(() => {
xmlTestProfile = new DOMParser().parseFromString(`<settings> xmlTestProfile = new DOMParser().parseFromString(`<settings>
<profiles> <profiles>
@ -27,6 +32,28 @@ beforeEach(() => {
}); });
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', () => { test('template should be read', () => {
const template = settings.getSettingsTemplate(); const template = settings.getSettingsTemplate();
@ -34,6 +61,19 @@ test('template should be read', () => {
expect(template).toBeDefined(); 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', () => { test('fillServers do nothing if no params', () => {
const xml = new DOMParser().parseFromString("<servers/>"); const xml = new DOMParser().parseFromString("<servers/>");
@ -43,7 +83,6 @@ test('fillServers do nothing if no params', () => {
const xmlStr = new XMLSerializer().serializeToString(xml); const xmlStr = new XMLSerializer().serializeToString(xml);
expect(xmlStr).toBe("<servers/>"); expect(xmlStr).toBe("<servers/>");
}); });
test('fillServers one server', () => { test('fillServers one server', () => {
@ -60,7 +99,6 @@ test('fillServers one server', () => {
expect(xmlStr).toBe("<servers>" + expect(xmlStr).toBe("<servers>" +
"<server><id>id1</id><username>username1</username><password>password1</password></server>" + "<server><id>id1</id><username>username1</username><password>password1</password></server>" +
"</servers>"); "</servers>");
}); });
test('fillServers two servers', () => { test('fillServers two servers', () => {
@ -131,3 +169,27 @@ test('fillProperties', () => {
</profiles> </profiles>
</settings>`); </settings>`);
}) })
test('fillProperties do nothing if no params', () => {
settings.fillProperties(xmlTestProfile);
const xmlStr = new XMLSerializer().serializeToString(xmlTestProfile);
expect(xmlStr).toBe(`<settings>
<profiles>
<profile>
<id>_properties_</id>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<properties/>
</profile>
<profile>
<id>_sonatype-snapshots_</id>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
</profile>
</profiles>
</settings>`);
})