-
Notifications
You must be signed in to change notification settings - Fork 28
Add simple e2e tests for all command
#15
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
Changes from all commits
a75888c
88cfa48
8eca8e4
f616f4e
f685238
ea638cc
237dd34
a006f7a
d880e52
aa76ba8
b337d8c
4c3a606
8226f2e
c3863f1
a592e5d
ec90ac6
d734561
61a3099
0654f06
7ec0d8b
8030051
99fb3d8
6ba7fe6
f19c352
743e08a
dee6a2d
451f496
ff9a2be
9401282
4d1a19c
daaddb3
3c9346b
7b731af
c08e6b4
d793ab7
53205c1
1d004fb
9f747f1
ae591cc
4533b47
6e0318c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| // Copyright (c) HashiCorp, Inc. | ||
| // SPDX-License-Identifier: MPL-2.0 | ||
|
|
||
| package cmd_test | ||
|
|
||
| import ( | ||
| "os" | ||
| "path" | ||
| "testing" | ||
|
|
||
| "github.com/google/go-cmp/cmp" | ||
| "github.com/hashicorp/terraform-plugin-codegen-framework/internal/cmd" | ||
| "github.com/mitchellh/cli" | ||
| ) | ||
|
|
||
| func TestAllCommand(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| testCases := map[string]struct { | ||
| irInputPath string | ||
| goldenFileDir string | ||
| }{ | ||
| "custom_and_external": { | ||
| irInputPath: "testdata/custom_and_external/ir.json", | ||
| goldenFileDir: "testdata/custom_and_external/output", | ||
| }, | ||
| } | ||
| for name, testCase := range testCases { | ||
| name, testCase := name, testCase | ||
| t.Run(name, func(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| testOutputDir := t.TempDir() | ||
| mockUi := cli.NewMockUi() | ||
| c := cmd.AllCommand{Ui: mockUi} | ||
| args := []string{ | ||
| "-input", testCase.irInputPath, | ||
| "-output", testOutputDir, | ||
| } | ||
|
|
||
| exitCode := c.Run(args) | ||
| if exitCode != 0 { | ||
| t.Fatalf("unexpected error running `all` cmd: %s", mockUi.ErrorWriter.String()) | ||
| } | ||
|
|
||
| compareDirectories(t, testCase.goldenFileDir, testOutputDir) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| // TODO: currently doesn't compare nested directory files | ||
| func compareDirectories(t *testing.T, wantDirPath, gotDirPath string) { | ||
| t.Helper() | ||
|
|
||
| wantDirEntries, err := os.ReadDir(wantDirPath) | ||
| if err != nil { | ||
| t.Fatalf("unexpected error reading `want` directory: %s", err) | ||
| } | ||
|
|
||
| gotDirEntries, err := os.ReadDir(gotDirPath) | ||
| if err != nil { | ||
| t.Fatalf("unexpected error reading `got` directory: %s", err) | ||
| } | ||
|
|
||
| if len(gotDirEntries) != len(wantDirEntries) { | ||
| t.Fatalf("mismatched files in output directory, wanted: %d file(s), got: %d file(s)", len(wantDirEntries), len(gotDirEntries)) | ||
| } | ||
|
|
||
| for i, wantEntry := range wantDirEntries { | ||
| gotEntry := gotDirEntries[i] | ||
|
|
||
| if gotEntry.Name() != wantEntry.Name() { | ||
| t.Errorf("mismatched file name, wanted: %s, got: %s", wantEntry.Name(), gotEntry.Name()) | ||
| continue | ||
| } | ||
|
|
||
| if gotEntry.Type() != wantEntry.Type() { | ||
| t.Errorf("mismatched file type, wanted: %s, got: %s", wantEntry.Type(), gotEntry.Type()) | ||
| 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())) | ||
| if err != nil { | ||
| t.Fatalf("unexpected error reading `want` file: %s", err) | ||
| } | ||
|
|
||
| if diff := cmp.Diff(string(gotFile), string(wantFile)); diff != "" { | ||
| t.Errorf("unexpected difference in %s: %s", wantEntry.Name(), diff) | ||
| } | ||
| } | ||
| } | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will need to be updated after hashicorp/terraform-plugin-codegen-spec#26 is merged |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice. TIL there's a
cli.NewMockUi().