pulumi/pkg/codegen/dotnet/test.go

142 lines
4.2 KiB
Go
Raw Permalink Normal View History

2022-03-16 18:42:30 +00:00
package dotnet
import (
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/pulumi/pulumi/pkg/v3/codegen"
"github.com/pulumi/pulumi/pkg/v3/codegen/testing/test"
"github.com/pulumi/pulumi/pkg/v3/testing/integration"
"github.com/pulumi/pulumi/sdk/v3/go/common/util/executable"
)
func Check(t *testing.T, path string, dependencies codegen.StringSet, pulumiSDKPath string) {
var err error
dir := filepath.Dir(path)
ex, err := executable.FindExecutable("dotnet")
require.NoError(t, err, "Failed to find dotnet executable")
// We create a new cs-project each time the test is run.
projectFile := filepath.Join(dir, filepath.Base(dir)+".csproj")
programFile := filepath.Join(dir, "Program.cs")
if err = os.Remove(projectFile); !os.IsNotExist(err) {
require.NoError(t, err)
}
if err = os.Remove(programFile); !os.IsNotExist(err) {
require.NoError(t, err)
}
err = integration.RunCommand(t, "create dotnet project",
[]string{ex, "new", "console"}, dir, &integration.ProgramTestOptions{})
require.NoError(t, err, "Failed to create C# project")
// Remove Program.cs again generated from "dotnet new console"
// because the generated C# program already has an entry point
if err = os.Remove(programFile); !os.IsNotExist(err) {
require.NoError(t, err)
}
2022-03-16 18:42:30 +00:00
// Add dependencies
pkgs := dotnetDependencies(dependencies)
if len(pkgs) != 0 {
for _, pkg := range pkgs {
pkg.install(t, ex, dir)
}
} else {
// We would like this regardless of other dependencies, but dotnet
// packages do not play well with package references.
if pulumiSDKPath != "" {
err = integration.RunCommand(t, "add sdk ref",
[]string{ex, "add", "reference", pulumiSDKPath},
dir, &integration.ProgramTestOptions{})
require.NoError(t, err, "Failed to dotnet sdk package reference")
} else {
dep{"Pulumi", ""}.install(t, ex, dir)
}
}
// Clean up build result
defer func() {
err = os.RemoveAll(filepath.Join(dir, "bin"))
assert.NoError(t, err, "Failed to remove bin result")
err = os.RemoveAll(filepath.Join(dir, "obj"))
assert.NoError(t, err, "Failed to remove obj result")
}()
TypeCheck(t, path, dependencies, pulumiSDKPath)
}
func TypeCheck(t *testing.T, path string, dependencies codegen.StringSet, pulumiSDKPath string) {
var err error
dir := filepath.Dir(path)
ex, err := executable.FindExecutable("dotnet")
require.NoError(t, err)
2022-03-16 18:42:30 +00:00
err = integration.RunCommand(t, "dotnet build",
[]string{ex, "build", "--nologo"}, dir, &integration.ProgramTestOptions{})
require.NoError(t, err, "Failed to build dotnet project")
}
type dep struct {
Name string
Version string
}
func (pkg dep) install(t *testing.T, ex, dir string) {
args := []string{ex, "add", "package", pkg.Name}
if pkg.Version != "" {
args = append(args, "--version", pkg.Version)
}
err := integration.RunCommand(t, "Add package",
args, dir, &integration.ProgramTestOptions{})
require.NoError(t, err, "Failed to add dependency %q %q", pkg.Name, pkg.Version)
}
// Converts from the hcl2 dependency format to the dotnet format.
//
// Example:
//
2022-09-14 02:12:02 +00:00
// "aws" => {"Pulumi.Aws", 4.21.1}
// "azure" => {"Pulumi.Azure", 4.21.1}
2022-03-16 18:42:30 +00:00
func dotnetDependencies(deps codegen.StringSet) []dep {
result := make([]dep, len(deps))
for i, d := range deps.SortedValues() {
switch d {
case "aws":
result[i] = dep{"Pulumi.Aws", test.AwsSchema}
case "azure-native":
result[i] = dep{"Pulumi.AzureNative", test.AzureNativeSchema}
case "azure":
// TODO: update constant in test.AzureSchema to v5.x
// because it has output-versioned function invokes
result[i] = dep{"Pulumi.Azure", "5.12.0"}
2022-03-16 18:42:30 +00:00
case "kubernetes":
result[i] = dep{"Pulumi.Kubernetes", test.KubernetesSchema}
case "random":
result[i] = dep{"Pulumi.Random", test.RandomSchema}
default:
Enable perfsprint linter (#14813) <!--- Thanks so much for your contribution! If this is your first time contributing, please ensure that you have read the [CONTRIBUTING](https://github.com/pulumi/pulumi/blob/master/CONTRIBUTING.md) documentation. --> # Description <!--- Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. --> Prompted by a comment in another review: https://github.com/pulumi/pulumi/pull/14654#discussion_r1419995945 This lints that we don't use `fmt.Errorf` when `errors.New` will suffice, it also covers a load of other cases where `Sprintf` is sub-optimal. Most of these edits were made by running `perfsprint --fix`. ## Checklist - [x] I have run `make tidy` to update any new dependencies - [x] I have run `make lint` to verify my code passes the lint check - [x] I have formatted my code using `gofumpt` <!--- Please provide details if the checkbox below is to be left unchecked. --> - [ ] I have added tests that prove my fix is effective or that my feature works <!--- User-facing changes require a CHANGELOG entry. --> - [ ] I have run `make changelog` and committed the `changelog/pending/<file>` documenting my change <!-- If the change(s) in this PR is a modification of an existing call to the Pulumi Cloud, then the service should honor older versions of the CLI where this change would not exist. You must then bump the API version in /pkg/backend/httpstate/client/api.go, as well as add it to the service. --> - [ ] Yes, there are changes in this PR that warrants bumping the Pulumi Cloud API version <!-- @Pulumi employees: If yes, you must submit corresponding changes in the service repo. -->
2023-12-12 12:19:42 +00:00
result[i] = dep{"Pulumi." + Title(d), ""}
2022-03-16 18:42:30 +00:00
}
}
return result
}
func GenerateProgramBatchTest(t *testing.T, testCases []test.ProgramTest) {
test.TestProgramCodegen(t,
test.ProgramCodegenOptions{
Language: "dotnet",
Extension: "cs",
OutputFile: "Program.cs",
Check: func(t *testing.T, path string, dependencies codegen.StringSet) {
2022-12-13 10:51:55 +00:00
Check(t, path, dependencies, "")
},
GenProgram: GenerateProgram,
TestCases: testCases,
},
)
}