-
Notifications
You must be signed in to change notification settings - Fork 92
Use cobra cli for docker-generate command #250
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
780de16
Use cobra cli for docker-generate command
ba2b971
refactor docker-generate test and metadata cmd
ae2f5e0
refactor the usage of cobra by using variable binding
chloeyin 863f57e
refactor flags
chloeyin c31a9e0
add maximum arg
chloeyin 0070de2
remove cmd from generateManifest
chloeyin fe5becc
refactor test
chloeyin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,13 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "github.com/urfave/cli/v2" | ||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| var generateCommand = &cli.Command{ | ||
| Name: "generate", | ||
| Subcommands: []*cli.Command{ | ||
| manifestCommand, | ||
| }, | ||
| func generateCommand() *cobra.Command { | ||
| cmd := &cobra.Command{ | ||
| Use: "generate", | ||
| } | ||
| cmd.AddCommand(generateManifestCommand(nil)) | ||
| return cmd | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| package main | ||
|
|
||
| import "testing" | ||
|
|
||
| func TestGenerateCommand(t *testing.T) { | ||
| cmd := generateCommand() | ||
| subCmds := cmd.Commands() | ||
| if len(subCmds) != 1 { | ||
| t.Fatalf("Expect generate command have 1 subcommand, got: %v", len(subCmds)) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| package main | ||
chloeyin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| import ( | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestGenerateManifestCmd(t *testing.T) { | ||
| tests := []struct { | ||
| generateManifestOpts | ||
| args []string | ||
| expectedErr bool | ||
| }{ | ||
| { | ||
| generateManifestOpts{ | ||
| reference: "reference", | ||
| output: "output", | ||
| }, | ||
| []string{"-o", "output", "reference"}, | ||
| false, | ||
| }, | ||
| { | ||
| generateManifestOpts{ | ||
| reference: "", | ||
| output: "output", | ||
| }, | ||
| []string{"-o", "output"}, | ||
| false, | ||
| }, | ||
| { | ||
| generateManifestOpts{ | ||
| reference: "reference", | ||
| output: "", | ||
| }, | ||
| []string{"reference"}, | ||
| false, | ||
| }, | ||
| { | ||
| generateManifestOpts{ | ||
| reference: "", | ||
| output: "", | ||
| }, | ||
| []string{}, | ||
| false, | ||
| }, | ||
| { | ||
| generateManifestOpts{ | ||
| reference: "reference", | ||
| output: "output", | ||
| }, | ||
| []string{"reference", "--output", "output"}, | ||
| false, | ||
| }, | ||
| { | ||
| args: []string{"-o", "output", "-n", "reference"}, | ||
| expectedErr: true, | ||
| }, | ||
| } | ||
| for _, test := range tests { | ||
| opts := &generateManifestOpts{} | ||
| cmd := generateManifestCommand(opts) | ||
| err := cmd.ParseFlags(test.args) | ||
| if err != nil && !test.expectedErr { | ||
| t.Fatalf("Test failed with error: %v", err) | ||
| } | ||
| if err == nil && test.expectedErr { | ||
| t.Fatalf("Expect test to error but it didn't: %v", test.args) | ||
| } | ||
| if err != nil { | ||
| continue | ||
| } | ||
| cmd.PreRun(cmd, cmd.Flags().Args()) | ||
| if *opts != test.generateManifestOpts { | ||
| t.Fatalf("Expect generate manifest opts: %v, got: %v", test.generateManifestOpts, *opts) | ||
| } | ||
| } | ||
|
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "io" | ||
| "os" | ||
| "testing" | ||
|
|
||
| "github.com/notaryproject/notation/internal/docker" | ||
| ) | ||
|
|
||
| func TestMetdaDatCommand(t *testing.T) { | ||
| oldStdOut := os.Stdout | ||
| r, w, err := os.Pipe() | ||
| if err != nil { | ||
| t.Fatalf("Create pipe for metadata cmd failed: %v", err) | ||
|
|
||
| } | ||
| os.Stdout = w | ||
| cmd := metadataCommand() | ||
| if err := cmd.RunE(cmd, []string{}); err != nil { | ||
| t.Fatalf("Running metadata cmd failed: %v", err) | ||
| } | ||
| w.Close() | ||
| data, err := io.ReadAll(r) | ||
| if err != nil { | ||
| t.Fatalf("Read metadata from stdout failed: %v", err) | ||
| } | ||
| var got docker.PluginMetadata | ||
| if err := json.Unmarshal(data, &got); err != nil { | ||
| t.Fatalf("Unmarshal metadata failed: %v", err) | ||
| } | ||
| if got != pluginMetadata { | ||
| t.Fatalf("Expect Metadata: %v, got: %v", data, pluginMetadata) | ||
| } | ||
| defer func() { | ||
| os.Stdout = oldStdOut | ||
| r.Close() | ||
| }() | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.