Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 38e3ffe

Browse files
committed
Add tests for the AsyncFunction
1 parent 6eefe48 commit 38e3ffe

File tree

7 files changed

+5147
-568
lines changed

7 files changed

+5147
-568
lines changed

__test__/async-function.test.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import {callAsyncFunction} from '../src/async-function'
2+
3+
describe('callAsyncFunction', () => {
4+
test('calls the function with its arguments', async () => {
5+
const result = await callAsyncFunction({foo: 'bar'}, 'return foo')
6+
expect(result).toEqual('bar')
7+
})
8+
9+
test('throws on ReferenceError', async () => {
10+
expect.assertions(1)
11+
12+
try {
13+
await callAsyncFunction({}, 'proces')
14+
} catch (err) {
15+
expect(err).toBeInstanceOf(ReferenceError)
16+
}
17+
})
18+
19+
test('can access process', async () => {
20+
await callAsyncFunction({}, 'process')
21+
})
22+
23+
test('can access console', async () => {
24+
await callAsyncFunction({}, 'console')
25+
})
26+
})

dist/index.js

Lines changed: 101 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ module.exports =
3434
/******/ // the startup function
3535
/******/ function startup() {
3636
/******/ // Load entry module and return exports
37-
/******/ return __webpack_require__(131);
37+
/******/ return __webpack_require__(833);
3838
/******/ };
3939
/******/ // initialize runtime
4040
/******/ runtime(__webpack_require__);
@@ -1442,64 +1442,6 @@ module.exports = uniq;
14421442

14431443
module.exports = require("child_process");
14441444

1445-
/***/ }),
1446-
1447-
/***/ 131:
1448-
/***/ (function(__unusedmodule, __webpack_exports__, __webpack_require__) {
1449-
1450-
"use strict";
1451-
__webpack_require__.r(__webpack_exports__);
1452-
/* harmony import */ var _actions_core__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(470);
1453-
/* harmony import */ var _actions_core__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(_actions_core__WEBPACK_IMPORTED_MODULE_0__);
1454-
/* harmony import */ var _actions_github__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(469);
1455-
/* harmony import */ var _actions_github__WEBPACK_IMPORTED_MODULE_1___default = /*#__PURE__*/__webpack_require__.n(_actions_github__WEBPACK_IMPORTED_MODULE_1__);
1456-
1457-
1458-
process.on('unhandledRejection', handleError);
1459-
main().catch(handleError);
1460-
async function main() {
1461-
const AsyncFunction = Object.getPrototypeOf(async () => { }).constructor;
1462-
const token = _actions_core__WEBPACK_IMPORTED_MODULE_0__.getInput('github-token', { required: true });
1463-
const debug = _actions_core__WEBPACK_IMPORTED_MODULE_0__.getInput('debug');
1464-
const userAgent = _actions_core__WEBPACK_IMPORTED_MODULE_0__.getInput('user-agent');
1465-
const previews = _actions_core__WEBPACK_IMPORTED_MODULE_0__.getInput('previews');
1466-
const opts = {};
1467-
if (debug === 'true')
1468-
opts.log = console;
1469-
if (userAgent != null)
1470-
opts.userAgent = userAgent;
1471-
if (previews != null)
1472-
opts.previews = previews.split(',');
1473-
const client = new _actions_github__WEBPACK_IMPORTED_MODULE_1__.GitHub(token, opts);
1474-
const script = _actions_core__WEBPACK_IMPORTED_MODULE_0__.getInput('script', { required: true });
1475-
const fn = new AsyncFunction('require', 'github', 'context', script);
1476-
const result = await fn(__webpack_require__(875), client, _actions_github__WEBPACK_IMPORTED_MODULE_1__.context);
1477-
let encoding = _actions_core__WEBPACK_IMPORTED_MODULE_0__.getInput('result-encoding');
1478-
encoding = encoding ? encoding : 'json';
1479-
let output;
1480-
switch (encoding) {
1481-
case 'json':
1482-
output = JSON.stringify(result);
1483-
break;
1484-
case 'string':
1485-
output = String(result);
1486-
break;
1487-
default:
1488-
throw new Error('"result-encoding" must be either "string" or "json"');
1489-
}
1490-
_actions_core__WEBPACK_IMPORTED_MODULE_0__.setOutput('result', output);
1491-
}
1492-
function handleError(err) {
1493-
console.error(err);
1494-
if (err && err.message) {
1495-
_actions_core__WEBPACK_IMPORTED_MODULE_0__.setFailed(err.message);
1496-
}
1497-
else {
1498-
_actions_core__WEBPACK_IMPORTED_MODULE_0__.setFailed(`Unhandled error: ${err}`);
1499-
}
1500-
}
1501-
1502-
15031445
/***/ }),
15041446

