From 4d454b6d5479bbc67b5bb97ffcfe3f64f9b1d03e Mon Sep 17 00:00:00 2001 From: Tuan Tran Date: Tue, 16 Aug 2022 12:28:20 +0700 Subject: [PATCH] fix: issue of cmd installation --- benchmarks/go.mod | 4 +--- benchmarks/go.sum | 2 ++ cmd/Makefile | 2 +- cmd/go.mod | 4 +--- cmd/go.sum | 2 ++ cmd/jsonconv/main.go | 5 +++-- cmd/jsonconv/main_test.go | 21 +++++++++++++++++++++ 7 files changed, 31 insertions(+), 9 deletions(-) create mode 100644 benchmarks/go.sum create mode 100644 cmd/jsonconv/main_test.go diff --git a/benchmarks/go.mod b/benchmarks/go.mod index 30b9c6f..f49081c 100644 --- a/benchmarks/go.mod +++ b/benchmarks/go.mod @@ -2,6 +2,4 @@ module github.com/tuan78/jsonconv/benchmarks go 1.18 -replace github.com/tuan78/jsonconv => ../ - -require github.com/tuan78/jsonconv v1.0.1 +require github.com/tuan78/jsonconv v1.0.2 diff --git a/benchmarks/go.sum b/benchmarks/go.sum new file mode 100644 index 0000000..d43c93f --- /dev/null +++ b/benchmarks/go.sum @@ -0,0 +1,2 @@ +github.com/tuan78/jsonconv v1.0.2 h1:tdmVOQDrVrZZ+ZivgNuW8mYr1f7lsOajd+1BVm4QHVc= +github.com/tuan78/jsonconv v1.0.2/go.mod h1:FFDL8xmSERH5fFmySGaR4ku/2RY0inkMZmhpBo8r9zc= diff --git a/cmd/Makefile b/cmd/Makefile index ef71822..e8efc79 100644 --- a/cmd/Makefile +++ b/cmd/Makefile @@ -1,5 +1,5 @@ .PHONY: all -all: build +all: test .PHONY: build build: diff --git a/cmd/go.mod b/cmd/go.mod index 961bdf9..ab3af55 100644 --- a/cmd/go.mod +++ b/cmd/go.mod @@ -2,12 +2,10 @@ module github.com/tuan78/jsonconv/cmd go 1.18 -replace github.com/tuan78/jsonconv => ../ - require ( github.com/spf13/cobra v1.5.0 github.com/spf13/pflag v1.0.5 - github.com/tuan78/jsonconv v1.0.1 + github.com/tuan78/jsonconv v1.0.2 ) require github.com/inconshreveable/mousetrap v1.0.0 // indirect diff --git a/cmd/go.sum b/cmd/go.sum index 0d85248..c414456 100644 --- a/cmd/go.sum +++ b/cmd/go.sum @@ -6,5 +6,7 @@ github.com/spf13/cobra v1.5.0 h1:X+jTBEBqF0bHN+9cSMgmfuvv2VHJ9ezmFNf9Y/XstYU= github.com/spf13/cobra v1.5.0/go.mod h1:dWXEIy2H428czQCjInthrTRUg7yKbok+2Qi/yBIJoUM= github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= +github.com/tuan78/jsonconv v1.0.2 h1:tdmVOQDrVrZZ+ZivgNuW8mYr1f7lsOajd+1BVm4QHVc= +github.com/tuan78/jsonconv v1.0.2/go.mod h1:FFDL8xmSERH5fFmySGaR4ku/2RY0inkMZmhpBo8r9zc= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= diff --git a/cmd/jsonconv/main.go b/cmd/jsonconv/main.go index 3b2c9fd..d7e3aaf 100644 --- a/cmd/jsonconv/main.go +++ b/cmd/jsonconv/main.go @@ -7,10 +7,11 @@ import ( "github.com/tuan78/jsonconv/cmd" ) +var exitFn = os.Exit + func main() { - // Execute command. if err := cmd.NewRootCmd().Execute(); err != nil { fmt.Fprintf(os.Stderr, "Command execution failed, err: %v\n", err) - os.Exit(1) + exitFn(1) } } diff --git a/cmd/jsonconv/main_test.go b/cmd/jsonconv/main_test.go new file mode 100644 index 0000000..28f5e3a --- /dev/null +++ b/cmd/jsonconv/main_test.go @@ -0,0 +1,21 @@ +package main + +import ( + "os" + "testing" +) + +func TestMainFn_GoodArgs(t *testing.T) { + os.Args = []string{"jsonconv", "--help"} + main() +} + +func TestMainFn_BadArgs(t *testing.T) { + exitCode := 0 + exitFn = func(code int) { exitCode = 1 } + os.Args = []string{"jsonconv", "bad-args"} + main() + if exitCode == 0 { + t.Errorf("it should exit with code 1 for bad args") + } +}