Skip to content

Commit

Permalink
Add a test
Browse files Browse the repository at this point in the history
  • Loading branch information
do-i-need-a-username committed Nov 29, 2023
1 parent 3359cf0 commit cc85bed
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 3 deletions.
21 changes: 18 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,14 @@ import (
"strings"
)

func main() {
input := flag.String("input", "", "Input JSON file")
output := flag.String("output", "", "Output file. Uses input with extension .ndjson if not provided")
var (
input = flag.String("input", "", "Input JSON file")
output = flag.String("output", "", "Output file. Uses input with extension .ndjson if not provided")
)

func run() error {
// input := flag.String("input", "", "Input JSON file")
// output := flag.String("output", "", "Output file. Uses input with extension .ndjson if not provided")

flag.Usage = func() {
fmt.Printf("Usage of %s:\n", os.Args[0])
Expand Down Expand Up @@ -71,4 +76,14 @@ func main() {
os.Exit(1)
}
}

return nil
}

func main() {
flag.Parse()
if err := run(); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
24 changes: 24 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package main

import (
"flag"
"os"
"testing"
)

func TestOutputFlag(t *testing.T) {
// Save the original command-line arguments and restore them at the end of the test.
origArgs := os.Args
defer func() { os.Args = origArgs }()

// Set the command-line arguments for this test.
os.Args = []string{"cmd", "-output", "test_output"}

// Parse the flags.
flag.Parse()

// Check that the output flag was correctly parsed.
if *output != "test_output" {
t.Errorf("Expected output flag to be 'test_output', but got '%s'", *output)
}
}

0 comments on commit cc85bed

Please sign in to comment.