Skip to content

Commit

Permalink
Adds test which validate the fixtures run correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
orta committed Oct 21, 2017
2 parents 1414c80 + 6c47f55 commit e735469
Show file tree
Hide file tree
Showing 34 changed files with 1,657 additions and 583 deletions.
10 changes: 8 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,15 @@ cache:

matrix:
include:
# Some normal CI test runs
- node_js: '8'
# Only normal CI test run :D
- node_js: '6'
- node_js: '8'

# Checks every example dangerfile can run in `danger runner`.
- node_js: '8.4'
script:
- yarn build
- node scripts/run-fixtures.js

# Does the real danger run
- node_js: node
Expand Down
37 changes: 20 additions & 17 deletions dangerfile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,43 @@
// This means we can re-use the type infra from the app, without having to
// fake the import.

// console.log(global)
// console.log(require)
// console.log(require.extensions)

import { DangerDSLType } from "./source/dsl/DangerDSL"
declare var danger: DangerDSLType
// declare var results: any
declare function warn(params: string): void
declare function fail(params: string): void
declare function message(params: string): void
// declare function message(params: string): void
// declare function markdown(params: string): void
// declare function schedule(promise: Promise<any | void>): void
// declare function schedule(promise: () => Promise<any | void>): void
// declare function schedule(callback: (resolve: any) => void): void
declare function schedule(promise: Promise<any | void>): void
declare function schedule(promise: () => Promise<any | void>): void
declare function schedule(callback: (resolve: any) => void): void

import * as fs from "fs"

message("Worked")

// Request a CHANGELOG entry if not declared #trivial
const hasChangelog = danger.git.modified_files.includes("changelog.md")
const isTrivial = (danger.github.pr.body + danger.github.pr.title).includes("#trivial")
const isGreenkeeper = danger.github.pr.user.login === "greenkeeper"
schedule(async () => {
// Request a CHANGELOG entry if not declared #trivial
const hasChangelog = danger.git.modified_files.includes("changelog.md")
const isTrivial = (danger.github.pr.body + danger.github.pr.title).includes("#trivial")
const isGreenkeeper = danger.github.pr.user.login === "greenkeeper"

if (!hasChangelog && !isTrivial && !isGreenkeeper) {
warn("Please add a changelog entry for your changes.")
if (!hasChangelog && !isTrivial && !isGreenkeeper) {
warn("Please add a changelog entry for your changes.")

// Politely ask for their name on the entry too
danger.git.diffForFile("changelog.md").then(changelogDiff => {
// Politely ask for their name on the entry too
const changelogDiff = await danger.git.diffForFile("changelog.md")
const contributorName = danger.github.pr.user.login
if (changelogDiff && changelogDiff.diff.includes(contributorName)) {
warn("Please add your GitHub name to the changelog entry, so we can attribute you correctly.")
}
})
}
}
})

import yarn from "danger-plugin-yarn"
yarn()
schedule(yarn())

import jest from "danger-plugin-jest"
jest()
Expand Down
20 changes: 13 additions & 7 deletions docs/usage/danger-process.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,21 @@ that feels natural in the environment of your app.
`danger process` expects one argument, the command to trigger the process for Danger JS to run. This command should
expect the Danger DSL as JSON in STDIN, and it is expected that it would post results to STDOUT via JSON.

You can preview the JSON using our Peril Staging environment, here are a few PRs:
You can preview the DSL which will be sent to your process via `danger pr` with the `--js` and `--json` flags, for example:

