From d0b98efd1bb91104e7970f249a89adcaffc0b41f Mon Sep 17 00:00:00 2001 From: Jon Webb Date: Fri, 27 Sep 2024 12:21:17 -0400 Subject: [PATCH] use make in GH actions --- .github/workflows/ci.yml | 14 +- .gitignore | 1 + Makefile | 83 ++++++--- getopt.go | 4 +- getopt_test.go | 14 +- testdata/cases.json | 4 +- testdata/fixtures.json | 368 +-------------------------------------- 7 files changed, 77 insertions(+), 411 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index daedbef..648a69b 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -23,16 +23,4 @@ jobs: - uses: actions/setup-go@v5 with: go-version: ${{ matrix.go }} - - run: go version - - run: go mod verify - - name: go mod tidy - run: | - go mod tidy - git diff --exit-code - - run: go vet ./... - - name: go fmt - run: | - go fmt ./... - git diff --exit-code - - name: go test - run: go test -v -count=1 -shuffle=on ./... + - run: make check diff --git a/.gitignore b/.gitignore index 8cc41fd..56b5a3d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ bin +tmp .vscode/* !.vscode/extensions.json diff --git a/Makefile b/Makefile index dba8773..3a201b7 100644 --- a/Makefile +++ b/Makefile @@ -1,40 +1,75 @@ -.DEFAULT_GOAL = check -.PHONY: FORCE +.DEFAULT_GOAL = all -CC = /usr/local/musl/bin/musl-gcc +GOCC ?= /usr/local/musl/bin/musl-gcc GOFLAGS = -ldflags '-linkmode external -extldflags "-static"' -OUTDIR = bin -TESTGEN_BINARY = $(OUTDIR)/testgen +BINDIR = bin +TMPDIR = tmp +TESTGEN_SRC = cmd/testgen/main.go +TESTGEN_BIN = $(BINDIR)/testgen +GOPKG = github.com/jon-codes/getopt -build-testgen: $(TESTGEN_BINARY) -.PHONY: build-testgen +## all: run development tasks (default target) +.PHONY: all +all: deps fmt vet test -clean: - rm -f $(TESTGEN_BINARY) -.PHONY: clean +.PHONY: check +check: deps-check fmt-check vet test + +## deps: clean deps +.PHONY: deps +deps: + go mod tidy -v +.PHONY: deps-check +deps-check: + go mod tidy -diff + go mod verify + +## fmt: go fmt +.PHONY: fmt fmt: go fmt ./... -.PHONY: fmt +.PHONY: fmt-check +fmt-check: + test -z "$(shell gofmt -l .)" + +## vet: go vet +.PHONY: vet vet: go vet ./... -.PHONY: vet -test: - go test -v -p=4 ./... +## test: go test .PHONY: test +test: + go test -v ./... -check: fmt vet test -.PHONY: check +## cover: go test coverage +.PHONY: cover +cover: temp + go test -v -coverprofile $(TMPDIR)/cover.out $(GOPKG) + go tool cover -html=$(TMPDIR)/cover.out -$(TESTGEN_BINARY): FORCE - @mkdir -p $(OUTDIR) - CC=$(CC) go build $(GOFLAGS) -o $@ ./cmd/testgen/main.go +## build-testgen: build test generator +.PHONY: build-testgen +build-testgen: + CC=$(GOCC) go build $(GOFLAGS) -o $(TESTGEN_BIN) $(TESTGEN_SRC) -deps: FORCE - go mod verify - go mod tidy +## testgen: generate tests +.PHONY: testgen +testgen: + $(TESTGEN_BIN) -all: clean deps -.PHONY: all +## clean: clean output +.PHONY: clean +clean: + rm -r $(BINDIR) $(TMPDIR) + +temp: + mkdir -p tmp + +## help: display this help +.PHONY: help +help: + @echo 'Usage:' + @sed -n 's/^##//p' ${MAKEFILE_LIST} | column -t -s ':' | sed -e 's/^/ /' diff --git a/getopt.go b/getopt.go index eb61317..96af8cb 100644 --- a/getopt.go +++ b/getopt.go @@ -241,9 +241,7 @@ func (s *State) readOpt(p Params) (res Result, err error) { } if checkNext && hasArg != NoArgument && s.OptIndex < len(s.Args) { - if s.Args[s.OptIndex] == "--" { - s.OptIndex++ - } else { + if s.Args[s.OptIndex] != "--" { res.OptArg = s.Args[s.OptIndex] s.OptIndex++ } diff --git a/getopt_test.go b/getopt_test.go index 311dc9c..bee6344 100644 --- a/getopt_test.go +++ b/getopt_test.go @@ -256,11 +256,23 @@ func TestGetOpt(t *testing.T) { }) t.Run("it treats arguments after '--' as parameters", func(t *testing.T) { + s := NewState(argsStr(`prgm -a -- -b`)) + p := Params{Opts: OptStr(`ab`), Function: function} + + wants := []assertion{ + {char: 'a', args: argsStr(`prgm -a -- -b`), optIndex: 2}, + {err: ErrDone, args: argsStr(`prgm -a -- -b`), optIndex: 3}, + } + + assertSeq(t, s, p, wants) + }) + + t.Run("it does not treat '--' as a potential option arguments", func(t *testing.T) { s := NewState(argsStr(`prgm -a -- p1`)) p := Params{Opts: OptStr(`a::`), Function: function} wants := []assertion{ - {char: 'a', args: argsStr(`prgm -a -- p1`), optIndex: 3}, + {char: 'a', args: argsStr(`prgm -a -- p1`), optIndex: 2}, {err: ErrDone, args: argsStr(`prgm -a -- p1`), optIndex: 3}, } diff --git a/testdata/cases.json b/testdata/cases.json index 88c8189..fe51488 100644 --- a/testdata/cases.json +++ b/testdata/cases.json @@ -1,3 +1 @@ -[ - { "label": "kitchen_sink", "args": "prgm -h --h --he -l -longa --longa a", "opts": "hl", "longopts": "help,longa:" } -] +[] diff --git a/testdata/fixtures.json b/testdata/fixtures.json index bba301d..fe51488 100644 --- a/testdata/fixtures.json +++ b/testdata/fixtures.json @@ -1,367 +1 @@ -[ - { - "label": "kitchen_sink", - "args": "prgm -h --h --he -l -longa --longa a", - "opts": "hl", - "longopts": "help,longa:", - "function": "getopt", - "mode": "gnu", - "want_args": "prgm -h --h --he -l -longa --longa a", - "want_optindex": 2, - "want_results": [ - { - "char": "h", - "name": "", - "opt_arg": "", - "err": "" - }, - { - "char": "-", - "name": "", - "opt_arg": "", - "err": "?" - } - ] - }, - { - "label": "kitchen_sink", - "args": "prgm -h --h --he -l -longa --longa a", - "opts": "hl", - "longopts": "help,longa:", - "function": "getopt", - "mode": "posix", - "want_args": "prgm -h --h --he -l -longa --longa a", - "want_optindex": 2, - "want_results": [ - { - "char": "h", - "name": "", - "opt_arg": "", - "err": "" - }, - { - "char": "-", - "name": "", - "opt_arg": "", - "err": "?" - } - ] - }, - { - "label": "kitchen_sink", - "args": "prgm -h --h --he -l -longa --longa a", - "opts": "hl", - "longopts": "help,longa:", - "function": "getopt", - "mode": "inorder", - "want_args": "prgm -h --h --he -l -longa --longa a", - "want_optindex": 2, - "want_results": [ - { - "char": "h", - "name": "", - "opt_arg": "", - "err": "" - }, - { - "char": "-", - "name": "", - "opt_arg": "", - "err": "?" - } - ] - }, - { - "label": "kitchen_sink", - "args": "prgm -h --h --he -l -longa --longa a", - "opts": "hl", - "longopts": "help,longa:", - "function": "getopt_long", - "mode": "gnu", - "want_args": "prgm -h --h --he -l -longa --longa a", - "want_optindex": 5, - "want_results": [ - { - "char": "h", - "name": "", - "opt_arg": "", - "err": "" - }, - { - "char": "", - "name": "help", - "opt_arg": "", - "err": "" - }, - { - "char": "", - "name": "help", - "opt_arg": "", - "err": "" - }, - { - "char": "l", - "name": "", - "opt_arg": "", - "err": "" - }, - { - "char": "l", - "name": "", - "opt_arg": "", - "err": "" - }, - { - "char": "o", - "name": "", - "opt_arg": "", - "err": "?" - } - ] - }, - { - "label": "kitchen_sink", - "args": "prgm -h --h --he -l -longa --longa a", - "opts": "hl", - "longopts": "help,longa:", - "function": "getopt_long", - "mode": "posix", - "want_args": "prgm -h --h --he -l -longa --longa a", - "want_optindex": 5, - "want_results": [ - { - "char": "h", - "name": "", - "opt_arg": "", - "err": "" - }, - { - "char": "", - "name": "help", - "opt_arg": "", - "err": "" - }, - { - "char": "", - "name": "help", - "opt_arg": "", - "err": "" - }, - { - "char": "l", - "name": "", - "opt_arg": "", - "err": "" - }, - { - "char": "l", - "name": "", - "opt_arg": "", - "err": "" - }, - { - "char": "o", - "name": "", - "opt_arg": "", - "err": "?" - } - ] - }, - { - "label": "kitchen_sink", - "args": "prgm -h --h --he -l -longa --longa a", - "opts": "hl", - "longopts": "help,longa:", - "function": "getopt_long", - "mode": "inorder", - "want_args": "prgm -h --h --he -l -longa --longa a", - "want_optindex": 5, - "want_results": [ - { - "char": "h", - "name": "", - "opt_arg": "", - "err": "" - }, - { - "char": "", - "name": "help", - "opt_arg": "", - "err": "" - }, - { - "char": "", - "name": "help", - "opt_arg": "", - "err": "" - }, - { - "char": "l", - "name": "", - "opt_arg": "", - "err": "" - }, - { - "char": "l", - "name": "", - "opt_arg": "", - "err": "" - }, - { - "char": "o", - "name": "", - "opt_arg": "", - "err": "?" - } - ] - }, - { - "label": "kitchen_sink", - "args": "prgm -h --h --he -l -longa --longa a", - "opts": "hl", - "longopts": "help,longa:", - "function": "getopt_long_only", - "mode": "gnu", - "want_args": "prgm -h --h --he -l -longa --longa a", - "want_optindex": 7, - "want_results": [ - { - "char": "h", - "name": "", - "opt_arg": "", - "err": "" - }, - { - "char": "", - "name": "help", - "opt_arg": "", - "err": "" - }, - { - "char": "", - "name": "help", - "opt_arg": "", - "err": "" - }, - { - "char": "l", - "name": "", - "opt_arg": "", - "err": "" - }, - { - "char": "", - "name": "longa", - "opt_arg": "--longa", - "err": "" - }, - { - "char": "", - "name": "", - "opt_arg": "", - "err": "-1" - } - ] - }, - { - "label": "kitchen_sink", - "args": "prgm -h --h --he -l -longa --longa a", - "opts": "hl", - "longopts": "help,longa:", - "function": "getopt_long_only", - "mode": "posix", - "want_args": "prgm -h --h --he -l -longa --longa a", - "want_optindex": 7, - "want_results": [ - { - "char": "h", - "name": "", - "opt_arg": "", - "err": "" - }, - { - "char": "", - "name": "help", - "opt_arg": "", - "err": "" - }, - { - "char": "", - "name": "help", - "opt_arg": "", - "err": "" - }, - { - "char": "l", - "name": "", - "opt_arg": "", - "err": "" - }, - { - "char": "", - "name": "longa", - "opt_arg": "--longa", - "err": "" - }, - { - "char": "", - "name": "", - "opt_arg": "", - "err": "-1" - } - ] - }, - { - "label": "kitchen_sink", - "args": "prgm -h --h --he -l -longa --longa a", - "opts": "hl", - "longopts": "help,longa:", - "function": "getopt_long_only", - "mode": "inorder", - "want_args": "prgm -h --h --he -l -longa --longa a", - "want_optindex": 8, - "want_results": [ - { - "char": "h", - "name": "", - "opt_arg": "", - "err": "" - }, - { - "char": "", - "name": "help", - "opt_arg": "", - "err": "" - }, - { - "char": "", - "name": "help", - "opt_arg": "", - "err": "" - }, - { - "char": "l", - "name": "", - "opt_arg": "", - "err": "" - }, - { - "char": "", - "name": "longa", - "opt_arg": "--longa", - "err": "" - }, - { - "char": "\u0001", - "name": "", - "opt_arg": "a", - "err": "" - }, - { - "char": "", - "name": "", - "opt_arg": "", - "err": "-1" - } - ] - }] \ No newline at end of file +[]