Skip to content

Commit

Permalink
Default to generating directory and package per data source, provider…
Browse files Browse the repository at this point in the history
… and resource (#39)

* Default to empty string for package flag, and use data source, provider, or resource name as package name (#20)

* Adding test coverage to verify package name can be overridden by supplying --package flag (#20)

* Add generated code to separate directories (#20)

* Only create directory and package per data source and resource if packageName flag is not specified (#20)

* Changing the name of data source, provider and resource in ir.json to "example" to highlight the default behaviour for directory and package generation (#20)

* Adding doc comments (#20)
  • Loading branch information
bendbennett authored Sep 7, 2023
1 parent b3e1d4e commit 63bc509
Show file tree
Hide file tree
Showing 19 changed files with 4,025 additions and 46 deletions.
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
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

0 comments on commit 63bc509

Please sign in to comment.