15051447
/***/ 141:
@@ -2089,7 +2031,7 @@ exports.getUserAgent = getUserAgent;
20892031
/***/ 215:
20902032
/***/ (function(module) {
20912033

2092-
module.exports = {"_from":"@octokit/rest@^16.43.1","_id":"@octokit/[email protected]","_inBundle":false,"_integrity":"sha512-gfFKwRT/wFxq5qlNjnW2dh+qh74XgTQ2B179UX5K1HYCluioWj8Ndbgqw2PVqa1NnVJkGHp2ovMpVn/DImlmkw==","_location":"/@octokit/rest","_phantomChildren":{},"_requested":{"type":"range","registry":true,"raw":"@octokit/rest@^16.43.1","name":"@octokit/rest","escapedName":"@octokit%2frest","scope":"@octokit","rawSpec":"^16.43.1","saveSpec":null,"fetchSpec":"^16.43.1"},"_requiredBy":["/@actions/github"],"_resolved":"https://registry.npmjs.org/@octokit/rest/-/rest-16.43.1.tgz","_shasum":"3b11e7d1b1ac2bbeeb23b08a17df0b20947eda6b","_spec":"@octokit/rest@^16.43.1","_where":"/Users/jclem/src/github.com/actions/github-script/node_modules/@actions/github","author":{"name":"Gregor Martynus","url":"https://github.com/gr2m"},"bugs":{"url":"https://github.com/octokit/rest.js/issues"},"bundleDependencies":false,"bundlesize":[{"path":"./dist/octokit-rest.min.js.gz","maxSize":"33 kB"}],"contributors":[{"name":"Mike de Boer","email":"[email protected]"},{"name":"Fabian Jakobs","email":"[email protected]"},{"name":"Joe Gallo","email":"[email protected]"},{"name":"Gregor Martynus","url":"https://github.com/gr2m"}],"dependencies":{"@octokit/auth-token":"^2.4.0","@octokit/plugin-paginate-rest":"^1.1.1","@octokit/plugin-request-log":"^1.0.0","@octokit/plugin-rest-endpoint-methods":"2.4.0","@octokit/request":"^5.2.0","@octokit/request-error":"^1.0.2","atob-lite":"^2.0.0","before-after-hook":"^2.0.0","btoa-lite":"^1.0.0","deprecation":"^2.0.0","lodash.get":"^4.4.2","lodash.set":"^4.3.2","lodash.uniq":"^4.5.0","octokit-pagination-methods":"^1.1.0","once":"^1.4.0","universal-user-agent":"^4.0.0"},"deprecated":false,"description":"GitHub REST API client for Node.js","devDependencies":{"@gimenete/type-writer":"^0.1.3","@octokit/auth":"^1.1.1","@octokit/fixtures-server":"^5.0.6","@octokit/graphql":"^4.2.0","@types/node":"^13.1.0","bundlesize":"^0.18.0","chai":"^4.1.2","compression-webpack-plugin":"^3.1.0","cypress":"^3.0.0","glob":"^7.1.2","http-proxy-agent":"^4.0.0","lodash.camelcase":"^4.3.0","lodash.merge":"^4.6.1","lodash.upperfirst":"^4.3.1","lolex":"^5.1.2","mkdirp":"^1.0.0","mocha":"^7.0.1","mustache":"^4.0.0","nock":"^11.3.3","npm-run-all":"^4.1.2","nyc":"^15.0.0","prettier":"^1.14.2","proxy":"^1.0.0","semantic-release":"^17.0.0","sinon":"^8.0.0","sinon-chai":"^3.0.0","sort-keys":"^4.0.0","string-to-arraybuffer":"^1.0.0","string-to-jsdoc-comment":"^1.0.0","typescript":"^3.3.1","webpack":"^4.0.0","webpack-bundle-analyzer":"^3.0.0","webpack-cli":"^3.0.0"},"files":["index.js","index.d.ts","lib","plugins"],"homepage":"https://github.com/octokit/rest.js#readme","keywords":["octokit","github","rest","api-client"],"license":"MIT","name":"@octokit/rest","nyc":{"ignore":["test"]},"publishConfig":{"access":"public"},"release":{"publish":["@semantic-release/npm",{"path":"@semantic-release/github","assets":["dist/*","!dist/*.map.gz"]}]},"repository":{"type":"git","url":"git+https://github.com/octokit/rest.js.git"},"scripts":{"build":"npm-run-all build:*","build:browser":"npm-run-all build:browser:*","build:browser:development":"webpack --mode development --entry . --output-library=Octokit --output=./dist/octokit-rest.js --profile --json > dist/bundle-stats.json","build:browser:production":"webpack --mode production --entry . --plugin=compression-webpack-plugin --output-library=Octokit --output-path=./dist --output-filename=octokit-rest.min.js --devtool source-map","build:ts":"npm run -s update-endpoints:typescript","coverage":"nyc report --reporter=html && open coverage/index.html","generate-bundle-report":"webpack-bundle-analyzer dist/bundle-stats.json --mode=static --no-open --report dist/bundle-report.html","lint":"prettier --check '{lib,plugins,scripts,test}/**/*.{js,json,ts}' 'docs/*.{js,json}' 'docs/src/**/*' index.js README.md package.json","lint:fix":"prettier --write '{lib,plugins,scripts,test}/**/*.{js,json,ts}' 'docs/*.{js,json}' 'docs/src/**/*' index.js README.md package.json","postvalidate:ts":"tsc --noEmit --target es6 test/typescript-validate.ts","prebuild:browser":"mkdirp dist/","pretest":"npm run -s lint","prevalidate:ts":"npm run -s build:ts","start-fixtures-server":"octokit-fixtures-server","test":"nyc mocha test/mocha-node-setup.js \"test/*/**/*-test.js\"","test:browser":"cypress run --browser chrome","update-endpoints":"npm-run-all update-endpoints:*","update-endpoints:fetch-json":"node scripts/update-endpoints/fetch-json","update-endpoints:typescript":"node scripts/update-endpoints/typescript","validate:ts":"tsc --target es6 --noImplicitAny index.d.ts"},"types":"index.d.ts","version":"16.43.1"};
2034+
module.exports = {"name":"@octokit/rest","version":"16.43.1","publishConfig":{"access":"public"},"description":"GitHub REST API client for Node.js","keywords":["octokit","github","rest","api-client"],"author":"Gregor Martynus (https://github.com/gr2m)","contributors":[{"name":"Mike de Boer","email":"[email protected]"},{"name":"Fabian Jakobs","email":"[email protected]"},{"name":"Joe Gallo","email":"[email protected]"},{"name":"Gregor Martynus","url":"https://github.com/gr2m"}],"repository":"https://github.com/octokit/rest.js","dependencies":{"@octokit/auth-token":"^2.4.0","@octokit/plugin-paginate-rest":"^1.1.1","@octokit/plugin-request-log":"^1.0.0","@octokit/plugin-rest-endpoint-methods":"2.4.0","@octokit/request":"^5.2.0","@octokit/request-error":"^1.0.2","atob-lite":"^2.0.0","before-after-hook":"^2.0.0","btoa-lite":"^1.0.0","deprecation":"^2.0.0","lodash.get":"^4.4.2","lodash.set":"^4.3.2","lodash.uniq":"^4.5.0","octokit-pagination-methods":"^1.1.0","once":"^1.4.0","universal-user-agent":"^4.0.0"},"devDependencies":{"@gimenete/type-writer":"^0.1.3","@octokit/auth":"^1.1.1","@octokit/fixtures-server":"^5.0.6","@octokit/graphql":"^4.2.0","@types/node":"^13.1.0","bundlesize":"^0.18.0","chai":"^4.1.2","compression-webpack-plugin":"^3.1.0","cypress":"^3.0.0","glob":"^7.1.2","http-proxy-agent":"^4.0.0","lodash.camelcase":"^4.3.0","lodash.merge":"^4.6.1","lodash.upperfirst":"^4.3.1","lolex":"^5.1.2","mkdirp":"^1.0.0","mocha":"^7.0.1","mustache":"^4.0.0","nock":"^11.3.3","npm-run-all":"^4.1.2","nyc":"^15.0.0","prettier":"^1.14.2","proxy":"^1.0.0","semantic-release":"^17.0.0","sinon":"^8.0.0","sinon-chai":"^3.0.0","sort-keys":"^4.0.0","string-to-arraybuffer":"^1.0.0","string-to-jsdoc-comment":"^1.0.0","typescript":"^3.3.1","webpack":"^4.0.0","webpack-bundle-analyzer":"^3.0.0","webpack-cli":"^3.0.0"},"types":"index.d.ts","scripts":{"coverage":"nyc report --reporter=html && open coverage/index.html","lint":"prettier --check '{lib,plugins,scripts,test}/**/*.{js,json,ts}' 'docs/*.{js,json}' 'docs/src/**/*' index.js README.md package.json","lint:fix":"prettier --write '{lib,plugins,scripts,test}/**/*.{js,json,ts}' 'docs/*.{js,json}' 'docs/src/**/*' index.js README.md package.json","pretest":"npm run -s lint","test":"nyc mocha test/mocha-node-setup.js \"test/*/**/*-test.js\"","test:browser":"cypress run --browser chrome","build":"npm-run-all build:*","build:ts":"npm run -s update-endpoints:typescript","prebuild:browser":"mkdirp dist/","build:browser":"npm-run-all build:browser:*","build:browser:development":"webpack --mode development --entry . --output-library=Octokit --output=./dist/octokit-rest.js --profile --json > dist/bundle-stats.json","build:browser:production":"webpack --mode production --entry . --plugin=compression-webpack-plugin --output-library=Octokit --output-path=./dist --output-filename=octokit-rest.min.js --devtool source-map","generate-bundle-report":"webpack-bundle-analyzer dist/bundle-stats.json --mode=static --no-open --report dist/bundle-report.html","update-endpoints":"npm-run-all update-endpoints:*","update-endpoints:fetch-json":"node scripts/update-endpoints/fetch-json","update-endpoints:typescript":"node scripts/update-endpoints/typescript","prevalidate:ts":"npm run -s build:ts","validate:ts":"tsc --target es6 --noImplicitAny index.d.ts","postvalidate:ts":"tsc --noEmit --target es6 test/typescript-validate.ts","start-fixtures-server":"octokit-fixtures-server"},"license":"MIT","files":["index.js","index.d.ts","lib","plugins"],"nyc":{"ignore":["test"]},"release":{"publish":["@semantic-release/npm",{"path":"@semantic-release/github","assets":["dist/*","!dist/*.map.gz"]}]},"bundlesize":[{"path":"./dist/octokit-rest.min.js.gz","maxSize":"33 kB"}],"_resolved":"https://registry.npmjs.org/@octokit/rest/-/rest-16.43.1.tgz","_integrity":"sha512-gfFKwRT/wFxq5qlNjnW2dh+qh74XgTQ2B179UX5K1HYCluioWj8Ndbgqw2PVqa1NnVJkGHp2ovMpVn/DImlmkw==","_from":"@octokit/[email protected]"};
20932035

20942036
/***/ }),
20952037

