Compare commits
8 Commits
9f6e503379
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
b044825d1a
|
|||
|
d856a3bcd7
|
|||
|
69d2eb70a1
|
|||
|
7910cec1e0
|
|||
|
664f82d4cf
|
|||
|
96f507fbe3
|
|||
|
c9bad27e1d
|
|||
|
309aa09114
|
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
/out/
|
||||
11
Makefile
11
Makefile
@@ -1,16 +1,21 @@
|
||||
GO_MODULE = qc
|
||||
GO_MODULE_FULL = gitea.kevinnlsamuel.com/kevinnls/$(GO_MODULE)
|
||||
MOD_NAME = qc
|
||||
GO_MODULE = gitea.kevinnlsamuel.com/kevinnls/$(MOD_NAME)
|
||||
OUTDIR = out/
|
||||
|
||||
ifeq "$(shell command -v go)" ""
|
||||
prefix = podman run --interactive --tty --rm \
|
||||
--workdir /usr/local/go/src/$(GO_MODULE) \
|
||||
--volume $(PWD)/:/usr/local/go/src/$(GO_MODULE)/:Z \
|
||||
golang:alpine
|
||||
else
|
||||
prefix =
|
||||
endif
|
||||
|
||||
run:
|
||||
$(prefix) go run $(GO_MODULE) $(ARGS)
|
||||
test:
|
||||
$(prefix) go test ./... $(ARGS)
|
||||
build:
|
||||
$(prefix) go build -v $(GO_MODULE) $(ARGS)
|
||||
$(prefix) go build -o $(OUTDIR)/ -v $(GO_MODULE) $(ARGS)
|
||||
sh:
|
||||
$(prefix) sh
|
||||
|
||||
@@ -1,30 +1,38 @@
|
||||
package argparser
|
||||
|
||||
import "errors"
|
||||
import (
|
||||
"errors"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func Parse(args []string) (err error, commit_type string, commit_scope string, shouldCommit bool, shouldStage bool, commit_message []string) {
|
||||
argparseloop:
|
||||
for i := 0; i < len(args); i++ {
|
||||
switch args[i] {
|
||||
case "-c", "--commit", "-commit":
|
||||
shouldCommit = true
|
||||
case "-a", "--add", "-add":
|
||||
shouldStage = true
|
||||
case "--":
|
||||
commit_message = args[i+1:]
|
||||
break argparseloop
|
||||
default:
|
||||
if commit_type == "" {
|
||||
commit_type = args[i]
|
||||
continue argparseloop
|
||||
}
|
||||
if commit_scope == "" {
|
||||
commit_scope = args[i]
|
||||
continue argparseloop
|
||||
}
|
||||
err = errors.New("unknown argument")
|
||||
return
|
||||
if strings.HasPrefix(args[i],"-") {
|
||||
switch args[i] {
|
||||
case "-c", "--commit", "-commit":
|
||||
shouldCommit = true
|
||||
case "-a", "--add", "-add":
|
||||
shouldStage = true
|
||||
case "--":
|
||||
commit_message = args[i+1:]
|
||||
break argparseloop
|
||||
default:
|
||||
err = errors.New("unknown argument")
|
||||
return
|
||||
}
|
||||
}
|
||||
if commit_type == "" {
|
||||
commit_type = args[i]
|
||||
} else if commit_scope == "" {
|
||||
commit_scope = args[i]
|
||||
} else {
|
||||
err = errors.New("unknown argument")
|
||||
return
|
||||
}
|
||||
}
|
||||
if commit_type == "" {
|
||||
err = errors.New("need at least type")
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
@@ -8,16 +8,31 @@ import (
|
||||
func TestParse(t *testing.T) {
|
||||
t.Run("no args", func(t *testing.T) {
|
||||
error,ctype,cscope,shouldCommit,shouldStage,cmessage := Parse([]string{})
|
||||
assert.Equal(t, error, nil)
|
||||
assert.NotEqual(t, error, nil)
|
||||
assert.Equal(t, ctype, "")
|
||||
assert.Equal(t, cscope, "")
|
||||
wantedMessage := []string{}
|
||||
for i := 0; i < len(wantedMessage); i++ {
|
||||
if wantedMessage[i] != cmessage[i] {
|
||||
t.Errorf("messages do not match")
|
||||
}
|
||||
}
|
||||
var wantedMessage []string
|
||||
assert.DeepEqual(t, cmessage, wantedMessage)
|
||||
assert.Equal(t, shouldCommit, false)
|
||||
assert.Equal(t, shouldStage, false)
|
||||
})
|
||||
t.Run("only type", func(t *testing.T) {
|
||||
error,ctype,cscope,shouldCommit,shouldStage,cmessage := Parse([]string{"fix"})
|
||||
assert.Equal(t, error, nil)
|
||||
assert.Equal(t, ctype, "fix")
|
||||
assert.Equal(t, cscope, "")
|
||||
var wantedMessage []string
|
||||
assert.DeepEqual(t, cmessage, wantedMessage)
|
||||
assert.Equal(t, shouldCommit, false)
|
||||
assert.Equal(t, shouldStage, false)
|
||||
})
|
||||
t.Run("fake arg", func(t *testing.T) {
|
||||
error,_,_,_,_,_ := Parse([]string{"-f"})
|
||||
assert.NotEqual(t, error, nil)
|
||||
})
|
||||
t.Run("excess arg", func(t *testing.T) {
|
||||
error,_,_,_,_,_ := Parse([]string{"type","scope","fool me"})
|
||||
assert.NotEqual(t, error, nil)
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -1,15 +1,24 @@
|
||||
package assert
|
||||
|
||||
import "testing"
|
||||
import (
|
||||
"testing"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
func Equal[T comparable](t *testing.T, got, want T) {
|
||||
if (got != want) {
|
||||
t.Errorf("not equal %v and %v", got, want)
|
||||
t.Errorf("not equal: %v and %v", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func NotEqual[T comparable](t *testing.T, got, want T) {
|
||||
if (got == want) {
|
||||
t.Errorf("unexpected equal %v and %v", got, want)
|
||||
t.Errorf("unexpected equal: %v and %v", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func DeepEqual(t *testing.T, got, want interface{}){
|
||||
if !(reflect.DeepEqual(got,want)) {
|
||||
t.Errorf("unequal deep structure: wanted %v; but got %v", want, got)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user