github.com/keybase/client/go@v0.0.0-20241007131713-f10651d043c8/updater/update_checker_test.go (about)

     1  // Copyright 2015 Keybase, Inc. All rights reserved. Use of
     2  // this source code is governed by the included BSD license.
     3  
     4  package updater
     5  
     6  import (
     7  	"fmt"
     8  	"testing"
     9  	"time"
    10  
    11  	"github.com/stretchr/testify/assert"
    12  	"github.com/stretchr/testify/require"
    13  )
    14  
    15  func TestUpdateCheckerStart(t *testing.T) {
    16  	testServer := testServerForUpdateFile(t, testZipPath)
    17  	defer func() {
    18  		// Give time for checker to stop before closing
    19  		time.Sleep(20 * time.Millisecond)
    20  		testServer.Close()
    21  	}()
    22  	updater, err := newTestUpdaterWithServer(t, testServer, testUpdate(testServer.URL), &testConfig{})
    23  	assert.NoError(t, err)
    24  
    25  	checker := NewUpdateChecker(updater, testUpdateCheckUI{}, 5*time.Millisecond, testLog)
    26  	defer checker.Stop()
    27  	started := checker.Start()
    28  	require.True(t, started)
    29  	started = checker.Start()
    30  	require.False(t, started)
    31  	// Wait for the count to increase (to prevent flakeyness on slow CIs)
    32  	for i := 0; checker.Count() == 0 && i < 10; i++ {
    33  		time.Sleep(5 * time.Millisecond)
    34  	}
    35  	assert.True(t, checker.Count() >= 1)
    36  
    37  	checker.Stop()
    38  }
    39  
    40  type testUpdateCheckUI struct {
    41  	verifyError error
    42  }
    43  
    44  func (u testUpdateCheckUI) BeforeUpdatePrompt(_ Update, _ UpdateOptions) error {
    45  	return nil
    46  }
    47  
    48  func (u testUpdateCheckUI) UpdatePrompt(_ Update, _ UpdateOptions, _ UpdatePromptOptions) (*UpdatePromptResponse, error) {
    49  	return &UpdatePromptResponse{Action: UpdateActionApply}, nil
    50  }
    51  
    52  func (u testUpdateCheckUI) BeforeApply(update Update) error {
    53  	return nil
    54  }
    55  
    56  func (u testUpdateCheckUI) Apply(update Update, options UpdateOptions, tmpDir string) error {
    57  	return nil
    58  }
    59  
    60  func (u testUpdateCheckUI) AfterApply(update Update) error {
    61  	return nil
    62  }
    63  
    64  func (u testUpdateCheckUI) GetUpdateUI() UpdateUI {
    65  	return u
    66  }
    67  
    68  func (u testUpdateCheckUI) Verify(update Update) error {
    69  	return u.verifyError
    70  }
    71  
    72  func (u testUpdateCheckUI) AfterUpdateCheck(update *Update) {}
    73  
    74  func (u testUpdateCheckUI) UpdateOptions() UpdateOptions {
    75  	return newDefaultTestUpdateOptions()
    76  }
    77  
    78  func (u testUpdateCheckUI) ReportAction(_ UpdatePromptResponse, _ *Update, _ UpdateOptions) {}
    79  
    80  func (u testUpdateCheckUI) ReportError(_ error, _ *Update, _ UpdateOptions) {}
    81  
    82  func (u testUpdateCheckUI) ReportSuccess(_ *Update, _ UpdateOptions) {}
    83  
    84  func (u testUpdateCheckUI) GetAppStatePath() string {
    85  	return ""
    86  }
    87  
    88  func (u testUpdateCheckUI) IsCheckCommand() bool {
    89  	return true
    90  }
    91  
    92  func (u testUpdateCheckUI) DeepClean() {}
    93  
    94  func TestUpdateCheckerError(t *testing.T) {
    95  	testServer := testServerForUpdateFile(t, testZipPath)
    96  	defer testServer.Close()
    97  	updater, err := newTestUpdaterWithServer(t, testServer, testUpdate(testServer.URL), &testConfig{})
    98  	assert.NoError(t, err)
    99  
   100  	checker := NewUpdateChecker(updater, testUpdateCheckUI{verifyError: fmt.Errorf("Test verify error")}, time.Minute, testLog)
   101  	err = checker.check()
   102  	require.Error(t, err)
   103  }