@@ -9194,6 +9136,75 @@ function sync (path, options) {
91949136
}
91959137

91969138

9139+
/***/ }),
9140+
9141+
/***/ 833:
9142+
/***/ (function(__unusedmodule, __webpack_exports__, __webpack_require__) {
9143+
9144+
"use strict";
9145+
__webpack_require__.r(__webpack_exports__);
9146+
9147+
// EXTERNAL MODULE: ./node_modules/@actions/core/lib/core.js
9148+
var core = __webpack_require__(470);
9149+
9150+
// EXTERNAL MODULE: ./node_modules/@actions/github/lib/github.js
9151+
var lib_github = __webpack_require__(469);
9152+
9153+
// CONCATENATED MODULE: ./src/async-function.ts
9154+
const AsyncFunction = Object.getPrototypeOf(async () => { }).constructor;
9155+
async function callAsyncFunction(args, source) {
9156+
const fn = new AsyncFunction(...Object.keys(args), source);
9157+
return fn(...Object.values(args));
9158+
}
9159+
9160+
// CONCATENATED MODULE: ./src/main.ts
9161+
9162+
9163+
9164+
process.on('unhandledRejection', handleError);
9165+
main().catch(handleError);
9166+
async function main() {
9167+
const token = Object(core.getInput)('github-token', { required: true });
9168+
const debug = Object(core.getInput)('debug');
9169+
const userAgent = Object(core.getInput)('user-agent');
9170+
const previews = Object(core.getInput)('previews');
9171+
const opts = {};
9172+
if (debug === 'true')
9173+
opts.log = console;
9174+
if (userAgent != null)
9175+
opts.userAgent = userAgent;
9176+
if (previews != null)
9177+
opts.previews = previews.split(',');
9178+
const github = new lib_github.GitHub(token, opts);
9179+
const script = Object(core.getInput)('script', { required: true });
9180+
// Using property/value shorthand on `require` (e.g. `{require}`) causes compilatin errors.
9181+
const result = callAsyncFunction({ require: __webpack_require__(875), github: github, context: lib_github.context }, script);
9182+
let encoding = Object(core.getInput)('result-encoding');
9183+
encoding = encoding ? encoding : 'json';
9184+
let output;
9185+
switch (encoding) {
9186+
case 'json':
9187+
output = JSON.stringify(result);
9188+
break;
9189+
case 'string':
9190+
output = String(result);
9191+
break;
9192+
default:
9193+
throw new Error('"result-encoding" must be either "string" or "json"');
9194+
}
9195+
Object(core.setOutput)('result', output);
9196+
}
9197+
function handleError(err) {
9198+
console.error(err);
9199+
if (err && err.message) {
9200+
Object(core.setFailed)(err.message);
9201+
}
9202+
else {
9203+
Object(core.setFailed)(`Unhandled error: ${err}`);
9204+
}
9205+
}
9206+
9207+
91979208
/***/ }),
91989209

