-
Notifications
You must be signed in to change notification settings - Fork 205
/
helpers.go
70 lines (59 loc) · 1.79 KB
/
helpers.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
package mixin
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"get.porter.sh/porter/pkg/pkgmgmt"
"get.porter.sh/porter/pkg/pkgmgmt/client"
"get.porter.sh/porter/pkg/portercontext"
)
type TestMixinProvider struct {
client.TestPackageManager
// LintResults allows you to provide linter.Results for your unit tests.
// It isn't of type linter.Results directly to avoid package cycles
LintResults interface{}
// ReturnBuildError will force the TestMixinProvider to return a build error
// if set to true
ReturnBuildError bool
}
// NewTestMixinProvider helps us test Porter.Mixins in our unit tests without actually hitting any real plugins on the file system.
func NewTestMixinProvider() *TestMixinProvider {
packages := []pkgmgmt.PackageMetadata{
&Metadata{
Name: "exec",
VersionInfo: pkgmgmt.VersionInfo{
Version: "v1.0",
Commit: "abc123",
Author: "Porter Authors",
},
},
}
provider := TestMixinProvider{
TestPackageManager: client.TestPackageManager{
PkgType: "mixins",
Packages: packages,
},
}
provider.RunAssertions = []func(pkgContext *portercontext.Context, name string, commandOpts pkgmgmt.CommandOptions) error{
provider.PrintExecOutput,
}
return &provider
}
func (p *TestMixinProvider) PrintExecOutput(pkgContext *portercontext.Context, name string, commandOpts pkgmgmt.CommandOptions) error {
switch commandOpts.Command {
case "build":
if p.ReturnBuildError {
return errors.New("encountered build error")
}
fmt.Fprintln(pkgContext.Out, "# exec mixin has no buildtime dependencies")
case "lint":
b, _ := json.Marshal(p.LintResults)
fmt.Fprintln(pkgContext.Out, string(b))
}
return nil
}
func (p *TestMixinProvider) GetSchema(name string) (string, error) {
b, err := ioutil.ReadFile("../exec/schema/exec.json")
return string(b), err
}