Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default to generating directory and package per data source, provider and resource #39

Merged
merged 6 commits into from
Sep 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,12 @@ generate:
testdata:
go run ./cmd/terraform-plugin-codegen-framework generate all \
--input ./internal/cmd/testdata/custom_and_external/ir.json \
--package generated \
--output ./internal/cmd/testdata/custom_and_external/all_output
--package specified \
--output ./internal/cmd/testdata/custom_and_external/all_output/specified_pkg_name

go run ./cmd/terraform-plugin-codegen-framework generate all \
--input ./internal/cmd/testdata/custom_and_external/ir.json \
--output ./internal/cmd/testdata/custom_and_external/all_output/default_pkg_name

go run ./cmd/terraform-plugin-codegen-framework generate resources \
--input ./internal/cmd/testdata/custom_and_external/ir.json \
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/generate_all.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func (cmd *GenerateAllCommand) Flags() *flag.FlagSet {
fs := flag.NewFlagSet("generate all", flag.ExitOnError)
fs.StringVar(&cmd.flagIRInputPath, "input", "", "path to intermediate representation (JSON)")
fs.StringVar(&cmd.flagOutputPath, "output", "./output", "directory path to output generated code files")
fs.StringVar(&cmd.flagPackageName, "package", "provider", "name of Go package for generated code files")
fs.StringVar(&cmd.flagPackageName, "package", "", "name of Go package for generated code files")

return fs
}
Expand Down
15 changes: 11 additions & 4 deletions internal/cmd/generate_all_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,27 @@ package cmd_test
import (
"testing"

"github.com/hashicorp/terraform-plugin-codegen-framework/internal/cmd"
"github.com/mitchellh/cli"

"github.com/hashicorp/terraform-plugin-codegen-framework/internal/cmd"
)

