func Test_OpenRemoteDownloadBuild(t *testing.T) { fakeData := []byte("aaaabbbb") server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Header().Set("content-length", fmt.Sprintf("%d", len(fakeData))) w.WriteHeader(200) w.Write(fakeData) })) defer server.CloseClientConnections() tfs := &testfs{server.URL} assert.NoError(t, RegisterHandler(tfs)) assert.Error(t, RegisterHandler(tfs)) defer DeregisterHandler(tfs) f, err := Open("nofs:///not-quite") assert.Error(t, err) f, err = Open("testfs:///now/we/are/talking") assert.NoError(t, err) stats, err := f.Stat() assert.NoError(t, err) assert.EqualValues(t, len(fakeData), stats.Size()) assert.NoError(t, f.Close()) }
func Test_NewHealer(t *testing.T) { _, err := NewHealer("", "/dev/null") assert.Error(t, err) _, err = NewHealer("nope,/dev/null", "invalid") assert.Error(t, err) healer, err := NewHealer("archive,/dev/null", "invalid") assert.NoError(t, err) _, ok := healer.(*ArchiveHealer) assert.True(t, ok) }
func TestToOneWithError(t *testing.T) { _, err := FromInts(1, 2).ToOneWithError() assert.Error(t, err) value, err := FromInts(3).ToOneWithError() assert.NoError(t, err) assert.Equal(t, 3, value) }
func TestTooManyArgsAfterCommand(t *testing.T) { a := newTestApp() a.Command("a", "") assert.NoError(t, a.init()) _, err := a.Parse([]string{"a", "b"}) assert.Error(t, err) }
func TestSubCommandRequired(t *testing.T) { app := newTestApp() c0 := app.Command("c0", "") c0.Command("c1", "") _, err := app.Parse([]string{"c0"}) assert.Error(t, err) }
func TestTopLevelArgCantBeUsedWithCommands(t *testing.T) { c := newTestApp() c.Arg("arg", "help").String() c.Command("cmd", "help") _, err := c.Parse([]string{}) assert.Error(t, err) }
func TestInvalidDefaultArgValueErrors(t *testing.T) { c := newTestApp() cmd := c.Command("cmd", "cmd") cmd.Arg("arg", "arg").Default("one").Int() _, err := c.Parse([]string{"cmd"}) assert.Error(t, err) }
func TestDuplicateLongFlag(t *testing.T) { app := newTestApp() app.Flag("a", "").String() app.Flag("a", "").String() _, err := app.Parse([]string{}) assert.Error(t, err) }
func TestDuplicateAlias(t *testing.T) { app := newTestApp() app.Command("one", "") app.Command("two", "").Alias("one") _, err := app.Parse([]string{"one"}) assert.Error(t, err) }
func TestMultipleDefaultCommands(t *testing.T) { app := newTestApp() app.Command("c0", "").Default() app.Command("c1", "").Default() _, err := app.Parse([]string{}) assert.Error(t, err) }
func TestNestedCommandWithDuplicateFlagErrors(t *testing.T) { app := New("app", "") app.Flag("test", "").Bool() app.Command("cmd0", "").Flag("test", "").Bool() err := app.init() assert.Error(t, err) }
func TestArgsRequiredAfterNonRequiredErrors(t *testing.T) { c := newTestApp() cmd := c.Command("cmd", "") cmd.Arg("a", "a").String() cmd.Arg("b", "b").Required().String() _, err := c.Parse([]string{"cmd"}) assert.Error(t, err) }
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) }
func TestRepeatableFlags(t *testing.T) { c := newTestApp() c.Flag("a", "a").String() c.Flag("b", "b").Strings() _, err := c.Parse([]string{"--a=foo", "--a=bar"}) assert.Error(t, err) _, err = c.Parse([]string{"--b=foo", "--b=bar"}) assert.NoError(t, err) }
func TestEnum(t *testing.T) { app := New("", "") a := app.Arg("a", "").Enum("one", "two", "three") _, err := app.Parse([]string{"moo"}) assert.Error(t, err) _, err = app.Parse([]string{"one"}) assert.NoError(t, err) assert.Equal(t, "one", *a) }
func TestNegateNonBool(t *testing.T) { fg := newFlagGroup() f := fg.Flag("b", "") f.Int() fg.init("") tokens := tokenize([]string{"--no-b"}, false) _, err := fg.parse(tokens) assert.Error(t, err) }
func TestRequiredFlags(t *testing.T) { c := newTestApp() c.Flag("a", "a").String() c.Flag("b", "b").Required().String() _, err := c.Parse([]string{"--a=foo"}) assert.Error(t, err) _, err = c.Parse([]string{"--b=foo"}) assert.NoError(t, err) }
func TestArgsMultipleRequiredThenNonRequired(t *testing.T) { c := newTestApp().Writer(ioutil.Discard) cmd := c.Command("cmd", "") cmd.Arg("a", "a").Required().String() cmd.Arg("b", "b").Required().String() cmd.Arg("c", "c").String() cmd.Arg("d", "d").String() _, err := c.Parse([]string{"cmd", "a", "b"}) assert.NoError(t, err) _, err = c.Parse([]string{}) assert.Error(t, err) }
func TestDefaultSubcommandWithFlags(t *testing.T) { app := newTestApp() c0 := app.Command("c0", "").Default() _ = c0.Flag("f0", "").Int() c0c1 := c0.Command("c1", "").Default() c0c1f1 := c0c1.Flag("f1", "").Int() selected, err := app.Parse([]string{"--f1=2"}) assert.NoError(t, err) assert.Equal(t, "c0 c1", selected) assert.Equal(t, 2, *c0c1f1) _, err = app.Parse([]string{"--f2"}) assert.Error(t, err) }
func TestRequiredFlag(t *testing.T) { app := newTestApp() app.Version("0.0.0").Writer(ioutil.Discard) exits := 0 app.Terminate(func(int) { exits++ }) app.Flag("a", "").Required().Bool() _, err := app.Parse([]string{"--a"}) assert.NoError(t, err) _, err = app.Parse([]string{}) assert.Error(t, err) _, err = app.Parse([]string{"--version"}) assert.Equal(t, 1, exits) }
func Test_OpenEmptyFile(t *testing.T) { f, err := Open("/dev/null") assert.NoError(t, err) s, err := f.Stat() assert.NoError(t, err) assert.EqualValues(t, 0, s.Size()) assert.False(t, s.IsDir()) assert.EqualValues(t, "/dev/null", s.Name()) assert.Nil(t, s.Sys()) assert.EqualValues(t, 0644, s.Mode()) assert.EqualValues(t, time.Time{}, s.ModTime()) buf := make([]byte, 1) _, err = f.Read(buf) assert.Error(t, err) _, err = f.ReadAt(buf, 0) assert.Error(t, err) assert.NoError(t, f.Close()) }
func TestParseExistingFile(t *testing.T) { f, err := ioutil.TempFile("", "") if err != nil { t.Fatal(err) } defer f.Close() defer os.Remove(f.Name()) p := parserMixin{} v := p.ExistingFile() err = p.value.Set(f.Name()) assert.NoError(t, err) assert.Equal(t, f.Name(), *v) err = p.value.Set("/etc/hostsDEFINITELYMISSING") assert.Error(t, err) }
func TestTimeout(t *testing.T) { wg := sync.WaitGroup{} start := time.Now() wg.Add(1) actual, err := CreateInt(func(observer IntObserver, subscription Subscription) { observer.Next(1) time.Sleep(time.Millisecond * 500) assert.True(t, subscription.Disposed()) wg.Done() }). Timeout(time.Millisecond * 250). ToArrayWithError() elapsed := time.Now().Sub(start) assert.Error(t, err) assert.Equal(t, ErrTimeout, err) assert.True(t, elapsed > time.Millisecond*250 && elapsed < time.Millisecond*500) assert.Equal(t, []int{1}, actual) wg.Wait() }
func TestInvalidFlagDefaultCanBeOverridden(t *testing.T) { app := newTestApp() app.Flag("a", "").Default("invalid").Bool() _, err := app.Parse([]string{}) assert.Error(t, err) }
func TestFlagMultipleValuesDefaultNonRepeatable(t *testing.T) { c := newTestApp() c.Flag("foo", "foo").Default("a", "b").String() _, err := c.Parse([]string{}) assert.Error(t, err) }
func TestRequiredWithEnvarMissingErrors(t *testing.T) { app := newTestApp() app.Flag("t", "").OverrideDefaultFromEnvar("TEST_ENVAR").Required().Int() _, err := app.Parse([]string{}) assert.Error(t, err) }
func TestEmptyShortFlagIsAnError(t *testing.T) { _, err := newTestApp().Parse([]string{"-"}) assert.Error(t, err) }
func TestTooManyArgs(t *testing.T) { a := newTestApp() a.Arg("a", "").String() _, err := a.Parse([]string{"a", "b"}) assert.Error(t, err) }
func TestArgsLooksLikeFlagsWithConsumeRemainder(t *testing.T) { a := newTestApp() a.Arg("opts", "").Required().Strings() _, err := a.Parse([]string{"hello", "-world"}) assert.Error(t, err) }
func TestInvalidDefaultFlagValueErrors(t *testing.T) { c := newTestApp() c.Flag("foo", "foo").Default("a").Int() _, err := c.Parse([]string{}) assert.Error(t, err) }