diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index dcf38551..6d5110f4 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -5,11 +5,22 @@ on: jobs: test: - runs-on: ubuntu-latest + strategy: + matrix: + os: [ubuntu-latest, windows-latest, macOS-latest] + fail-fast: false + runs-on: ${{ matrix.os }} + steps: - uses: actions/checkout@v1 + - uses: actions/setup-node@v1 + with: + node-version: '12.x' + - run: npm version - run: npm ci + - run: npm audit - run: npm test + - uses: ./ with: servers: '[{"id": "serverId", "username": "username", "password": "password"}]' @@ -17,3 +28,4 @@ jobs: sonatypeSnapshots: true - run: cat ~/.m2/settings.xml + shell: bash diff --git a/README.md b/README.md index f2727d1e..d4828ae5 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,5 @@ # maven-settings-action +[![Test Action](https://github.com/s4u/maven-settings-action/workflows/Test%20Action/badge.svg)](https://github.com/s4u/maven-settings-action/actions) This action setup maven environment for use in action by: - create maven setings.xml diff --git a/index.js b/index.js index 7cfeb72c..2f9a2183 100644 --- a/index.js +++ b/index.js @@ -1,6 +1,7 @@ const core = require('@actions/core'); const path = require('path'); const fs = require('fs'); +const os = require('os'); const settings = require('./settings'); @@ -8,10 +9,11 @@ const settings = require('./settings'); async function run() { try { - const settingsPath = path.join(process.env.HOME, '.m2', 'settings.xml'); - core.info('Prepare maven setings: ' + settingsPath); + const settingsPath = path.join(os.homedir(), '.m2', 'settings.xml'); - if ( fs.existsSync(settingsPath) ) { + core.info('Prepare maven setings: ' + settingsPath); + + if (fs.existsSync(settingsPath)) { core.warning('maven settings.xml already exists - skip'); return; } diff --git a/index.test.js b/index.test.js index e6076432..3415e3d4 100644 --- a/index.test.js +++ b/index.test.js @@ -3,7 +3,7 @@ const cp = require('child_process'); const path = require('path'); const fs = require('fs'); -const testHomePath = path.join(__dirname, 'temp'); +const testHomePath = fs.mkdtempSync(".m2"); const settingsPath = path.join(testHomePath, '.m2', 'settings.xml'); const indexPath = path.join(__dirname, 'index.js'); @@ -13,22 +13,21 @@ beforeAll(() => { } process.env['HOME'] = testHomePath; + process.env['USERPROFILE'] = testHomePath; }); afterEach(() => { try { fs.unlinkSync(settingsPath); } catch (error) { - console.error(error.message); } }); afterAll(() => { try { - fs.rmdirSync(path.join(testHomePath, ".m2")); + fs.rmdirSync(path.dirname(settingsPath)); fs.rmdirSync(testHomePath); } catch (error) { - console.error(error.message); } }); diff --git a/package-lock.json b/package-lock.json index 58f81d6b..9147ca38 100644 --- a/package-lock.json +++ b/package-lock.json @@ -5,9 +5,9 @@ "requires": true, "dependencies": { "@actions/core": { - "version": "1.1.2", - "resolved": "https://registry.npmjs.org/@actions/core/-/core-1.1.2.tgz", - "integrity": "sha512-l8Ol2wEzFeuhV1v7LbGB2x8ghXSnmUiSVKVSjsNwgqAU4i5LvGRXpM+kli9gNMkGVudwZYHET4qZ5DTep5Sssw==" + "version": "1.1.3", + "resolved": "https://registry.npmjs.org/@actions/core/-/core-1.1.3.tgz", + "integrity": "sha512-2BIib53Jh4Cfm+1XNuZYYGTeRo8yiWEAUMoliMh1qQGMaqTF4VUlhhcsBylTu4qWmUx45DrY0y0XskimAHSqhw==" }, "@babel/code-frame": { "version": "7.5.5", diff --git a/package.json b/package.json index 71875689..edaf0977 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ "main": "index.js", "scripts": { "lint": "eslint index.js settings.js", - "test": "eslint index.js settings.js && jest" + "test": "eslint index.js settings.js && jest --coverage" }, "repository": { "type": "git", @@ -24,7 +24,7 @@ }, "homepage": "https://github.com/s4u/maven-settings-action#readme", "dependencies": { - "@actions/core": "^1.1.2", + "@actions/core": "^1.1.3", "xmldom": "^0.1.27", "xpath": "0.0.27" }, diff --git a/settings.test.js b/settings.test.js index 6dde1b5d..a8e89715 100644 --- a/settings.test.js +++ b/settings.test.js @@ -1,11 +1,16 @@ 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(` @@ -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', () => { const template = settings.getSettingsTemplate(); @@ -34,6 +61,19 @@ test('template should be read', () => { 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(""); @@ -43,7 +83,6 @@ test('fillServers do nothing if no params', () => { const xmlStr = new XMLSerializer().serializeToString(xml); expect(xmlStr).toBe(""); - }); test('fillServers one server', () => { @@ -60,7 +99,6 @@ test('fillServers one server', () => { expect(xmlStr).toBe("" + "id1username1password1" + ""); - }); test('fillServers two servers', () => { @@ -131,3 +169,27 @@ test('fillProperties', () => { `); }) + +test('fillProperties do nothing if no params', () => { + + settings.fillProperties(xmlTestProfile); + + const xmlStr = new XMLSerializer().serializeToString(xmlTestProfile); + expect(xmlStr).toBe(` + + + _properties_ + + false + + + + + _sonatype-snapshots_ + + false + + + + `); +})