-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Return empty array when using --json flag for list commands (#736)
Co-authored-by: Will Vedder <[email protected]>
- Loading branch information
Showing
4 changed files
with
148 additions
and
65 deletions.
There are no files selected for viewing
This file contains 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 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 |
---|---|---|
|
@@ -3,14 +3,10 @@ package display | |
import ( | ||
"bytes" | ||
"io" | ||
"sync" | ||
"testing" | ||
"time" | ||
|
||
"github.com/auth0/go-auth0/management" | ||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/auth0/auth0-cli/internal/auth0" | ||
) | ||
|
||
func TestTimeAgo(t *testing.T) { | ||
|
@@ -44,68 +40,72 @@ func TestTimeAgo(t *testing.T) { | |
} | ||
} | ||
|
||
func TestStream(t *testing.T) { | ||
func TestIndent(t *testing.T) { | ||
assert.Equal(t, "foo", indent("foo", "")) | ||
assert.Equal(t, " foo", indent("foo", " ")) | ||
assert.Equal(t, " line1\n line2\n line3", indent("line1\nline2\nline3", " ")) | ||
} | ||
|
||
func TestRenderer_Results(t *testing.T) { | ||
var stdout bytes.Buffer | ||
mockRender := &Renderer{ | ||
MessageWriter: io.Discard, | ||
ResultWriter: &stdout, | ||
} | ||
|
||
results := []View{ | ||
&logView{ | ||
Log: &management.Log{ | ||
LogID: auth0.String("354234"), | ||
Type: auth0.String("sapi"), | ||
Description: auth0.String("Update branding settings"), | ||
var testCases = []struct { | ||
name string | ||
givenData []View | ||
givenFormat string | ||
expectedResults string | ||
}{ | ||
{ | ||
name: "it can correctly output members as a table", | ||
givenData: []View{ | ||
&membersView{ | ||
ID: "123", | ||
Name: "John", | ||
Email: "[email protected]", | ||
}, | ||
}, | ||
expectedResults: " ID NAME EMAIL PICTURE \n 123 John [email protected] \n", | ||
}, | ||
} | ||
|
||
t.Run("Stream correctly handles nil channel", func(t *testing.T) { | ||
mockRender.Stream(results, nil) | ||
expectedResult := `TYPE DESCRIPTION DATE CONNECTION CLIENT | ||
API Operation Update branding settings Jan 01 00:00:00.000 N/A N/A | ||
` | ||
assert.Equal(t, expectedResult, stdout.String()) | ||
stdout.Reset() | ||
}) | ||
|
||
t.Run("Stream successfully", func(t *testing.T) { | ||
viewChan := make(chan View) | ||
|
||
var wg sync.WaitGroup | ||
wg.Add(1) | ||
go func() { | ||
defer wg.Done() | ||
mockRender.Stream(results, viewChan) | ||
}() | ||
|
||
wg.Add(1) | ||
go func() { | ||
defer wg.Done() | ||
viewChan <- &logView{ | ||
Log: &management.Log{ | ||
LogID: auth0.String("354236"), | ||
Type: auth0.String("sapi"), | ||
Description: auth0.String("Update tenant settings"), | ||
{ | ||
name: "it can correctly output members as json", | ||
givenData: []View{ | ||
&membersView{ | ||
ID: "123", | ||
Name: "John", | ||
Email: "[email protected]", | ||
raw: struct { | ||
ID string | ||
Name string | ||
Email string | ||
}{ | ||
ID: "123", | ||
Name: "John", | ||
Email: "[email protected]", | ||
}, | ||
}, | ||
} | ||
close(viewChan) | ||
}() | ||
|
||
wg.Wait() | ||
}, | ||
givenFormat: string(OutputFormatJSON), | ||
expectedResults: "[\n {\n \"ID\": \"123\",\n \"Name\": \"John\",\n \"Email\": \"[email protected]\"\n }\n]", | ||
}, | ||
{ | ||
name: "it can correctly output an empty json array when no data", | ||
givenData: []View{}, | ||
givenFormat: string(OutputFormatJSON), | ||
expectedResults: "[]", | ||
}, | ||
} | ||
|
||
expectedResult := `TYPE DESCRIPTION DATE CONNECTION CLIENT | ||
API Operation Update branding settings Jan 01 00:00:00.000 N/A N/A | ||
API Operation Update tenant settings Jan 01 00:00:00.000 N/A N/A | ||
` | ||
assert.Equal(t, expectedResult, stdout.String()) | ||
stdout.Reset() | ||
}) | ||
} | ||
for _, testCase := range testCases { | ||
t.Run(testCase.name, func(t *testing.T) { | ||
mockRender.Format = OutputFormat(testCase.givenFormat) | ||
mockRender.Results(testCase.givenData) | ||
|
||
func TestIndent(t *testing.T) { | ||
assert.Equal(t, "foo", indent("foo", "")) | ||
assert.Equal(t, " foo", indent("foo", " ")) | ||
assert.Equal(t, " line1\n line2\n line3", indent("line1\nline2\nline3", " ")) | ||
assert.Equal(t, testCase.expectedResults, stdout.String()) | ||
stdout.Reset() | ||
}) | ||
} | ||
} |
This file contains 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,73 @@ | ||
package display | ||
|
||
import ( | ||
"bytes" | ||
"io" | ||
"sync" | ||
"testing" | ||
|
||
"github.com/auth0/go-auth0/management" | ||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/auth0/auth0-cli/internal/auth0" | ||
) | ||
|
||
func TestStream(t *testing.T) { | ||
var stdout bytes.Buffer | ||
mockRender := &Renderer{ | ||
MessageWriter: io.Discard, | ||
ResultWriter: &stdout, | ||
} | ||
|
||
results := []View{ | ||
&logView{ | ||
Log: &management.Log{ | ||
LogID: auth0.String("354234"), | ||
Type: auth0.String("sapi"), | ||
Description: auth0.String("Update branding settings"), | ||
}, | ||
}, | ||
} | ||
|
||
t.Run("Stream correctly handles nil channel", func(t *testing.T) { | ||
mockRender.Stream(results, nil) | ||
expectedResult := `TYPE DESCRIPTION DATE CONNECTION CLIENT | ||
API Operation Update branding settings Jan 01 00:00:00.000 N/A N/A | ||
` | ||
assert.Equal(t, expectedResult, stdout.String()) | ||
stdout.Reset() | ||
}) | ||
|
||
t.Run("Stream successfully", func(t *testing.T) { | ||
viewChan := make(chan View) | ||
|
||
var wg sync.WaitGroup | ||
wg.Add(1) | ||
go func() { | ||
defer wg.Done() | ||
mockRender.Stream(results, viewChan) | ||
}() | ||
|
||
wg.Add(1) | ||
go func() { | ||
defer wg.Done() | ||
viewChan <- &logView{ | ||
Log: &management.Log{ | ||
LogID: auth0.String("354236"), | ||
Type: auth0.String("sapi"), | ||
Description: auth0.String("Update tenant settings"), | ||
}, | ||
} | ||
close(viewChan) | ||
}() | ||
|
||
wg.Wait() | ||
|
||
expectedResult := `TYPE DESCRIPTION DATE CONNECTION CLIENT | ||
API Operation Update branding settings Jan 01 00:00:00.000 N/A N/A | ||
API Operation Update tenant settings Jan 01 00:00:00.000 N/A N/A | ||
` | ||
assert.Equal(t, expectedResult, stdout.String()) | ||
stdout.Reset() | ||
}) | ||
} |
This file contains 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