Skip to content

Commit

Permalink
Merge pull request #5 from gostaticanalysis/add-unit-tests
Browse files Browse the repository at this point in the history
Added unit tests
  • Loading branch information
tenntenn authored Dec 9, 2021
2 parents 71f0347 + 9a6f2aa commit f6e5b33
Show file tree
Hide file tree
Showing 26 changed files with 612 additions and 0 deletions.
142 changes: 142 additions & 0 deletions fsys_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
package skeletonkit_test

import (
"embed"
"flag"
"io"
"path/filepath"
"strings"
"testing"
"text/template"

"github.com/gostaticanalysis/skeletonkit"
"github.com/tenntenn/golden"
)

//go:embed testdata/template
var testTmplFS embed.FS

var (
flagUpdate bool
)

func init() {
flag.BoolVar(&flagUpdate, "update", false, "update golden files")
}

func TestFsysCreateDir(t *testing.T) {
t.Parallel()
F := golden.TxtarWith
T := func(opts ...skeletonkit.TemplateOption) []skeletonkit.TemplateOption {
return opts
}
C := func(opts ...skeletonkit.CreatorOption) []skeletonkit.CreatorOption {
return opts
}

type appInfo struct {
Name string
ModulePath string
}

type errs struct {
parse bool
exec bool
create bool
}

cases := map[string]struct {
dirinit string
root string
info interface{}

tmplOpts []skeletonkit.TemplateOption
creatorOpts []skeletonkit.CreatorOption

input string

wantErr errs
}{
"clean": {"", "example", appInfo{"example", "example.com/example"}, nil, nil, "", errs{false, false, false}},
"clean-relative": {"", ".", appInfo{"example", "example.com/example"}, nil, nil, "", errs{false, false, false}},

"overwrite-cancel": {F(t, "example/main.go", "// not overwritten"), "example", appInfo{"example", "example.com/example"}, nil, nil, "1\n", errs{false, false, false}},
"overwrite-cancel-relative": {F(t, "main.go", "// not overwritten"), ".", appInfo{"example", "example.com/example"}, nil, nil, "1\n", errs{false, false, false}},

"overwrite-force": {F(t, "example/main.go", "// not overwritten"), "example", appInfo{"example", "example.com/example"}, nil, nil, "2\n", errs{false, false, false}},
"overwrite-force-relative": {F(t, "main.go", "// not overwritten"), ".", appInfo{"example", "example.com/example"}, nil, nil, "2\n", errs{false, false, false}},

"overwrite-confirm-yes": {F(t, "example/main.go", "// not overwritten"), "example", appInfo{"example", "example.com/example"}, nil, nil, "3\ny\n", errs{false, false, false}},
"overwrite-confirm-yes-relative": {F(t, "main.go", "// not overwritten"), ".", appInfo{"example", "example.com/example"}, nil, nil, "3\ny\n", errs{false, false, false}},

"overwrite-confirm-no": {F(t, "example/main.go", "// not overwritten"), "example", appInfo{"example", "example.com/example"}, nil, nil, "3\nn\n", errs{false, false, false}},
"overwrite-confirm-no-relative": {F(t, "main.go", "// not overwritten"), ".", appInfo{"example", "example.com/example"}, nil, nil, "3\nn\n", errs{false, false, false}},

"overwrite-newonly": {F(t, "example/go.mod", "// not overwritten"), "example", appInfo{"example", "example.com/example"}, nil, nil, "4\n", errs{false, false, false}},
"overwrite-newonly-relative": {F(t, "go.mod", "// not overwritten"), ".", appInfo{"example", "example.com/example"}, nil, nil, "4\n", errs{false, false, false}},

"prompt-choose-invalidinput": {F(t, "go.mod", "// not overwritten"), ".", appInfo{"example", "example.com/example"}, nil, nil, "INVALID\n", errs{false, false, true}},
"prompt-yesno-invalidinput": {F(t, "go.mod", "// not overwritten"), ".", appInfo{"example", "example.com/example"}, nil, nil, "3\nINVALID\n", errs{false, false, true}},

"templateopts-delims": {"", "example", appInfo{"example", "example.com/example"}, T(skeletonkit.TemplateWithDelims("$$", "$$")), nil, "", errs{false, false, false}},
"templateopts-funcs": {"", "example", appInfo{"example", "example.com/example"}, T(skeletonkit.TemplateWithFuncs(template.FuncMap{"gomod": func() string { return "DIFFERENT-GOMOD" }})), nil, "", errs{false, false, false}},
"creatoropts-empty": {"", "example", appInfo{"example", "example.com/example"}, nil, C(skeletonkit.CreatorWithEmpty(true)), "", errs{false, false, false}},
"creatoropts-policy": {F(t, "example/main.go", "// not overwritten"), "example", appInfo{"example", "example.com/example"}, nil, C(skeletonkit.CreatorWithPolicy(skeletonkit.Confirm)), "n\n", errs{false, false, false}},
}

if flagUpdate {
golden.RemoveAll(t, "testdata")
}

for name, tt := range cases {
name, tt := name, tt
t.Run(name, func(t *testing.T) {
t.Parallel()

dir := t.TempDir()
if tt.dirinit != "" {
golden.DirInit(t, dir, tt.dirinit)
}

prompt := &skeletonkit.Prompt{
Input: strings.NewReader(tt.input),
Output: io.Discard,
ErrOutput: io.Discard,
}

tmpl, err := skeletonkit.ParseTemplate(testTmplFS, "example", "testdata/template", tt.tmplOpts...)
switch {
case tt.wantErr.parse && err == nil:
t.Error("expected error did not occur")
case !tt.wantErr.parse && err != nil:
t.Error("unexpected error:", err)
}

fsys, err := skeletonkit.ExecuteTemplate(tmpl, tt.info)
switch {
case tt.wantErr.exec && err == nil:
t.Error("expected error did not occur")
case !tt.wantErr.exec && err != nil:
t.Error("unexpected error:", err)
}

err = skeletonkit.CreateDir(prompt, filepath.Join(dir, tt.root), fsys, tt.creatorOpts...)
switch {
case tt.wantErr.create && err == nil:
t.Error("expected error did not occur")
case !tt.wantErr.create && err != nil:
t.Error("unexpected error:", err)
}

got := golden.Txtar(t, dir)

if flagUpdate {
golden.Update(t, "testdata", name, got)
}

if diff := golden.Diff(t, "testdata", name, got); diff != "" {
t.Error(diff)
}
})
}
}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ go 1.17

