From 2c0d17ad3a6b91a6608e2bbe360f42d42dcd1af5 Mon Sep 17 00:00:00 2001 From: Marcusk19 Date: Tue, 5 Mar 2024 19:45:03 -0500 Subject: [PATCH] add unit test --- cmd/pretty.go | 7 +++++-- cmd/root.go | 8 ++++---- go.mod | 4 ++++ go.sum | 7 +++++++ test/fixtures/test_pretty.txt | 1 + test/pretty_test.go | 22 ++++++++++++++++++++++ 6 files changed, 43 insertions(+), 6 deletions(-) create mode 100644 test/fixtures/test_pretty.txt create mode 100644 test/pretty_test.go diff --git a/cmd/pretty.go b/cmd/pretty.go index d94c9a1..e46d279 100644 --- a/cmd/pretty.go +++ b/cmd/pretty.go @@ -11,12 +11,15 @@ import ( ) func init() { - rootCmd.AddCommand(prettyCmd) + RootCmd.AddCommand(prettyCmd) } var prettyCmd = &cobra.Command { Use: "pretty", Run: func(cmd *cobra.Command, args []string) { + if (len(args) <= 0) { + log.Fatal("no arguments provided") + } var filename = args[0] f, err := os.Open(filename) if err != nil { @@ -31,7 +34,7 @@ var prettyCmd = &cobra.Command { line := scanner.Text() formattedLine := strings.Replace(line, "\\n", "\n", -1) formattedLine = strings.Replace(formattedLine, "\\t", "\t", -1) - fmt.Printf(formattedLine) + fmt.Fprintf(cmd.OutOrStdout(), formattedLine) } if err := scanner.Err(); err != nil { diff --git a/cmd/root.go b/cmd/root.go index 4f2a005..1ea8830 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -12,8 +12,8 @@ import ( -// rootCmd represents the base command when called without any subcommands -var rootCmd = &cobra.Command{ +// RootCmd represents the base command when called without any subcommands +var RootCmd = &cobra.Command{ Use: "bender", Short: "Change literals to whitespace", Long: `A longer description that spans multiple lines and likely contains @@ -30,7 +30,7 @@ to quickly create a Cobra application.`, // 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() { - err := rootCmd.Execute() + err := RootCmd.Execute() if err != nil { os.Exit(1) } @@ -43,7 +43,7 @@ func init() { // Cobra also supports local flags, which will only run // when this action is called directly. - rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") + RootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") } diff --git a/go.mod b/go.mod index f25108a..eb2105c 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,11 @@ module github.com/Marcusk19/bender go 1.22.0 require ( + github.com/davecgh/go-spew v1.1.1 // indirect github.com/inconshreveable/mousetrap v1.1.0 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect github.com/spf13/cobra v1.8.0 // indirect github.com/spf13/pflag v1.0.5 // indirect + github.com/stretchr/testify v1.9.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index d0e8c2c..ecd1abe 100644 --- a/go.sum +++ b/go.sum @@ -1,10 +1,17 @@ github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8= github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/spf13/cobra v1.8.0 h1:7aJaZx1B85qltLMc546zn58BxxfZdR/W22ej9CFoEf0= github.com/spf13/cobra v1.8.0/go.mod h1:WXLWApfZ71AjXPya3WOlMsY9yMs7YeiHhFVlvLyhcho= 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/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= +github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/test/fixtures/test_pretty.txt b/test/fixtures/test_pretty.txt new file mode 100644 index 0000000..0d0b3e4 --- /dev/null +++ b/test/fixtures/test_pretty.txt @@ -0,0 +1 @@ +The end of this sentence should start a newline. \nThe next sentence should be indented below this one.\n\tHello this is the end of the text diff --git a/test/pretty_test.go b/test/pretty_test.go new file mode 100644 index 0000000..99604e2 --- /dev/null +++ b/test/pretty_test.go @@ -0,0 +1,22 @@ +package test + +import ( + "bytes" + "testing" + + "github.com/Marcusk19/bender/cmd" + "github.com/stretchr/testify/assert" +) + +func TestPrettyCommand(t *testing.T) { + bender := cmd.RootCmd + actual := new(bytes.Buffer) + bender.SetOut(actual) + bender.SetErr(actual) + bender.SetArgs([]string{"pretty", "fixtures/test_pretty.txt"}) + bender.Execute() + + expected := "The end of this sentence should start a newline. \nThe next sentence should be indented below this one.\n\tHello this is the end of the text" + assert.Equal(t, expected, actual.String(), "actual value differs from expected") +} +