Skip to content
Permalink
Browse files

add first test script

testscripts will let us write independent but simple tests for each of
the corner cases. At the cost of a few extra files, we'll make the tests
much easier to maintain and much more reliable.
  • Loading branch information...
mvdan committed Jan 21, 2019
1 parent 9b519df commit 6091117ab661bd7019e70e9964f2302aab3305e5
Showing with 69 additions and 12 deletions.
  1. +4 −1 go.mod
  2. +11 −0 go.sum
  3. +23 −11 main.go
  4. +22 −0 main_test.go
  5. +9 −0 testdata/scripts/cmds.txt
5 go.mod
@@ -1,3 +1,6 @@
module mvdan.cc/unparam

require golang.org/x/tools v0.0.0-20190121143147-24cd39ecf745
require (
github.com/rogpeppe/go-internal v1.1.0
golang.org/x/tools v0.0.0-20190121143147-24cd39ecf745
)
11 go.sum
@@ -1,2 +1,13 @@
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/rogpeppe/go-internal v1.1.0 h1:g0fH8RicVgNl+zVZDCDfbdWxAWoAEJyI7I3TZYXFiig=
github.com/rogpeppe/go-internal v1.1.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
golang.org/x/tools v0.0.0-20190121143147-24cd39ecf745 h1:aGoTCTvIjOOSnuAeiybrXAIbqAuPVB4DvRXlCrKT02s=
golang.org/x/tools v0.0.0-20190121143147-24cd39ecf745/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/errgo.v2 v2.1.0 h1:0vLT13EuvQ0hNvakwLuFZ/jYrLp5F3kcWHXdRggjCE8=
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
34 main.go
@@ -15,33 +15,45 @@ import (
)

var (
algo = flag.String("algo", "cha", `call graph construction algorithm (cha, rta).
flagSet = flag.NewFlagSet("unparam", flag.ContinueOnError)

algo = flagSet.String("algo", "cha", `call graph construction algorithm (cha, rta).
in general, use cha for libraries, and rta for programs with main packages.`)
tests = flag.Bool("tests", true, "include tests")
exported = flag.Bool("exported", false, "inspect exported functions")
debug = flag.Bool("debug", false, "debug prints")
tests = flagSet.Bool("tests", true, "include tests")
exported = flagSet.Bool("exported", false, "inspect exported functions")
debug = flagSet.Bool("debug", false, "debug prints")
)

func init() {
flag.Var((*buildutil.TagsFlag)(&build.Default.BuildTags), "tags",
flagSet.Var((*buildutil.TagsFlag)(&build.Default.BuildTags), "tags",
buildutil.TagsFlagDoc)
}

func main() {
flag.Usage = func() {
os.Exit(main1())
}

func main1() int {
flagSet.Usage = func() {
fmt.Fprintln(os.Stderr, "usage: unparam [flags] [package ...]")
flag.PrintDefaults()
flagSet.PrintDefaults()
}
if err := flagSet.Parse(os.Args[1:]); err != nil {
if err != flag.ErrHelp {
fmt.Fprintln(os.Stderr, err)
}
return 1
}
flag.Parse()
warns, err := check.UnusedParams(*tests, *algo, *exported, *debug, flag.Args()...)
warns, err := check.UnusedParams(*tests, *algo, *exported, *debug, flagSet.Args()...)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
return 1
}
for _, warn := range warns {
fmt.Println(warn)
}
if len(warns) > 0 {
os.Exit(1)
return 1
}
return 0
}
@@ -0,0 +1,22 @@
package main

import (
"os"
"path/filepath"
"testing"

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

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

func TestScripts(t *testing.T) {
t.Parallel()
testscript.Run(t, testscript.Params{
Dir: filepath.Join("testdata", "scripts"),
})
}
@@ -0,0 +1,9 @@
! unparam -h
stderr '^usage: unparam \[flags'
! stderr 'test\.' # don't include flags from testing
! stderr 'command not specified'
! stdout .

! unparam -badflag -- somepkg
stderr '-badflag'
stderr '^usage: unparam \[flags'

0 comments on commit 6091117

Please sign in to comment.
You can’t perform that action at this time.