-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcontroller_test.go
103 lines (79 loc) · 2.59 KB
/
controller_test.go
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package halfpipe
import (
"errors"
"testing"
"github.com/springernature/halfpipe/mapper"
"github.com/springernature/halfpipe/project"
"github.com/spf13/afero"
"github.com/springernature/halfpipe/defaults"
"github.com/springernature/halfpipe/linters"
"github.com/springernature/halfpipe/manifest"
"github.com/stretchr/testify/assert"
)
var validHalfpipeManifest = manifest.Manifest{
Team: "asdf",
Pipeline: "my-pipeline",
Tasks: manifest.TaskList{
manifest.DockerCompose{},
},
}
type fakeRenderer struct{}
func (f fakeRenderer) Render(manifest manifest.Manifest) (string, error) {
return "fake output", nil
}
func testController() controller {
var fs = afero.Afero{Fs: afero.NewMemMapFs()}
_ = fs.MkdirAll("/pwd/foo/.git", 0777)
return controller{
defaulter: defaults.New(defaults.Concourse, project.Data{}),
mapper: mapper.New(),
renderer: fakeRenderer{},
}
}
func TestWorksForHalfpipeFileWithYMLExtension(t *testing.T) {
c := testController()
response, _ := c.Process(validHalfpipeManifest)
assert.Len(t, response.LintResults.Error(), 0)
}
func TestWorksForHalfpipeFile(t *testing.T) {
c := testController()
response, _ := c.Process(validHalfpipeManifest)
assert.Len(t, response.LintResults.Error(), 0)
}
type fakeLinter struct {
Error error
}
func (f fakeLinter) Lint(manifest manifest.Manifest) linters.LintResult {
return linters.NewLintResult("fake", "url", []error{f.Error})
}
func TestAppliesAllLinters(t *testing.T) {
c := testController()
linter1 := fakeLinter{errors.New("error from linter1")}
linter2 := fakeLinter{errors.New("error from linter2")}
c.linters = []linters.Linter{linter1, linter2}
response, _ := c.Process(validHalfpipeManifest)
assert.Empty(t, response.ConfigYaml)
assert.Len(t, response.LintResults, 2)
assert.Equal(t, linter1.Error, response.LintResults[0].Issues[0])
assert.Equal(t, linter2.Error, response.LintResults[1].Issues[0])
}
func TestGivesBackConfigWhenLinterPasses(t *testing.T) {
c := testController()
response, _ := c.Process(validHalfpipeManifest)
assert.Len(t, response.LintResults, 0)
assert.Equal(t, "fake output", response.ConfigYaml)
}
type FakeMapper struct {
err error
}
func (f FakeMapper) Apply(original manifest.Manifest) (updated manifest.Manifest, err error) {
return original, f.err
}
func TestGivesBackABadTestResultWhenAMapperFails(t *testing.T) {
c := testController()
c.mapper = FakeMapper{err: errors.New("blurgh")}
response, _ := c.Process(validHalfpipeManifest)
assert.Len(t, response.LintResults, 1)
assert.True(t, response.LintResults.HasErrors())
assert.False(t, response.LintResults.HasWarnings())
}