Skip to content

Commit

Permalink
add test of action directly, mocking @actions/core
Browse files Browse the repository at this point in the history
  • Loading branch information
haarg committed Sep 14, 2024
1 parent 320d951 commit cd9dff1
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 6 deletions.
11 changes: 11 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"esbuild": "^0.23.1",
"eslint": "^9.9.1",
"eslint-plugin-mocha": "^10.5.0",
"esmock": "^2.6.7",
"globals": "^15.9.0",
"mocha": "^10.7.3",
"peggy": "^4.0.3"
Expand Down
17 changes: 11 additions & 6 deletions src/main.mjs
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import * as core from '@actions/core';
import core from '@actions/core';
import { getPrereqs } from './get-prereqs.mjs';
import { cpanmVersion, dottedVersion } from './cpan-versions.mjs';

const run = async () => {
const phases = core.getInput('phases').split(/\s+/);
const relationships = core.getInput('relationships').split(/\s+/);
const features = core.getInput('features').split(/\s+/);
const sources = core.getInput('sources').split(/\s+/);
const phasesInput = core.getInput('phases');
const relationshipsInput = core.getInput('relationships');
const featuresInput = core.getInput('features');
const sourcesInput = core.getInput('sources');

const phases = new Set(phasesInput.split(/\s+/));
const relationships = new Set(relationshipsInput.split(/\s+/));
const features = new Set(featuresInput.split(/\s+/));
const sources = sourcesInput.split(/\s+/);

const { perl, ...prereqs } = await getPrereqs({
phases,
Expand All @@ -31,7 +36,7 @@ const run = async () => {
core.setOutput('prereqsJSON', JSON.stringify(prereqs));
};

(async () => {
await (async () => {
try {
await run();
}
Expand Down
44 changes: 44 additions & 0 deletions test/action.test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { describe, it } from 'mocha';
import { expect } from 'chai';

import { dirname, join as joinPath } from 'node:path';
import { fileURLToPath } from 'url';

import esmock from 'esmock';

const __dirname = dirname(fileURLToPath(import.meta.url));

const main = async (inputs) => {
const outputs = {};
let failed;
await esmock('../src/main.mjs', {
'@actions/core': {
getInput: key => (inputs[key] || '').trim(),
getMultilineInput: key => (inputs[key] || '').split(/\n/).map(t => t.trim()),
setOutput: (key, value) => { outputs[key] = value; },
setFailed: (message) => { failed = message; },
},
});
if (failed) {
throw new Error(failed);
}
return {
outputs,
};
};

describe('GitHub action', function () {
it('runs', async function () {
const { outputs } = await main({
sources: joinPath(__dirname, 'corpus', 'META.json'),
phases: 'test',
relationships: 'recommends',
features: '',
});
expect(outputs).to.be.deep.equal({
'prereqs': 'CPAN::Meta~2.120900\n',
'prereqs-no-version': 'CPAN::Meta\n',
'prereqsJSON': '{"CPAN::Meta":">=2.120900"}',
});
});
});

0 comments on commit cd9dff1

Please sign in to comment.