2021-06-03 13:06:57 +00:00
|
|
|
package jsonnet
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"testing"
|
2021-06-03 13:15:41 +00:00
|
|
|
|
|
|
|
"github.com/drone/drone/core"
|
2021-06-03 13:06:57 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestParse(t *testing.T) {
|
|
|
|
before, err := ioutil.ReadFile("../testdata/input.jsonnet")
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
after, err := ioutil.ReadFile("../testdata/input.jsonnet.golden")
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
req := &core.ConvertArgs{
|
|
|
|
Build: &core.Build{
|
|
|
|
After: "3d21ec53a331a6f037a91c368710b99387d012c1",
|
|
|
|
},
|
|
|
|
Repo: &core.Repository{
|
|
|
|
Slug: "octocat/hello-world",
|
|
|
|
Config: ".drone.yml",
|
|
|
|
},
|
|
|
|
Config: &core.Config{},
|
|
|
|
}
|
|
|
|
|
|
|
|
template := &core.Template{
|
|
|
|
Name: "my_template.jsonnet",
|
|
|
|
Data: string(before),
|
|
|
|
}
|
|
|
|
|
|
|
|
templateData := map[string]interface{}{
|
|
|
|
"stepName": "my_step",
|
|
|
|
"image": "my_image",
|
|
|
|
"commands": "my_command",
|
|
|
|
}
|
|
|
|
|
|
|
|
req.Config.Data = string(before)
|
|
|
|
|
2021-09-21 08:37:12 +00:00
|
|
|
parsedFile, err := Parse(req, nil, 0, template, templateData)
|
2021-06-03 13:06:57 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if want, got := parsedFile, string(after); want != got {
|
|
|
|
t.Errorf("Want %q got %q", want, got)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseJsonnetNotTemplateFile(t *testing.T) {
|
|
|
|
before, err := ioutil.ReadFile("../testdata/single.jsonnet")
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
after, err := ioutil.ReadFile("../testdata/input.jsonnet.golden")
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
req := &core.ConvertArgs{
|
|
|
|
Build: &core.Build{
|
|
|
|
After: "3d21ec53a331a6f037a91c368710b99387d012c1",
|
|
|
|
},
|
|
|
|
Repo: &core.Repository{
|
|
|
|
Slug: "octocat/hello-world",
|
|
|
|
Config: ".drone.jsonnet",
|
|
|
|
},
|
|
|
|
Config: &core.Config{},
|
|
|
|
}
|
|
|
|
|
|
|
|
req.Repo.Config = "plugin.jsonnet"
|
|
|
|
req.Config.Data = string(before)
|
|
|
|
|
2021-09-21 08:37:12 +00:00
|
|
|
parsedFile, err := Parse(req, nil, 0, nil, nil)
|
2021-06-03 13:06:57 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if want, got := parsedFile, string(after); want != got {
|
|
|
|
t.Errorf("Want %q got %q", want, got)
|
|
|
|
}
|
|
|
|
}
|