Skip to content

Commit

Permalink
chore: simplify test output (#252)
Browse files Browse the repository at this point in the history
  • Loading branch information
rhamzeh committed Feb 13, 2024
2 parents 86be8fc + 8262cb3 commit cbf766b
Show file tree
Hide file tree
Showing 18 changed files with 88 additions and 57 deletions.
2 changes: 1 addition & 1 deletion cmd/man.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ var manCmd = &cobra.Command{
DisableFlagsInUseLine: true,
Hidden: true,
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
RunE: func(_ *cobra.Command, _ []string) error {
manPage, err := mcobra.NewManPage(1, rootCmd.Root())
if err != nil {
return err //nolint:wrapcheck
Expand Down
2 changes: 1 addition & 1 deletion cmd/model/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ var getCmd = &cobra.Command{
Short: "Read a Single Authorization Model",
Long: "Read an authorization model, pass in an empty model ID to get latest.",
Example: `fga model get --store-id=01H0H015178Y2V4CX10C2KGHF4 --model-id=01GXSA8YR785C4FYS3C0RTG7B1`,
RunE: func(cmd *cobra.Command, args []string) error {
RunE: func(cmd *cobra.Command, _ []string) error {
clientConfig := cmdutils.GetClientConfig(cmd)

fgaClient, err := clientConfig.GetFgaClient()
Expand Down
2 changes: 1 addition & 1 deletion cmd/model/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ var listCmd = &cobra.Command{
Short: "Read Authorization Models",
Long: "List authorization models in a store.",
Example: "fga model list --store-id=01H0H015178Y2V4CX10C2KGHF4",
RunE: func(cmd *cobra.Command, args []string) error {
RunE: func(cmd *cobra.Command, _ []string) error {
clientConfig := cmdutils.GetClientConfig(cmd)
fgaClient, err := clientConfig.GetFgaClient()
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion cmd/model/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ var testCmd = &cobra.Command{
Short: "Test an Authorization Model",
Long: "Run a set of tests against a particular Authorization Model.",
Example: `fga model test --tests model.fga.yaml`,
RunE: func(cmd *cobra.Command, args []string) error {
RunE: func(cmd *cobra.Command, _ []string) error {
clientConfig := cmdutils.GetClientConfig(cmd)

fgaClient, err := clientConfig.GetFgaClient()
Expand Down
2 changes: 2 additions & 0 deletions cmd/model/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,12 @@ func TestValidate(t *testing.T) {
t.Parallel()

model := authorizationmodel.AuthzModel{}

err := model.ReadFromJSONString(test.Input)
if err != nil {
return
}

output := validate(model)

if !reflect.DeepEqual(output, test.ExpectedOutput) {
Expand Down
3 changes: 2 additions & 1 deletion cmd/store/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package store

import (
"context"
"errors"
"fmt"

"github.com/openfga/go-sdk/client"
Expand Down Expand Up @@ -60,7 +61,7 @@ func CreateStoreWithModel(
response := CreateStoreAndModelResponse{}

if storeName == "" {
return nil, fmt.Errorf(`required flag(s) "name" not set`) //nolint:goerr113
return nil, errors.New(`required flag(s) "name" not set`) //nolint:goerr113
}

createStoreResponse, err := create(fgaClient, storeName)
Expand Down
2 changes: 1 addition & 1 deletion cmd/store/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ var deleteCmd = &cobra.Command{
Short: "Delete Store",
Long: "Mark a store as deleted.",
Example: "fga store delete --store-id=01H0H015178Y2V4CX10C2KGHF4",
RunE: func(cmd *cobra.Command, args []string) error {
RunE: func(cmd *cobra.Command, _ []string) error {
clientConfig := cmdutils.GetClientConfig(cmd)
// First, confirm whether this is intended
force, err := cmd.Flags().GetBool("force")
Expand Down
2 changes: 1 addition & 1 deletion cmd/store/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ var getCmd = &cobra.Command{
Short: "Get Store",
Long: `Get a particular store.`,
Example: "fga store get --store-id=01H0H015178Y2V4CX10C2KGHF4",
RunE: func(cmd *cobra.Command, args []string) error {
RunE: func(cmd *cobra.Command, _ []string) error {
clientConfig := cmdutils.GetClientConfig(cmd)
fgaClient, err := clientConfig.GetFgaClient()
if err != nil {
Expand Down
4 changes: 3 additions & 1 deletion cmd/store/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,13 @@ func importStore(
} else {
authModel := authorizationmodel.AuthzModel{}
clientConfig.StoreID = storeID

if format == authorizationmodel.ModelFormatJSON {
err = authModel.ReadFromJSONString(storeData.Model)
} else {
err = authModel.ReadFromDSLString(storeData.Model)
}

if err != nil {
return err //nolint:wrapcheck
}
Expand Down Expand Up @@ -90,7 +92,7 @@ var importCmd = &cobra.Command{
Short: "Import Store Data",
Long: `Import a store: updating the name, model and appending the global tuples`,
Example: "fga store import --file=model.fga.yaml",
RunE: func(cmd *cobra.Command, args []string) error {
RunE: func(cmd *cobra.Command, _ []string) error {
clientConfig := cmdutils.GetClientConfig(cmd)

storeID, err := cmd.Flags().GetString("store-id")
Expand Down
2 changes: 1 addition & 1 deletion cmd/store/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ var listCmd = &cobra.Command{
Short: "List Stores",
Long: `Get a list of stores.`,
Example: "fga store list",
RunE: func(cmd *cobra.Command, args []string) error {
RunE: func(cmd *cobra.Command, _ []string) error {
clientConfig := cmdutils.GetClientConfig(cmd)
fgaClient, err := clientConfig.GetFgaClient()
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion cmd/tuple/changes.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ var changesCmd = &cobra.Command{
Short: "Read Relationship Tuple Changes (Watch)",
Long: "Get a list of relationship tuple changes (Writes and Deletes) across time.",
Example: "fga tuple changes --store-id=01H0H015178Y2V4CX10C2KGHF4 --type document --continuation-token=MXw=",
RunE: func(cmd *cobra.Command, args []string) error {
RunE: func(cmd *cobra.Command, _ []string) error {
clientConfig := cmdutils.GetClientConfig(cmd)

fgaClient, err := clientConfig.GetFgaClient()
Expand Down
2 changes: 1 addition & 1 deletion cmd/tuple/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ var importCmd = &cobra.Command{
Deprecated: "use the write/delete command with the flag --file instead",
Long: "Imports Relationship Tuples to the store. " +
"This will write the tuples in chunks and at the end will report the tuple chunks that failed.",
RunE: func(cmd *cobra.Command, args []string) error {
RunE: func(cmd *cobra.Command, _ []string) error {
clientConfig := cmdutils.GetClientConfig(cmd)

fgaClient, err := clientConfig.GetFgaClient()
Expand Down
2 changes: 1 addition & 1 deletion cmd/tuple/read.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ var readCmd = &cobra.Command{
Short: "Read Relationship Tuples",
Long: "Read relationship tuples that exist in the system (does not evaluate).",
Example: "fga tuple read --store-id=01H0H015178Y2V4CX10C2KGHF4 --user user:anne --relation can_view --object document:roadmap", //nolint:lll
RunE: func(cmd *cobra.Command, args []string) error {
RunE: func(cmd *cobra.Command, _ []string) error {
clientConfig := cmdutils.GetClientConfig(cmd)

fgaClient, err := clientConfig.GetFgaClient()
Expand Down
3 changes: 2 additions & 1 deletion cmd/tuple/write.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package tuple

import (
"context"
"errors"
"fmt"

"github.com/openfga/go-sdk/client"
Expand Down Expand Up @@ -114,7 +115,7 @@ func writeTuplesFromFile(flags *flag.FlagSet, fgaClient *client.OpenFgaClient) e
}

if fileName == "" {
return fmt.Errorf("file name cannot be empty") //nolint:goerr113
return errors.New("file name cannot be empty") //nolint:goerr113
}

maxTuplesPerWrite, err := flags.GetInt("max-tuples-per-write")
Expand Down
2 changes: 1 addition & 1 deletion cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ var versionCmd *cobra.Command = &cobra.Command{
Use: "version",
Short: "Reports the FGA CLI version",
Long: "Reports the FGA CLI version.",
RunE: func(cmd *cobra.Command, args []string) error {
RunE: func(_ *cobra.Command, _ []string) error {
fmt.Printf("fga version %s\n", versionStr)

return nil
Expand Down
3 changes: 2 additions & 1 deletion internal/cmdutils/get-contextual-tuples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ func TestGetContextualTuplesWithNoError(t *testing.T) {
test := tests[index]
t.Run("TestGetContextualTuplesWithNoError"+string(rune(index)), func(t *testing.T) {
t.Parallel()

tuples, err := cmdutils.ParseContextualTuplesInner(test.raw)
if err != nil {
t.Error(err)
Expand Down Expand Up @@ -109,8 +110,8 @@ func TestGetContextualTuplesWithError(t *testing.T) {
test := tests[index]
t.Run("TestGetContextualTuplesWithNoError"+string(rune(index)), func(t *testing.T) {
t.Parallel()
_, err := cmdutils.ParseContextualTuplesInner(test.raw)

_, err := cmdutils.ParseContextualTuplesInner(test.raw)
if err == nil {
t.Error("Expect error but there is none")
}
Expand Down
2 changes: 2 additions & 0 deletions internal/confirmation/confirmation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,12 @@ func TestConfirmation(t *testing.T) {
test := test
t.Run(test._name, func(t *testing.T) {
t.Parallel()

result, err := askForConfirmation(bufio.NewReader(strings.NewReader(test.input)), "test")
if err != nil {
t.Error(err)
}

if result != test.result {
t.Errorf("Expect result %v actual %v", test.result, result)
}
Expand Down
106 changes: 64 additions & 42 deletions internal/storetest/testresult.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func (result TestResult) IsPassing() bool {
}

//nolint:cyclop
func (result TestResult) FriendlyDisplay() string {
func (result TestResult) FriendlyFailuresDisplay() string {
totalCheckCount := len(result.CheckResults)
failedCheckCount := 0
totalListObjectsCount := len(result.ListObjectsResults)
Expand All @@ -71,16 +71,7 @@ func (result TestResult) FriendlyDisplay() string {
for index := 0; index < totalCheckCount; index++ {
checkResult := result.CheckResults[index]

if checkResult.IsPassing() {
checkResultsOutput = fmt.Sprintf(
"%s\n✓ Check(user=%s,relation=%s,object=%s, context=%v)",
checkResultsOutput,
checkResult.Request.User,
checkResult.Request.Relation,
checkResult.Request.Object,
checkResult.Request.Context,
)
} else {
if !checkResult.IsPassing() {
failedCheckCount++

got := "N/A"
Expand All @@ -107,16 +98,7 @@ func (result TestResult) FriendlyDisplay() string {
for index := 0; index < totalListObjectsCount; index++ {
listObjectsResult := result.ListObjectsResults[index]

if listObjectsResult.IsPassing() {
listObjectsResultsOutput = fmt.Sprintf(
"%s\n✓ ListObjects(user=%s,relation=%s,type=%s, context=%v)",
listObjectsResultsOutput,
listObjectsResult.Request.User,
listObjectsResult.Request.Relation,
listObjectsResult.Request.Type,
listObjectsResult.Request.Context,
)
} else {
if !listObjectsResult.IsPassing() {
failedListObjectsCount++

got := "N/A"
Expand All @@ -139,30 +121,30 @@ func (result TestResult) FriendlyDisplay() string {
}
}

testStatus := "PASSING"
if failedCheckCount+failedListObjectsCount != 0 {
testStatus = "FAILING"
}

output := fmt.Sprintf(
"(%s) %s: Checks (%d/%d passing) | ListObjects (%d/%d passing)",
testStatus,
result.Name,
totalCheckCount-failedCheckCount,
totalCheckCount,
totalListObjectsCount-failedListObjectsCount,
totalListObjectsCount,
)
testStatus := "FAILING"
output := fmt.Sprintf(
"(%s) %s: Checks (%d/%d passing) | ListObjects (%d/%d passing)",
testStatus,
result.Name,
totalCheckCount-failedCheckCount,
totalCheckCount,
totalListObjectsCount-failedListObjectsCount,
totalListObjectsCount,
)

if failedCheckCount > 0 {
output = fmt.Sprintf("%s%s", output, checkResultsOutput)
}

if failedCheckCount > 0 {
output = fmt.Sprintf("%s%s", output, checkResultsOutput)
}
if failedListObjectsCount > 0 {
output = fmt.Sprintf("%s%s", output, listObjectsResultsOutput)
}

if failedListObjectsCount > 0 {
output = fmt.Sprintf("%s%s", output, listObjectsResultsOutput)
return output
}

return output
return ""
}

type TestResults struct {
Expand All @@ -184,8 +166,48 @@ func (test TestResults) FriendlyDisplay() string {
friendlyResults := []string{}

for index := 0; index < len(test.Results); index++ {
friendlyResults = append(friendlyResults, test.Results[index].FriendlyDisplay())
if !test.Results[index].IsPassing() {
friendlyResults = append(friendlyResults, test.Results[index].FriendlyFailuresDisplay())
}
}

failuresText := strings.Join(friendlyResults, "\n---\n")

totalTestCount := len(test.Results)
failedTestCount := 0
totalCheckCount := 0
failedCheckCount := 0
totalListObjectsCount := 0
failedListObjectsCount := 0

for _, testResult := range test.Results {
if !testResult.IsPassing() {
failedTestCount++
}

totalCheckCount += len(testResult.CheckResults)

for _, checkResult := range testResult.CheckResults {
if !checkResult.IsPassing() {
failedCheckCount++
}
}

totalListObjectsCount += len(testResult.ListObjectsResults)

for _, listObjectsResult := range testResult.ListObjectsResults {
if !listObjectsResult.IsPassing() {
failedListObjectsCount++
}
}
}

return strings.Join(friendlyResults, "\n---\n")
summary := fmt.Sprintf(
"\n---\n\n# Test Summary #\nTests %d/%d passing\nChecks %d/%d passing\nListObjects %d/%d passing",
totalTestCount-failedTestCount, totalTestCount,
totalCheckCount-failedCheckCount, totalCheckCount,
totalListObjectsCount-failedListObjectsCount, totalListObjectsCount,
)

return failuresText + summary
}

0 comments on commit cbf766b

Please sign in to comment.