Skip to content

Commit

Permalink
add: basic tests
Browse files Browse the repository at this point in the history
  • Loading branch information
KarolosLykos committed Jan 18, 2022
1 parent 1d944d5 commit 2fec4af
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 23 deletions.
8 changes: 4 additions & 4 deletions cmd/date.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
26 changes: 26 additions & 0 deletions cmd/date_test.go
Original file line number Diff line number Diff line change
@@ -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())
}
23 changes: 4 additions & 19 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -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.`,
Expand All @@ -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()
}
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down Expand Up @@ -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=
Expand Down

0 comments on commit 2fec4af

Please sign in to comment.