Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Creates e2e tests #26

Merged
merged 1 commit into from
Sep 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,12 @@ jobs:
run: npm ci

- name: 'Test'
run: |
npm test -- --reporter json --reporter-options output=${{ runner.temp }}/report-${{ matrix.os }}.json
cat ${{ runner.temp }}/report-${{ matrix.os }}.json | jq
run: npm test -- --reporter json --reporter-options output=${{ runner.temp }}/report-${{ matrix.os }}.json

- name: 'Print Tests'
# run this step even if previous step failed
if: (success() || failure())
run: cat ${{ runner.temp }}/report-${{ matrix.os }}.json | jq

- name: 'Publish Tests'
uses: 'dorny/test-reporter@v1'
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"main": "dist/index.js",
"scripts": {
"build": "ncc build -o dist src/index.js",
"test": "mocha --reporter spec --timeout 5000"
"test": "mocha --reporter spec --timeout 5000 test/**.spec.js"
},
"repository": {
"type": "git",
Expand Down
18 changes: 18 additions & 0 deletions test/bootstrap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
'use strict';

// Convert all of the arguments into environment variables to get
// around hyphenated names being problematic on non-windows platforms.
const args = process.argv.splice(2);
for (let i = 0; i < args.length; i += 2) {
const key = args[i];
const value = args[i + 1];
if (!value) {
console.warn('missing arg value for', key);
continue;
}

console.log('assigning arg to env', key, value);
process.env[key] = value;
}

require('../dist');
117 changes: 117 additions & 0 deletions test/e2e.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
'use strict';

const assert = require('assert');
const { exec } = require('child_process');
const { access, unlink } = require('fs/promises');
const path = require('path');

const exists = async (path) => {
try {
await access(path);
return true;
}
catch {
return false;
}
};

/**
* @param {string} path
*/
const unlinkIfExistsAsync = async (path) => {
if (await exists(path)) {
unlink(path);
}
};

describe('e2e', () => {
before(async () => {
const promises = ['./basic.exe', './with-plugins.exe']
.map(unlinkIfExistsAsync);

await Promise.all(promises);
});

/**
* @typedef {{
* customArguments?: string,
* additionalPluginPaths?: string[],
* scriptFile?: string,
* }} RunOptions
* @param {RunOptions} options
*/
const run = async (options = {}) => {
const {
customArguments,
additionalPluginPaths,
scriptFile
} = options;

const args = [];
const env = {
...process.env,
debug: 'true',
};
if (customArguments) {
args.push('INPUT_ARGUMENTS', customArguments);
}
if (additionalPluginPaths && additionalPluginPaths.length) {
args.push('INPUT_ADDITIONAL-PLUGIN-PATHS', additionalPluginPaths.join('\n'));
}
if (scriptFile) {
args.push('INPUT_SCRIPT-FILE', scriptFile);
}

// Call bootstrap.js do avoid a problem where hyphenated environment
// variables are unable to be assigned on non-windows platforms.
const programPath = require.resolve('./bootstrap');
const testDir = path.dirname(programPath);
const cwd = path.join(testDir, '../');
const promise = new Promise((resolve, reject) => {
exec(`node ${programPath} ${args.join(' ')}`, {
env,
cwd,
}, (error, stdout, stderr) => {
console.log('cwd', cwd);
console.log('stdout', stdout);
console.log('stderr', stderr);
if (error) {
reject(error);
} else {
resolve();
}
});
});

await promise;
};

/**
* @param {string} script
* @param {(options: RunOptions) => void} fn
*/
const test = (script, fn) => {
it(`should create installer for ${script}.nsi`, async () => {
const options = {
scriptFile: `./test/${script}.nsi`
};
if (fn) {
fn(options);
}

await run(options);

const actual = await exists(`./test/${script}.exe`);

assert(
actual,
`Installer \`./test/${script}.exe\` should exist`
);
});
};

test('basic');
test('with-plugins', options => options.additionalPluginPaths = [
'./test/EnVar'
]);
});