Skip to content

Commit

Permalink
Use github.com/hknutzen/testtxt
Browse files Browse the repository at this point in the history
  • Loading branch information
hknutzen committed Feb 1, 2024
1 parent d5dbbd8 commit 690c70a
Show file tree
Hide file tree
Showing 12 changed files with 58 additions and 463 deletions.
9 changes: 6 additions & 3 deletions go/go.mod
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
module github.com/hknutzen/Netspoc/go

go 1.18
go 1.21.4

replace github.com/hknutzen/testtxt => ../../testtxt

require (
github.com/google/go-cmp v0.5.9
github.com/hknutzen/testtxt v0.0.0-20240201092256-1f14e38b7e01
github.com/octago/sflags v0.3.1-0.20210726012706-20f2a9c31dfc
github.com/spf13/pflag v1.0.5
go4.org/netipx v0.0.0-20220812043211-3cc044ffd68d
gopkg.in/yaml.v3 v3.0.1
golang.org/x/exp v0.0.0-20220907003533-145caa8ea1d0
)

require (
github.com/stretchr/testify v1.8.0 // indirect
golang.org/x/exp v0.0.0-20220907003533-145caa8ea1d0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
2 changes: 0 additions & 2 deletions go/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ github.com/asaskevich/govalidator v0.0.0-20180720115003-f9ffefc3facf/go.mod h1:l
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg=
github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/octago/sflags v0.2.0/go.mod h1:G0bjdxh4qPRycF74a2B8pU36iTp9QHGx0w0dFZXPt80=
Expand Down
47 changes: 37 additions & 10 deletions go/test/netspoc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
"github.com/hknutzen/Netspoc/go/pkg/rename"
"github.com/hknutzen/Netspoc/go/pkg/transposeservice"
"github.com/hknutzen/Netspoc/go/test/capture"
"github.com/hknutzen/Netspoc/go/test/tstdata"
"github.com/hknutzen/testtxt"

"github.com/google/go-cmp/cmp"
)
Expand Down Expand Up @@ -82,14 +82,32 @@ func TestNetspoc(t *testing.T) {
})
}

type descr struct {
Title string
Setup string
Input string
ReusePrev string
Options string
FileOption string
Job string
Param string
Params string
Output string
Warning string
Error string
ShowDiag bool
Todo bool
WithOutdir bool
}

func runTestFiles(t *testing.T, tc test) {
dataFiles := tstdata.GetFiles("../testdata/" + tc.dir)
dataFiles := testtxt.GetFiles("../testdata/" + tc.dir)
for _, file := range dataFiles {
file := file // capture range variable
t.Run(path.Base(file), func(t *testing.T) {
t.Parallel()
l, err := tstdata.ParseFile(file)
if err != nil {
var l []descr
if err := testtxt.ParseFile(file, &l); err != nil {
t.Fatal(err)
}
for _, descr := range l {
Expand All @@ -103,8 +121,16 @@ func runTestFiles(t *testing.T, tc test) {
}
}

func runTest(t *testing.T, tc test, d *tstdata.Descr) {

func runTest(t *testing.T, tc test, d descr) {
if d.Input == "" {
t.Fatal("missing =INPUT= in test")
}
if d.Output == "" && d.Warning == "" && d.Error == "" {
t.Fatal("missing =OUTPUT|WARNING|ERROR= in test")
}
if d.Error != "" && d.Warning != "" {
t.Fatalf("must not define =ERROR= together with =WARNING=")
}
if d.Todo {
t.Skip("skipping TODO test")
}
Expand All @@ -116,7 +142,7 @@ func runTest(t *testing.T, tc test, d *tstdata.Descr) {
// Prepare output directory.
var outDir string
if tc.typ == outDirT && d.Output != "" || tc.typ == outDirStdoutT ||
d.WithOutD {
d.WithOutdir {

outDir = path.Join(workDir, "out")
}
Expand All @@ -133,9 +159,9 @@ func runTest(t *testing.T, tc test, d *tstdata.Descr) {
}

// Prepare file for option '-f file'
if d.FOption != "" {
if d.FileOption != "" {
name := path.Join(workDir, "file")
if err := os.WriteFile(name, []byte(d.FOption), 0644); err != nil {
if err := os.WriteFile(name, []byte(d.FileOption), 0644); err != nil {
t.Fatal(err)
}
args = append(args, "-f", name)
Expand All @@ -144,7 +170,8 @@ func runTest(t *testing.T, tc test, d *tstdata.Descr) {
var inDir string
if input != "NONE" || outDir != "" {
// Prepare input file or directory.
inDir = tstdata.PrepareInDir(workDir, input)
src := path.Join(workDir, "netspoc")
inDir = testtxt.PrepareInDir(src, "INPUT", input)
args = append(args, inDir)

// Add location of output directory.
Expand Down
12 changes: 6 additions & 6 deletions go/test/oslink_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/hknutzen/Netspoc/go/pkg/oslink"
"github.com/hknutzen/Netspoc/go/pkg/pass1"
"github.com/hknutzen/Netspoc/go/test/capture"
"github.com/hknutzen/Netspoc/go/test/tstdata"
"github.com/hknutzen/testtxt"
)

func TestOsLink(t *testing.T) {
Expand Down Expand Up @@ -62,10 +62,10 @@ DIAG: Removed duplicate permit src=host:h1; dst=network:n1; prt=tcp 22; of servi
diag: true,
},
{
title: "Test stdout",
param: "network:n1",
run: pass1.PrintGroupMain,
input: "network:n1 = { ip = 10.1.1.0/24; }",
title: "Test stdout",
param: "network:n1",
run: pass1.PrintGroupMain,
input: "network:n1 = { ip = 10.1.1.0/24; }",
stdout: "10.1.1.0/24 network:n1\n",
},
}
Expand All @@ -76,7 +76,7 @@ DIAG: Removed duplicate permit src=host:h1; dst=network:n1; prt=tcp 22; of servi
os.Chdir(workDir)
os.Args = []string{"PROGRAM", "-q"}
inDir := "netspoc"
tstdata.PrepareInDir(inDir, descr.input)
testtxt.PrepareInDir(inDir, "INPUT", descr.input)
os.Args = append(os.Args, inDir)
if p := descr.param; p != "" {
os.Args = append(os.Args, p)
Expand Down
Loading

0 comments on commit 690c70a

Please sign in to comment.