Improve features for verify (#27)
* let contains condition support multiple level attribute, show error message when template error
* Add test case and fix add indent way
diff --git a/commands/verify/verify.go b/commands/verify/verify.go
index 3307f43..3f5146a 100644
--- a/commands/verify/verify.go
+++ b/commands/verify/verify.go
@@ -78,7 +78,7 @@
if me, ok := err.(*verifier.MismatchError); ok {
return fmt.Errorf("failed to verify the output: %s, error: %v", sourceName, me.Error())
}
- return fmt.Errorf("failed to verify the output: %s", sourceName)
+ return fmt.Errorf("failed to verify the output: %s, error: %v", sourceName, err)
}
logger.Log.Infof("verified the output: %s\n", sourceName)
return nil
diff --git a/internal/components/verifier/verifier_test.go b/internal/components/verifier/verifier_test.go
index 0d6b7e7..d2a287c 100644
--- a/internal/components/verifier/verifier_test.go
+++ b/internal/components/verifier/verifier_test.go
@@ -139,6 +139,62 @@
},
wantErr: true,
},
+ {
+ name: "multiple level attribute and contains greater and equals 2",
+ args: args{
+ actualData: `
+metrics:
+ key:
+ - name: business-zone::projectA
+ id: YnVzaW5lc3Mtem9uZTo6cHJvamVjdEE=.1
+ value: 1
+ - name: system::load balancer1
+ id: c3lzdGVtOjpsb2FkIGJhbGFuY2VyMQ==.1
+ value: 0
+ - name: system::load balancer2
+ id: WW91cl9BcHBsaWNhdGlvbk5hbWU=.1
+ value: 2
+`,
+ expectedTemplate: `
+metrics:
+ key:
+ {{- contains .metrics.key }}
+ - name: {{ notEmpty .name }}
+ id: {{ notEmpty .id }}
+ value: {{ ge .value 2 }}
+ {{- end }}
+`,
+ },
+ wantErr: false,
+ },
+ {
+ name: "multiple level attribute and contains greater 2",
+ args: args{
+ actualData: `
+metrics:
+ key:
+ - name: business-zone::projectA
+ id: YnVzaW5lc3Mtem9uZTo6cHJvamVjdEE=.1
+ value: 1
+ - name: system::load balancer1
+ id: c3lzdGVtOjpsb2FkIGJhbGFuY2VyMQ==.1
+ value: 0
+ - name: system::load balancer2
+ id: WW91cl9BcHBsaWNhdGlvbk5hbWU=.1
+ value: 2
+`,
+ expectedTemplate: `
+metrics:
+ key:
+ {{- contains .metrics.key }}
+ - name: {{ notEmpty .name }}
+ id: {{ notEmpty .id }}
+ value: {{ gt .value 2 }}
+ {{- end }}
+`,
+ },
+ wantErr: true,
+ },
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
diff --git a/third-party/go/template/exec.go b/third-party/go/template/exec.go
index 0d88f70..24ff03d 100644
--- a/third-party/go/template/exec.go
+++ b/third-party/go/template/exec.go
@@ -456,6 +456,11 @@
}
}
+ var addRootIndent = func(b []byte, n int) []byte {
+ prefix := append([]byte("\n"), bytes.Repeat([]byte(" "), n)...)
+ b = append(prefix[1:], b...) // Indent first line
+ return bytes.ReplaceAll(b, []byte("\n"), prefix)
+ }
var marshal []byte
if len(matched) == expectedSize {
value, _ := printableValue(val)
@@ -463,6 +468,9 @@
} else {
marshal, _ = yaml.Marshal(output)
}
+
+ listTokenIndex := strings.Index(strings.TrimPrefix(r.List.Nodes[0].String(), "\n"), "-")
+ marshal = addRootIndent(marshal, listTokenIndex)
s.wr.Write(append([]byte("\n"), marshal...))
return
case reflect.Map: