Skip to content

Commit

Permalink
adding cli tests using testscript - gives 97.7% (all but logfatal and…
Browse files Browse the repository at this point in the history
… actual 1 line main)
  • Loading branch information
ldemailly committed Jan 8, 2023
1 parent 4baa6a0 commit a6cbebe
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 11 deletions.
2 changes: 2 additions & 0 deletions README.md
Expand Up @@ -32,3 +32,5 @@ REMOVED: gone1
NEW: new1
NEW: new2
```

See also [delta.txtar](delta.txtar) for examples (tests)
28 changes: 19 additions & 9 deletions delta.go
Expand Up @@ -34,16 +34,16 @@ var (
shortV string
)

func usage(msg string) {
func usage(msg string, args ...any) {
_, _ = fmt.Fprintf(os.Stderr, "Fortio delta %s usage:\n\t%s [flags] fileA fileB\nflags:\n",
shortV,
os.Args[0])
flag.PrintDefaults()
_, _ = fmt.Fprintf(os.Stderr, "Command flags (-a and -b) are space separeted and the lines are passed as the last argument")
_, _ = fmt.Fprintf(os.Stderr, "Command flags (-a and -b) are space separeted and the lines are passed as the last argument\n")
if msg != "" {
fmt.Fprintln(os.Stderr, msg)
fmt.Fprintf(os.Stderr, msg, args...)
fmt.Fprintln(os.Stderr)
}
os.Exit(1)
}

func toMap(filename string) (map[string]bool, error) {
Expand All @@ -65,6 +65,7 @@ func toMap(filename string) (map[string]bool, error) {

func removeCommon(a, b map[string]bool) {
if len(a) > len(b) {
log.LogVf("Swapping A and B iteration order as B is smaller")
a, b = b, a
}
for e := range a {
Expand Down Expand Up @@ -97,17 +98,23 @@ func runCmd(cmd0 string, args ...string) {
}

func main() {
os.Exit(Main())
}

func Main() int {
flag.CommandLine.Usage = func() { usage("") }
log.SetFlagDefaultsForClientTools()
sV, longV, fullV := version.FromBuildInfo()
shortV = sV
flag.Parse()
if *fullVersion {
fmt.Print(fullV)
os.Exit(0)
return 0
}
if len(flag.Args()) != 2 {
usage("Need 2 arguments (old and new files)")
numArgs := len(flag.Args())
if numArgs != 2 {
usage("Need 2 arguments (old and new files), got %d (%v)", numArgs, flag.Args())
return 1
}
cmdList := strings.Split(*aCmd, " ")
aCmd0 := cmdList[0]
Expand All @@ -126,11 +133,13 @@ func main() {
// read file content into map
aSet, err := toMap(flag.Arg(0))
if err != nil {
log.Fatalf("Error reading old file: %v", err)
log.Errf("Error reading file A: %v", err)
return 1
}
bSet, err := toMap(flag.Arg(1))
if err != nil {
log.Fatalf("Error reading new file: %v", err)
log.Errf("Error reading file B: %v", err)
return 1
}
// remove common entries
removeCommon(aSet, bSet)
Expand All @@ -150,4 +159,5 @@ func main() {
runCmd(bCmd0, bArgs...)
}
}
return 0
}
66 changes: 66 additions & 0 deletions delta.txtar
@@ -0,0 +1,66 @@
# testscript framework tests for delta command line

# Basic usage test
! delta
! stdout .
stderr 'Need 2 arguments'

# -version
delta -version
stdout 'path github.com/fortio/delta'
! stderr .

# -foo (bad flag)
! delta -foo
! stdout .
stderr 'flag provided but not defined: -foo'

# non existent input file A
! delta foo1 foo2
stderr 'E Error reading file A: open foo1: no such file or directory'
! stdout .

# non existent input file B
! delta oldFile foo2
stderr 'E Error reading file B: open foo2: no such file or directory'
! stdout .

# positive test (includes order of A/B iteration swap)
delta -loglevel verbose -b 'echo NEW:' -a 'echo REMOVED:' oldFile newFile
cmp stdout expected1
stderr 'Fortio delta.*started - will run "echo REMOVED:" on entries unique to oldFile, and "echo NEW:" on ones unique to newFile'
stderr 'Swapping A and B iteration order as B is smaller'

# swapping a and b test
delta -loglevel verbose -a 'echo NEW:' -b 'echo REMOVED:' newFile oldFile
cmp stdout expected2
! stderr 'Swapping A and B iteration order as B is smaller'

-- expected1 --
REMOVED: gone 2
REMOVED: gone1
REMOVED: goneA
REMOVED: goneZ
NEW: new1
NEW: new2
-- expected2 --
NEW: new1
NEW: new2
REMOVED: gone 2
REMOVED: gone1
REMOVED: goneA
REMOVED: goneZ
-- oldFile --
goneZ
old1
goneA
old2
gone1
old3
gone 2
-- newFile --
new1
old1
old2
old3
new2
18 changes: 18 additions & 0 deletions delta_test.go
@@ -0,0 +1,18 @@
package main

import (
"os"
"testing"

"github.com/rogpeppe/go-internal/testscript"
)

func TestMain(m *testing.M) {
os.Exit(testscript.RunMain(m, map[string]func() int{
"delta": Main,
}))
}

func TestDelta(t *testing.T) {
testscript.Run(t, testscript.Params{Dir: "./"})
}
10 changes: 8 additions & 2 deletions go.mod
Expand Up @@ -2,6 +2,12 @@ module github.com/fortio/delta

go 1.18

require fortio.org/fortio v1.38.4-cli1
require (
fortio.org/fortio v1.38.4-cli1
github.com/rogpeppe/go-internal v1.9.0
)

require golang.org/x/exp v0.0.0-20221111204811-129d8d6c17ab // indirect
require (
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e // indirect
golang.org/x/exp v0.0.0-20221111204811-129d8d6c17ab // indirect
)
4 changes: 4 additions & 0 deletions go.sum
@@ -1,5 +1,9 @@
fortio.org/assert v1.1.2 h1:t6WGDqPD5VFrUvx30U0+3mgXXcoPonrdKqt0vfJHn8E=
fortio.org/fortio v1.38.4-cli1 h1:qGGkqmh1soBVCKG+LZc4l3F3BE4oHVow6AUslunmzGU=
fortio.org/fortio v1.38.4-cli1/go.mod h1:4DhI17BkI3sU0DR4Z2HwochdpphTae+8NgZwWy6epRk=
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e h1:aoZm08cpOy4WuID//EZDgcC4zIxODThtZNPirFr42+A=
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
golang.org/x/exp v0.0.0-20221111204811-129d8d6c17ab h1:1S7USr8/C0Sgk4egxq4zZ07zYt2Xh1IiFp8hUMXH/us=
golang.org/x/exp v0.0.0-20221111204811-129d8d6c17ab/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc=

0 comments on commit a6cbebe

Please sign in to comment.