feat(build): validate SDK specs before generation
This commit is contained in:
parent
8091a1186f
commit
599a6ecff8
2 changed files with 50 additions and 2 deletions
|
|
@ -23,6 +23,10 @@ func runBuildSDK(ctx context.Context, specPath, lang, version string, dryRun boo
|
|||
return coreerr.E("build.SDK", "failed to get working directory", err)
|
||||
}
|
||||
|
||||
return runBuildSDKInDir(ctx, projectDir, specPath, lang, version, dryRun)
|
||||
}
|
||||
|
||||
func runBuildSDKInDir(ctx context.Context, projectDir, specPath, lang, version string, dryRun bool) error {
|
||||
// Load config
|
||||
config := sdk.DefaultConfig()
|
||||
if specPath != "" {
|
||||
|
|
@ -40,8 +44,8 @@ func runBuildSDK(ctx context.Context, specPath, lang, version string, dryRun boo
|
|||
}
|
||||
cli.Blank()
|
||||
|
||||
// Detect spec
|
||||
detectedSpec, err := s.DetectSpec()
|
||||
// Validate the spec before generating anything.
|
||||
detectedSpec, err := s.ValidateSpec(ctx)
|
||||
if err != nil {
|
||||
cli.Print("%s %v\n", buildErrorStyle.Render(i18n.T("common.label.error")), err)
|
||||
return err
|
||||
|
|
|
|||
44
cmd/build/cmd_sdk_test.go
Normal file
44
cmd/build/cmd_sdk_test.go
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
package buildcmd
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"dappco.re/go/core/build/internal/ax"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
const validBuildOpenAPISpec = `openapi: "3.0.0"
|
||||
info:
|
||||
title: Test API
|
||||
version: "1.0.0"
|
||||
paths:
|
||||
/health:
|
||||
get:
|
||||
operationId: getHealth
|
||||
responses:
|
||||
"200":
|
||||
description: OK
|
||||
`
|
||||
|
||||
func TestRunBuildSDKInDir_ValidSpecDryRun_Good(t *testing.T) {
|
||||
tmpDir := t.TempDir()
|
||||
require.NoError(t, ax.WriteFile(ax.Join(tmpDir, "openapi.yaml"), []byte(validBuildOpenAPISpec), 0o644))
|
||||
|
||||
err := runBuildSDKInDir(context.Background(), tmpDir, "", "go", "", true)
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
func TestRunBuildSDKInDir_InvalidDocument_Bad(t *testing.T) {
|
||||
tmpDir := t.TempDir()
|
||||
require.NoError(t, ax.WriteFile(ax.Join(tmpDir, "openapi.yaml"), []byte(`openapi: "3.0.0"
|
||||
info:
|
||||
title: Test API
|
||||
paths: {}
|
||||
`), 0o644))
|
||||
|
||||
err := runBuildSDKInDir(context.Background(), tmpDir, "", "", "", true)
|
||||
require.Error(t, err)
|
||||
assert.Contains(t, err.Error(), "invalid OpenAPI spec")
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue