2020-09-30 22:41:52 +02:00
|
|
|
// Copyright 2016-2020, 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 test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"io/ioutil"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/pulumi/pulumi/pkg/v2/codegen/schema"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
// GenPkgSignature corresponds to the shape of the codegen GeneratePackage functions.
|
|
|
|
type GenPkgSignature func(string, *schema.Package, map[string][]byte) (map[string][]byte, error)
|
|
|
|
|
|
|
|
// GeneratePackageFilesFromSchema loads a schema and generates files using the provided GeneratePackage function.
|
|
|
|
func GeneratePackageFilesFromSchema(schemaPath string, genPackageFunc GenPkgSignature) (map[string][]byte, error) {
|
|
|
|
// Read in, decode, and import the schema.
|
|
|
|
schemaBytes, err := ioutil.ReadFile(schemaPath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var pkgSpec schema.PackageSpec
|
|
|
|
err = json.Unmarshal(schemaBytes, &pkgSpec)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
pkg, err := schema.ImportSpec(pkgSpec, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return genPackageFunc("test", pkg, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
// LoadFiles loads the provided list of files from a directory.
|
2020-10-10 22:47:23 +02:00
|
|
|
func LoadFiles(dir, lang string, files []string) (map[string][]byte, error) {
|
2020-09-30 22:41:52 +02:00
|
|
|
result := map[string][]byte{}
|
|
|
|
for _, file := range files {
|
2020-10-10 22:47:23 +02:00
|
|
|
fileBytes, err := ioutil.ReadFile(filepath.Join(dir, lang, file))
|
2020-09-30 22:41:52 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
result[file] = fileBytes
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ValidateFileEquality compares maps of files for equality.
|
|
|
|
func ValidateFileEquality(t *testing.T, actual, expected map[string][]byte) {
|
|
|
|
for name, file := range expected {
|
|
|
|
assert.Contains(t, actual, name)
|
2020-10-08 03:13:10 +02:00
|
|
|
assert.Equal(t, string(file), string(actual[name]))
|
2020-09-30 22:41:52 +02:00
|
|
|
}
|
|
|
|
}
|