* [artsy/eidolon#609](https://peril-staging.herokuapp.com/api/v1/pr/dsl?owner=artsy&repo=eidolon&number=609)
* [danger/danger-swift#2](https://peril-staging.herokuapp.com/api/v1/pr/dsl?owner=danger&repo=danger-swift&number=2)
* [orta/danger-plugin-spellcheck#7](https://peril-staging.herokuapp.com/api/v1/pr/dsl?owner=orta&repo=danger-plugin-spellcheck&number=7)
```sh
danger pr https://github.com/danger/danger-js/pull/395 --js
```

This shows you DSL as a JavaScript object - this is easier to read and syntax highlighted, if you'd like a data fixture however, use `--json`:

*Note*: This DSL response does not include the GitHub API metadata, I plan to add this.
```sh
danger pr https://github.com/danger/danger-js/pull/395 --json > danger-js-395.dsl.json
```

You can change the PR params to an public repo's params to get a sense of what it looks like on your own PRs.
This will work for any open repo, and if you've set up your local shell to include `DANGER_GITHUB_API_TOKEN` then you can
use this with any private repository too. The JSON schema is documented in Danger JS's types as [DangerJSONDSLType][]. I
plan to add a full reference for this, similar to the reference for the user's DSL in the future in these docs.

A runner can output anything during the process to STDOUT, and it will be logged to the user. However, Danger JS is
listening for a JSON response in this format:
Expand Down Expand Up @@ -106,4 +112,4 @@ It's pretty likely that your CI already has node, so it can just be `npm install
[swift-dangerfile]: https://github.com/danger/danger-swift/blob/1576e336e41698861456533463c8821675427258/Dangerfile.swift
[swift-stdout]: https://github.com/danger/danger-swift/blob/1576e336e41698861456533463c8821675427258/Sources/Runner/main.swift#L48-L50
[swift-first-pr]: https://github.com/danger/danger-swift/pull/12

[DangerJSONDSLType]: https://github.com/danger/danger-js/blob/master/source/dsl/DangerDSL.ts
9 changes: 6 additions & 3 deletions scripts/danger-dts.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import * as fs from "fs"

const mapLines = (s: string, func: (s: string) => string) => s.split("\n").map(func).join("\n")
const mapLines = (s: string, func: (s: string) => string) =>
s
.split("\n")
.map(func)
.join("\n")

const createDTS = () => {
const header = `//
Expand All @@ -16,10 +20,9 @@ declare module "danger" {

let fileOutput = ""

const extras = ["source/platforms/messaging/violation.ts"]
const dslFiles = fs.readdirSync("source/dsl").map(f => `source/dsl/${f}`)

dslFiles.concat(extras).forEach(file => {
dslFiles.forEach(file => {
// Sometimes they have more stuff, in those cases
// offer a way to crop the file.
const content = fs.readFileSync(file).toString()
Expand Down
93 changes: 93 additions & 0 deletions scripts/run-fixtures.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
// Note: Keep this ES6 only, want to be able to run this directly via node
// to ensur that something like ts-node doesn't mess up paths etc

// yarn build; cat source/_tests/fixtures/danger-js-pr-395.json | env DANGER_FAKE_CI="YEP" DANGER_TEST_REPO='danger/danger-js' DANGER_TEST_PR='395' node --inspect distribution/commands/danger-runner.js --text-only --dangerfile /Users/orta/dev/projects/danger/danger-js/source/runner/_tests/fixtures/__DangerfileAsync.js

const fs = require("fs")
const child_process = require("child_process")
const { resolve } = require("path")
const chalk = require("chalk")
const expect = require("expect")

// Toggle this on to update the JSON files for each run
const writeResults = false

const runnerFileJS = "distribution/commands/danger-runner.js"

// Get all the fixtures
const dangerDSLFixture = resolve(__dirname, "../source/_tests/fixtures/danger-js-pr-395.json")
const dslJSON = fs.readFileSync(dangerDSLFixture, "utf8")

// Folders for the details
const dangerFileFixtures = resolve(__dirname, "../source/runner/_tests/fixtures/")
const dangerFileResultsFixtures = resolve(__dirname, "../source/runner/_tests/fixtures/results")

const fixtures = fs
.readdirSync(dangerFileFixtures, "utf8")
.filter(f => f.includes("__"))
.filter(f => !f.includes("Throws"))
.filter(f => !f.includes("BadSyntax"))

// Runs the danger runner over a fixture, then compares it to the
// fixtured JSON data
const runDangerfile = fixture => {
let allLogs = ""
const dangerfile = `${dangerFileFixtures}/${fixture}`
const dangerfileResults = `${dangerFileResultsFixtures}/${fixture}.json`

console.log("Running fixture for " + chalk.bold(dangerfile))

// Setup the command
const commandArgs = ["node", runnerFileJS, "--text-only", "--dangerfile", dangerfile]
const env = {
DANGER_FAKE_CI: "YEP",
DANGER_TEST_REPO: "danger/danger",
DANGER_TEST_PR: "395",
DANGER_GITHUB_API_TOKEN: "1234",
}
const command = commandArgs.join(" ")

const child = child_process.exec(command, { env: Object.assign({}, process.env, env) })
child.stdin.write(dslJSON)
child.stdin.end()

child.stderr.on("data", data => {
console.log(`stderr: ${data}`)
})

child.stdout.on("data", data => {
data = data.toString()
const trimmed = data.trim()
if (trimmed.startsWith("{") && trimmed.endsWith("}") && trimmed.includes("markdowns")) {
const runtimeResults = JSON.parse(trimmed)

if (writeResults) {
fs.writeFileSync(dangerfileResults, trimmed)
}

const fixturedResults = JSON.parse(fs.readFileSync(dangerfileResults, "utf8"))
// Fails include traces etc
expect(runtimeResults).toEqual(fixturedResults)

next()
} else {
allLogs += data
}
})
}

// Keep an index and loop through the fixtures
var index = 0
const next = () => {
const nextFixture = fixtures[index++]
if (nextFixture) {
runDangerfile(nextFixture)
}
}

process.on("unhandledRejection", function(reason, _p) {
console.log(chalk.red("Error: "), reason)
process.exitCode = 1
})

next()
Loading

0 comments on commit e735469

Please sign in to comment.