-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Add output format validator
Signed-off-by: Sergiy Kulanov <[email protected]>
- Loading branch information
Showing
5 changed files
with
140 additions
and
0 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
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 | ||
} |
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,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) | ||
} | ||
}) | ||
} | ||
} |
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
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