Skip to content

Commit

Permalink
add tests for option parsing again
Browse files Browse the repository at this point in the history
  • Loading branch information
rhysd committed Jun 22, 2024
1 parent 0ce773d commit 7fe50ae
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 15 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
module github.com/rhysd/vim-startuptime

go 1.16

require github.com/google/go-cmp v0.6.0 // indirect
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
46 changes: 31 additions & 15 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"flag"
"fmt"
"io"
"os"
)

Expand All @@ -23,25 +24,40 @@ const usageHeader = `Usage: vim-startuptime [flags] [-- VIMARGS...]
Flags:`

func usage() {
fmt.Fprintln(os.Stderr, usageHeader)
flag.PrintDefaults()
}
func parseOptions(out io.Writer, args []string) (*options, int) {
fs := flag.NewFlagSet(args[0], flag.ContinueOnError)
fs.SetOutput(out)

func main() {
opts := options{}
o := &options{}
fs.UintVar(&o.count, "count", 10, "How many times measure startup time")
fs.StringVar(&o.vimPath, "vimpath", "vim", "Command to run Vim or Neovim")
fs.BoolVar(&o.script, "script", false, "Only collects script loading times")
fs.UintVar(&o.warmup, "warmup", 1, "How many times start Vim at warm-up phase")
fs.BoolVar(&o.verbose, "verbose", false, "Verbose output to stderr while measurements")
fs.Usage = func() {
fmt.Fprintln(out, usageHeader)
fs.PrintDefaults()
}

if err := fs.Parse(args[1:]); err != nil {
if err == flag.ErrHelp {
return nil, 0
}
fmt.Fprintf(out, "error while parsing command line arguments: %s\n", err)
return nil, 1
}

flag.UintVar(&opts.count, "count", 10, "How many times measure startup time")
flag.StringVar(&opts.vimPath, "vimpath", "vim", "Command to run Vim or Neovim")
flag.BoolVar(&opts.script, "script", false, "Only collects script loading times")
flag.UintVar(&opts.warmup, "warmup", 1, "How many times start Vim at warm-up phase")
flag.BoolVar(&opts.verbose, "verbose", false, "Verbose output to stderr while measurements")
o.extraArgs = fs.Args()
return o, -1
}

flag.Usage = usage
flag.Parse()
opts.extraArgs = flag.Args()
func main() {
opts, code := parseOptions(os.Stderr, os.Args)
if code >= 0 {
os.Exit(code)
}

collected, err := collectMeasurements(&opts)
collected, err := collectMeasurements(opts)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
Expand Down
64 changes: 64 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package main

import (
"bytes"
"strings"
"testing"

"github.com/google/go-cmp/cmp"
)

func TestParseOptionsOK(t *testing.T) {
var b bytes.Buffer

o, s := parseOptions(&b, []string{"vim-startuptime", "-count", "3", "-vimpath", "nvim", "-script", "-warmup", "5", "-verbose", "--", "--foo"})
if s >= 0 {
t.Fatal("unexpected exit with status:", s)
}

want := &options{
count: 3,
vimPath: "nvim",
script: true,
extraArgs: []string{"--foo"},
warmup: 5,
verbose: true,
}

if !cmp.Equal(o, want, cmp.AllowUnexported(options{})) {
t.Fatal(cmp.Diff(o, want, cmp.AllowUnexported(options{})))
}

stderr := b.String()
if stderr != "" {
t.Fatalf("Unexpected stderr output %q", stderr)
}
}

func TestParseOptionsUnknownFlag(t *testing.T) {
var b bytes.Buffer

_, s := parseOptions(&b, []string{"vim-startuptime", "-foo"})
if s <= 0 {
t.Fatal("unexpected status:", s)
}

stderr := b.String()
if !strings.Contains(stderr, "flag provided but not defined: -foo") {
t.Fatal("unexpected error output to stderr:", stderr)
}
}

func TestParseOptionsHelpOutput(t *testing.T) {
var b bytes.Buffer

_, s := parseOptions(&b, []string{"vim-startuptime", "-help"})
if s != 0 {
t.Fatal("unexpected status:", s)
}

stderr := b.String()
if !strings.HasPrefix(stderr, "Usage: vim-startuptime [flags] [-- VIMARGS...]") {
t.Fatal("unexpected help output to stderr:", stderr)
}
}

0 comments on commit 7fe50ae

Please sign in to comment.