require (
github.com/josharian/txtarfs v0.0.0-20210615234325-77aca6df5bca
github.com/tenntenn/golden v0.2.0
golang.org/x/mod v0.4.2
golang.org/x/tools v0.1.7
)

require (
github.com/google/go-cmp v0.5.6 // indirect
github.com/josharian/mapfs v0.0.0-20210615234106-095c008854e6 // indirect
golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e // indirect
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
Expand Down
10 changes: 10 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
github.com/google/go-cmp v0.5.6 h1:BKbKCqvP6I+rmFHt06ZmyQtvB8xAkWdhFyr0ZUNZcxQ=
github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/josharian/mapfs v0.0.0-20210615234106-095c008854e6 h1:c+ctPFdISggaSNCfU1IueNBAsqetJSvMcpQlT+0OVdY=
github.com/josharian/mapfs v0.0.0-20210615234106-095c008854e6/go.mod h1:Rv/momJI8DgrWnBZip+SgagpcgORIZQE5SERlxNb8LY=
github.com/josharian/txtarfs v0.0.0-20210615234325-77aca6df5bca h1:a8xeK4GsWLE4LYo5VI4u1Cn7ZvT1NtXouXR3DdKLB8Q=
github.com/josharian/txtarfs v0.0.0-20210615234325-77aca6df5bca/go.mod h1:UbC32ft9G/jG+sZI8wLbIBNIrYr7vp/yqMDa9SxVBNA=
github.com/tenntenn/golden v0.2.0 h1:ENbHNS5P2Bcnh2QWQcwtNPDYnIvFGuK4lKVDkCq4AHs=
github.com/tenntenn/golden v0.2.0/go.mod h1:OB8A7xwUZ9xE19KXoOMPl223hhcH4uD8oeQS9fLTiEE=
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
github.com/yuin/goldmark v1.4.0/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
Expand All @@ -13,6 +18,7 @@ golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM=
golang.org/x/net v0.0.0-20210805182204-aaa1db679c0d/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
Expand All @@ -22,7 +28,9 @@ golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210119212857-b64e53b001e4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e h1:WUoyKPm6nCo1BnNUvPGnFG3T5DUVem42yDJZZ4CNxMA=
golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
Expand All @@ -32,9 +40,11 @@ golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.0/go.mod h1:xkSsbof2nBLbhDlRMhhhyNLN/zl3eTqcnHD5viDpcZ0=
golang.org/x/tools v0.1.5/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/tools v0.1.7 h1:6j8CgantCy3yc8JGBqkDLMKWqZ0RDU2g1HVgacojGWQ=
golang.org/x/tools v0.1.7/go.mod h1:LGqMHiF4EqQNHR1JncWGqT5BVaXmza+X+BDGol+dOxo=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
30 changes: 30 additions & 0 deletions testdata/clean-relative.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
-- config.go --
// Code generated from skeletonkit. DO NOT EDIT.

package main

import (
"fmt"
)

func init() {
fmt.Println("initialising configuration")
}
-- go.mod --
module example.com/example

go 1.17

-- main.go --
// Code generated from skeletonkit. DO NOT EDIT.

package main

import (
"fmt"
)

func main() {
fmt.Println("testing skeletonkit. generating for appname: example.")
fmt.Println("different delim: $$.Name$$")
}
30 changes: 30 additions & 0 deletions testdata/clean.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
-- example/config.go --
// Code generated from skeletonkit. DO NOT EDIT.

package main

import (
"fmt"
)

func init() {
fmt.Println("initialising configuration")
}
-- example/go.mod --
module example.com/example

go 1.17

-- example/main.go --
// Code generated from skeletonkit. DO NOT EDIT.

package main

import (
"fmt"
)

func main() {
fmt.Println("testing skeletonkit. generating for appname: example.")
fmt.Println("different delim: $$.Name$$")
}
32 changes: 32 additions & 0 deletions testdata/creatoropts-empty.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
-- example/cmd/.gitkeep --
-- example/config.go --
// Code generated from skeletonkit. DO NOT EDIT.

package main

import (
"fmt"
)

func init() {
fmt.Println("initialising configuration")
}
-- example/go.mod --
module example.com/example

go 1.17

-- example/internal/.gitkeep --
-- example/main.go --
// Code generated from skeletonkit. DO NOT EDIT.

package main

import (
"fmt"
)

func main() {
fmt.Println("testing skeletonkit. generating for appname: example.")
fmt.Println("different delim: $$.Name$$")
}
19 changes: 19 additions & 0 deletions testdata/creatoropts-policy.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
-- example/config.go --
// Code generated from skeletonkit. DO NOT EDIT.

package main

import (
"fmt"
)

func init() {
fmt.Println("initialising configuration")
}
-- example/go.mod --
module example.com/example

go 1.17

-- example/main.go --
// not overwritten
19 changes: 19 additions & 0 deletions testdata/overwrite-cancel-relative.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
-- config.go --
// Code generated from skeletonkit. DO NOT EDIT.

package main

import (
"fmt"
)

func init() {
fmt.Println("initialising configuration")
}
-- go.mod --
module example.com/example

go 1.17

-- main.go --
// not overwritten
19 changes: 19 additions & 0 deletions testdata/overwrite-cancel.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
-- example/config.go --
// Code generated from skeletonkit. DO NOT EDIT.

package main

import (
"fmt"
)

func init() {
fmt.Println("initialising configuration")
}
-- example/go.mod --
module example.com/example

go 1.17

-- example/main.go --
// not overwritten
19 changes: 19 additions & 0 deletions testdata/overwrite-confirm-no-relative.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
-- config.go --
// Code generated from skeletonkit. DO NOT EDIT.

package main

import (
"fmt"
)

func init() {
fmt.Println("initialising configuration")
}
-- go.mod --
module example.com/example

go 1.17

-- main.go --
// not overwritten
19 changes: 19 additions & 0 deletions testdata/overwrite-confirm-no.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
-- example/config.go --
// Code generated from skeletonkit. DO NOT EDIT.

package main

import (
"fmt"
)

func init() {
fmt.Println("initialising configuration")
}
-- example/go.mod --
module example.com/example

go 1.17

-- example/main.go --
// not overwritten
Loading

0 comments on commit f6e5b33

Please sign in to comment.