Skip to content

Commit

Permalink
test draft flag
Browse files Browse the repository at this point in the history
Signed-off-by: Aleksandar Stojanov <me@fnd.works>
  • Loading branch information
losisin committed Oct 25, 2023
1 parent 3dbf09a commit 47ce51a
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions schema_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package main

import (
"bufio"
"fmt"
"os"
"os/exec"
"strings"
"testing"
)

Expand Down Expand Up @@ -62,3 +66,57 @@ func TestMergeMaps(t *testing.T) {
t.Errorf("Merged map should contain key3 from mapB")
}
}

func TestMainWithDynamicFlags(t *testing.T) {
testCases := []struct {
draftVersion int
expectedString string
}{
{4, `"$schema": "http://json-schema.org/draft-04/schema#",`},
{6, `"$schema": "http://json-schema.org/draft-06/schema#",`},
{7, `"$schema": "http://json-schema.org/draft-07/schema#",`},
{2019, `"$schema": "https://json-schema.org/draft/2019-09/schema",`},
{2020, `"$schema": "https://json-schema.org/draft/2020-12/schema",`},
}

for _, testCase := range testCases {
// Run schema.go with the specified draft version flag.
cmd := exec.Command("go", "run", "schema.go", "--input=values.yaml", fmt.Sprintf("--draft=%d", testCase.draftVersion))

// Capture the command's output (stdout and stderr).
cmdOutput, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("Command execution failed: %v\nOutput:\n%s", err, cmdOutput)
}

// Check the exit status to ensure it ran successfully.
if cmd.ProcessState.ExitCode() != 0 {
t.Fatalf("Command execution returned a non-zero exit code: %d\nOutput:\n%s", cmd.ProcessState.ExitCode(), cmdOutput)
}

// Now, read and inspect the contents of values.schema.json.
file, err := os.Open("values.schema.json")
if err != nil {
t.Fatalf("Failed to open values.schema.json: %v", err)
}
defer file.Close()

scanner := bufio.NewScanner(file)
lineNumber := 1

for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
if lineNumber == 2 {
if line != testCase.expectedString {
t.Errorf("Expected line 2 to be:\n%s\nGot:\n%s", testCase.expectedString, line)
}
break
}
lineNumber++
}

if err := scanner.Err(); err != nil {
t.Fatalf("Error reading values.schema.json: %v", err)
}
}
}

0 comments on commit 47ce51a

Please sign in to comment.