Skip to content

Commit

Permalink
refactor: Add output format validator
Browse files Browse the repository at this point in the history
Signed-off-by: Sergiy Kulanov <[email protected]>
  • Loading branch information
SergK committed Oct 7, 2023
1 parent a579cbe commit 5732b71
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 0 deletions.
25 changes: 25 additions & 0 deletions pkg/cli/prerun/prerun.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package prerun

import (
"fmt"
)

// Define the allowed output formats
var ValidOutputFormats = []string{"dot", "puml", "mmd"}

func ValidateGraphPreRunE(outputFormat string) error {
if !contains(ValidOutputFormats, outputFormat) {
return fmt.Errorf("Invalid output format: %s. Allowed formats are: %v", outputFormat, ValidOutputFormats)
}
return nil
}

// Helper function to check if a string is in a slice of strings
func contains(s []string, e string) bool {
for _, a := range s {
if a == e {
return true
}
}
return false
}
33 changes: 33 additions & 0 deletions pkg/cli/prerun/prerun_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package prerun

import (
"testing"
)

func TestValidateGraphPreRunE(t *testing.T) {
testCases := []struct {
name string
outputFormat string
wantErr bool
}{
{
name: "Invalid output format",
outputFormat: "invalid",
wantErr: true,
},
{
name: "Valid output format",
outputFormat: "dot",
wantErr: false,
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
err := ValidateGraphPreRunE(tc.outputFormat)
if (err != nil) != tc.wantErr {
t.Errorf("ValidateGraphPreRunE() error = %v, wantErr %v", err, tc.wantErr)
}
})
}
}
4 changes: 4 additions & 0 deletions pkg/cmd/pipeline/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package pipeline
import (
"fmt"

"github.com/sergk/tkn-graph/pkg/cli/prerun"
pipelinepkg "github.com/sergk/tkn-graph/pkg/pipeline"
"github.com/sergk/tkn-graph/pkg/taskgraph"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -35,6 +36,9 @@ func graphCommand(p cli.Params) *cobra.Command {
}
return nil
},
PreRunE: func(cmd *cobra.Command, args []string) error {
return prerun.ValidateGraphPreRunE(opts.OutputFormat)
},
RunE: func(cmd *cobra.Command, args []string) error {
cs, err := p.Clients()
if err != nil {
Expand Down
4 changes: 4 additions & 0 deletions pkg/cmd/pipelinerun/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package pipelinerun
import (
"fmt"

"github.com/sergk/tkn-graph/pkg/cli/prerun"
pipelinerunpkg "github.com/sergk/tkn-graph/pkg/pipelinerun"
"github.com/sergk/tkn-graph/pkg/taskgraph"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -35,6 +36,9 @@ func graphCommand(p cli.Params) *cobra.Command {
}
return nil
},
PreRunE: func(cmd *cobra.Command, args []string) error {
return prerun.ValidateGraphPreRunE(opts.OutputFormat)
},
RunE: func(cmd *cobra.Command, args []string) error {
cs, err := p.Clients()
if err != nil {
Expand Down
74 changes: 74 additions & 0 deletions pkg/taskgraph/taskgraph_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package taskgraph

import (
"os"
"path/filepath"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -255,3 +257,75 @@ func TestPrintAllGraphsWithUnsupportedFormat(t *testing.T) {
// contains error message
assert.Contains(t, err.Error(), "Invalid output format: FAIL")
}

func TestWriteAllGraphs(t *testing.T) {
// Create a temporary directory for testing
tempDir, err := os.MkdirTemp("", "test-output")
if err != nil {
t.Fatalf("Failed to create temporary directory: %v", err)
}

defer func() {
// Remove the temporary directory and check for errors
if err = os.RemoveAll(tempDir); err != nil {
t.Errorf("Failed to remove temporary directory: %v", err)
}
}()

// Create a test graph
testGraph := &TaskGraph{
PipelineName: "test-pipeline",
Nodes: map[string]*TaskNode{
"task1": {
Name: "task1",
TaskRefName: "taskRef1",
Dependencies: []*TaskNode{
{
Name: "task2",
TaskRefName: "taskRef2",
},
},
},
"task2": {
Name: "task2",
TaskRefName: "taskRef2",
Dependencies: []*TaskNode{
{
Name: "task3",
TaskRefName: "taskRef3",
},
},
},
"task3": {
Name: "task3",
TaskRefName: "taskRef3",
Dependencies: []*TaskNode{
{
Name: "task4",
TaskRefName: "taskRef4",
},
},
},
"task4": {
Name: "task4",
TaskRefName: "taskRef4",
},
},
}

// Write the test graph to all supported formats
err = WriteAllGraphs([]*TaskGraph{testGraph}, "dot", tempDir, false)
assert.NoError(t, err)
err = WriteAllGraphs([]*TaskGraph{testGraph}, "puml", tempDir, false)
assert.NoError(t, err)
err = WriteAllGraphs([]*TaskGraph{testGraph}, "mmd", tempDir, false)
assert.NoError(t, err)

// Check that the files were created
_, err = os.Stat(filepath.Join(tempDir, "test-pipeline.dot"))
assert.NoError(t, err)
_, err = os.Stat(filepath.Join(tempDir, "test-pipeline.puml"))
assert.NoError(t, err)
_, err = os.Stat(filepath.Join(tempDir, "test-pipeline.mmd"))
assert.NoError(t, err)
}

0 comments on commit 5732b71

Please sign in to comment.