From 2fec4afcff1c4eaf97fc01f9b53e19de82c96fab Mon Sep 17 00:00:00 2001 From: karolos lykos Date: Tue, 18 Jan 2022 03:48:10 +0200 Subject: [PATCH] add: basic tests --- cmd/date.go | 8 ++++---- cmd/date_test.go | 26 ++++++++++++++++++++++++++ cmd/root.go | 23 ++++------------------- go.mod | 4 ++++ go.sum | 3 +++ 5 files changed, 41 insertions(+), 23 deletions(-) create mode 100644 cmd/date_test.go diff --git a/cmd/date.go b/cmd/date.go index 1be2b1e..a474092 100644 --- a/cmd/date.go +++ b/cmd/date.go @@ -7,17 +7,17 @@ import ( "github.com/spf13/cobra" ) -var dateCmd = &cobra.Command{ +var DateCmd = &cobra.Command{ Use: "date", Short: "Print the current date.", Run: func(cmd *cobra.Command, args []string) { format, _ := cmd.Flags().GetString("format") - fmt.Println(time.Now().Format(format)) + fmt.Fprintf(cmd.OutOrStdout(), time.Now().Format(format)) }, } func init() { - rootCmd.AddCommand(dateCmd) + RootCmd.AddCommand(DateCmd) - dateCmd.Flags().StringP("format", "f", "02 Jan 06", "specify a custom date format") + DateCmd.Flags().StringP("format", "f", "02 Jan 06", "specify a custom date format") } diff --git a/cmd/date_test.go b/cmd/date_test.go new file mode 100644 index 0000000..2cac675 --- /dev/null +++ b/cmd/date_test.go @@ -0,0 +1,26 @@ +package cmd_test + +import ( + "bytes" + "testing" + "time" + + "github.com/stretchr/testify/assert" + + "github.com/KarolosLykos/cli-template/cmd" +) + +func TestDateCmd(t *testing.T) { + root := cmd.RootCmd + + buf := new(bytes.Buffer) + root.SetOut(buf) + root.SetErr(buf) + root.SetArgs([]string{"date"}) + + if err := root.Execute(); err != nil { + panic(err) + } + + assert.Equal(t, time.Now().Format("02 Jan 06"), buf.String()) +} diff --git a/cmd/root.go b/cmd/root.go index be5d02c..6b3b15f 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -1,15 +1,11 @@ package cmd import ( - "os" - "os/signal" - - "github.com/pterm/pterm" "github.com/spf13/cobra" ) -// dateCmd command to print the date. -var rootCmd = &cobra.Command{ +// RootCmd command to print the date. +var RootCmd = &cobra.Command{ Use: "cli-template", Short: "This cli template shows nothing", Long: `This is a template CLI application, which can be used as a boilerplate for awesome CLI tools written in Go.`, @@ -19,17 +15,6 @@ var rootCmd = &cobra.Command{ // Execute adds all child commands to the root command and sets flags appropriately. // This is called by main.main(). It only needs to happen once to the rootCmd. -func Execute() { - c := make(chan os.Signal, 1) - signal.Notify(c, os.Interrupt) - - go func() { - <-c - pterm.Print("exiting....") - os.Exit(0) - }() - - if err := rootCmd.Execute(); err != nil { - os.Exit(1) - } +func Execute() error { + return RootCmd.Execute() } diff --git a/go.mod b/go.mod index 04cf5c6..2d64162 100644 --- a/go.mod +++ b/go.mod @@ -5,16 +5,20 @@ go 1.17 require ( github.com/pterm/pterm v0.12.34 github.com/spf13/cobra v1.3.0 + github.com/stretchr/testify v1.7.0 ) require ( github.com/atomicgo/cursor v0.0.1 // indirect + github.com/davecgh/go-spew v1.1.1 // indirect github.com/gookit/color v1.5.0 // indirect github.com/inconshreveable/mousetrap v1.0.0 // indirect github.com/mattn/go-runewidth v0.0.13 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect github.com/rivo/uniseg v0.2.0 // indirect github.com/spf13/pflag v1.0.5 // indirect github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778 // indirect golang.org/x/sys v0.0.0-20220114195835-da31bd327af9 // indirect golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 // indirect + gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect ) diff --git a/go.sum b/go.sum index 5604f4e..21ef195 100644 --- a/go.sum +++ b/go.sum @@ -251,8 +251,10 @@ github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxv github.com/kr/fs v0.1.0/go.mod h1:FFnZGqtBN9Gxj7eW1uZ42v5BccTP0vu6NEaFoC2HwRg= github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= +github.com/kr/pretty v0.2.0 h1:s5hAObm+yFO5uHYt5dYjxi2rXrsnmRpJx4OYvIWUaQs= github.com/kr/pretty v0.2.0/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= 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/lyft/protoc-gen-star v0.5.3/go.mod h1:V0xaHgaf5oCCqmcxYcWiDfTiKsZsRc87/1qhoTACD8w= github.com/magiconair/properties v1.8.5/go.mod h1:y3VJvCyxH9uVvJTWEGAELF3aiYNyPKd5NZ3oSwXrF60= @@ -770,6 +772,7 @@ google.golang.org/protobuf v1.27.1/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQ gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo= gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI= gopkg.in/ini.v1 v1.66.2/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=