File: reporter_test.go

package info (click to toggle)
golang-github-approvals-go-approval-tests 0.0~git20170712.0.c1e747e-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 272 kB
  • ctags: 154
  • sloc: xml: 16; makefile: 3
file content (80 lines) | stat: -rw-r--r-- 1,895 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package reporters

import (
	"os"
	"testing"

	"github.com/approvals/go-approval-tests/utils"
)

type testReporter struct {
	called    bool
	succeeded bool
}

func newTestReporter(succeeded bool) *testReporter {
	return &testReporter{
		called:    false,
		succeeded: succeeded,
	}
}

func (s *testReporter) Report(approved, received string) bool {
	s.called = true
	return s.succeeded
}

func TestFirstWorkingReporter(t *testing.T) {
	a := newTestReporter(false)
	b := newTestReporter(true)
	c := newTestReporter(true)

	testSubject := NewFirstWorkingReporter(Reporter(a), Reporter(b), Reporter(c))
	testSubject.Report("a.txt", "b.txt")

	utils.AssertEqual(t, true, a.called, "a.called")
	utils.AssertEqual(t, true, b.called, "b.called")
	utils.AssertEqual(t, false, c.called, "c.called")
}

func TestMultiReporter(t *testing.T) {
	a := newTestReporter(true)
	b := newTestReporter(true)

	testSubject := NewMultiReporter(Reporter(a), Reporter(b))
	result := testSubject.Report("a.txt", "b.txt")

	utils.AssertEqual(t, true, a.called, "a.called")
	utils.AssertEqual(t, true, b.called, "b.called")
	utils.AssertEqual(t, true, result, "result")
}

func TestMultiReporterWithNoWorkingReporters(t *testing.T) {
	a := newTestReporter(false)
	b := newTestReporter(false)

	testSubject := NewMultiReporter(Reporter(a), Reporter(b))
	result := testSubject.Report("a.txt", "b.txt")

	utils.AssertEqual(t, true, a.called, "a.called")
	utils.AssertEqual(t, true, b.called, "b.called")
	utils.AssertEqual(t, false, result, "result")
}

func restoreEnv(exists bool, key, value string) {
	if exists {
		os.Setenv(key, value)
	} else {
		os.Unsetenv(key)
	}
}

func TestCIReporter(t *testing.T) {
	value, exists := os.LookupEnv("CI")

	os.Setenv("CI", "true")
	defer restoreEnv(exists, "CI", value)

	r := NewContinuousIntegrationReporter()
	utils.AssertEqual(t, true, r.Report("", ""), "did not detect CI")
}