Skip to content

Commit

Permalink
Merge branch 'master' into feature/gitlab
Browse files Browse the repository at this point in the history
  • Loading branch information
bigkraig committed Apr 14, 2019
2 parents 4acda3b + 816810d commit 06ea6d9
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 72 deletions.
25 changes: 25 additions & 0 deletions CHANGELOG.md
Expand Up @@ -16,7 +16,32 @@
<!-- Your comment below this -->

- Adds GitLab & GitLab CI support. - [@notjosh, @bigkraig]
- Cleans up the declarations a little bit - [@orta]

# 7.1.2

- Update ts-jest to 24.0.2 - [@friederbluemle]
- Adds a fix for the default name of Danger in status - [@orta]
- Adds `danger.git.fileMatch.getKeyedPaths()`, providing more convenient access to paths. This replaces
`fileMatch.tap()` and `fileMatch.debug()`.

```ts
const components = fileMatch("components/**/*.js", "!**/*.test.js")
const componentTests = fileMatch("!**/*.test.js")

if (components.edited && !componentTests.edited) {
warn(
[
"This PR modified some components but none of their tests. <br>",
"That's okay so long as it's refactoring existing code. <br>",
"Affected files: ",
components.getKeyedPaths().edited.join(", "),
].join("")
)
}
```

This makes it much simpler to compose a collection of file checks - [@paulmelnikow]

# 7.1.0

Expand Down
5 changes: 3 additions & 2 deletions package.json
@@ -1,6 +1,6 @@
{
"name": "danger",
"version": "7.1.0",
"version": "7.1.2",
"description": "Unit tests for Team Culture",
"main": "distribution/danger.js",
"typings": "distribution/danger.d.ts",
Expand Down Expand Up @@ -103,6 +103,7 @@
"@types/node": "^10.11.3",
"@types/node-fetch": "^2.1.2",
"@types/p-limit": "^2.0.0",
"@types/prettier": "^1.16.1",
"@types/readline-sync": "^1.4.3",
"@types/voca": "^1.4.0",
"danger-plugin-jest": "^1.0.1",
Expand All @@ -118,7 +119,7 @@
"prettier": "^1.14.2",
"release-it": "^7.6.1",
"shx": "^0.3.2",
"ts-jest": "^23.10.5",
"ts-jest": "^24.0.2",
"ts-node": "^8.0.2",
"tslint": "^5.11.0",
"tslint-config-prettier": "^1.15.0",
Expand Down
24 changes: 22 additions & 2 deletions scripts/danger-dts.ts
@@ -1,4 +1,5 @@
import * as fs from "fs"
import * as prettier from "prettier"

const mapLines = (s: string, func: (s: string) => string) =>
s
Expand Down Expand Up @@ -66,7 +67,15 @@ import * as GitHub from "@octokit/rest"

fileOutput += context

fileOutput += fs.readFileSync("distribution/commands/utils/chainsmoker.d.ts", "utf8")
const chainDefs = fs.readFileSync("distribution/commands/utils/chainsmoker.d.ts", "utf8")
const chainDefsMinusDefaultExport = chainDefs
.split("\n")
.filter(line => {
return !line.startsWith("export default function")
})
.join("\n")

fileOutput += chainDefsMinusDefaultExport

// Remove all JS-y bits
fileOutput = fileOutput
Expand All @@ -81,7 +90,18 @@ import * as GitHub from "@octokit/rest"
.replace(/export interface/g, "interface")
.replace(/export type/g, "type")
const indentedBody = mapLines(noRedundantExports, line => (line.length ? line : ""))
return header + indentedBody + footer

const def = header + indentedBody + footer

return prettier.format(def, {
parser: "typescript",
printWidth: 120,
semi: false,
singleQuote: false,
trailingComma: "es5",
bracketSpacing: true,
proseWrap: "always",
})
}

export default createDTS
83 changes: 53 additions & 30 deletions source/commands/utils/_tests/chainsmoker.test.ts
@@ -1,4 +1,4 @@
import chainsmoker, { MatchResult } from "../chainsmoker"
import chainsmoker, { _MatchResult } from "../chainsmoker"

