1
1
// Note: Keep this ES6 only, want to be able to run this directly via node
2
2
// to ensure that something like ts-node doesn't mess up paths etc
3
3
4
- // 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
4
+ // yarn test:fixtures
5
+
6
+ // Toggle this on to update the JSON files for each run
7
+ const writeResults = false
5
8
6
9
const fs = require ( "fs" )
7
10
const child_process = require ( "child_process" )
8
11
const { resolve } = require ( "path" )
12
+ const { basename } = require ( "path" )
9
13
const chalk = require ( "chalk" )
10
14
const expect = require ( "expect" )
11
15
12
- // Toggle this on to update the JSON files for each run
13
- const writeResults = false
14
- console . log ( "If this script fails, you probably want to update the fixtures - just edit script/run-fixtures.js" )
15
16
const runnerFileJS = "distribution/commands/danger-runner.js"
16
17
17
- // Get all the fixtures
18
+ // Use a DSL fixture, and emulate being the `danger ci` host process
18
19
const dangerDSLFixture = resolve ( __dirname , "../source/_tests/fixtures/danger-js-pr-395.json" )
19
20
const dslJSON = fs . readFileSync ( dangerDSLFixture , "utf8" )
20
21
@@ -28,14 +29,18 @@ const fixtures = fs
28
29
. filter ( f => ! f . includes ( "Throws" ) )
29
30
. filter ( f => ! f . includes ( "BadSyntax" ) )
30
31
32
+ let runCount = 0
33
+
34
+ console . log ( "Running Fixures for Danger JS. This uses the built version of danger.\n" )
35
+
31
36
// Runs the danger runner over a fixture, then compares it to the
32
37
// fixtured JSON data
33
38
const runDangerfile = fixture => {
34
39
let allLogs = ""
35
40
const dangerfile = `${ dangerFileFixtures } /${ fixture } `
36
41
const dangerfileResults = `${ dangerFileResultsFixtures } /${ fixture } .json`
37
42
38
- console . log ( "Running fixture for " + chalk . bold ( dangerfile ) )
43
+ process . stdout . write ( chalk . bold ( basename ( dangerfile ) ) )
39
44
40
45
// Setup the command
41
46
const commandArgs = [ "node" , runnerFileJS , "--text-only" , "--dangerfile" , dangerfile ]
@@ -58,31 +63,48 @@ const runDangerfile = fixture => {
58
63
59
64
child . stdout . on ( "data" , data => {
60
65
data = data . toString ( )
66
+ // console.log(`stdout: ${data}`)
67
+
61
68
const trimmed = data . trim ( )
62
- if ( trimmed . startsWith ( "{" ) && trimmed . endsWith ( "}" ) && trimmed . includes ( "markdowns" ) ) {
63
- const runtimeResults = JSON . parse ( trimmed )
69
+ const maybeJSON = getJSONURLFromSTDOUT ( data )
70
+ const url = maybeJSON . replace ( "danger-results:/" , "" )
71
+ const runtimeResults = JSON . parse ( fs . readFileSync ( url , "utf8" ) )
72
+ if ( writeResults ) {
73
+ fs . writeFileSync ( dangerfileResults , trimmed )
74
+ }
64
75
65
- if ( writeResults ) {
66
- fs . writeFileSync ( dangerfileResults , trimmed )
67
- }
76
+ const fixturedResults = JSON . parse ( fs . readFileSync ( dangerfileResults , "utf8" ) )
77
+ // Fails include traces etc
78
+ expect ( runtimeResults ) . toEqual ( fixturedResults )
68
79
69
- const fixturedResults = JSON . parse ( fs . readFileSync ( dangerfileResults , "utf8" ) )
70
- // Fails include traces etc
71
- expect ( runtimeResults ) . toEqual ( fixturedResults )
80
+ const tick = chalk . bold . greenBright ( "✓" )
81
+ process . stdout . write ( " " + tick )
72
82
73
- next ( )
74
- } else {
75
- allLogs += data
76
- }
83
+ runCount ++
84
+ next ( )
77
85
} )
78
86
}
79
87
88
+ /** Pulls out a URL that's from the STDOUT */
89
+ const getJSONURLFromSTDOUT = stdout => {
90
+ const match = stdout . match ( / d a n g e r - r e s u l t s : \/ \/ * .+ j s o n / )
91
+ if ( ! match ) {
92
+ return undefined
93
+ }
94
+ return match [ 0 ]
95
+ }
96
+
80
97
// Keep an index and loop through the fixtures
81
98
var index = 0
82
99
const next = ( ) => {
83
100
const nextFixture = fixtures [ index ++ ]
84
101
if ( nextFixture ) {
102
+ if ( index > 1 ) {
103
+ process . stdout . write ( ", " )
104
+ }
85
105
runDangerfile ( nextFixture )
106
+ } else {
107
+ expect ( runCount ) . toEqual ( fixtures . length )
86
108
}
87
109
}
88
110
0 commit comments