Skip to content

Commit

Permalink
add some more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
losisin committed Dec 10, 2023
1 parent abb5f92 commit c606b57
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 7 deletions.
6 changes: 3 additions & 3 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ func TestMain(t *testing.T) {
},
{
name: "SuccessfulRun",
args: []string{"schema", "-input", "testdata/fail.yaml"},
expectedOut: "",
args: []string{"schema", "-input", "testdata/basic.yaml"},
expectedOut: "JSON schema successfully generated",
expectedError: "",
},
{
name: "GenerateError",
args: []string{"schema", "-input", "fail.yaml", "-draft", "2020"},
expectedOut: "Error: open fail.yaml: no such file or directory",
expectedOut: "error reading YAML file(s)",
expectedError: "",
},
}
Expand Down
9 changes: 6 additions & 3 deletions pkg/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ func GenerateJsonSchema(config *Config) error {
for _, filePath := range config.input {
content, err := os.ReadFile(filePath)
if err != nil {
return err
return errors.New("error reading YAML file(s)")
}

var node yaml.Node
if err := yaml.Unmarshal(content, &node); err != nil {
return errors.New("error reading YAML file(s)")
return errors.New("error unmarshaling YAML")
}

if len(node.Content) == 0 {
Expand Down Expand Up @@ -75,10 +75,13 @@ func GenerateJsonSchema(config *Config) error {
return err
}

Check warning on line 76 in pkg/generator.go

View check run for this annotation

Codecov / codecov/patch

pkg/generator.go#L75-L76

Added lines #L75 - L76 were not covered by tests

// Write the JSON schema to the output file
outputPath := config.outputPath
if err := os.WriteFile(outputPath, jsonBytes, 0644); err != nil {
return fmt.Errorf("error writing JSON schema to file '%s': %v", outputPath, err)
return errors.New("error writing schema to file")
}

fmt.Println("JSON schema successfully generated")

return nil
}
66 changes: 66 additions & 0 deletions pkg/generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package pkg

import (
"encoding/json"
"errors"
"os"
"testing"

Expand Down Expand Up @@ -37,3 +38,68 @@ func TestGenerateJsonSchema(t *testing.T) {

os.Remove(config.outputPath)
}

func TestGenerateJsonSchema_Errors(t *testing.T) {
tests := []struct {
name string
config *Config
setupFunc func() error
cleanupFunc func() error
expectedErr error
}{
{
name: "Invalid draft version",
config: &Config{
input: []string{"../testdata/basic.yaml"},
draft: 5,
},
expectedErr: errors.New("invalid draft version"),
},
{
name: "Missing file",
config: &Config{
input: []string{"missing.yaml"},
draft: 2020,
},
expectedErr: errors.New("error reading YAML file(s)"),
},
{
name: "Fail Unmarshal",
config: &Config{
input: []string{"../testdata/fail"},
outputPath: "testdata/failure/output_readonly_schema.json",
draft: 2020,
},
expectedErr: errors.New("error unmarshaling YAML"),
},
{
name: "Read-only filesystem",
config: &Config{
input: []string{"../testdata/basic.yaml"},
outputPath: "testdata/failure/output_readonly_schema.json",
draft: 2020,
},
expectedErr: errors.New("error writing schema to file"),
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.setupFunc != nil {
err := tt.setupFunc()
assert.NoError(t, err)
}

err := GenerateJsonSchema(tt.config)
assert.Error(t, err)
if err != nil {
assert.Contains(t, err.Error(), tt.expectedErr.Error())
}

if tt.cleanupFunc != nil {
err := tt.cleanupFunc()
assert.NoError(t, err)
}
})
}
}
2 changes: 1 addition & 1 deletion pkg/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func TestGetSchemaURL(t *testing.T) {
},
{
name: "Invalid Draft",
draft: 3, // assuming 3 is an invalid draft version
draft: 5,
expectedURL: "",
expectedErr: errors.New("invalid draft version. Please use one of: 4, 6, 7, 2019, 2020"),
},
Expand Down
1 change: 1 addition & 0 deletions testdata/fail
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{

0 comments on commit c606b57

Please sign in to comment.