describe("chainsmoker", () => {
const keyedPaths = {
Expand All @@ -9,51 +9,74 @@ describe("chainsmoker", () => {

const fileMatch = chainsmoker(keyedPaths)

it.each<[string[], MatchResult<typeof keyedPaths>]>([
[["**/*.md"], { created: false, modified: true, deleted: true }],
[["**/*.js"], { created: true, modified: true, deleted: true }],
it.each<[string[], typeof keyedPaths, _MatchResult<typeof keyedPaths>]>([
[
["**/*.md"],
{ created: [], modified: ["changed.md"], deleted: ["also/deleted.md"] },
{
created: false,
modified: true,
deleted: true,
},
],
[
["**/*.js"],
{
created: ["added.js", "also/added.js", "this/was/also/Added.js"],
modified: ["changed.js", "also/changed.js"],
deleted: ["deleted.js"],
},
{
created: true,
modified: true,
deleted: true,
},
],
[
["**/*[A-Z]*.js"],
{
created: ["this/was/also/Added.js"],
modified: [],
deleted: [],
},
{
created: true,
modified: false,
deleted: false,
},
],
[["**/*_*"], { created: false, modified: true, deleted: false }],
[
["**/*_*"],
{
created: [],
modified: ["this_is_changed.sh"],
deleted: [],
},
{
created: false,
modified: true,
deleted: false,
},
],
[
["also/*", "!**/*.md"],
{
created: ["also/added.js"],
modified: ["also/changed.js"],
deleted: [],
},
{
created: true,
modified: true,
deleted: false,
},
],
])("fileMatch(%s)", (patterns, expected) => expect(fileMatch(...patterns)).toEqual(expected))

it("tap()", () => {
const callback = jest.fn()
expect(fileMatch.tap(callback)("**/*.md")).toEqual({ created: false, modified: true, deleted: true })
expect(callback).toBeCalledWith({
created: [],
modified: ["changed.md"],
deleted: ["also/deleted.md"],
])("fileMatch(%s)", (patterns, keyedPaths, matchResult) => {
const matched = fileMatch(...patterns)
expect(matched.getKeyedPaths()).toEqual(keyedPaths)
expect(matched).toEqual({
...matchResult,
getKeyedPaths: expect.any(Function),
})
})

it("debug()", () => {
const mockConsoleLog = jest.spyOn(console, "log").mockImplementation(() => undefined)
expect(fileMatch.debug("**/*.md")).toEqual({ created: false, modified: true, deleted: true })
expect(mockConsoleLog).toBeCalledWith(
JSON.stringify(
{
created: [],
modified: ["changed.md"],
deleted: ["also/deleted.md"],
},
undefined,
2
)
)
})
})
40 changes: 14 additions & 26 deletions source/commands/utils/chainsmoker.ts
Expand Up @@ -7,16 +7,17 @@ import mapValues from "lodash.mapvalues"

export type Pattern = string
export type Path = string
export type KeyedPatterns<T> = { [K in Extract<keyof T, string>]: Pattern[] }
export type KeyedPaths<T> = { [K in Extract<keyof T, string>]: Path[] }
export type MatchResult<T> = { [K in Extract<keyof T, string>]: boolean }

export interface Chainsmoker<T> {
(...patterns: Pattern[]): MatchResult<T>
debug(...patterns: Pattern[]): MatchResult<T>
tap(callback: (keyedPaths: KeyedPaths<T>) => void): (...patterns: Pattern[]) => MatchResult<T>
export type KeyedPatterns<T> = { readonly [K in keyof T]: Pattern[] }
export type KeyedPaths<T> = { readonly [K in keyof T]: Path[] }
export type _MatchResult<T> = { readonly [K in keyof T]: boolean }
export type MatchResult<T> = _MatchResult<T> & {
/** Returns an object containing arrays of matched files instead of the usual boolean values. */
getKeyedPaths(): KeyedPaths<T>
}

/** A vendored copy of the Chainsmoker module on NPM */
export type Chainsmoker<T> = (...patterns: Pattern[]) => MatchResult<T>

const isExclude = (p: Pattern) => p.startsWith("!")

export default function chainsmoker<T>(keyedPaths: KeyedPaths<T>): Chainsmoker<T> {
Expand All @@ -35,24 +36,11 @@ export default function chainsmoker<T>(keyedPaths: KeyedPaths<T>): Chainsmoker<T
}

function finalize(keyedPaths: KeyedPaths<T>): MatchResult<T> {
return mapValues(keyedPaths, (paths: Path[]) => paths.length > 0) as MatchResult<T>
}

const fileMatch = ((...patterns) => finalize(matchPatterns(patterns))) as Chainsmoker<T>

/** Logs an object containing matched files before returning the usual boolean values. */
fileMatch.debug = (...patterns) => {
const results = matchPatterns(patterns)
console.log(JSON.stringify(results, undefined, 2))
return finalize(results)
}

/** Invoke the callback with an object containing matched files before returning the usual boolean values. */
fileMatch.tap = callback => (...patterns) => {
const results = matchPatterns(patterns)
callback(results)
return finalize(results)
return {
...mapValues(keyedPaths, (paths: Path[]) => paths.length > 0),
getKeyedPaths: () => keyedPaths,
} as MatchResult<T>
}

return fileMatch
return (...patterns) => finalize(matchPatterns(patterns))
}
15 changes: 7 additions & 8 deletions source/danger.d.ts
Expand Up @@ -1324,12 +1324,11 @@ declare const peril: PerilDSL
declare const results: DangerRuntimeContainer
export declare type Pattern = string
export declare type Path = string
export declare type KeyedPatterns<T> = { [K in Extract<keyof T, string>]: Pattern[] }
export declare type KeyedPaths<T> = { [K in Extract<keyof T, string>]: Path[] }
export declare type MatchResult<T> = { [K in Extract<keyof T, string>]: boolean }
interface Chainsmoker<T> {
(...patterns: Pattern[]): MatchResult<T>
debug(...patterns: Pattern[]): MatchResult<T>
tap(callback: (keyedPaths: KeyedPaths<T>) => void): (...patterns: Pattern[]) => MatchResult<T>
export declare type KeyedPatterns<T> = { readonly [K in keyof T]: Pattern[] }
export declare type KeyedPaths<T> = { readonly [K in keyof T]: Path[] }
export declare type _MatchResult<T> = { readonly [K in keyof T]: boolean }
export declare type MatchResult<T> = _MatchResult<T> & {
/** Returns an object containing arrays of matched files instead of the usual boolean values. */
getKeyedPaths(): KeyedPaths<T>
}
export default function chainsmoker<T>(keyedPaths: KeyedPaths<T>): Chainsmoker<T>
export declare type Chainsmoker<T> = (...patterns: Pattern[]) => MatchResult<T>
13 changes: 9 additions & 4 deletions yarn.lock
Expand Up @@ -970,6 +970,11 @@
resolved "https://registry.yarnpkg.com/@types/p-limit/-/p-limit-2.0.0.tgz#c076b7daa9163108a35899ea6a9d927526943ea2"
integrity sha512-vx0HhwWQcrIzFn+Bx5OS6hk3wQ9++QjRyZgybM0+20MKqq1uYYgVjDxXk8+fFDfeGArep5l1laHCxEwltfzwYQ==

"@types/prettier@^1.16.1":
version "1.16.1"
resolved "https://registry.yarnpkg.com/@types/prettier/-/prettier-1.16.1.tgz#328d1c9b54402e44119398bcb6a31b7bbd606d59"
integrity sha512-db6pZL5QY3JrlCHBhYQzYDci0xnoDuxfseUuguLRr3JNk+bnCfpkK6p8quiUDyO8A0vbpBKkk59Fw125etrNeA==

"@types/readline-sync@^1.4.3":
version "1.4.3"
resolved "https://registry.yarnpkg.com/@types/readline-sync/-/readline-sync-1.4.3.tgz#eac55a39d5a349912062c9e5216cd550c07fd9c8"
Expand Down Expand Up @@ -8841,10 +8846,10 @@ trim-right@^1.0.1:
resolved "https://registry.yarnpkg.com/trim-right/-/trim-right-1.0.1.tgz#cb2e1203067e0c8de1f614094b9fe45704ea6003"
integrity sha1-yy4SAwZ+DI3h9hQJS5/kVwTqYAM=

ts-jest@^23.10.5:
version "23.10.5"
resolved "https://registry.yarnpkg.com/ts-jest/-/ts-jest-23.10.5.tgz#cdb550df4466a30489bf70ba867615799f388dd5"
integrity sha512-MRCs9qnGoyKgFc8adDEntAOP64fWK1vZKnOYU1o2HxaqjdJvGqmkLCPCnVq1/If4zkUmEjKPnCiUisTrlX2p2A==
ts-jest@^24.0.2:
version "24.0.2"
resolved "https://registry.yarnpkg.com/ts-jest/-/ts-jest-24.0.2.tgz#8dde6cece97c31c03e80e474c749753ffd27194d"
integrity sha512-h6ZCZiA1EQgjczxq+uGLXQlNgeg02WWJBbeT8j6nyIBRQdglqbvzDoHahTEIiS6Eor6x8mK6PfZ7brQ9Q6tzHw==
dependencies:
bs-logger "0.x"
buffer-from "1.x"
Expand Down

0 comments on commit 06ea6d9

Please sign in to comment.