Beispiel #1
0
func TestCombinedShortFlags(t *testing.T) {
	app := New("test", "")
	a := app.Flag("short0", "").Short('0').Bool()
	b := app.Flag("short1", "").Short('1').Bool()
	c := app.Flag("short2", "").Short('2').Bool()
	_, err := app.Parse([]string{"-01"})
	assert.NoError(t, err)
	assert.True(t, *a)
	assert.True(t, *b)
	assert.False(t, *c)
}
Beispiel #2
0
func TestBool(t *testing.T) {
	app := New("test", "")
	b := app.Flag("b", "").Bool()
	_, err := app.Parse([]string{"--b"})
	assert.NoError(t, err)
	assert.True(t, *b)
}
Beispiel #3
0
func TestShortFlag(t *testing.T) {
	app := New("test", "")
	f := app.Flag("long", "").Short('s').Bool()
	_, err := app.Parse([]string{"-s"})
	assert.NoError(t, err)
	assert.True(t, *f)
}
Beispiel #4
0
func TestNestedCommandWithMergedFlags(t *testing.T) {
	app := New("app", "")
	cmd0 := app.Command("a", "")
	cmd0f0 := cmd0.Flag("aflag", "").Bool()
	// cmd1 := app.Command("b", "")
	// cmd1f0 := cmd0.Flag("bflag", "").Bool()
	cmd00 := cmd0.Command("aa", "")
	cmd00f0 := cmd00.Flag("aaflag", "").Bool()
	err := app.init()
	assert.NoError(t, err)
	context := tokenize(strings.Split("a aa --aflag --aaflag", " "))
	selected, err := parseAndExecute(app, context)
	assert.NoError(t, err)
	assert.True(t, *cmd0f0)
	assert.True(t, *cmd00f0)
	assert.Equal(t, "a aa", selected)
}
Beispiel #5
0
func TestDispatchCallbackIsCalled(t *testing.T) {
	dispatched := false
	c := New("test", "")
	c.Command("cmd", "").Action(func(*ParseContext) error {
		dispatched = true
		return nil
	})

	_, err := c.Parse([]string{"cmd"})
	assert.NoError(t, err)
	assert.True(t, dispatched)
}
Beispiel #6
0
func TestNestedCommandsWithArgs(t *testing.T) {
	app := New("app", "")
	cmd := app.Command("a", "").Command("b", "")
	a := cmd.Arg("a", "").String()
	b := cmd.Arg("b", "").String()
	context := tokenize([]string{"a", "b", "c", "d"})
	selected, err := parseAndExecute(app, context)
	assert.NoError(t, err)
	assert.True(t, context.EOL())
	assert.Equal(t, "a b", selected)
	assert.Equal(t, "c", *a)
	assert.Equal(t, "d", *b)
}
Beispiel #7
0
func TestNestedCommandsWithFlags(t *testing.T) {
	app := New("app", "")
	cmd := app.Command("a", "").Command("b", "")
	a := cmd.Flag("aaa", "").Short('a').String()
	b := cmd.Flag("bbb", "").Short('b').String()
	err := app.init()
	assert.NoError(t, err)
	context := tokenize(strings.Split("a b --aaa x -b x", " "))
	selected, err := parseAndExecute(app, context)
	assert.NoError(t, err)
	assert.True(t, context.EOL())
	assert.Equal(t, "a b", selected)
	assert.Equal(t, "x", *a)
	assert.Equal(t, "x", *b)
}
Beispiel #8
0
func TestArgMultipleRequired(t *testing.T) {
	terminated := false
	app := New("test", "")
	app.Version("0.0.0").Writer(ioutil.Discard)
	app.Arg("a", "").Required().String()
	app.Arg("b", "").Required().String()
	app.Terminate(func(int) { terminated = true })

	_, err := app.Parse([]string{})
	assert.Error(t, err)
	_, err = app.Parse([]string{"A"})
	assert.Error(t, err)
	_, err = app.Parse([]string{"A", "B"})
	assert.NoError(t, err)
	_, err = app.Parse([]string{"--version"})
	assert.True(t, terminated)
}
Beispiel #9
0
func TestNestedCommands(t *testing.T) {
	app := New("app", "")
	sub1 := app.Command("sub1", "")
	sub1.Flag("sub1", "")
	subsub1 := sub1.Command("sub1sub1", "")
	subsub1.Command("sub1sub1end", "")

	sub2 := app.Command("sub2", "")
	sub2.Flag("sub2", "")
	sub2.Command("sub2sub1", "")

	context := tokenize([]string{"sub1", "sub1sub1", "sub1sub1end"})
	selected, err := parseAndExecute(app, context)
	assert.NoError(t, err)
	assert.True(t, context.EOL())
	assert.Equal(t, "sub1 sub1sub1 sub1sub1end", selected)
}
Beispiel #10
0
func TestCache(t *testing.T) {
	td, err := ioutil.TempDir("", "gotest")
	if err != nil {
		t.Fatal(err)
	}
	defer os.RemoveAll(td)
	c, err := NewCache(filepath.Join(td, "cache"))
	if err != nil {
		t.Fatal(err)
	}
	assert.EqualValues(t, 0, c.Info().Filled)
	c.WalkItems(func(i ItemInfo) {})
	_, err = c.OpenFile("/", os.O_CREATE)
	assert.NotNil(t, err)
	_, err = c.OpenFile("", os.O_CREATE)
	assert.NotNil(t, err)
	c.WalkItems(func(i ItemInfo) {})
	require.Equal(t, 0, c.Info().NumItems)
	_, err = c.OpenFile("notexist", 0)
	assert.True(t, os.IsNotExist(err), err)
	_, err = c.OpenFile("/notexist", 0)
	assert.True(t, os.IsNotExist(err), err)
	_, err = c.OpenFile("/dir/notexist", 0)
	assert.True(t, os.IsNotExist(err), err)
	f, err := c.OpenFile("dir/blah", os.O_CREATE)
	require.NoError(t, err)
	defer f.Close()
	c.WalkItems(func(i ItemInfo) {})
	assert.True(t, missinggo.FilePathExists(filepath.Join(td, filepath.FromSlash("cache/dir/blah"))))
	assert.True(t, missinggo.FilePathExists(filepath.Join(td, filepath.FromSlash("cache/dir/"))))
	assert.Equal(t, 1, c.Info().NumItems)
	f.Remove()
	assert.False(t, missinggo.FilePathExists(filepath.Join(td, filepath.FromSlash("dir/blah"))))
	assert.False(t, missinggo.FilePathExists(filepath.Join(td, filepath.FromSlash("dir/"))))
	_, err = f.Read(nil)
	assert.NotEqual(t, io.EOF, err)
	a, err := c.OpenFile("/a", os.O_CREATE|os.O_WRONLY)
	defer a.Close()
	require.Nil(t, err)
	b, err := c.OpenFile("b", os.O_CREATE|os.O_WRONLY)
	defer b.Close()
	require.Nil(t, err)
	c.mu.Lock()
	assert.True(t, c.pathInfo("a").Accessed.Before(c.pathInfo("b").Accessed))
	c.mu.Unlock()
	n, err := a.Write([]byte("hello"))
	assert.Nil(t, err)
	assert.EqualValues(t, 5, n)
	assert.EqualValues(t, 5, c.Info().Filled)
	assert.True(t, c.pathInfo("b").Accessed.Before(c.pathInfo("a").Accessed))
	c.SetCapacity(5)
	n, err = a.Write([]byte(" world"))
	assert.NotNil(t, err)
	_, err = b.Write([]byte("boom!"))
	// "a" and "b" have been evicted.
	assert.NotNil(t, err)
	assert.EqualValues(t, 0, c.Info().Filled)
	assert.EqualValues(t, 0, c.Info().NumItems)
	_, err = a.Seek(0, os.SEEK_SET)
	assert.NotNil(t, err)
}