2018-10-16 13:02:10 -04:00
|
|
|
// +build !windows
|
|
|
|
|
2015-05-19 17:25:56 -04:00
|
|
|
package template
|
|
|
|
|
|
|
|
import (
|
2019-02-23 17:38:04 -05:00
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
2015-06-14 01:50:02 -04:00
|
|
|
"path/filepath"
|
2015-05-19 17:25:56 -04:00
|
|
|
"reflect"
|
2015-05-26 12:41:42 -04:00
|
|
|
"strings"
|
2015-05-19 17:25:56 -04:00
|
|
|
"testing"
|
2015-05-21 15:34:44 -04:00
|
|
|
"time"
|
2019-02-20 06:48:52 -05:00
|
|
|
|
|
|
|
"github.com/google/go-cmp/cmp"
|
2015-05-19 17:25:56 -04:00
|
|
|
)
|
|
|
|
|
2019-04-03 14:32:49 -04:00
|
|
|
func boolPointer(tf bool) *bool {
|
|
|
|
return &tf
|
|
|
|
}
|
|
|
|
|
2015-05-19 17:25:56 -04:00
|
|
|
func TestParse(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
File string
|
|
|
|
Result *Template
|
|
|
|
Err bool
|
|
|
|
}{
|
2015-05-21 15:34:44 -04:00
|
|
|
/*
|
|
|
|
* Builders
|
|
|
|
*/
|
2015-05-19 17:25:56 -04:00
|
|
|
{
|
|
|
|
"parse-basic.json",
|
|
|
|
&Template{
|
|
|
|
Builders: map[string]*Builder{
|
2016-11-01 17:08:04 -04:00
|
|
|
"something": {
|
2015-05-19 17:25:56 -04:00
|
|
|
Name: "something",
|
|
|
|
Type: "something",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
2019-02-23 17:38:04 -05:00
|
|
|
{
|
|
|
|
"parse-basic-config.json",
|
|
|
|
&Template{
|
|
|
|
Builders: map[string]*Builder{
|
|
|
|
"something": {
|
|
|
|
Name: "something",
|
|
|
|
Type: "something",
|
|
|
|
Config: map[string]interface{}{
|
|
|
|
"foo": "bar",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
2015-05-19 17:25:56 -04:00
|
|
|
{
|
|
|
|
"parse-builder-no-type.json",
|
|
|
|
nil,
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"parse-builder-repeat.json",
|
|
|
|
nil,
|
|
|
|
true,
|
|
|
|
},
|
2015-05-21 15:34:44 -04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Provisioners
|
|
|
|
*/
|
|
|
|
{
|
|
|
|
"parse-provisioner-basic.json",
|
|
|
|
&Template{
|
|
|
|
Provisioners: []*Provisioner{
|
2016-11-01 17:08:04 -04:00
|
|
|
{
|
2015-05-21 15:34:44 -04:00
|
|
|
Type: "something",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
2019-02-23 17:38:04 -05:00
|
|
|
{
|
|
|
|
"parse-provisioner-config.json",
|
|
|
|
&Template{
|
|
|
|
Provisioners: []*Provisioner{
|
|
|
|
{
|
|
|
|
Type: "something",
|
|
|
|
Config: map[string]interface{}{
|
|
|
|
"inline": "echo 'foo'",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
2015-05-21 15:34:44 -04:00
|
|
|
{
|
|
|
|
"parse-provisioner-pause-before.json",
|
|
|
|
&Template{
|
|
|
|
Provisioners: []*Provisioner{
|
2016-11-01 17:08:04 -04:00
|
|
|
{
|
2015-05-21 15:34:44 -04:00
|
|
|
Type: "something",
|
|
|
|
PauseBefore: 1 * time.Second,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
false,
|
2019-04-04 10:37:46 -04:00
|
|
|
},
|
|
|
|
|
2020-04-16 05:58:54 -04:00
|
|
|
{
|
|
|
|
"parse-provisioner-retry.json",
|
|
|
|
&Template{
|
|
|
|
Provisioners: []*Provisioner{
|
|
|
|
{
|
|
|
|
Type: "something",
|
2020-08-05 11:41:20 -04:00
|
|
|
MaxRetries: "5",
|
2020-04-16 05:58:54 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
|
2019-04-04 10:37:46 -04:00
|
|
|
{
|
|
|
|
"parse-provisioner-timeout.json",
|
|
|
|
&Template{
|
|
|
|
Provisioners: []*Provisioner{
|
|
|
|
{
|
|
|
|
Type: "something",
|
|
|
|
Timeout: 5 * time.Minute,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
false,
|
2015-05-21 15:34:44 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
"parse-provisioner-only.json",
|
|
|
|
&Template{
|
|
|
|
Provisioners: []*Provisioner{
|
2016-11-01 17:08:04 -04:00
|
|
|
{
|
2015-05-21 15:34:44 -04:00
|
|
|
Type: "something",
|
|
|
|
OnlyExcept: OnlyExcept{
|
|
|
|
Only: []string{"foo"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
"parse-provisioner-except.json",
|
|
|
|
&Template{
|
|
|
|
Provisioners: []*Provisioner{
|
2016-11-01 17:08:04 -04:00
|
|
|
{
|
2015-05-21 15:34:44 -04:00
|
|
|
Type: "something",
|
|
|
|
OnlyExcept: OnlyExcept{
|
|
|
|
Except: []string{"foo"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
"parse-provisioner-override.json",
|
|
|
|
&Template{
|
|
|
|
Provisioners: []*Provisioner{
|
2016-11-01 17:08:04 -04:00
|
|
|
{
|
2015-05-21 15:34:44 -04:00
|
|
|
Type: "something",
|
|
|
|
Override: map[string]interface{}{
|
|
|
|
"foo": map[string]interface{}{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
"parse-provisioner-no-type.json",
|
|
|
|
nil,
|
|
|
|
true,
|
|
|
|
},
|
2015-05-21 15:40:33 -04:00
|
|
|
|
|
|
|
{
|
|
|
|
"parse-variable-default.json",
|
|
|
|
&Template{
|
|
|
|
Variables: map[string]*Variable{
|
2016-11-01 17:08:04 -04:00
|
|
|
"foo": {
|
2015-05-21 15:40:33 -04:00
|
|
|
Default: "foo",
|
2019-02-23 17:38:04 -05:00
|
|
|
Key: "foo",
|
2015-05-21 15:40:33 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
"parse-variable-required.json",
|
|
|
|
&Template{
|
|
|
|
Variables: map[string]*Variable{
|
2016-11-01 17:08:04 -04:00
|
|
|
"foo": {
|
2015-05-21 15:40:33 -04:00
|
|
|
Required: true,
|
2019-02-23 17:38:04 -05:00
|
|
|
Key: "foo",
|
2015-05-21 15:40:33 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
2015-05-21 16:32:22 -04:00
|
|
|
|
|
|
|
{
|
|
|
|
"parse-pp-basic.json",
|
|
|
|
&Template{
|
|
|
|
PostProcessors: [][]*PostProcessor{
|
2016-11-01 17:08:04 -04:00
|
|
|
{
|
|
|
|
{
|
2019-02-20 06:49:21 -05:00
|
|
|
Name: "foo",
|
2015-05-21 16:32:22 -04:00
|
|
|
Type: "foo",
|
|
|
|
Config: map[string]interface{}{
|
|
|
|
"foo": "bar",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
"parse-pp-keep.json",
|
|
|
|
&Template{
|
|
|
|
PostProcessors: [][]*PostProcessor{
|
2016-11-01 17:08:04 -04:00
|
|
|
{
|
|
|
|
{
|
2019-02-20 06:49:21 -05:00
|
|
|
Name: "foo",
|
2015-05-21 16:32:22 -04:00
|
|
|
Type: "foo",
|
2019-04-03 14:32:49 -04:00
|
|
|
KeepInputArtifact: boolPointer(true),
|
2015-05-21 16:32:22 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
|
2015-06-04 23:26:33 -04:00
|
|
|
{
|
|
|
|
"parse-pp-only.json",
|
|
|
|
&Template{
|
|
|
|
PostProcessors: [][]*PostProcessor{
|
2016-11-01 17:08:04 -04:00
|
|
|
{
|
|
|
|
{
|
2019-02-20 06:49:21 -05:00
|
|
|
Name: "foo",
|
2015-06-04 23:26:33 -04:00
|
|
|
Type: "foo",
|
|
|
|
OnlyExcept: OnlyExcept{
|
|
|
|
Only: []string{"bar"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
"parse-pp-except.json",
|
|
|
|
&Template{
|
|
|
|
PostProcessors: [][]*PostProcessor{
|
2016-11-01 17:08:04 -04:00
|
|
|
{
|
|
|
|
{
|
2019-02-20 06:49:21 -05:00
|
|
|
Name: "foo",
|
2015-06-04 23:26:33 -04:00
|
|
|
Type: "foo",
|
|
|
|
OnlyExcept: OnlyExcept{
|
|
|
|
Except: []string{"bar"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
|
2015-05-21 16:32:22 -04:00
|
|
|
{
|
|
|
|
"parse-pp-string.json",
|
|
|
|
&Template{
|
|
|
|
PostProcessors: [][]*PostProcessor{
|
2016-11-01 17:08:04 -04:00
|
|
|
{
|
|
|
|
{
|
2019-02-20 06:49:21 -05:00
|
|
|
Name: "foo",
|
2015-05-21 16:32:22 -04:00
|
|
|
Type: "foo",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
"parse-pp-map.json",
|
|
|
|
&Template{
|
|
|
|
PostProcessors: [][]*PostProcessor{
|
2016-11-01 17:08:04 -04:00
|
|
|
{
|
|
|
|
{
|
2019-02-20 06:49:21 -05:00
|
|
|
Name: "foo",
|
2015-05-21 16:32:22 -04:00
|
|
|
Type: "foo",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
"parse-pp-slice.json",
|
|
|
|
&Template{
|
|
|
|
PostProcessors: [][]*PostProcessor{
|
2016-11-01 17:08:04 -04:00
|
|
|
{
|
|
|
|
{
|
2019-02-20 06:49:21 -05:00
|
|
|
Name: "foo",
|
2015-05-21 16:32:22 -04:00
|
|
|
Type: "foo",
|
|
|
|
},
|
|
|
|
},
|
2016-11-01 17:08:04 -04:00
|
|
|
{
|
|
|
|
{
|
2019-02-20 06:49:21 -05:00
|
|
|
Name: "bar",
|
2015-05-21 16:32:22 -04:00
|
|
|
Type: "bar",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
"parse-pp-multi.json",
|
|
|
|
&Template{
|
|
|
|
PostProcessors: [][]*PostProcessor{
|
2016-11-01 17:08:04 -04:00
|
|
|
{
|
|
|
|
{
|
2019-02-20 06:49:21 -05:00
|
|
|
Name: "foo",
|
2015-05-21 16:32:22 -04:00
|
|
|
Type: "foo",
|
|
|
|
},
|
2016-11-01 17:08:04 -04:00
|
|
|
{
|
2019-02-20 06:49:21 -05:00
|
|
|
Name: "bar",
|
2015-05-21 16:32:22 -04:00
|
|
|
Type: "bar",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
"parse-pp-no-type.json",
|
|
|
|
nil,
|
|
|
|
true,
|
|
|
|
},
|
2015-05-21 16:41:33 -04:00
|
|
|
|
|
|
|
{
|
|
|
|
"parse-description.json",
|
|
|
|
&Template{
|
|
|
|
Description: "foo",
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
"parse-min-version.json",
|
|
|
|
&Template{
|
|
|
|
MinVersion: "1.2",
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
2015-05-21 16:44:29 -04:00
|
|
|
|
2015-06-13 16:19:25 -04:00
|
|
|
{
|
|
|
|
"parse-comment.json",
|
|
|
|
&Template{
|
|
|
|
Builders: map[string]*Builder{
|
2016-11-01 17:08:04 -04:00
|
|
|
"something": {
|
2015-06-13 16:19:25 -04:00
|
|
|
Name: "something",
|
|
|
|
Type: "something",
|
|
|
|
},
|
|
|
|
},
|
2019-02-22 19:09:07 -05:00
|
|
|
Comments: map[string]string{
|
|
|
|
"_info": "foo",
|
|
|
|
},
|
2015-06-13 16:19:25 -04:00
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
2019-02-23 17:38:04 -05:00
|
|
|
{
|
|
|
|
"parse-monolithic.json",
|
|
|
|
&Template{
|
|
|
|
Comments: map[string]string{
|
|
|
|
"_comment": "comment",
|
|
|
|
},
|
|
|
|
Description: "Description Test",
|
|
|
|
MinVersion: "1.3.0",
|
|
|
|
SensitiveVariables: []*Variable{
|
|
|
|
{
|
|
|
|
Required: false,
|
|
|
|
Key: "one",
|
|
|
|
Default: "1",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Variables: map[string]*Variable{
|
|
|
|
"one": {
|
|
|
|
Required: false,
|
|
|
|
Key: "one",
|
|
|
|
Default: "1",
|
|
|
|
},
|
|
|
|
"two": {
|
|
|
|
Required: false,
|
|
|
|
Key: "two",
|
|
|
|
Default: "2",
|
|
|
|
},
|
|
|
|
"three": {
|
|
|
|
Required: true,
|
|
|
|
Key: "three",
|
|
|
|
Default: "",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Builders: map[string]*Builder{
|
|
|
|
"amazon-ebs": {
|
|
|
|
Name: "amazon-ebs",
|
|
|
|
Type: "amazon-ebs",
|
|
|
|
Config: map[string]interface{}{
|
|
|
|
"ami_name": "AMI Name",
|
|
|
|
"instance_type": "t2.micro",
|
|
|
|
"ssh_username": "ec2-user",
|
|
|
|
"source_ami": "ami-aaaaaaaaaaaaaa",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"docker": {
|
|
|
|
Name: "docker",
|
|
|
|
Type: "docker",
|
|
|
|
Config: map[string]interface{}{
|
|
|
|
"image": "ubuntu",
|
|
|
|
"export_path": "image.tar",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Provisioners: []*Provisioner{
|
|
|
|
{
|
|
|
|
Type: "shell",
|
|
|
|
Config: map[string]interface{}{
|
|
|
|
"script": "script.sh",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Type: "shell",
|
|
|
|
Config: map[string]interface{}{
|
|
|
|
"script": "script.sh",
|
|
|
|
},
|
|
|
|
Override: map[string]interface{}{
|
|
|
|
"docker": map[string]interface{}{
|
|
|
|
"execute_command": "echo 'override'",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
PostProcessors: [][]*PostProcessor{
|
|
|
|
{
|
|
|
|
{
|
2019-02-27 09:35:41 -05:00
|
|
|
Name: "compress",
|
2019-02-23 17:38:04 -05:00
|
|
|
Type: "compress",
|
|
|
|
},
|
|
|
|
{
|
2019-02-27 09:35:41 -05:00
|
|
|
Name: "vagrant",
|
2019-02-23 17:38:04 -05:00
|
|
|
Type: "vagrant",
|
|
|
|
OnlyExcept: OnlyExcept{
|
|
|
|
Only: []string{"docker"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
{
|
2019-02-27 09:35:41 -05:00
|
|
|
Name: "shell-local",
|
2019-02-23 17:38:04 -05:00
|
|
|
Type: "shell-local",
|
|
|
|
Config: map[string]interface{}{
|
|
|
|
"inline": []interface{}{"echo foo"},
|
|
|
|
},
|
|
|
|
OnlyExcept: OnlyExcept{
|
|
|
|
Except: []string{"amazon-ebs"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
2015-05-19 17:25:56 -04:00
|
|
|
}
|
|
|
|
|
2019-02-20 06:48:52 -05:00
|
|
|
for i, tc := range cases {
|
2015-06-14 01:50:02 -04:00
|
|
|
path, _ := filepath.Abs(fixtureDir(tc.File))
|
2015-05-23 18:44:54 -04:00
|
|
|
tpl, err := ParseFile(fixtureDir(tc.File))
|
2015-05-19 17:25:56 -04:00
|
|
|
if (err != nil) != tc.Err {
|
2019-02-23 17:38:04 -05:00
|
|
|
t.Fatalf("%s\n\nerr: %s", tc.File, err)
|
2015-05-19 17:25:56 -04:00
|
|
|
}
|
|
|
|
|
2015-05-29 17:05:13 -04:00
|
|
|
if tc.Result != nil {
|
|
|
|
tc.Result.Path = path
|
|
|
|
}
|
2015-05-26 12:38:02 -04:00
|
|
|
if tpl != nil {
|
|
|
|
tpl.RawContents = nil
|
|
|
|
}
|
2019-02-20 06:48:52 -05:00
|
|
|
if diff := cmp.Diff(tpl, tc.Result); diff != "" {
|
2019-02-20 06:54:57 -05:00
|
|
|
t.Fatalf("[%d]bad: %s\n%v", i, tc.File, diff)
|
2015-05-19 17:25:56 -04:00
|
|
|
}
|
2019-02-23 17:38:04 -05:00
|
|
|
|
|
|
|
// Only test template writing if the template is valid
|
|
|
|
if tc.Err == false {
|
|
|
|
// Get rawTemplate
|
|
|
|
raw, err := tpl.Raw()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to convert back to raw template: %s\n\n%v\n\n%s", tc.File, tpl, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
out, _ := json.MarshalIndent(raw, "", " ")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to marshal raw template: %s\n\n%v\n\n%s", tc.File, raw, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write JSON to a buffer (emulates filesystem write without dirtying the workspace)
|
|
|
|
fileBuf := bytes.NewBuffer(out)
|
|
|
|
|
|
|
|
// Parse the JSON template we wrote to our buffer
|
|
|
|
tplRewritten, err := Parse(fileBuf)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to re-read marshalled template: %s\n\n%v\n\n%s\n\n%s", tc.File, tpl, out, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Override the metadata we don't care about (file path, raw file contents)
|
|
|
|
tplRewritten.Path = path
|
|
|
|
tplRewritten.RawContents = nil
|
|
|
|
|
|
|
|
// Test that our output raw template is functionally equal
|
|
|
|
if !reflect.DeepEqual(tpl, tplRewritten) {
|
|
|
|
t.Fatalf("Data lost when writing template to file: %s\n\n%v\n\n%v\n\n%s", tc.File, tpl, tplRewritten, out)
|
|
|
|
}
|
|
|
|
}
|
2015-05-19 17:25:56 -04:00
|
|
|
}
|
|
|
|
}
|
2015-05-26 12:41:42 -04:00
|
|
|
|
|
|
|
func TestParse_contents(t *testing.T) {
|
|
|
|
tpl, err := ParseFile(fixtureDir("parse-contents.json"))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
actual := strings.TrimSpace(string(tpl.RawContents))
|
|
|
|
expected := `{"builders":[{"type":"test"}]}`
|
|
|
|
if actual != expected {
|
|
|
|
t.Fatalf("bad: %s\n\n%s", actual, expected)
|
|
|
|
}
|
|
|
|
}
|
2015-10-25 15:28:06 -04:00
|
|
|
|
|
|
|
func TestParse_bad(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
File string
|
|
|
|
Expected string
|
|
|
|
}{
|
2016-02-10 14:52:26 -05:00
|
|
|
{"error-beginning.json", "line 1, column 1 (offset 1)"},
|
|
|
|
{"error-middle.json", "line 5, column 6 (offset 50)"},
|
|
|
|
{"error-end.json", "line 1, column 30 (offset 30)"},
|
2017-05-19 16:22:16 -04:00
|
|
|
{"malformed.json", "line 16, column 3 (offset 433)"},
|
2015-10-25 15:28:06 -04:00
|
|
|
}
|
|
|
|
for _, tc := range cases {
|
|
|
|
_, err := ParseFile(fixtureDir(tc.File))
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("expected error")
|
|
|
|
}
|
|
|
|
if !strings.Contains(err.Error(), tc.Expected) {
|
|
|
|
t.Fatalf("file: %s\nExpected: %s\n%s\n", tc.File, tc.Expected, err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-02-12 08:34:20 -05:00
|
|
|
|
|
|
|
func TestParse_checkForDuplicateFields(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
File string
|
|
|
|
Expected string
|
|
|
|
}{
|
|
|
|
{"error-duplicate-variables.json", "template has duplicate field: variables"},
|
|
|
|
{"error-duplicate-config.json", "template has duplicate field: foo"},
|
|
|
|
}
|
|
|
|
for _, tc := range cases {
|
|
|
|
_, err := ParseFile(fixtureDir(tc.File))
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("expected error")
|
|
|
|
}
|
|
|
|
if !strings.Contains(err.Error(), tc.Expected) {
|
|
|
|
t.Fatalf("file: %s\nExpected: %s\n%s\n", tc.File, tc.Expected, err.Error())
|
|
|
|
}
|
|
|
|
}
|
2020-02-12 08:44:28 -05:00
|
|
|
}
|