diff --git a/source/runner/DangerfileRunnerTwo.ts b/source/runner/DangerfileRunnerTwo.ts index c3bdee4a0..e03e267a2 100644 --- a/source/runner/DangerfileRunnerTwo.ts +++ b/source/runner/DangerfileRunnerTwo.ts @@ -7,6 +7,18 @@ import { Path } from "./types" import { NodeVM, NodeVMOptions } from "vm2" +let hasTypeScript = false +let hasBabel = false + +try { + require.resolve("typescript") + hasTypeScript = true +} catch (e) {} // tslint:disable-line +try { + require.resolve("babel-core") + hasBabel = true +} catch (e) {} // tslint:disable-line + /** * Executes a Dangerfile at a specific path, with a context. * The values inside a Danger context are applied as globals to the Dangerfiles runtime. @@ -26,19 +38,18 @@ export async function createDangerfileRuntimeEnvironment(dangerfileContext: Dang return { require: { external: true, + context: "sandbox", + builtin: ["*"], }, sandbox, compiler: (code, filename) => { const filetype = path.extname(filename) - // Add a check for TSC/babel etc - if (filetype.startsWith(".ts")) { - console.log("TSX") + if (filename.includes("node_modules")) { + return code + } else if (hasTypeScript && filetype.startsWith(".ts")) { return typescriptify(code) - } else if (filetype === ".js") { - // TODO: Check for babelrc - console.log("Babel") + } else if (hasBabel && filetype === ".js") { const output = babelify(code, filename) - console.log(output) return output } diff --git a/source/runner/_tests/_danger_runner_two.test.ts b/source/runner/_tests/_danger_runner_two.test.ts index fe8326bf4..a32c64b29 100644 --- a/source/runner/_tests/_danger_runner_two.test.ts +++ b/source/runner/_tests/_danger_runner_two.test.ts @@ -35,7 +35,7 @@ describe("with fixtures", () => { it("handles a blank Dangerfile", async () => { const context = await setupDangerfileContext() const runtime = await createDangerfileRuntimeEnvironment(context) - const results = await runDangerfileEnvironment(resolve(fixtures, "__DangerfileEmpty.js"), runtime, null) + const results = await runDangerfileEnvironment(resolve(fixtures, "__DangerfileEmpty.js"), runtime) expect(results).toEqual({ fails: [], @@ -48,7 +48,7 @@ describe("with fixtures", () => { it("handles a full set of messages", async () => { const context = await setupDangerfileContext() const runtime = await createDangerfileRuntimeEnvironment(context) - const results = await runDangerfileEnvironment(resolve(fixtures, "__DangerfileFullMessages.js"), runtime, null) + const results = await runDangerfileEnvironment(resolve(fixtures, "__DangerfileFullMessages.js"), runtime) expect(results).toEqual({ fails: [{ message: "this is a failure" }], @@ -63,7 +63,7 @@ describe("with fixtures", () => { const runtime = await createDangerfileRuntimeEnvironment(context) try { - await runDangerfileEnvironment(resolve(fixtures, "__DangerfileBadSyntax.js"), runtime, null) + await runDangerfileEnvironment(resolve(fixtures, "__DangerfileBadSyntax.js"), runtime) throw new Error("Do not get to this") } catch (e) { // expect(e.message === ("Do not get to this")).toBeFalsy() @@ -74,13 +74,13 @@ describe("with fixtures", () => { it("handles relative imports correctly", async () => { const context = await setupDangerfileContext() const runtime = await createDangerfileRuntimeEnvironment(context) - await runDangerfileEnvironment(resolve(fixtures, "__DangerfileImportRelative.js"), runtime, "typescript") + await runDangerfileEnvironment(resolve(fixtures, "__DangerfileImportRelative.js"), runtime) }) it("handles scheduled (async) code", async () => { const context = await setupDangerfileContext() const runtime = await createDangerfileRuntimeEnvironment(context) - const results = await runDangerfileEnvironment(resolve(fixtures, "__DangerfileScheduled.js"), runtime, "babel") + const results = await runDangerfileEnvironment(resolve(fixtures, "__DangerfileScheduled.js"), runtime) expect(results).toEqual({ fails: [], messages: [], @@ -101,28 +101,39 @@ describe("with fixtures", () => { }) }) - // This adds > 6 seconds to the tests! Only orta should be forced into that. - if (process.env["USER"] === "orta") { - it.only("can execute async/await scheduled functions", async () => { - // this test takes *forever* because of babel-polyfill being required - const context = await setupDangerfileContext() - const runtime = await createDangerfileRuntimeEnvironment(context) - const results = await runDangerfileEnvironment(resolve(fixtures, "__DangerfileAsync.js"), runtime) - expect(results.warnings).toEqual([ - { - message: "Async Function", - }, - { - message: "After Async Function", - }, - ]) - }) - } + it("in Typescript it handles multiple scheduled statements and all message types", async () => { + const context = await setupDangerfileContext() + const runtime = await createDangerfileRuntimeEnvironment(context) + const results = await runDangerfileEnvironment(resolve(fixtures, "__DangerfileAsync.ts"), runtime) + expect(results.warnings).toEqual([ + { + message: "Async Function", + }, + { + message: "After Async Function", + }, + ]) + }) + + it("in babel it can execute async/await scheduled functions", async () => { + // this test takes *forever* because of babel-polyfill being required + const context = await setupDangerfileContext() + const runtime = await createDangerfileRuntimeEnvironment(context) + const results = await runDangerfileEnvironment(resolve(fixtures, "__DangerfileAsync.js"), runtime) + expect(results.warnings).toEqual([ + { + message: "Async Function", + }, + { + message: "After Async Function", + }, + ]) + }) it("can schedule callback-based promised ", async () => { const context = await setupDangerfileContext() const runtime = await createDangerfileRuntimeEnvironment(context) - const results = await runDangerfileEnvironment(resolve(fixtures, "__DangerfileCallback.js"), runtime, "typescript") + const results = await runDangerfileEnvironment(resolve(fixtures, "__DangerfileCallback.js"), runtime) expect(results.warnings).toEqual([ { message: "Scheduled a callback", @@ -133,11 +144,7 @@ describe("with fixtures", () => { it("can handle TypeScript based Dangerfiles", async () => { const context = await setupDangerfileContext() const runtime = await createDangerfileRuntimeEnvironment(context) - const results = await runDangerfileEnvironment( - resolve(fixtures, "__DangerfileTypeScript.ts"), - runtime, - "typescript" - ) + const results = await runDangerfileEnvironment(resolve(fixtures, "__DangerfileTypeScript.ts"), runtime) expect(results.messages).toEqual([ { message: "Honey, we got Types", @@ -148,7 +155,7 @@ describe("with fixtures", () => { it("can handle a plugin (which is already used in Danger)", async () => { const context = await setupDangerfileContext() const runtime = await createDangerfileRuntimeEnvironment(context) - const results = await runDangerfileEnvironment(resolve(fixtures, "__DangerfilePlugin.js"), runtime, "babel") + const results = await runDangerfileEnvironment(resolve(fixtures, "__DangerfilePlugin.js"), runtime) expect(results.fails[0].message).toContain("@types dependencies were added to package.json") }) diff --git a/source/runner/_tests/fixtures/__DangerfileAsync.ts b/source/runner/_tests/fixtures/__DangerfileAsync.ts new file mode 100644 index 000000000..c88de8e50 --- /dev/null +++ b/source/runner/_tests/fixtures/__DangerfileAsync.ts @@ -0,0 +1,14 @@ +/* tslint-disable */ + +const asyncAction = () => + new Promise(res => { + setTimeout(() => { + warn("Async Function") + res() + }, 50) + }) + +schedule(async () => { + await asyncAction() + warn("After Async Function") +})