Ejemplo n.º 1
0
func TestCalInadequateInitialization(t *testing.T) {
	// mock cli.Ui
	ui := new(cli.MockUi)
	// yes to init prompt to create a new calendar, 3 times
	ui.InputReader = bytes.NewBufferString("y\ny\ny\n")

	// memory db
	db := mem.NewDB()

	// a new user, stored in db
	user := newTestUser(t, db)

	// note: this CalCommand is missing a cli.Ui
	missingUI := &CalCommand{
		UserID: user.ID().String(),
		DB:     db,
	}

	// note: this CalCommand has no UserID
	missingUserID := &CalCommand{
		UI: ui,
		DB: db,
	}

	// note: this CalCommand lacks a database (DB field)
	missingDB := &CalCommand{
		UI:     ui,
		UserID: user.ID().String(),
	}

	t.Log("Run command that doesn't have a UI")

	// expect missing a ui to fail
	if o := missingUI.Run([]string{"new"}); o != failure {
		t.Fatal("CalCommand without ui should fail on run")
	}

	t.Log("Completed")

	t.Log("Run command that doesn't have a user id")

	// expect missing a user id to fail
	if o := missingUserID.Run([]string{"new"}); o != failure {
		t.Fatal("CalCommand without user id should fail on run")
	}

	t.Log("Completed")

	t.Log("Run command that doesn't have a db")

	// expect missing a db to fail
	if o := missingDB.Run([]string{"new"}); o != failure {
		t.Fatal("CalCommand without db should fail on run")
	}

	t.Log("Completed")
}
Ejemplo n.º 2
0
func TestHostChange(t *testing.T) {
	f, err := ioutil.TempFile("", "configtest")
	if err != nil {
		t.Fatal(err)
	}
	defer f.Close()

	conf := &command.Config{
		Path: f.Name(),
	}

	// Remove the file to test the scenario in which it doesn't exist
	os.Remove(f.Name())

	ui := new(cli.MockUi)

	c := &command.ConfCommand{
		Ui:     ui,
		Config: conf,
	}

	newHost := "0.0.0.0:8000"

	ui.InputReader = bytes.NewBufferString(newHost + "\n")

	c.Run([]string{"host", "edit"})

	writtenConf, err := command.ParseConfigFile(conf.Path)
	if err != nil {
		t.Fatalf("ParseConfigFile: %s", err)
	}

	if writtenConf.Host != newHost {
		t.Fatalf("Host should be %s, not %s", newHost, writtenConf.Host)
	}

	os.Remove(writtenConf.Path)
}