harness-drone/plugin/converter/jsonnet_test.go

105 lines
2.2 KiB
Go
Raw Normal View History

2019-09-03 06:05:30 +00:00
// Copyright 2019 Drone.IO Inc. All rights reserved.
// Use of this source code is governed by the Drone Non-Commercial License
// that can be found in the LICENSE file.
// +build !oss
package converter
import (
"testing"
"github.com/drone/drone/core"
)
const jsonnetFile = `{"foo": "bar"}`
const jsonnetFileAfter = `---
{
"foo": "bar"
}
`
const jsonnetStream = `[{"foo": "bar"}]`
const jsonnetStreamAfter = `---
{
"foo": "bar"
}
`
func TestJsonnet_Stream(t *testing.T) {
args := &core.ConvertArgs{
Repo: &core.Repository{Config: ".drone.jsonnet"},
Config: &core.Config{Data: jsonnetStream},
}
service := Jsonnet(true)
res, err := service.Convert(noContext, args)
if err != nil {
t.Error(err)
return
}
if res == nil {
t.Errorf("Expected a converted file, got nil")
return
}
if got, want := res.Data, jsonnetStreamAfter; got != want {
t.Errorf("Want converted file %q, got %q", want, got)
}
}
func TestJsonnet_Snippet(t *testing.T) {
args := &core.ConvertArgs{
Repo: &core.Repository{Config: ".drone.jsonnet"},
Config: &core.Config{Data: jsonnetFile},
}
service := Jsonnet(true)
res, err := service.Convert(noContext, args)
if err != nil {
t.Error(err)
return
}
if res == nil {
t.Errorf("Expected a converted file, got nil")
return
}
if got, want := res.Data, jsonnetFileAfter; got != want {
t.Errorf("Want converted file %q, got %q", want, got)
}
}
func TestJsonnet_Error(t *testing.T) {
args := &core.ConvertArgs{
Repo: &core.Repository{Config: ".drone.jsonnet"},
Config: &core.Config{Data: "\\"}, // invalid jsonnet
}
service := Jsonnet(true)
_, err := service.Convert(noContext, args)
if err == nil {
t.Errorf("Expect jsonnet parsing error, got nil")
}
}
func TestJsonnet_Disabled(t *testing.T) {
service := Jsonnet(false)
res, err := service.Convert(noContext, nil)
if err != nil {
t.Error(err)
}
if res != nil {
t.Errorf("Expect nil response when disabled")
}
}
func TestJsonnet_NotJsonnet(t *testing.T) {
args := &core.ConvertArgs{
Repo: &core.Repository{Config: ".drone.yml"},
}
service := Jsonnet(true)
res, err := service.Convert(noContext, args)
if err != nil {
t.Error(err)
}
if res != nil {
t.Errorf("Expect nil response when not jsonnet")
}
}