Skip to content

Commit

Permalink
Miscellaneous tests (colors, auth0 logout, build info) (#705)
Browse files Browse the repository at this point in the history
* Buildinfo tests

* Removing dead code and tests to ansi file

* Adding auth0 logout

* Fixing env check conditional

* Fixing linting error

---------

Co-authored-by: Will Vedder <[email protected]>
  • Loading branch information
willvedd and willvedd authored Apr 3, 2023
1 parent aaec788 commit 927259a
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 48 deletions.
44 changes: 1 addition & 43 deletions internal/ansi/ansi.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package ansi

import (
"fmt"
"os"

"github.com/logrusorgru/aurora"
Expand All @@ -10,15 +9,6 @@ import (
"github.com/auth0/auth0-cli/internal/iostream"
)

var darkTerminalStyle = &pretty.Style{
Key: [2]string{"\x1B[34m", "\x1B[0m"},
String: [2]string{"\x1B[30m", "\x1B[0m"},
Number: [2]string{"\x1B[94m", "\x1B[0m"},
True: [2]string{"\x1B[35m", "\x1B[0m"},
False: [2]string{"\x1B[35m", "\x1B[0m"},
Null: [2]string{"\x1B[31m", "\x1B[0m"},
}

// ForceColors forces the use of colors and other ANSI sequences.
var ForceColors = false

Expand Down Expand Up @@ -56,31 +46,16 @@ func Initialize(shouldDisableColors bool) {

// ColorizeJSON returns a colorized version of the input JSON, if the writer
// supports colors.
func ColorizeJSON(json string, darkStyle bool) string {
func ColorizeJSON(json string) string {
if !shouldUseColors() {
return json
}

style := (*pretty.Style)(nil)
if darkStyle {
style = darkTerminalStyle
}

return string(pretty.Color([]byte(json), style))
}

// ColorizeStatus returns a colorized number for HTTP status code.
func ColorizeStatus(status int) aurora.Value {
switch {
case status >= 500:
return color.Red(status).Bold()
case status >= 300:
return color.Yellow(status).Bold()
default:
return color.Green(status).Bold()
}
}

// Faint returns slightly offset color text if the writer supports it.
func Faint(text string) string {
return color.Sprintf(color.Faint(text))
Expand Down Expand Up @@ -131,23 +106,6 @@ func Cyan(text string) string {
return color.Sprintf(color.BrightCyan(text))
}

// Linkify returns an ANSI escape sequence with an hyperlink, if the writer
// supports colors.
func Linkify(text, url string) string {
if !shouldUseColors() {
return text
}

// See https://gist.github.com/egmontkob/eb114294efbcd5adb1944c9f3cb5feda
// for more information about this escape sequence.
return fmt.Sprintf("\x1b]8;;%s\x1b\\%s\x1b]8;;\x1b\\", url, text)
}

// StrikeThrough returns struck though text if the writer supports colors.
func StrikeThrough(text string) string {
return color.Sprintf(color.StrikeThrough(text))
}

func shouldUseColors() bool {
useColors := ForceColors || iostream.IsOutputTerminal()

Expand Down
22 changes: 22 additions & 0 deletions internal/ansi/ansi_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package ansi

import (
"os"
"testing"

"github.com/stretchr/testify/assert"
)

func TestShouldUseColors(t *testing.T) {
os.Setenv("CLICOLOR_FORCE", "true")
assert.True(t, shouldUseColors())

os.Setenv("CLICOLOR_FORCE", "0")
assert.False(t, shouldUseColors())

os.Unsetenv("CLI_COLOR_FORCE")

os.Setenv("CLICOLOR", "0")
assert.False(t, shouldUseColors())
os.Unsetenv("CLICOLOR")
}
24 changes: 24 additions & 0 deletions internal/buildinfo/buildinfo_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package buildinfo

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestNewDefaultBuildInfo(t *testing.T) {
assert.Contains(t, NewDefaultBuildInfo().GoVersion, "go1.")
}

func TestNewBuildInfo(t *testing.T) {
mockBuildInfo := NewBuildInfo("mock-version", "mock-branch", "mock-build-date", "mock-build-user", "mock-go-version", "mock-revision")

assert.Equal(t, mockBuildInfo, BuildInfo{
Version: "mock-version",
Branch: "mock-branch",
BuildDate: "mock-build-date",
BuildUser: "mock-build-user",
GoVersion: "mock-go-version",
Revision: "mock-revision",
})
}
2 changes: 1 addition & 1 deletion internal/cli/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ func apiCmdRun(cli *cli, inputs *apiCmdInputs) func(cmd *cobra.Command, args []s
return fmt.Errorf("failed to prepare json output: %w", err)
}

cli.renderer.Output(ansi.ColorizeJSON(prettyJSON.String(), false))
cli.renderer.Output(ansi.ColorizeJSON(prettyJSON.String()))

return nil
}
Expand Down
4 changes: 2 additions & 2 deletions internal/display/branding_texts.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func (r *Renderer) BrandingTextShow(brandingTextJSON, prompt, language string) {
ansi.Bold(language),
),
)
r.Output(ansi.ColorizeJSON(brandingTextJSON, false))
r.Output(ansi.ColorizeJSON(brandingTextJSON))
r.Newline()
}

Expand All @@ -26,6 +26,6 @@ func (r *Renderer) BrandingTextUpdate(brandingTextJSON string, prompt, language
ansi.Bold(language),
),
)
r.Output(ansi.ColorizeJSON(brandingTextJSON, false))
r.Output(ansi.ColorizeJSON(brandingTextJSON))
r.Newline()
}
2 changes: 1 addition & 1 deletion internal/display/display.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func (r *Renderer) JSONResult(data interface{}) {
r.Errorf("couldn't marshal results as JSON: %v", err)
return
}
r.Output(ansi.ColorizeJSON(string(b), false))
r.Output(ansi.ColorizeJSON(string(b)))
}

func (r *Renderer) Results(data []View) {
Expand Down
2 changes: 1 addition & 1 deletion internal/display/organizations.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func makeOrganizationView(organization *management.Organization) *organizationVi
LogoURL: organization.GetBranding().GetLogoURL(),
AccentColor: accentColor,
BackgroundColor: backgroundColor,
Metadata: ansi.ColorizeJSON(metadata, false),
Metadata: ansi.ColorizeJSON(metadata),
raw: organization,
}
}
7 changes: 7 additions & 0 deletions test/integration/scripts/run-test-suites.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

set -e

if [[ -z "${AUTH0_CLI_CLIENT_DOMAIN}" || -z "${AUTH0_CLI_CLIENT_ID}" || -z "${AUTH0_CLI_CLIENT_SECRET}" ]]; then
echo "Error: AUTH0_CLI_CLIENT_DOMAIN, AUTH0_CLI_CLIENT_ID and AUTH0_CLI_CLIENT_SECRET environment variables need to be set"
exit 1
fi

auth0 login \
--domain "${AUTH0_CLI_CLIENT_DOMAIN}" \
--client-id "${AUTH0_CLI_CLIENT_ID}" \
Expand All @@ -15,4 +20,6 @@ exit_code=$?

bash ./test/integration/scripts/test-cleanup.sh

auth0 logout $AUTH0_CLI_CLIENT_DOMAIN

exit $exit_code

0 comments on commit 927259a

Please sign in to comment.