Skip to content

Commit

Permalink
test: cleanup ginkgo & gomega
Browse files Browse the repository at this point in the history
  • Loading branch information
JacksonTian authored and yndu13 committed Jan 4, 2024
1 parent 516e575 commit dd359ed
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 77 deletions.
9 changes: 4 additions & 5 deletions cli/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
Expand Down Expand Up @@ -56,10 +56,9 @@ type Command struct {
// auto compete
AutoComplete func(ctx *Context, args []string) []string

suggestDistance int
parent *Command
subCommands []*Command
flags *FlagSet
parent *Command
subCommands []*Command
flags *FlagSet
}

func (c *Command) AddSubCommand(cmd *Command) {
Expand Down
6 changes: 2 additions & 4 deletions cli/completion_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
Expand All @@ -17,7 +17,6 @@ import (
"bufio"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
)
Expand Down Expand Up @@ -90,13 +89,12 @@ func removeFromFile(name string, content string) error {
}

func removeContentToTempFile(name, content string) (string, error) {

rf, err := os.Open(name)
if err != nil {
return "", err
}
defer rf.Close()
wf, err := ioutil.TempFile("", "complete-")
wf, err := os.CreateTemp("", "complete-")
if err != nil {
return "", err
}
Expand Down
130 changes: 63 additions & 67 deletions cli/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@ import (
"testing"

"github.com/stretchr/testify/assert"

"github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)

type testContext struct {
Expand Down Expand Up @@ -60,70 +57,69 @@ func newTestParser(args ...string) (*Parser, *FlagSet) {
return parser, tc.fs
}

var _ = ginkgo.Describe("Parser", func() {
ginkgo.It("1. can parse command args", func() {
parser, _ := newTestParser()

flag, v, err := parser.parseCommandArg("--test")
Expect(err).NotTo(HaveOccurred())
Expect(flag.Name).Should(Equal("test"))
Expect(v).Should(Equal(""))

flag, v, err = parser.parseCommandArg("-t")
Expect(err).NotTo(HaveOccurred())
Expect(flag.Name).Should(Equal("test2"))
Expect(v).Should(Equal(""))

flag, v, err = parser.parseCommandArg("-t=ccc")
Expect(err).NotTo(HaveOccurred())
Expect(flag.Name).Should(Equal("test2"))
Expect(v).Should(Equal("ccc"))

flag, v, err = parser.parseCommandArg("-t:ccc")
Expect(err).NotTo(HaveOccurred())
Expect(flag.Name).Should(Equal("test2"))
Expect(v).Should(Equal("ccc"))

flag, v, err = parser.parseCommandArg("--test2:ccc")
Expect(err).NotTo(HaveOccurred())
Expect(flag.Name).Should(Equal("test2"))
Expect(v).Should(Equal("ccc"))

flag, v, err = parser.parseCommandArg("ccc")
Expect(err).NotTo(HaveOccurred())
Expect(flag).Should(BeNil())
Expect(v).Should(Equal("ccc"))

flag, v, err = parser.parseCommandArg("ccc=aaa")
Expect(err).NotTo(HaveOccurred())
Expect(flag).Should(BeNil())
Expect(v).Should(Equal("ccc=aaa"))
})

ginkgo.It("2. can parse args and flags", func() {
parser, _ := newTestParser("s1", "s2", "--test", "aaa", "--test2=bbb")

ginkgo.By("first arg")
s, _, err := parser.ReadNextArg()
Expect(err).NotTo(HaveOccurred())
Expect(s).Should(Equal("s1"))

ginkgo.By("remain args")
s2, err := parser.ReadAll()
Expect(err).NotTo(HaveOccurred())
Expect(len(s2)).Should(Equal(1))
Expect(s2[0]).Should(Equal("s2"))
})

ginkgo.It("3. can read next arg skip prev flag", func() {
parser, fs := newTestParser("--prev", "s1", "s2")
s, _, err := parser.ReadNextArg()

Expect(err).NotTo(HaveOccurred())
Expect(s).Should(Equal("s1"))
Expect(fs.Get("prev")).ShouldNot(Equal(nil))
})
})
// 1. can parse command args
func TestParser1(t *testing.T) {
parser, _ := newTestParser()

flag, v, err := parser.parseCommandArg("--test")
assert.Nil(t, err)
assert.Equal(t, "test", flag.Name)
assert.Equal(t, "", v)

flag, v, err = parser.parseCommandArg("-t")
assert.Nil(t, err)
assert.Equal(t, "test2", flag.Name)
assert.Equal(t, "", v)

flag, v, err = parser.parseCommandArg("-t=ccc")
assert.Nil(t, err)
assert.Equal(t, "test2", flag.Name)
assert.Equal(t, "ccc", v)

flag, v, err = parser.parseCommandArg("-t:ccc")
assert.Nil(t, err)
assert.Equal(t, "test2", flag.Name)
assert.Equal(t, "ccc", v)

flag, v, err = parser.parseCommandArg("--test2:ccc")
assert.Nil(t, err)
assert.Equal(t, "test2", flag.Name)
assert.Equal(t, "ccc", v)

flag, v, err = parser.parseCommandArg("ccc")
assert.Nil(t, err)
assert.Nil(t, flag)
assert.Equal(t, "ccc", v)

flag, v, err = parser.parseCommandArg("ccc=aaa")
assert.Nil(t, err)
assert.Nil(t, flag)
assert.Equal(t, "ccc=aaa", v)
}

// 2. can parse args and flags
func TestParser2(t *testing.T) {
parser, _ := newTestParser("s1", "s2", "--test", "aaa", "--test2=bbb")

s, _, err := parser.ReadNextArg()
assert.Nil(t, err)
assert.Equal(t, "s1", s)

s2, err := parser.ReadAll()
assert.Nil(t, err)
assert.Equal(t, 1, len(s2))
assert.Equal(t, "s2", s2[0])
}

// 3. can read next arg skip prev flag
func TestParser3(t *testing.T) {
parser, fs := newTestParser("--prev", "s1", "s2")
s, _, err := parser.ReadNextArg()

assert.Nil(t, err)
assert.Equal(t, "s1", s)
assert.NotNil(t, fs.Get("prev"))
}

func TestUnquoteString(t *testing.T) {
str := UnquoteString(`"nicai"`)
Expand Down
2 changes: 1 addition & 1 deletion config/legacy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func TestMigrateConfigure(t *testing.T) {

test, err := os.Create("testconf.ini")
assert.Nil(t, err)
_, err = test.WriteString(`
test.WriteString(`
[DEFAULT]
aliyun_access_key_id = DEFAULT_aliyun_access_key_id
aliyun_access_key_secret = DEFAULT_aliyun_access_key_secret
Expand Down

0 comments on commit dd359ed

Please sign in to comment.