Skip to content

Commit

Permalink
Add test for editor setup
Browse files Browse the repository at this point in the history
  • Loading branch information
briancain committed Nov 27, 2023
1 parent 4df13ff commit 7a68323
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 2 deletions.
8 changes: 8 additions & 0 deletions editor.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ type Editor struct {
}

func NewEditor(args []string) (*Editor, error) {
var err error
if len(args) == 0 {
args, err = setupDefaultEditorArgs()
if err != nil {
return nil, err
}
}

return &Editor{
Args: args,
}, nil
Expand Down
23 changes: 22 additions & 1 deletion editor_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,27 @@
package main

import "testing"
import (
"os"
"testing"

"github.com/stretchr/testify/require"
)

func TestEditor(t *testing.T) {
t.Run("creates a new editor with args", func(t *testing.T) {
r := require.New(t)
edit, err := NewEditor([]string{defaultEditor})
r.NoError(err)
r.Equal(defaultEditor, edit.Args[0])
})

t.Run("creates a new editor with default args", func(t *testing.T) {
testDefaultShell := os.Getenv("SHELL")
r := require.New(t)
edit, err := NewEditor([]string{})
r.NoError(err)
r.Equal(testDefaultShell, edit.Args[0])
r.Equal("-c", edit.Args[1])
r.Equal("EDITOR", edit.Args[2])
})
}
8 changes: 7 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,15 @@ module github.com/briancain/editor-cli

go 1.21

require github.com/spf13/cobra v1.8.0
require (
github.com/spf13/cobra v1.8.0
github.com/stretchr/testify v1.8.4
)

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/pflag v1.0.5 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
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.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
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=

0 comments on commit 7a68323

Please sign in to comment.