[codegen/schema] Add a schema checker (#7865)
- Change the schema package to report semantic errors as diagnostics rather than Go errors - Add a `pulumi schema check` command to the CLI for static checking of package schemas The semantic checker can be extended in the future to add support for target-specific checks.
This commit is contained in:
parent
4380a63ad9
commit
76ee1b8ccf
|
@ -10,4 +10,7 @@
|
|||
making it easier to compose functions/datasources with Pulumi
|
||||
resources. [#7784](https://github.com/pulumi/pulumi/pull/7784)
|
||||
|
||||
- [codegen/schema] Add a `pulumi schema check` command to validate package schemas.
|
||||
[#7865](https://github.com/pulumi/pulumi/pull/7865)
|
||||
|
||||
### Bug Fixes
|
||||
|
|
|
@ -208,6 +208,7 @@ func NewPulumiCmd() *cobra.Command {
|
|||
cmd.AddCommand(newPluginCmd())
|
||||
cmd.AddCommand(newVersionCmd())
|
||||
cmd.AddCommand(newConsoleCmd())
|
||||
cmd.AddCommand(newSchemaCmd())
|
||||
|
||||
// Less common, and thus hidden, commands:
|
||||
cmd.AddCommand(newGenCompletionCmd(cmd))
|
||||
|
|
35
pkg/cmd/pulumi/schema.go
Normal file
35
pkg/cmd/pulumi/schema.go
Normal file
|
@ -0,0 +1,35 @@
|
|||
// Copyright 2016-2021, Pulumi Corporation.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/pulumi/pulumi/sdk/v3/go/common/util/cmdutil"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
func newSchemaCmd() *cobra.Command {
|
||||
cmd := &cobra.Command{
|
||||
Use: "schema",
|
||||
Short: "Analyze package schemas",
|
||||
Long: `Analyze package schemas
|
||||
|
||||
Subcommands of this command can be used to analyze Pulumi package schemas. This can be useful to check hand-authored
|
||||
package schemas for errors.`,
|
||||
Args: cmdutil.NoArgs,
|
||||
}
|
||||
|
||||
cmd.AddCommand(newSchemaCheckCommand())
|
||||
return cmd
|
||||
}
|
79
pkg/cmd/pulumi/schema_check.go
Normal file
79
pkg/cmd/pulumi/schema_check.go
Normal file
|
@ -0,0 +1,79 @@
|
|||
// Copyright 2016-2021, Pulumi Corporation.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/hashicorp/hcl/v2"
|
||||
"github.com/spf13/cobra"
|
||||
"gopkg.in/yaml.v3"
|
||||
|
||||
"github.com/pulumi/pulumi/pkg/v3/codegen/schema"
|
||||
"github.com/pulumi/pulumi/sdk/v3/go/common/util/cmdutil"
|
||||
"github.com/pulumi/pulumi/sdk/v3/go/common/util/contract"
|
||||
)
|
||||
|
||||
func newSchemaCheckCommand() *cobra.Command {
|
||||
cmd := &cobra.Command{
|
||||
Use: "check",
|
||||
Args: cmdutil.ExactArgs(1),
|
||||
Short: "Check a Pulumi package schema for errors",
|
||||
Long: "Check a Pulumi package schema for errors.\n" +
|
||||
"\n" +
|
||||
"Ensure that a Pulumi package schema meets the requirements imposed by the\n" +
|
||||
"schema spec as well as additional requirements imposed by the supported\n" +
|
||||
"target languages.",
|
||||
Run: cmdutil.RunFunc(func(cmd *cobra.Command, args []string) error {
|
||||
file := args[0]
|
||||
|
||||
// Read from stdin or a specified file
|
||||
reader := os.Stdin
|
||||
if file != "-" {
|
||||
f, err := os.Open(file)
|
||||
if err != nil {
|
||||
return fmt.Errorf("could not open file %v: %w", file, err)
|
||||
}
|
||||
reader = f
|
||||
}
|
||||
schemaBytes, err := ioutil.ReadAll(reader)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to read schema: %w", err)
|
||||
}
|
||||
|
||||
var pkgSpec schema.PackageSpec
|
||||
if ext := filepath.Ext(file); ext == ".yaml" || ext == ".yml" {
|
||||
err = yaml.Unmarshal(schemaBytes, &pkgSpec)
|
||||
} else {
|
||||
err = json.Unmarshal(schemaBytes, &pkgSpec)
|
||||
}
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to unmarshal schema: %w", err)
|
||||
}
|
||||
|
||||
_, diags, err := schema.BindSpec(pkgSpec, nil)
|
||||
diagWriter := hcl.NewDiagnosticTextWriter(os.Stderr, nil, 0, true)
|
||||
wrErr := diagWriter.WriteDiagnostics(diags)
|
||||
contract.IgnoreError(wrErr)
|
||||
return err
|
||||
}),
|
||||
}
|
||||
|
||||
return cmd
|
||||
}
|
|
@ -94,10 +94,13 @@ func (l *pluginLoader) LoadPackage(pkg string, version *semver.Version) (*Packag
|
|||
return nil, err
|
||||
}
|
||||
|
||||
p, err := importSpec(spec, nil, l)
|
||||
p, diags, err := bindSpec(spec, nil, l)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if diags.HasErrors() {
|
||||
return nil, diags
|
||||
}
|
||||
|
||||
l.m.Lock()
|
||||
defer l.m.Unlock()
|
||||
|
|
File diff suppressed because it is too large
Load diff
|
@ -324,9 +324,9 @@ func Test_parseTypeSpecRef(t *testing.T) {
|
|||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got, err := typs.parseTypeSpecRef(tt.ref)
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Errorf("parseTypeSpecRef() error = %v, wantErr %v", err, tt.wantErr)
|
||||
got, diags := typs.parseTypeSpecRef("ref", tt.ref)
|
||||
if diags.HasErrors() != tt.wantErr {
|
||||
t.Errorf("parseTypeSpecRef() diags = %v, wantErr %v", diags, tt.wantErr)
|
||||
return
|
||||
}
|
||||
if !reflect.DeepEqual(got, tt.want) {
|
||||
|
@ -370,27 +370,27 @@ func TestMethods(t *testing.T) {
|
|||
},
|
||||
{
|
||||
filename: "bad-methods-1.json",
|
||||
expectedError: "unknown function xyz:index:Foo/bar for method bar",
|
||||
expectedError: "unknown function xyz:index:Foo/bar",
|
||||
},
|
||||
{
|
||||
filename: "bad-methods-2.json",
|
||||
expectedError: "function xyz:index:Foo/bar for method baz is already a method",
|
||||
expectedError: "function xyz:index:Foo/bar is already a method",
|
||||
},
|
||||
{
|
||||
filename: "bad-methods-3.json",
|
||||
expectedError: "invalid function token format xyz:index:Foo for method bar",
|
||||
expectedError: "invalid function token format xyz:index:Foo",
|
||||
},
|
||||
{
|
||||
filename: "bad-methods-4.json",
|
||||
expectedError: "invalid function token format xyz:index:Baz/bar for method bar",
|
||||
expectedError: "invalid function token format xyz:index:Baz/bar",
|
||||
},
|
||||
{
|
||||
filename: "bad-methods-5.json",
|
||||
expectedError: "function xyz:index:Foo/bar for method bar is missing __self__ parameter",
|
||||
expectedError: "function xyz:index:Foo/bar has no __self__ parameter",
|
||||
},
|
||||
{
|
||||
filename: "bad-methods-6.json",
|
||||
expectedError: "property and method have the same name bar",
|
||||
expectedError: "xyz:index:Foo already has a property named bar",
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
|
|
Loading…
Reference in a new issue