Skip to content

Commit

Permalink
More work on vm2 support
Browse files Browse the repository at this point in the history
  • Loading branch information
orta committed Aug 11, 2017
1 parent a7f7249 commit e2e8a7d
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 36 deletions.
25 changes: 18 additions & 7 deletions source/runner/DangerfileRunnerTwo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
}

Expand Down
65 changes: 36 additions & 29 deletions source/runner/_tests/_danger_runner_two.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: [],
Expand All @@ -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" }],
Expand All @@ -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()
Expand All @@ -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: [],
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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")
})
Expand Down
14 changes: 14 additions & 0 deletions source/runner/_tests/fixtures/__DangerfileAsync.ts
Original file line number Diff line number Diff line change
@@ -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")
})

0 comments on commit e2e8a7d

Please sign in to comment.