Files
releases/cmd/capabilities_test.go
T
Anders Eknert db035b09fc Add support for Go 1.27 & jsonv2 (#8947)
Makes OPA build and pass its tests on Go 1.27, while keeping Go 1.25 and
1.26 working. JSON output is unchanged on every supported version.

Go 1.27 json package honours `encoding.TextAppender`. Many v1 ast types
implement AppendText to build their Rego string cheaply, so on 1.27 they
would have marshalled as Rego text. Files built only with 1.27 now
implement MarshalJSONTo.

Library users should keep using `json.Marshal` etc. The MarshalJSONTo
methods are implementation details, are absent from 1.25 and 1.26
builds, and may change.

---------

Signed-off-by: Anders Eknert <anders.eknert@apple.com>
Signed-off-by: Charlie Egan <charlie_egan@apple.com>
Co-authored-by: Charlie Egan <charlie_egan@apple.com>
2026-07-30 17:41:28 +01:00

219 lines
4.1 KiB
Go

//go:build !go1.27
// Copyright 2022 The OPA Authors. All rights reserved.
// Use of this source code is governed by an Apache2
// license that can be found in the LICENSE file.
package cmd
import (
"bytes"
"path"
"slices"
"sort"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/open-policy-agent/opa/v1/ast"
"github.com/open-policy-agent/opa/v1/util/test"
)
func TestCapabilitiesNoArgs(t *testing.T) {
t.Run("test with no arguments", func(t *testing.T) {
_, err := doCapabilities(capabilitiesParams{})
if err != nil {
t.Fatal("expected success", err)
}
})
}
func TestCapabilitiesVersion(t *testing.T) {
t.Run("test with version", func(t *testing.T) {
params := capabilitiesParams{
version: "v0.39.0",
}
_, err := doCapabilities(params)
if err != nil {
t.Fatal("expected success", err)
}
})
}
func TestCapabilitiesFile(t *testing.T) {
t.Run("test with file", func(t *testing.T) {
files := map[string]string{
"test-capabilities.json": `
{
"builtins": [
{
"name": "plus",
"infix": "+",
"decl": {
"type": "function",
"args": [
{
"type": "number"
},
{
"type": "number"
}
],
"result": {
"type": "number"
}
}
}
]
}
`,
}
test.WithTempFS(files, func(root string) {
params := capabilitiesParams{
file: path.Join(root, "test-capabilities.json"),
}
_, err := doCapabilities(params)
if err != nil {
t.Fatal("expected success", err)
}
})
})
}
func TestCapabilitiesJSONOutputBytes(t *testing.T) {
files := map[string]string{
"test-capabilities.json": `
{
"builtins": [
{
"name": "plus",
"infix": "+",
"decl": {
"type": "function",
"args": [
{
"type": "number"
},
{
"type": "number"
}
],
"result": {
"type": "number"
}
}
}
]
}
`,
}
test.WithTempFS(files, func(root string) {
params := capabilitiesParams{
file: path.Join(root, "test-capabilities.json"),
}
got, err := doCapabilities(params)
if err != nil {
t.Fatal("expected success", err)
}
expected := `{
"builtins": [
{
"name": "plus",
"decl": {
"args": [
{
"type": "number"
},
{
"type": "number"
}
],
"result": {
"type": "number"
},
"type": "function"
},
"infix": "+"
}
]
}`
if diff := cmp.Diff(expected, got); diff != "" {
t.Errorf("unexpected result (-want, +got):\n%s", diff)
}
})
}
func TestCapabilitiesCurrent(t *testing.T) {
tests := []struct {
note string
v0Compatible bool
expFeatures []string
expFutureKeywords []string
}{
{
note: "current",
expFeatures: []string{
ast.FeatureRegoV1,
ast.FeatureKeywordsInRefs,
ast.FeatureTemplateStrings,
},
expFutureKeywords: []string{
"not",
},
},
{
note: "current --v0-compatible",
v0Compatible: true,
expFeatures: []string{
ast.FeatureRefHeadStringPrefixes,
ast.FeatureRefHeads,
ast.FeatureRegoV1Import,
ast.FeatureRegoV1,
ast.FeatureKeywordsInRefs,
},
expFutureKeywords: []string{
"in",
"every",
"contains",
"if",
"not",
},
},
}
for _, tc := range tests {
t.Run(tc.note, func(t *testing.T) {
// These are sorted in the output
sort.Strings(tc.expFutureKeywords)
sort.Strings(tc.expFeatures)
params := capabilitiesParams{
showCurrent: true,
v0Compatible: tc.v0Compatible,
}
capsStr, err := doCapabilities(params)
if err != nil {
t.Fatal("expected success", err)
}
caps, err := ast.LoadCapabilitiesJSON(bytes.NewReader([]byte(capsStr)))
if err != nil {
t.Fatal("expected success", err)
}
if !slices.Equal(caps.Features, tc.expFeatures) {
t.Errorf("expected features:\n\n%v\n\nbut got:\n\n%v", tc.expFeatures, caps.Features)
}
if !slices.Equal(caps.FutureKeywords, tc.expFutureKeywords) {
t.Errorf("expected future keywords:\n\n%v\n\nbut got:\n\n%v", tc.expFutureKeywords, caps.FutureKeywords)
}
})
}
}