Skip to content

Commit

Permalink
Start adding tests
Browse files Browse the repository at this point in the history
  • Loading branch information
orta committed Dec 10, 2016
1 parent 2fe0ce9 commit 4e91872
Show file tree
Hide file tree
Showing 12 changed files with 2,065 additions and 35 deletions.
59 changes: 36 additions & 23 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -1,25 +1,38 @@
{
"version": "0.2.0",
"configurations": [
{
"name": "Launch",
"type": "node",
"request": "launch",
"program": "${workspaceRoot}/source/commands/danger-run.js",
"stopOnEntry": false,
"args": [],
"cwd": "${workspaceRoot}",
"preLaunchTask": "build",
"runtimeExecutable": null,
"runtimeArgs": [
"--nolazy"
],
"env": {
"NODE_ENV": "development"
},
"console": "internalConsole",
"sourceMaps": true,
"outDir": "${workspaceRoot}/distribution"
}
]
}
"configurations": [{
"name": "Launch",
"type": "node",
"request": "launch",
"program": "${workspaceRoot}/source/commands/danger-run.js",
"stopOnEntry": false,
"args": [],
"cwd": "${workspaceRoot}",
"preLaunchTask": "build",
"runtimeExecutable": null,
"runtimeArgs": [
"--nolazy"
],
"env": {
"NODE_ENV": "development"
},
"console": "internalConsole",
"sourceMaps": true,
"outDir": "${workspaceRoot}/distribution"
}, {
"name": "Run Tests With Debugger (slower, use npm run watch for normal work)",
"type": "node",
"request": "launch",
"port": 5858,
"address": "localhost",
"stopOnEntry": false,
"runtimeExecutable": null,
"runtimeArgs": [
"--debug-brk",
"./node_modules/.bin/jest",
"-i"
],
"cwd": "${workspaceRoot}",
"sourceMaps": true
}]
}
9 changes: 6 additions & 3 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@
"**/.svn": true,
"**/.hg": true,
"**/.DS_Store": true,
"distribution/": true,
"distribution/": true
},
"search.exclude": {
"**/node_modules": true,
"distribution/": true,
"distribution/": true
},
}
"files.associations": {
"*.js": "javascriptreact"
}
}
14 changes: 10 additions & 4 deletions dangerfile.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
// @flow

import { danger, fail, warn } from "danger"
import { danger, warn } from "danger"
const fs = require("fs")

// warn on changes in Package.json and not in shrinkwrap
// Request a CHANGELOG entry
const hasChangelog = danger.git.modified_files.includes("changelog.md")
if (!hasChangelog) {
fail("No Changelog changes!")
if (!hasChangelog) { warn("Please add a changelog entry for your changes.") }

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

const jsFiles = danger.git.created_files.filter(path => path.endsWith("js"))
Expand All @@ -22,3 +27,4 @@ const unFlowedFiles = jsFiles.filter(path => !path.endsWith("test.js"))
if (unFlowedFiles.length > 0) {
warn(`These new JS files do not have Flow enabled: ${unFlowedFiles.join(", ")}`)
}

1 change: 0 additions & 1 deletion source/dsl/DangerDSL.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ export interface DangerDSLType {
git: GitDSL;
/**
* The GitHub metadata.
* Currently, this is just the raw PR information.
*/
github: GitHubDSL;
}
Expand Down
5 changes: 4 additions & 1 deletion source/dsl/GitDSL.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ export interface GitDSL {
* Removed filepaths relative to the git root
* @type {string[]}
*/
deleted_files: string[]
deleted_files: string[],

/** Offers the diff for a specific file */
diffForFile(filename: string): ?string
}

3 changes: 2 additions & 1 deletion source/platforms/FakePlatform.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ export default class FakePlatform {
return {
modified_files: [],
created_files: [],
deleted_files: []
deleted_files: [],
diffForFile: () => ""
}
}

Expand Down
7 changes: 6 additions & 1 deletion source/platforms/GitHub.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,12 @@ export class GitHub {
return {
modified_files: modifiedDiffs.map((d: any) => d.to),
created_files: addedDiffs.map((d: any) => d.to),
deleted_files: removedDiffs.map((d: any) => d.from)
deleted_files: removedDiffs.map((d: any) => d.from),
diffForFile: (name: string) => {
const diff = fileDiffs.find((diff) => diff.from === name || diff.to === name)
if (!diff) { return null }
return diff.chunks.reduce((chunk) => chunk.content.reduce((content) => content.join("")))
}
}
}

Expand Down
56 changes: 55 additions & 1 deletion source/platforms/_tests/GitHub.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@

import { GitHub } from "../GitHub"
import Fake from "../../ci_source/Fake"
import { readFileSync } from "fs"
import { resolve } from "path"
import type { GitDSL } from "../../dsl/GitDSL"
const fixtures = resolve(__dirname, "fixtures")

// Gets a mocked out GitHub class for checking a get path
const mockGitHubWithGetForPath = (expectedPath): GitHub => {
const mockSource = new Fake({})
const github = new GitHub("Token", mockSource)

// $FlowFixMe: Ignore an error from setting an unexistant func
github.get = (path: string, headers: any = {}, body: any = {}, method: string = "GET"): Promise<any> => {
github.get = (path: string, headers: any = {}, body: any = {}, method: string = "GET"): Promise < any > => {
return new Promise((resolve: any, reject: any) => {
expect(path).toBe(expectedPath)
resolve({})
Expand All @@ -19,6 +23,22 @@ const mockGitHubWithGetForPath = (expectedPath): GitHub => {
return github
}

/** Returns JSON from the fixtured dir */
const requestWithFixturedJSON = (path: string): Promise <Response> => {
const json = JSON.parse(readFileSync(`${fixtures}/${path}`, {}).toString())
return () => { return {
json: () => Promise.resolve(json)
} }
}

/** Returns arbitrary text value from a request */
const requestWithFixturedContent = (path: string): Promise <Response> => {
const content = readFileSync(`${fixtures}/${path}`, {}).toString()
return () => { return {
text: () => Promise.resolve(content)
} }
}

describe("API results", () => {
it("sets the correct paths for pull request comments", () => {
const expectedPath = "repos/artsy/emission/issues/327/comments"
Expand All @@ -32,3 +52,37 @@ describe("API results", () => {
expect(github.getPullRequestDiff())
})
})

describe("with fixtured data", () => {
it("returns the correct github data", async () => {
const mockSource = new Fake({})
const github:Platform = new GitHub("Token", mockSource)
github.getPullRequestInfo = requestWithFixturedJSON("github_pr.json")

const info = await github.getReviewInfo()
expect(info.title).toEqual("Adds support for showing the metadata and trending Artists to a Gene VC")
})

describe("the dangerfile gitDSL", () => {
let github = {}
beforeEach(() => {
github = new GitHub("Token", new Fake({}))
github.getPullRequestDiff = requestWithFixturedContent("github_diff.diff")
})

it("sets the modified/created/deleted", async () => {
const gitDSL:GitDSL = await github.getReviewDiff()

expect(gitDSL.modified_files).toEqual(["CHANGELOG.md", "data/schema.graphql", "data/schema.json", "externals/metaphysics", "lib/__mocks__/react-relay.js", "lib/components/artist/about.js", "lib/components/gene/header.js", "lib/containers/__tests__/__snapshots__/gene-tests.js.snap", "lib/containers/__tests__/gene-tests.js", "lib/containers/gene.js", "tsconfig.json"])

expect(gitDSL.created_files).toEqual(["lib/components/gene/about.js", "lib/components/gene/biography.js", "lib/components/related_artists/index.js", "lib/components/related_artists/related_artist.js"])

expect(gitDSL.deleted_files).toEqual(["lib/components/artist/related_artists/index.js", "lib/components/artist/related_artists/related_artist.js", "lib/components/gene/about_gene.js"])
})

it("shows the diff for a specific file", async () => {
const gitDSL:GitDSL = await github.getReviewDiff()
expect(gitDSL.diffForFile("CHANGELOG.md")).toEqual("sads")
})
})
})
114 changes: 114 additions & 0 deletions source/platforms/_tests/fixtures/github_comments.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
[
{
"url": "https://api.github.com/repos/artsy/emission/issues/comments/250755850",
"html_url": "https://github.com/artsy/emission/pull/327#issuecomment-250755850",
"issue_url": "https://api.github.com/repos/artsy/emission/issues/327",
"id": 250755850,
"user": {
"login": "alloy",
"id": 2320,
"avatar_url": "https://avatars.githubusercontent.com/u/2320?v=3",
"gravatar_id": "",
"url": "https://api.github.com/users/alloy",
"html_url": "https://github.com/alloy",
"followers_url": "https://api.github.com/users/alloy/followers",
"following_url": "https://api.github.com/users/alloy/following{/other_user}",
"gists_url": "https://api.github.com/users/alloy/gists{/gist_id}",
"starred_url": "https://api.github.com/users/alloy/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/alloy/subscriptions",
"organizations_url": "https://api.github.com/users/alloy/orgs",
"repos_url": "https://api.github.com/users/alloy/repos",
"events_url": "https://api.github.com/users/alloy/events{/privacy}",
"received_events_url": "https://api.github.com/users/alloy/received_events",
"type": "User",
"site_admin": false
},
"created_at": "2016-09-30T14:17:23Z",
"updated_at": "2016-09-30T14:17:23Z",
"body": "Looking good!\n\nThere’s a bunch of overlap with components on the artist view, will those be sharable in the future?\n"
},
{
"url": "https://api.github.com/repos/artsy/emission/issues/comments/250770683",
"html_url": "https://github.com/artsy/emission/pull/327#issuecomment-250770683",
"issue_url": "https://api.github.com/repos/artsy/emission/issues/327",
"id": 250770683,
"user": {
"login": "ArtsyOpenSource",
"id": 12397828,
"avatar_url": "https://avatars.githubusercontent.com/u/12397828?v=3",
"gravatar_id": "",
"url": "https://api.github.com/users/ArtsyOpenSource",
"html_url": "https://github.com/ArtsyOpenSource",
"followers_url": "https://api.github.com/users/ArtsyOpenSource/followers",
"following_url": "https://api.github.com/users/ArtsyOpenSource/following{/other_user}",
"gists_url": "https://api.github.com/users/ArtsyOpenSource/gists{/gist_id}",
"starred_url": "https://api.github.com/users/ArtsyOpenSource/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/ArtsyOpenSource/subscriptions",
"organizations_url": "https://api.github.com/users/ArtsyOpenSource/orgs",
"repos_url": "https://api.github.com/users/ArtsyOpenSource/repos",
"events_url": "https://api.github.com/users/ArtsyOpenSource/events{/privacy}",
"received_events_url": "https://api.github.com/users/ArtsyOpenSource/received_events",
"type": "User",
"site_admin": false
},
"created_at": "2016-09-30T15:13:10Z",
"updated_at": "2016-09-30T15:23:04Z",
"body": "<table>\n <thead>\n <tr>\n <th width=\"50\"></th>\n <th width=\"100%\" data-kind=\"Error\">\n :white_check_mark: Good on 'ya.\n </th>\n </tr>\n </thead>\n <tbody>\n <tr>\n <td>:white_check_mark:</td>\n <td data-sticky=\"true\"><del>No CHANGELOG added.</del></td>\n </tr>\n </tbody>\n</table>\n\n<table>\n <thead>\n <tr>\n <th width=\"50\"></th>\n <th width=\"100%\" data-kind=\"Warning\">\n 1 Warning\n </th>\n </tr>\n </thead>\n <tbody>\n <tr>\n <td>:warning:</td>\n <td data-sticky=\"true\">The following files do not have tests: <a href=\"https://github.com/artsy/emission/blob/cfa8fb80d2b65f4c4fa0b54d25352a3a0ff58f75/lib/__mocks__/react-relay.js\">lib/<strong>mocks</strong>/react-relay.js</a>, <a href=\"https://github.com/artsy/emission/blob/cfa8fb80d2b65f4c4fa0b54d25352a3a0ff58f75/lib/components/artist/about.js\">lib/components/artist/about.js</a>, <a href=\"https://github.com/artsy/emission/blob/cfa8fb80d2b65f4c4fa0b54d25352a3a0ff58f75/lib/components/artist/related_artists/index.js\">lib/components/artist/related_artists/index.js</a>, <a href=\"https://github.com/artsy/emission/blob/cfa8fb80d2b65f4c4fa0b54d25352a3a0ff58f75/lib/components/artist/related_artists/related_artist.js\">lib/components/artist/related_artists/related_artist.js</a>, <a href=\"https://github.com/artsy/emission/blob/cfa8fb80d2b65f4c4fa0b54d25352a3a0ff58f75/lib/components/gene/about.js\">lib/components/gene/about.js</a>, <a href=\"https://github.com/artsy/emission/blob/cfa8fb80d2b65f4c4fa0b54d25352a3a0ff58f75/lib/components/gene/about_gene.js\">lib/components/gene/about_gene.js</a>, <a href=\"https://github.com/artsy/emission/blob/cfa8fb80d2b65f4c4fa0b54d25352a3a0ff58f75/lib/components/gene/biography.js\">lib/components/gene/biography.js</a>, <a href=\"https://github.com/artsy/emission/blob/cfa8fb80d2b65f4c4fa0b54d25352a3a0ff58f75/lib/components/gene/header.js\">lib/components/gene/header.js</a>, <a href=\"https://github.com/artsy/emission/blob/cfa8fb80d2b65f4c4fa0b54d25352a3a0ff58f75/lib/components/related_artists/index.js\">lib/components/related_artists/index.js</a> &amp; <a href=\"https://github.com/artsy/emission/blob/cfa8fb80d2b65f4c4fa0b54d25352a3a0ff58f75/lib/components/related_artists/related_artist.js\">lib/components/related_artists/related_artist.js</a></td>\n </tr>\n <tr>\n <td>:white_check_mark:</td>\n <td data-sticky=\"true\"><del>The following files do not have tests: <a href=\"https://github.com/artsy/emission/blob/466bdef69fdbcdceb3b35242370edc328605ae1e/lib/__mocks__/react-relay.js\">lib/<strong>mocks</strong>/react-relay.js</a>, <a href=\"https://github.com/artsy/emission/blob/466bdef69fdbcdceb3b35242370edc328605ae1e/lib/components/artist/about.js\">lib/components/artist/about.js</a>, <a href=\"https://github.com/artsy/emission/blob/466bdef69fdbcdceb3b35242370edc328605ae1e/lib/components/artist/related_artists/index.js\">lib/components/artist/related_artists/index.js</a>, <a href=\"https://github.com/artsy/emission/blob/466bdef69fdbcdceb3b35242370edc328605ae1e/lib/components/artist/related_artists/related_artist.js\">lib/components/artist/related_artists/related_artist.js</a>, <a href=\"https://github.com/artsy/emission/blob/466bdef69fdbcdceb3b35242370edc328605ae1e/lib/components/gene/about.js\">lib/components/gene/about.js</a>, <a href=\"https://github.com/artsy/emission/blob/466bdef69fdbcdceb3b35242370edc328605ae1e/lib/components/gene/about_gene.js\">lib/components/gene/about_gene.js</a>, <a href=\"https://github.com/artsy/emission/blob/466bdef69fdbcdceb3b35242370edc328605ae1e/lib/components/gene/biography.js\">lib/components/gene/biography.js</a>, <a href=\"https://github.com/artsy/emission/blob/466bdef69fdbcdceb3b35242370edc328605ae1e/lib/components/gene/header.js\">lib/components/gene/header.js</a>, <a href=\"https://github.com/artsy/emission/blob/466bdef69fdbcdceb3b35242370edc328605ae1e/lib/components/related_artists/index.js\">lib/components/related_artists/index.js</a> &amp; <a href=\"https://github.com/artsy/emission/blob/466bdef69fdbcdceb3b35242370edc328605ae1e/lib/components/related_artists/related_artist.js\">lib/components/related_artists/related_artist.js</a></del></td>\n </tr>\n </tbody>\n</table>\n\n<p align=\"right\" data-meta=\"generated_by_danger\">\n Generated by :no_entry_sign: <a href=\"http://danger.systems/\">danger</a>\n</p>\n"
},
{
"url": "https://api.github.com/repos/artsy/emission/issues/comments/251047897",
"html_url": "https://github.com/artsy/emission/pull/327#issuecomment-251047897",
"issue_url": "https://api.github.com/repos/artsy/emission/issues/327",
"id": 251047897,
"user": {
"login": "orta",
"id": 49038,
"avatar_url": "https://avatars.githubusercontent.com/u/49038?v=3",
"gravatar_id": "",
"url": "https://api.github.com/users/orta",
"html_url": "https://github.com/orta",
"followers_url": "https://api.github.com/users/orta/followers",
"following_url": "https://api.github.com/users/orta/following{/other_user}",
"gists_url": "https://api.github.com/users/orta/gists{/gist_id}",
"starred_url": "https://api.github.com/users/orta/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/orta/subscriptions",
"organizations_url": "https://api.github.com/users/orta/orgs",
"repos_url": "https://api.github.com/users/orta/repos",
"events_url": "https://api.github.com/users/orta/events{/privacy}",
"received_events_url": "https://api.github.com/users/orta/received_events",
"type": "User",
"site_admin": false
},
"created_at": "2016-10-03T08:02:20Z",
"updated_at": "2016-12-09T08:36:38Z",
"body": "\n\n<table>\n <thead>\n <tr>\n <th width=\"50\"></th>\n <th width=\"100%\" data-danger-table=\"true\">Fails</th>\n </tr>\n </thead>\n <tbody><tr>\n <td>:no_entry_sign:</td>\n <td>No Changelog changes!</td>\n </tr>\n </tbody>\n</table>\n\n\n\n\n<p align=\"right\">\n Generated by :no_entry_sign: <a href=\"http://github.com/danger/danger-js/\">dangerJS</a>\n</p>\n"
},
{
"url": "https://api.github.com/repos/artsy/emission/issues/comments/251065326",
"html_url": "https://github.com/artsy/emission/pull/327#issuecomment-251065326",
"issue_url": "https://api.github.com/repos/artsy/emission/issues/327",
"id": 251065326,
"user": {
"login": "mennenia",
"id": 373860,
"avatar_url": "https://avatars.githubusercontent.com/u/373860?v=3",
"gravatar_id": "",
"url": "https://api.github.com/users/mennenia",
"html_url": "https://github.com/mennenia",
"followers_url": "https://api.github.com/users/mennenia/followers",
"following_url": "https://api.github.com/users/mennenia/following{/other_user}",
"gists_url": "https://api.github.com/users/mennenia/gists{/gist_id}",
"starred_url": "https://api.github.com/users/mennenia/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/mennenia/subscriptions",
"organizations_url": "https://api.github.com/users/mennenia/orgs",
"repos_url": "https://api.github.com/users/mennenia/repos",
"events_url": "https://api.github.com/users/mennenia/events{/privacy}",
"received_events_url": "https://api.github.com/users/mennenia/received_events",
"type": "User",
"site_admin": false
},
"created_at": "2016-10-03T09:39:14Z",
"updated_at": "2016-10-03T09:39:14Z",
"body": "Looks great! \n"
}
]

0 comments on commit 4e91872

Please sign in to comment.