Skip to content

Commit 0189eb2

Browse files
authored
Add Results method to danger.T (#8)
1 parent fd5d7d0 commit 0189eb2

File tree

4 files changed

+86
-52
lines changed

4 files changed

+86
-52
lines changed

api.go

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
package danger
22

3+
import (
4+
"encoding/json"
5+
"fmt"
6+
)
7+
38
type T struct {
4-
Results Results
9+
results Results
510
}
611

712
func New() *T {
813
return &T{
9-
Results: Results{
14+
results: Results{
1015
Fails: []Violation{},
1116
Messages: []Violation{},
1217
Warnings: []Violation{},
@@ -15,10 +20,20 @@ func New() *T {
1520
}
1621
}
1722

23+
// Results returns the JSON marshalled from the messages, warnings, failures,
24+
// and markdowns that was added so far.
25+
func (s *T) Results() (string, error) {
26+
bb, err := json.Marshal(s.results)
27+
if err != nil {
28+
return "", fmt.Errorf("marshalling results: %w", err)
29+
}
30+
return string(bb), nil
31+
}
32+
1833
// Message adds the message to the Danger table. The only difference between
1934
// this and Warn is the emoji which shows in the table.
2035
func (s *T) Message(message string, file string, line int) {
21-
s.Results.Messages = append(s.Results.Messages,
36+
s.results.Messages = append(s.results.Messages,
2237
Violation{
2338
Message: message,
2439
File: file,
@@ -29,7 +44,7 @@ func (s *T) Message(message string, file string, line int) {
2944
// Warn adds the message to the Danger table. The message highlights
3045
// low-priority issues, but does not fail the build.
3146
func (s *T) Warn(message string, file string, line int) {
32-
s.Results.Warnings = append(s.Results.Warnings,
47+
s.results.Warnings = append(s.results.Warnings,
3348
Violation{
3449
Message: message,
3550
File: file,
@@ -39,7 +54,7 @@ func (s *T) Warn(message string, file string, line int) {
3954

4055
// Fail a build, outputting a specific reason for failing into an HTML table.
4156
func (s *T) Fail(message string, file string, line int) {
42-
s.Results.Fails = append(s.Results.Fails,
57+
s.results.Fails = append(s.results.Fails,
4358
Violation{
4459
Message: message,
4560
File: file,
@@ -50,7 +65,7 @@ func (s *T) Fail(message string, file string, line int) {
5065
// Markdown adds the message as raw markdown into the Danger comment, under the
5166
// table.
5267
func (s *T) Markdown(message string, file string, line int) {
53-
s.Results.Markdowns = append(s.Results.Markdowns,
68+
s.results.Markdowns = append(s.results.Markdowns,
5469
Violation{
5570
Message: message,
5671
File: file,

api_internal_test.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package danger
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/require"
7+
)
8+
9+
func TestMessage(t *testing.T) {
10+
d := New()
11+
12+
d.Message("a message", "", 0)
13+
14+
require.Equal(t,
15+
[]Violation{
16+
{Message: "a message"},
17+
},
18+
d.results.Messages)
19+
}
20+
21+
func TestWarn(t *testing.T) {
22+
d := New()
23+
24+
d.Warn("a warning", "", 0)
25+
26+
require.Equal(t,
27+
[]Violation{
28+
{Message: "a warning"},
29+
},
30+
d.results.Warnings)
31+
}
32+
33+
func TestFail(t *testing.T) {
34+
d := New()
35+
d.Fail("a failure", "", 0)
36+
37+
require.Equal(t,
38+
[]Violation{
39+
{Message: "a failure"},
40+
},
41+
d.results.Fails)
42+
}
43+
44+
func TestMarkdown(t *testing.T) {
45+
d := New()
46+
47+
d.Markdown("some markdown", "", 0)
48+
49+
require.Equal(t,
50+
[]Violation{
51+
{Message: "some markdown"},
52+
},
53+
d.results.Markdowns)
54+
}

api_test.go

Lines changed: 9 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -7,49 +7,14 @@ import (
77
"github.com/stretchr/testify/require"
88
)
99

10-
func TestMessage(t *testing.T) {
10+
func TestResults(t *testing.T) {
1111
d := danger.New()
12-
13-
d.Message("a message", "", 0)
14-
15-
require.Equal(t,
16-
[]danger.Violation{
17-
{Message: "a message"},
18-
},
19-
d.Results.Messages)
20-
}
21-
22-
func TestWarn(t *testing.T) {
23-
d := danger.New()
24-
25-
d.Warn("a warning", "", 0)
26-
27-
require.Equal(t,
28-
[]danger.Violation{
29-
{Message: "a warning"},
30-
},
31-
d.Results.Warnings)
32-
}
33-
34-
func TestFail(t *testing.T) {
35-
d := danger.New()
36-
d.Fail("a failure", "", 0)
37-
38-
require.Equal(t,
39-
[]danger.Violation{
40-
{Message: "a failure"},
41-
},
42-
d.Results.Fails)
43-
}
44-
45-
func TestMarkdown(t *testing.T) {
46-
d := danger.New()
47-
48-
d.Markdown("some markdown", "", 0)
49-
50-
require.Equal(t,
51-
[]danger.Violation{
52-
{Message: "some markdown"},
53-
},
54-
d.Results.Markdowns)
12+
r, err := d.Results()
13+
require.Nil(t, err)
14+
require.Equal(t, `{"fails":[],"warnings":[],"messages":[],"markdowns":[]}`, r)
15+
16+
d.Message("test", "", 0)
17+
r, err = d.Results()
18+
require.Nil(t, err)
19+
require.Equal(t, `{"fails":[],"warnings":[],"messages":[{"message":"test"}],"markdowns":[]}`, r)
5520
}

cmd/danger-go/runner/runner.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,11 @@ func Run() {
6363

6464
d := danger.New()
6565
fn(d, jsonData.Danger)
66-
respJSON, err := json.Marshal(d.Results)
66+
respJSON, err := d.Results()
6767
if err != nil {
6868
log.Fatalf("marshalling response: %s", err.Error())
6969
}
70-
fmt.Print(string(respJSON))
70+
fmt.Print(respJSON)
7171
}
7272

7373
// buildPlugin builds the plugin and stores the artifacts in a temporary

0 commit comments

Comments
 (0)