mirror of https://github.com/pulumi/pulumi.git
97 lines
2.3 KiB
Go
97 lines
2.3 KiB
Go
package python
|
|
|
|
import (
|
|
filesystem "io/fs"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
"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/sdk/v3/go/common/testing/iotest"
|
|
"github.com/pulumi/pulumi/sdk/v3/python"
|
|
)
|
|
|
|
func Check(t *testing.T, path string, _ codegen.StringSet) {
|
|
pyCompileCheck(t, filepath.Dir(path))
|
|
}
|
|
|
|
// Checks generated code for syntax errors with `python -m compile`.
|
|
func pyCompileCheck(t *testing.T, codeDir string) {
|
|
pythonFiles := []string{}
|
|
err := filepath.Walk(codeDir, func(path string, info filesystem.FileInfo, err error) error {
|
|
require.NoError(t, err) // an error in the walk
|
|
|
|
if info.Mode().IsDir() && info.Name() == "venv" {
|
|
return filepath.SkipDir
|
|
}
|
|
|
|
if info.Mode().IsRegular() && strings.HasSuffix(info.Name(), ".py") {
|
|
path, err = filepath.Abs(path)
|
|
require.NoError(t, err)
|
|
|
|
pythonFiles = append(pythonFiles, path)
|
|
}
|
|
return nil
|
|
})
|
|
require.NoError(t, err)
|
|
|
|
ex, _, err := python.CommandPath()
|
|
require.NoError(t, err)
|
|
args := append([]string{"-m", "py_compile"}, pythonFiles...)
|
|
test.RunCommand(t, "python syntax check", codeDir, ex, args...)
|
|
}
|
|
|
|
// Checks typing of generated code with mypy.
|
|
func pyTypeCheck(t *testing.T, codeDir string) {
|
|
venvDir, err := virtualEnvPath()
|
|
if err != nil {
|
|
t.Error(err)
|
|
return
|
|
}
|
|
|
|
cmd := func(name string, args ...string) error {
|
|
t.Logf("cd %s && %s %s", codeDir, name, strings.Join(args, " "))
|
|
cmd := python.VirtualEnvCommand(venvDir, name, args...)
|
|
cmd.Dir = codeDir
|
|
|
|
outw := iotest.LogWriter(t)
|
|
cmd.Stderr = outw
|
|
cmd.Stdout = outw
|
|
return cmd.Run()
|
|
}
|
|
|
|
installPackage := func() error {
|
|
venvMutex.Lock()
|
|
defer venvMutex.Unlock()
|
|
return cmd("python", "-m", "pip", "install", "-e", ".")
|
|
}
|
|
|
|
if err = installPackage(); err != nil {
|
|
t.Error(err)
|
|
return
|
|
}
|
|
|
|
if err = cmd(
|
|
"python", "-m", "mypy", ".",
|
|
"--exclude", "/_utilities\\.py$", // the generated _utilities.py has some type errors, which we ignore for now.
|
|
); err != nil {
|
|
t.Error(err)
|
|
return
|
|
}
|
|
}
|
|
|
|
func GenerateProgramBatchTest(t *testing.T, testCases []test.ProgramTest) {
|
|
test.TestProgramCodegen(t,
|
|
test.ProgramCodegenOptions{
|
|
Language: "python",
|
|
Extension: "py",
|
|
OutputFile: "__main__.py",
|
|
Check: Check,
|
|
GenProgram: GenerateProgram,
|
|
TestCases: testCases,
|
|
})
|
|
}
|