From c05c493733acee03597c1524612891eb55d8616a Mon Sep 17 00:00:00 2001 From: Paul Melnikow Date: Sun, 7 Apr 2019 13:06:36 -0500 Subject: [PATCH 1/6] Add `danger.git.fileMatch.getKeyedPaths()`, providing more convenient access to paths --- CHANGELOG.md | 21 +++++ .../commands/utils/_tests/chainsmoker.test.ts | 83 ++++++++++++------- source/commands/utils/chainsmoker.ts | 38 +++------ 3 files changed, 86 insertions(+), 56 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e4debe366..d52c96d09 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,27 @@ +- Add `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.
", + "That's okay so long as it's refactoring existing code.
", + "Affected files: ", + components.getKeyedPaths().edited.join(", "), + ].join("") + ) + } + ``` + + This makes it much simpler to compose a collection of file checks - [@paulmelnikow] + # 7.1.0 - Adds Chainsmoker, and expands the Danger DSL with the addition of `danger.git.fileMatch`. diff --git a/source/commands/utils/_tests/chainsmoker.test.ts b/source/commands/utils/_tests/chainsmoker.test.ts index 31b636daf..d4d6d33ff 100644 --- a/source/commands/utils/_tests/chainsmoker.test.ts +++ b/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 = { @@ -9,51 +9,74 @@ describe("chainsmoker", () => { const fileMatch = chainsmoker(keyedPaths) - it.each<[string[], MatchResult]>([ - [["**/*.md"], { created: false, modified: true, deleted: true }], - [["**/*.js"], { created: true, modified: true, deleted: true }], + it.each<[string[], typeof keyedPaths, _MatchResult]>([ + [ + ["**/*.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 - ) - ) - }) }) diff --git a/source/commands/utils/chainsmoker.ts b/source/commands/utils/chainsmoker.ts index b0e48e38a..61e5535ba 100644 --- a/source/commands/utils/chainsmoker.ts +++ b/source/commands/utils/chainsmoker.ts @@ -7,15 +7,14 @@ import mapValues from "lodash.mapvalues" export type Pattern = string export type Path = string -export type KeyedPatterns = { [K in Extract]: Pattern[] } -export type KeyedPaths = { [K in Extract]: Path[] } -export type MatchResult = { [K in Extract]: boolean } - -export interface Chainsmoker { - (...patterns: Pattern[]): MatchResult - debug(...patterns: Pattern[]): MatchResult - tap(callback: (keyedPaths: KeyedPaths) => void): (...patterns: Pattern[]) => MatchResult +export type KeyedPatterns = { readonly [K in keyof T]: Pattern[] } +export type KeyedPaths = { readonly [K in keyof T]: Path[] } +export type _MatchResult = { readonly [K in keyof T]: boolean } +export type MatchResult = _MatchResult & { + /** Returns an object containing arrays of matched files instead of the usual boolean values. */ + getKeyedPaths(): KeyedPaths } +export type Chainsmoker = (...patterns: Pattern[]) => MatchResult const isExclude = (p: Pattern) => p.startsWith("!") @@ -35,24 +34,11 @@ export default function chainsmoker(keyedPaths: KeyedPaths): Chainsmoker): MatchResult { - return mapValues(keyedPaths, (paths: Path[]) => paths.length > 0) as MatchResult + return { + ...mapValues(keyedPaths, (paths: Path[]) => paths.length > 0), + getKeyedPaths: () => keyedPaths, + } as MatchResult } - const fileMatch = ((...patterns) => finalize(matchPatterns(patterns))) as Chainsmoker - - /** 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 fileMatch + return (...patterns) => finalize(matchPatterns(patterns)) } From 46046a39db76e6387a3af66fb0e5730090a01021 Mon Sep 17 00:00:00 2001 From: Frieder Bluemle Date: Fri, 12 Apr 2019 13:54:06 +0800 Subject: [PATCH 2/6] Update ts-jest to 24.0.2 --- CHANGELOG.md | 1 + package.json | 2 +- yarn.lock | 8 ++++---- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f36c4243a..d90a5c2f2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ +- Update ts-jest to 24.0.2 - [@friederbluemle] - Adds a fix for the default name of Danger in status - [@orta] # 7.1.0 diff --git a/package.json b/package.json index 5f6ad430f..82f7f50d1 100644 --- a/package.json +++ b/package.json @@ -118,7 +118,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", diff --git a/yarn.lock b/yarn.lock index ba944ce8c..f396b0727 100644 --- a/yarn.lock +++ b/yarn.lock @@ -8685,10 +8685,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" From 630b1a0becb81c16ecd494ff1939bd5102b1f545 Mon Sep 17 00:00:00 2001 From: Orta Therox Date: Sat, 13 Apr 2019 06:54:41 -0400 Subject: [PATCH 3/6] Prepare for release --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0ef11eb96..330e65dc8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,8 @@ +# 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 From b3c8f9ddbf0d403bacb2f01bda5ed97b4a466c00 Mon Sep 17 00:00:00 2001 From: Orta Therox Date: Sat, 13 Apr 2019 06:56:15 -0400 Subject: [PATCH 4/6] Release 7.1.1 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 82f7f50d1..533198e1f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "danger", - "version": "7.1.0", + "version": "7.1.1", "description": "Unit tests for Team Culture", "main": "distribution/danger.js", "typings": "distribution/danger.d.ts", From 98584eb428bf27ba4a69a962aa9a5a95d51453cb Mon Sep 17 00:00:00 2001 From: Orta Therox Date: Sat, 13 Apr 2019 07:06:59 -0400 Subject: [PATCH 5/6] Release 7.1.2 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 533198e1f..9b916e563 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "danger", - "version": "7.1.1", + "version": "7.1.2", "description": "Unit tests for Team Culture", "main": "distribution/danger.js", "typings": "distribution/danger.d.ts", From c94dce6f26f84e45410dd1c11f652ebfbfb178a4 Mon Sep 17 00:00:00 2001 From: Orta Therox Date: Sat, 13 Apr 2019 07:30:22 -0400 Subject: [PATCH 6/6] Cleans up the declartions and runs prettier over them in memory --- CHANGELOG.md | 2 ++ package.json | 1 + scripts/danger-dts.ts | 24 ++++++++++++++++++++++-- source/commands/utils/chainsmoker.ts | 2 ++ source/danger.d.ts | 15 +++++++-------- yarn.lock | 5 +++++ 6 files changed, 39 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 330e65dc8..d5bd7409a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,8 @@ +- Cleans up the declarations a little bit - [@orta] + # 7.1.2 - Update ts-jest to 24.0.2 - [@friederbluemle] diff --git a/package.json b/package.json index 9b916e563..a23cb1c18 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/scripts/danger-dts.ts b/scripts/danger-dts.ts index fc468bd23..33507fe19 100644 --- a/scripts/danger-dts.ts +++ b/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 @@ -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 @@ -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 diff --git a/source/commands/utils/chainsmoker.ts b/source/commands/utils/chainsmoker.ts index 61e5535ba..9418b3426 100644 --- a/source/commands/utils/chainsmoker.ts +++ b/source/commands/utils/chainsmoker.ts @@ -14,6 +14,8 @@ export type MatchResult = _MatchResult & { /** Returns an object containing arrays of matched files instead of the usual boolean values. */ getKeyedPaths(): KeyedPaths } + +/** A vendored copy of the Chainsmoker module on NPM */ export type Chainsmoker = (...patterns: Pattern[]) => MatchResult const isExclude = (p: Pattern) => p.startsWith("!") diff --git a/source/danger.d.ts b/source/danger.d.ts index 0aaade1be..9655cc8f4 100644 --- a/source/danger.d.ts +++ b/source/danger.d.ts @@ -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 = { [K in Extract]: Pattern[] } -export declare type KeyedPaths = { [K in Extract]: Path[] } -export declare type MatchResult = { [K in Extract]: boolean } -interface Chainsmoker { - (...patterns: Pattern[]): MatchResult - debug(...patterns: Pattern[]): MatchResult - tap(callback: (keyedPaths: KeyedPaths) => void): (...patterns: Pattern[]) => MatchResult +export declare type KeyedPatterns = { readonly [K in keyof T]: Pattern[] } +export declare type KeyedPaths = { readonly [K in keyof T]: Path[] } +export declare type _MatchResult = { readonly [K in keyof T]: boolean } +export declare type MatchResult = _MatchResult & { + /** Returns an object containing arrays of matched files instead of the usual boolean values. */ + getKeyedPaths(): KeyedPaths } -export default function chainsmoker(keyedPaths: KeyedPaths): Chainsmoker +export declare type Chainsmoker = (...patterns: Pattern[]) => MatchResult diff --git a/yarn.lock b/yarn.lock index f396b0727..f0ada99e5 100644 --- a/yarn.lock +++ b/yarn.lock @@ -951,6 +951,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"