func TestGenerateAllCommand(t *testing.T) {
t.Parallel()

testCases := map[string]struct {
irInputPath string
pkgName string
goldenFileDir string
}{
"custom_and_external": {
"specified_pkg_name": {
irInputPath: "testdata/custom_and_external/ir.json",
pkgName: "specified",
goldenFileDir: "testdata/custom_and_external/all_output/specified_pkg_name",
},
"default_pkg_name": {
irInputPath: "testdata/custom_and_external/ir.json",
goldenFileDir: "testdata/custom_and_external/all_output",
goldenFileDir: "testdata/custom_and_external/all_output/default_pkg_name",
},
}
for name, testCase := range testCases {
Expand All @@ -35,7 +42,7 @@ func TestGenerateAllCommand(t *testing.T) {

args := []string{
"--input", testCase.irInputPath,
"--package", "generated",
"--package", testCase.pkgName,
"--output", testOutputDir,
}

Expand Down
4 changes: 2 additions & 2 deletions internal/cmd/generate_data_sources.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (cmd *GenerateDataSourcesCommand) Flags() *flag.FlagSet {
fs := flag.NewFlagSet("generate data-sources", flag.ExitOnError)
fs.StringVar(&cmd.flagIRInputPath, "input", "./ir.json", "path to intermediate representation (JSON)")
fs.StringVar(&cmd.flagOutputPath, "output", "./output", "directory path to output generated code files")
fs.StringVar(&cmd.flagPackageName, "package", "provider", "name of Go package for generated code files")
fs.StringVar(&cmd.flagPackageName, "package", "", "name of Go package for generated code files")

return fs
}
Expand Down Expand Up @@ -183,7 +183,7 @@ func generateDataSourceCode(spec spec.Specification, outputPath, packageName, ge
}

// write code
err = output.WriteDataSources(formattedDataSourcesSchema, formattedDataSourcesModels, formattedDataSourcesModelObjectHelpers, formattedDataSourcesToFrom, outputPath)
err = output.WriteDataSources(formattedDataSourcesSchema, formattedDataSourcesModels, formattedDataSourcesModelObjectHelpers, formattedDataSourcesToFrom, outputPath, packageName)
if err != nil {
return fmt.Errorf("error writing Go code to output: %w", err)
}
Expand Down
4 changes: 2 additions & 2 deletions internal/cmd/generate_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (cmd *GenerateProviderCommand) Flags() *flag.FlagSet {
fs := flag.NewFlagSet("generate provider", flag.ExitOnError)
fs.StringVar(&cmd.flagIRInputPath, "input", "./ir.json", "path to intermediate representation (JSON)")
fs.StringVar(&cmd.flagOutputPath, "output", "./output", "directory path to output generated code files")
fs.StringVar(&cmd.flagPackageName, "package", "provider", "name of Go package for generated code files")
fs.StringVar(&cmd.flagPackageName, "package", "", "name of Go package for generated code files")

return fs
}
Expand Down Expand Up @@ -183,7 +183,7 @@ func generateProviderCode(spec spec.Specification, outputPath, packageName, gene
}

// write code
err = output.WriteProviders(formattedProvidersSchema, formattedProvidersModels, formattedProvidersModelObjectHelpers, formattedProviderToFrom, outputPath)
err = output.WriteProviders(formattedProvidersSchema, formattedProvidersModels, formattedProvidersModelObjectHelpers, formattedProviderToFrom, outputPath, packageName)
if err != nil {
return fmt.Errorf("error writing Go code to output: %w", err)
}
Expand Down
4 changes: 2 additions & 2 deletions internal/cmd/generate_resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (cmd *GenerateResourcesCommand) Flags() *flag.FlagSet {
fs := flag.NewFlagSet("generate resources", flag.ExitOnError)
fs.StringVar(&cmd.flagIRInputPath, "input", "./ir.json", "path to intermediate representation (JSON)")
fs.StringVar(&cmd.flagOutputPath, "output", "./output", "directory path to output generated code files")
fs.StringVar(&cmd.flagPackageName, "package", "provider", "name of Go package for generated code files")
fs.StringVar(&cmd.flagPackageName, "package", "", "name of Go package for generated code files")

return fs
}
Expand Down Expand Up @@ -183,7 +183,7 @@ func generateResourceCode(spec spec.Specification, outputPath, packageName, gene
}

// write code
err = output.WriteResources(formattedResourcesSchema, formattedResourcesModels, formattedResourcesModelObjectHelpers, formattedResourcesToFrom, outputPath)
err = output.WriteResources(formattedResourcesSchema, formattedResourcesModels, formattedResourcesModelObjectHelpers, formattedResourcesToFrom, outputPath, packageName)
if err != nil {
return fmt.Errorf("error writing Go code to output: %w", err)
}
Expand Down
50 changes: 41 additions & 9 deletions internal/cmd/generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/google/go-cmp/cmp"
)

// TODO: currently doesn't compare nested directory files
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice! 😎

func compareDirectories(t *testing.T, wantDirPath, gotDirPath string) {
t.Helper()

Expand Down Expand Up @@ -42,17 +41,50 @@ func compareDirectories(t *testing.T, wantDirPath, gotDirPath string) {
continue
}

gotFile, err := os.ReadFile(path.Join(gotDirPath, gotEntry.Name()))
if err != nil {
t.Fatalf("unexpected error reading `got` file: %s", err)
}
wantFile, _ := os.ReadFile(path.Join(wantDirPath, wantEntry.Name()))
compareFiles(t, path.Join(gotDirPath, gotEntry.Name()), path.Join(wantDirPath, wantEntry.Name()))
}
}

func compareFiles(t *testing.T, got, want string) {
gotFile, err := os.Open(got)

if err != nil {
t.Fatalf("unexpected error opening %s: %s", got, err)
}

gotFileInfo, err := gotFile.Stat()

if err != nil {
t.Fatalf("unexpected error stat %s: %s", got, err)
}

if gotFileInfo.IsDir() {
dirEntries, err := os.ReadDir(got)

if err != nil {
t.Fatalf("unexpected error reading `want` file: %s", err)
t.Fatalf("unexpected error reading dir %s: %s", got, err)
}

if diff := cmp.Diff(string(gotFile), string(wantFile)); diff != "" {
t.Errorf("unexpected difference in %s: %s", wantEntry.Name(), diff)
for _, dirEntry := range dirEntries {
compareFiles(t, path.Join(got, dirEntry.Name()), path.Join(want, dirEntry.Name()))
}

return
}

gotFileBytes, err := os.ReadFile(got)

if err != nil {
t.Fatalf("unexpected error reading `got` file: %s", err)
}

wantFileBytes, err := os.ReadFile(want)

if err != nil {
t.Fatalf("unexpected error reading `want` file: %s", err)
}

if diff := cmp.Diff(string(gotFileBytes), string(wantFileBytes)); diff != "" {
t.Errorf("unexpected difference in %s: %s", got, diff)
}
}
Loading