Skip to content
This repository has been archived by the owner on Jan 8, 2024. It is now read-only.

Commit

Permalink
Prompt test fixes (timeout)
Browse files Browse the repository at this point in the history
  • Loading branch information
gabriel committed May 6, 2016
1 parent 95437b7 commit cef8139
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 31 deletions.
4 changes: 2 additions & 2 deletions keybase/platform_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func (c context) UpdatePrompt(update updater.Update, options updater.UpdateOptio
if err != nil {
return nil, err
}
return c.updatePrompt(promptPath, update, options, promptOptions)
return c.updatePrompt(promptPath, update, options, promptOptions, time.Hour)
}

// PausedPrompt is called when the we can't update cause the app is in use.
Expand All @@ -89,7 +89,7 @@ func (c context) PausedPrompt() bool {
c.log.Warningf("Error trying to get prompt path: %s", err)
return false
}
cancelUpdate, err := c.pausedPrompt(promptPath)
cancelUpdate, err := c.pausedPrompt(promptPath, 5*time.Minute)
if err != nil {
c.log.Warningf("Error in paused prompt: %s", err)
return false
Expand Down
11 changes: 4 additions & 7 deletions keybase/prompt.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,7 @@ type updaterPromptInputResult struct {
AutoUpdate bool `json:"autoUpdate"`
}

// promptTimeout is a long timeout here cause it might show the prompt while the user is not present
var promptTimeout = time.Hour

func (c context) updatePrompt(promptCommand string, update updater.Update, options updater.UpdateOptions, promptOptions updater.UpdatePromptOptions) (*updater.UpdatePromptResponse, error) {
func (c context) updatePrompt(promptCommand string, update updater.Update, options updater.UpdateOptions, promptOptions updater.UpdatePromptOptions, timeout time.Duration) (*updater.UpdatePromptResponse, error) {
description := update.Description
if description == "" {
description = "Please visit https://keybase.io for more information."
Expand All @@ -44,7 +41,7 @@ func (c context) updatePrompt(promptCommand string, update updater.Update, optio
}

var result updaterPromptInputResult
if err := command.ExecForJSON(promptCommand, []string{string(promptJSONInput)}, &result, promptTimeout, c.log); err != nil {
if err := command.ExecForJSON(promptCommand, []string{string(promptJSONInput)}, &result, timeout, c.log); err != nil {
return nil, fmt.Errorf("Error running command: %s", err)
}

Expand Down Expand Up @@ -81,7 +78,7 @@ type promptInputResult struct {
// pausedPrompt returns whether to cancel update and/or error.
// If the user explicit wants to cancel the update, this may be different from
// an error occurring, in which case
func (c context) pausedPrompt(promptCommand string) (bool, error) {
func (c context) pausedPrompt(promptCommand string, timeout time.Duration) (bool, error) {
const btnForce = "Force update"
const btnCancel = "Try again later"
promptJSONInput, err := json.Marshal(promptInput{
Expand All @@ -95,7 +92,7 @@ func (c context) pausedPrompt(promptCommand string) (bool, error) {
}

var result promptInputResult
if err := command.ExecForJSON(promptCommand, []string{string(promptJSONInput)}, &result, 5*time.Minute, c.log); err != nil {
if err := command.ExecForJSON(promptCommand, []string{string(promptJSONInput)}, &result, timeout, c.log); err != nil {
return false, fmt.Errorf("Error running command: %s", err)
}

Expand Down
30 changes: 8 additions & 22 deletions keybase/prompt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,7 @@ func testPromptWithCommand(t *testing.T, promptCommand string, timeout time.Dura
updaterOptions := cfg.updaterOptions()

promptOptions := updater.UpdatePromptOptions{AutoUpdate: false}

if timeout > 0 {
defaultPromptTimeout := promptTimeout
promptTimeout = timeout
defer func() { promptTimeout = defaultPromptTimeout }()
}

return ctx.updatePrompt(promptCommand, update, updaterOptions, promptOptions)
return ctx.updatePrompt(promptCommand, update, updaterOptions, promptOptions, timeout)
}

func TestPromptTimeout(t *testing.T) {
Expand All @@ -46,14 +39,14 @@ func TestPromptTimeout(t *testing.T) {

func TestPromptInvalidResponse(t *testing.T) {
promptCommand := filepath.Join(os.Getenv("GOPATH"), "src/github.com/keybase/go-updater/test/prompt-invalid.sh")
resp, err := testPromptWithCommand(t, promptCommand, 10*time.Millisecond)
resp, err := testPromptWithCommand(t, promptCommand, time.Second)
assert.Error(t, err)
assert.Nil(t, resp)
}

func TestPromptApply(t *testing.T) {
promptCommand := filepath.Join(os.Getenv("GOPATH"), "src/github.com/keybase/go-updater/test/prompt-apply.sh")
resp, err := testPromptWithCommand(t, promptCommand, 0)
resp, err := testPromptWithCommand(t, promptCommand, time.Second)
assert.NoError(t, err)
if assert.NotNil(t, resp) {
assert.True(t, resp.AutoUpdate)
Expand All @@ -63,7 +56,7 @@ func TestPromptApply(t *testing.T) {

func TestPromptSnooze(t *testing.T) {
promptCommand := filepath.Join(os.Getenv("GOPATH"), "src/github.com/keybase/go-updater/test/prompt-snooze.sh")
resp, err := testPromptWithCommand(t, promptCommand, 0)
resp, err := testPromptWithCommand(t, promptCommand, time.Second)
assert.NoError(t, err)
if assert.NotNil(t, resp) {
assert.False(t, resp.AutoUpdate)
Expand All @@ -73,7 +66,7 @@ func TestPromptSnooze(t *testing.T) {

func TestPromptCancel(t *testing.T) {
promptCommand := filepath.Join(os.Getenv("GOPATH"), "src/github.com/keybase/go-updater/test/prompt-cancel.sh")
resp, err := testPromptWithCommand(t, promptCommand, 0)
resp, err := testPromptWithCommand(t, promptCommand, time.Second)
assert.NoError(t, err)
if assert.NotNil(t, resp) {
assert.False(t, resp.AutoUpdate)
Expand All @@ -82,7 +75,7 @@ func TestPromptCancel(t *testing.T) {
}

func TestPromptNoOutput(t *testing.T) {
resp, err := testPromptWithCommand(t, "echo", 0)
resp, err := testPromptWithCommand(t, "echo", time.Second)
assert.NoError(t, err)
if assert.NotNil(t, resp) {
assert.False(t, resp.AutoUpdate)
Expand All @@ -92,7 +85,7 @@ func TestPromptNoOutput(t *testing.T) {

func TestPromptError(t *testing.T) {
promptCommand := filepath.Join(os.Getenv("GOPATH"), "src/github.com/keybase/go-updater/test/prompt-error.sh")
resp, err := testPromptWithCommand(t, promptCommand, 0)
resp, err := testPromptWithCommand(t, promptCommand, time.Second)
assert.Error(t, err)
assert.Nil(t, resp)
}
Expand All @@ -101,14 +94,7 @@ func testPausedPromptWithCommand(t *testing.T, promptCommand string, timeout tim
cfg, _ := testConfig(t)
ctx := newContext(&cfg, log)
assert.NotNil(t, ctx)

if timeout > 0 {
defaultPromptTimeout := promptTimeout
promptTimeout = timeout
defer func() { promptTimeout = defaultPromptTimeout }()
}

return ctx.pausedPrompt(promptCommand)
return ctx.pausedPrompt(promptCommand, timeout)
}

func TestPausedPromptForce(t *testing.T) {
Expand Down

0 comments on commit cef8139

Please sign in to comment.