github.com/pengwynn/gh@v1.0.1-0.20140118055701-14327ca3942e/commands/updater_test.go (about)

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