-
Notifications
You must be signed in to change notification settings - Fork 1
feat: introduce automatic registration of --no-headers on list commands and cloud-provider list to use it #109
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
Open
Davidonium
wants to merge
7
commits into
main
Choose a base branch
from
feat/dhernando/global-no-headers
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
5bef9d7
feat: introduce automatic registration of --no-headers on list comman…
Davidonium 4f5f3fc
refactor: rename Renderable to TableRenderer
Davidonium ae8caa2
fix: rename missing reference to output.Renderable
Davidonium 6d6864d
fix: panic when the user does not set neither OutputTable nor PrintTe…
Davidonium 05fd850
Merge branch 'main' into feat/dhernando/global-no-headers
Davidonium 0fb2b82
fix: avoid coyppasting the fetch function for testing the base list s…
Davidonium 948c67d
chore: code format
Davidonium 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
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,91 @@ | ||
| package base_test | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "fmt" | ||
| "io" | ||
| "testing" | ||
|
|
||
| "github.com/spf13/cobra" | ||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
|
|
||
| "github.com/qdrant/qcloud-cli/internal/cmd/base" | ||
| "github.com/qdrant/qcloud-cli/internal/cmd/output" | ||
| "github.com/qdrant/qcloud-cli/internal/state" | ||
| ) | ||
|
|
||
| var fetchHello = func(_ *state.State, _ *cobra.Command) (string, error) { return "hello", nil } | ||
|
|
||
| func execListCmd(t *testing.T, lc base.ListCmd[string], args ...string) (string, error) { | ||
| t.Helper() | ||
| cmd := lc.CobraCommand(state.New("")) | ||
| var buf bytes.Buffer | ||
| cmd.SetOut(&buf) | ||
| cmd.SetArgs(args) | ||
| err := cmd.Execute() | ||
| return buf.String(), err | ||
| } | ||
|
|
||
| func stringTableRenderer(out io.Writer, val string) output.TableRenderer { | ||
| tbl := output.NewTable[string](out) | ||
| tbl.AddField("VALUE", func(v string) string { return v }) | ||
| tbl.SetItems([]string{val}) | ||
| return tbl | ||
| } | ||
|
|
||
| func TestListCmd_OutputTable(t *testing.T) { | ||
| lc := base.ListCmd[string]{ | ||
| Use: "test", | ||
| Fetch: fetchHello, | ||
| OutputTable: func(_ *cobra.Command, out io.Writer, resp string) output.TableRenderer { | ||
| return stringTableRenderer(out, resp) | ||
| }, | ||
| } | ||
|
|
||
| stdout, err := execListCmd(t, lc) | ||
| require.NoError(t, err) | ||
| assert.Contains(t, stdout, "VALUE") | ||
| assert.Contains(t, stdout, "hello") | ||
| } | ||
|
|
||
| func TestListCmd_OutputTable_NoHeaders(t *testing.T) { | ||
| lc := base.ListCmd[string]{ | ||
| Use: "test", | ||
| Fetch: fetchHello, | ||
| OutputTable: func(_ *cobra.Command, out io.Writer, resp string) output.TableRenderer { | ||
| return stringTableRenderer(out, resp) | ||
| }, | ||
| } | ||
|
|
||
| stdout, err := execListCmd(t, lc, "--no-headers") | ||
| require.NoError(t, err) | ||
| assert.NotContains(t, stdout, "VALUE") | ||
| assert.Contains(t, stdout, "hello") | ||
| } | ||
|
|
||
| func TestListCmd_PrintText(t *testing.T) { | ||
| lc := base.ListCmd[string]{ | ||
| Use: "test", | ||
| Fetch: fetchHello, | ||
| PrintText: func(_ *cobra.Command, out io.Writer, resp string) error { | ||
| _, err := fmt.Fprintln(out, resp) | ||
| return err | ||
| }, | ||
| } | ||
|
|
||
| stdout, err := execListCmd(t, lc) | ||
| require.NoError(t, err) | ||
| assert.Contains(t, stdout, "hello") | ||
| } | ||
|
|
||
| func TestListCmd_NeitherOutputTableNorPrintText_Panics(t *testing.T) { | ||
| lc := base.ListCmd[string]{ | ||
| Use: "test", | ||
| Fetch: fetchHello, | ||
| } | ||
|
|
||
| assert.Panics(t, func() { | ||
| _, _ = execListCmd(t, lc) | ||
| }) | ||
| } |
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
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,66 @@ | ||
| package output_test | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
|
|
||
| "github.com/qdrant/qcloud-cli/internal/cmd/output" | ||
| ) | ||
|
|
||
| type testItem struct { | ||
| ID string | ||
| Name string | ||
| } | ||
|
|
||
| func TestTable_Render_WithHeaders(t *testing.T) { | ||
| var buf bytes.Buffer | ||
| tbl := output.NewTable[testItem](&buf) | ||
| tbl.AddField("ID", func(v testItem) string { return v.ID }) | ||
| tbl.AddField("NAME", func(v testItem) string { return v.Name }) | ||
| tbl.SetItems([]testItem{ | ||
| {ID: "1", Name: "alpha"}, | ||
| }) | ||
| tbl.Render() | ||
|
|
||
| out := buf.String() | ||
| assert.Contains(t, out, "ID") | ||
| assert.Contains(t, out, "NAME") | ||
| assert.Contains(t, out, "1") | ||
| assert.Contains(t, out, "alpha") | ||
| } | ||
|
|
||
| func TestTable_Render_NoHeaders(t *testing.T) { | ||
| var buf bytes.Buffer | ||
| tbl := output.NewTable[testItem](&buf) | ||
| tbl.AddField("ID", func(v testItem) string { return v.ID }) | ||
| tbl.AddField("NAME", func(v testItem) string { return v.Name }) | ||
| tbl.SetItems([]testItem{ | ||
| {ID: "1", Name: "alpha"}, | ||
| }) | ||
| tbl.SetNoHeaders(true) | ||
| tbl.Render() | ||
|
|
||
| out := buf.String() | ||
| assert.NotContains(t, out, "ID") | ||
| assert.NotContains(t, out, "NAME") | ||
| assert.Contains(t, out, "1") | ||
| assert.Contains(t, out, "alpha") | ||
| } | ||
|
|
||
| func TestTable_Write_BackwardCompat(t *testing.T) { | ||
| var buf bytes.Buffer | ||
| tbl := output.NewTable[testItem](&buf) | ||
| tbl.AddField("ID", func(v testItem) string { return v.ID }) | ||
| tbl.AddField("NAME", func(v testItem) string { return v.Name }) | ||
| tbl.Write([]testItem{ | ||
| {ID: "1", Name: "alpha"}, | ||
| }) | ||
|
|
||
| out := buf.String() | ||
| assert.Contains(t, out, "ID") | ||
| assert.Contains(t, out, "NAME") | ||
| assert.Contains(t, out, "1") | ||
| assert.Contains(t, out, "alpha") | ||
| } |
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.
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.
Does it make sense to return error here when both
OutputTableandPrintTextare not set?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.
It does, the code should panic since it's a development mistake. Adding it!