105 lines
3.6 KiB
Go
105 lines
3.6 KiB
Go
package apigen
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestPrimitiveArrayContracts(t *testing.T) {
|
|
for _, name := range []string{"string", "bool", "int", "int8", "int16", "int32", "int64", "uint", "uint16", "uint32", "uint64", "float32", "float64", "any"} {
|
|
t.Run(name, func(t *testing.T) {
|
|
spec, err := parseText(t, fmt.Sprintf("service arrays{\n@handler list\nget /list returns ([]%s)\n}", name))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
c, err := Compile(spec)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
schema := operation(t, document(t, c), "/list", "get")["responses"].(map[string]any)["200"].(map[string]any)["content"].(map[string]any)["application/json"].(map[string]any)["schema"].(map[string]any)
|
|
data := schema["properties"].(map[string]any)["data"].(map[string]any)
|
|
if data["type"] != "array" || data["items"] == nil {
|
|
t.Fatal(data)
|
|
}
|
|
if name == "any" && len(data["items"].(map[string]any)) != 0 {
|
|
t.Fatal("dynamic JSON was constrained")
|
|
}
|
|
if responseGoType("[]"+name) != "[]"+name || responseNeedsTypes("[]"+name) {
|
|
t.Fatal("wrong Go signature/import")
|
|
}
|
|
})
|
|
}
|
|
for _, name := range []string{"uint8", "File", "Unknown", "[]string"} {
|
|
spec, err := parseText(t, fmt.Sprintf("service arrays{\n@handler list\nget /list returns ([]%s)\n}", name))
|
|
if err == nil {
|
|
_, err = Compile(spec)
|
|
}
|
|
if err == nil {
|
|
t.Fatal("unsupported root accepted", name)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestPrimitiveArrayConsumer(t *testing.T) {
|
|
if os.Getenv("GO_OMNI_SCAFFOLD_INTEGRATION") != "1" {
|
|
t.Skip("set GO_OMNI_SCAFFOLD_INTEGRATION=1")
|
|
}
|
|
dir := filepath.Join(t.TempDir(), "arrays")
|
|
o := Options{Dir: dir, API: filepath.Join(frameworkRoot(t), "examples/contracts/arrays.api"), Module: "example.com/arrays", FrameworkDir: frameworkRoot(t)}
|
|
if err := generateManagedTestProject(t, o); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, name := range []string{"strings", "integers", "dynamic", "empty", "raw"} {
|
|
writeFile(t, filepath.Join(dir, "internal/logic/arrays/"+name+".go"), "package arrays\n")
|
|
}
|
|
writeFile(t, filepath.Join(dir, "internal/logic/arrays/values.go"), readFile(t, "testdata/arrays/values.go"))
|
|
writeFile(t, filepath.Join(dir, "consumer_test.go"), readFile(t, "testdata/arrays/consumer_test.go"))
|
|
o.API = filepath.Join(dir, "api/arrays.api")
|
|
if err := generateManagedTestProject(t, o); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !strings.Contains(readFile(t, filepath.Join(dir, "internal/logic/arrays/values.go")), "企业") {
|
|
t.Fatal("logic overwritten")
|
|
}
|
|
o.Check = true
|
|
if err := generateManagedTestProject(t, o); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
ctx, cancel := context.WithTimeout(context.Background(), 90*time.Second)
|
|
defer cancel()
|
|
cmd := exec.CommandContext(ctx, "go", "test", "-race", "-mod=mod", "-count=1", "./...")
|
|
cmd.Dir = dir
|
|
cmd.Env = append(os.Environ(), "GOWORK=off")
|
|
if out, err := cmd.CombinedOutput(); err != nil {
|
|
t.Fatalf("%v\n%s", err, out)
|
|
}
|
|
if os.Getenv("GO_OMNI_RELEASE_CLI") != "" {
|
|
cmd = exec.CommandContext(ctx, "go", "list", "-m", "-json", frameworkModule)
|
|
cmd.Dir = dir
|
|
cmd.Env = append(os.Environ(), "GOWORK=off")
|
|
raw, err := cmd.CombinedOutput()
|
|
if err != nil {
|
|
t.Fatal(err, string(raw))
|
|
}
|
|
var module struct {
|
|
Version, Sum string
|
|
Replace any
|
|
}
|
|
if err = json.Unmarshal(raw, &module); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if module.Version != os.Getenv("GO_OMNI_RELEASE_VERSION") || module.Replace != nil || module.Sum == "" {
|
|
t.Fatal("not a fixed remote consumer")
|
|
}
|
|
t.Logf("remote framework %s checksum %s; no local replace", module.Version, module.Sum)
|
|
}
|
|
|
|
}
|