github.com/trevoraustin/hub@v2.2.0-preview1.0.20141105230840-96d8bfc654cc+incompatible/commands/updater_test.go (about) 1 package commands 2 3 import ( 4 "fmt" 5 "io" 6 "io/ioutil" 7 "net/http" 8 "net/http/httptest" 9 "os" 10 "path/filepath" 11 "testing" 12 13 "github.com/bmizerany/assert" 14 "github.com/github/hub/fixtures" 15 ) 16 17 func TestUpdater_downloadFile(t *testing.T) { 18 mux := http.NewServeMux() 19 server := httptest.NewServer(mux) 20 defer server.Close() 21 22 mux.HandleFunc("/gh.zip", func(w http.ResponseWriter, r *http.Request) { 23 assert.Equal(t, "GET", r.Method) 24 fmt.Fprint(w, "1234") 25 }) 26 27 path, err := downloadFile(fmt.Sprintf("%s/gh.zip", server.URL)) 28 assert.Equal(t, nil, err) 29 30 content, err := ioutil.ReadFile(path) 31 assert.Equal(t, nil, err) 32 assert.Equal(t, "1234", string(content)) 33 assert.Equal(t, "gh.zip", filepath.Base(path)) 34 } 35 36 func TestUpdater_unzipExecutable(t *testing.T) { 37 target, _ := ioutil.TempFile("", "unzip-test") 38 defer target.Close() 39 40 source, _ := os.Open(filepath.Join("..", "fixtures", "gh.zip")) 41 defer source.Close() 42 43 _, err := io.Copy(target, source) 44 assert.Equal(t, nil, err) 45 46 exec, err := unzipExecutable(target.Name()) 47 assert.Equal(t, nil, err) 48 assert.Equal(t, "gh", filepath.Base(exec)) 49 } 50 51 func TestUpdater_timeToUpdate(t *testing.T) { 52 // file doesn't exist 53 timestampDir, _ := ioutil.TempDir("", "timestampDir-test") 54 timestampPath := filepath.Join(timestampDir, "gh-update") 55 updater := Updater{timestampPath: timestampPath} 56 57 assert.T(t, updater.timeToUpdate()) 58 timestamp, err := ioutil.ReadFile(timestampPath) 59 assert.Equal(t, nil, err) 60 assert.NotEqual(t, "", string(timestamp)) 61 62 // invalid timestamp format 63 timestampFile, _ := ioutil.TempFile("", "timestampFile-test") 64 updater = Updater{timestampPath: timestampFile.Name()} 65 assert.T(t, updater.timeToUpdate()) 66 timestamp, err = ioutil.ReadFile(timestampFile.Name()) 67 assert.Equal(t, nil, err) 68 assert.NotEqual(t, "", string(timestamp)) 69 70 // dev version 71 updater = Updater{CurrentVersion: "dev"} 72 assert.T(t, !updater.timeToUpdate()) 73 } 74 75 func TestSaveAlwaysAutoUpdateOption(t *testing.T) { 76 checkSavedAutoUpdateOption(t, true, "a", "always") 77 checkSavedAutoUpdateOption(t, true, "always", "always") 78 } 79 80 func TestSaveNeverAutoUpdateOption(t *testing.T) { 81 checkSavedAutoUpdateOption(t, false, "e", "never") 82 checkSavedAutoUpdateOption(t, false, "never", "never") 83 } 84 85 func TestDoesntSaveYesAutoUpdateOption(t *testing.T) { 86 checkSavedAutoUpdateOption(t, false, "y", "") 87 checkSavedAutoUpdateOption(t, false, "yes", "") 88 } 89 90 func TestDoesntSaveNoAutoUpdateOption(t *testing.T) { 91 checkSavedAutoUpdateOption(t, false, "n", "") 92 checkSavedAutoUpdateOption(t, false, "no", "") 93 } 94 95 func checkSavedAutoUpdateOption(t *testing.T, always bool, confirm, expected string) { 96 repo := fixtures.SetupTestRepo() 97 defer repo.TearDown() 98 99 saveAutoUpdateConfiguration(confirm, always) 100 assert.Equal(t, expected, autoUpdateConfig()) 101 }