Skip to content

Commit

Permalink
fix(model): exit with error when any model test fails
Browse files Browse the repository at this point in the history
  • Loading branch information
rhamzeh committed Sep 21, 2023
1 parent 185f462 commit 5fe324f
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 16 deletions.
22 changes: 14 additions & 8 deletions cmd/model/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"fmt"
"os"
"path"
"strings"

"github.com/openfga/cli/internal/cmdutils"
"github.com/openfga/cli/internal/output"
Expand Down Expand Up @@ -66,7 +65,7 @@ var testCmd = &cobra.Command{
return err //nolint:wrapcheck
}

results, err := storetest.RunTests(
test, err := storetest.RunTests(
fgaClient,
storeData,
path.Dir(testsFileName),
Expand All @@ -75,18 +74,25 @@ var testCmd = &cobra.Command{
return fmt.Errorf("error running tests due to %w", err)
}

passing := test.IsPassing()

if verbose {
return output.Display(results) //nolint:wrapcheck
err = output.Display(test.Results)
if err != nil {
return fmt.Errorf("error displaying test results due to %w", err)
}

if !passing {
os.Exit(1)
}
}

friendlyResults := []string{}
fmt.Println(test.FriendlyDisplay())

for index := 0; index < len(results); index++ {
friendlyResults = append(friendlyResults, results[index].FriendlyDisplay())
if !passing {
os.Exit(1)
}

fmt.Printf("%v", strings.Join(friendlyResults, "\n---\n"))

return nil
},
}
Expand Down
47 changes: 45 additions & 2 deletions internal/storetest/testresult.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package storetest

import (
"fmt"
"strings"

"github.com/openfga/cli/internal/comparison"
"github.com/openfga/go-sdk/client"
Expand Down Expand Up @@ -38,6 +39,23 @@ type TestResult struct {
ListObjectsResults []ModelTestListObjectsSingleResult `json:"list_objects_results"`
}

// IsPassing - indicates whether a Test has succeeded completely or has any failing parts.
func (result TestResult) IsPassing() bool {
for index := 0; index < len(result.CheckResults); index++ {
if !result.CheckResults[index].IsPassing() {
return false
}
}

for index := 0; index < len(result.ListObjectsResults); index++ {
if !result.ListObjectsResults[index].IsPassing() {
return false
}
}

return true
}

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

if result.CheckResults[index].IsPassing() {
if checkResult.IsPassing() {
checkResultsOutput = fmt.Sprintf(
"%s\n✓ Check(user=%s,relation=%s,object=%s)",
checkResultsOutput,
Expand Down Expand Up @@ -85,7 +103,7 @@ func (result TestResult) FriendlyDisplay() string {
for index := 0; index < totalListObjectsCount; index++ {
listObjectsResult := result.ListObjectsResults[index]

if result.ListObjectsResults[index].IsPassing() {
if listObjectsResult.IsPassing() {
listObjectsResultsOutput = fmt.Sprintf(
"%s\n✓ ListObjects(user=%s,relation=%s,type=%s)",
listObjectsResultsOutput,
Expand Down Expand Up @@ -140,3 +158,28 @@ func (result TestResult) FriendlyDisplay() string {

return output
}

type TestResults struct {
Results []TestResult `json:"results"`
}

// IsPassing - indicates whether a Test Suite has succeeded completely or has any failing tests.
func (test TestResults) IsPassing() bool {
for index := 0; index < len(test.Results); index++ {
if !test.Results[index].IsPassing() {
return false
}
}

return true
}

func (test TestResults) FriendlyDisplay() string {
friendlyResults := []string{}

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

return fmt.Sprintf("%v", strings.Join(friendlyResults, "\n---\n"))
}
12 changes: 6 additions & 6 deletions internal/storetest/tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ func RunTests(
fgaClient *client.OpenFgaClient,
storeData StoreData,
basePath string,
) ([]TestResult, error) {
results := []TestResult{}
) (TestResults, error) {
test := TestResults{}

fgaServer, authModel, err := getLocalServerAndModel(storeData, basePath)
if err != nil {
return results, err
return test, err
}

for index := 0; index < len(storeData.Tests); index++ {
Expand All @@ -49,11 +49,11 @@ func RunTests(
authModel,
)
if err != nil {
return results, err
return test, err
}

results = append(results, result)
test.Results = append(test.Results, result)
}

return results, nil
return test, nil
}

0 comments on commit 5fe324f

Please sign in to comment.