91999210
/***/ 835:
@@ -25309,6 +25320,36 @@ function onceStrict (fn) {
2530925320
/******/ };
2531025321
/******/ }();
2531125322
/******/
25323+
/******/ /* webpack/runtime/define property getter */
25324+
/******/ !function() {
25325+
/******/ // define getter function for harmony exports
25326+
/******/ var hasOwnProperty = Object.prototype.hasOwnProperty;
25327+
/******/ __webpack_require__.d = function(exports, name, getter) {
25328+
/******/ if(!hasOwnProperty.call(exports, name)) {
25329+
/******/ Object.defineProperty(exports, name, { enumerable: true, get: getter });
25330+
/******/ }
25331+
/******/ };
25332+
/******/ }();
25333+
/******/
25334+
/******/ /* webpack/runtime/create fake namespace object */
25335+
/******/ !function() {
25336+
/******/ // create a fake namespace object
25337+
/******/ // mode & 1: value is a module id, require it
25338+
/******/ // mode & 2: merge all properties of value into the ns
25339+
/******/ // mode & 4: return value when already ns object
25340+
/******/ // mode & 8|1: behave like require
25341+
/******/ __webpack_require__.t = function(value, mode) {
25342+
/******/ if(mode & 1) value = this(value);
25343+
/******/ if(mode & 8) return value;
25344+
/******/ if((mode & 4) && typeof value === 'object' && value && value.__esModule) return value;
25345+
/******/ var ns = Object.create(null);
25346+
/******/ __webpack_require__.r(ns);
25347+
/******/ Object.defineProperty(ns, 'default', { enumerable: true, value: value });
25348+
/******/ if(mode & 2 && typeof value != 'string') for(var key in value) __webpack_require__.d(ns, key, function(key) { return value[key]; }.bind(null, key));
25349+
/******/ return ns;
25350+
/******/ };
25351+
/******/ }();
25352+
/******/
2531225353
/******/ /* webpack/runtime/compat get default export */
2531325354
/******/ !function() {
2531425355
/******/ // getDefaultExport function for compatibility with non-harmony modules
@@ -25321,16 +25362,5 @@ function onceStrict (fn) {
2532125362
/******/ };
2532225363
/******/ }();
2532325364
/******/
25324-
/******/ /* webpack/runtime/define property getter */
25325-
/******/ !function() {
25326-
/******/ // define getter function for harmony exports
25327-
/******/ var hasOwnProperty = Object.prototype.hasOwnProperty;
25328-
/******/ __webpack_require__.d = function(exports, name, getter) {
25329-
/******/ if(!hasOwnProperty.call(exports, name)) {
25330-
/******/ Object.defineProperty(exports, name, { enumerable: true, get: getter });
25331-
/******/ }
25332-
/******/ };
25333-
/******/ }();
25334-
/******/
2533525365
/******/ }
2533625366
);

0 commit comments

Comments
 (0)