github.com/tiagovtristao/plz@v13.4.0+incompatible/src/update/update_test.go (about)

     1  package update
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"net/http"
     7  	"net/http/httptest"
     8  	"os"
     9  	"path"
    10  	"runtime"
    11  	"testing"
    12  
    13  	"github.com/stretchr/testify/assert"
    14  	"gopkg.in/op/go-logging.v1"
    15  
    16  	"github.com/thought-machine/please/src/cli"
    17  	"github.com/thought-machine/please/src/core"
    18  )
    19  
    20  var server *httptest.Server
    21  
    22  type fakeLogBackend struct{}
    23  
    24  func (*fakeLogBackend) Log(level logging.Level, calldepth int, rec *logging.Record) error {
    25  	if level == logging.CRITICAL {
    26  		panic(rec.Message())
    27  	}
    28  	fmt.Printf("%s\n", rec.Message())
    29  	return nil
    30  }
    31  
    32  func TestVerifyNewPlease(t *testing.T) {
    33  	assert.True(t, verifyNewPlease("src/please", core.PleaseVersion.String()))
    34  	assert.False(t, verifyNewPlease("src/please", "wibble"))
    35  	assert.False(t, verifyNewPlease("wibble", core.PleaseVersion.String()))
    36  }
    37  
    38  func TestFindLatestVersion(t *testing.T) {
    39  	assert.Equal(t, "42.0.0", findLatestVersion(server.URL).String())
    40  	assert.Panics(t, func() { findLatestVersion(server.URL + "/blah") })
    41  	assert.Panics(t, func() { findLatestVersion("notaurl") })
    42  }
    43  
    44  func TestFileMode(t *testing.T) {
    45  	assert.Equal(t, os.FileMode(0775), fileMode("please"))
    46  	assert.Equal(t, os.FileMode(0664), fileMode("junit_runner.jar"))
    47  	assert.Equal(t, os.FileMode(0664), fileMode("libplease_parser_pypy.so"))
    48  }
    49  
    50  func TestLinkNewFile(t *testing.T) {
    51  	c := makeConfig("linknewfile")
    52  	dir := path.Join(c.Please.Location, c.Please.Version.String())
    53  	assert.NoError(t, os.MkdirAll(dir, core.DirPermissions))
    54  	assert.NoError(t, ioutil.WriteFile(path.Join(dir, "please"), []byte("test"), 0775))
    55  	linkNewFile(c, "please")
    56  	assert.True(t, core.PathExists(path.Join(c.Please.Location, "please")))
    57  	assert.NoError(t, ioutil.WriteFile(path.Join(c.Please.Location, "exists"), []byte("test"), 0775))
    58  }
    59  
    60  func TestDownloadNewPlease(t *testing.T) {
    61  	c := makeConfig("downloadnewplease")
    62  	downloadPlease(c, false)
    63  	// Should have written new file
    64  	assert.True(t, core.PathExists(path.Join(c.Please.Location, c.Please.Version.String(), "please")))
    65  	// Should not have written this yet though
    66  	assert.False(t, core.PathExists(path.Join(c.Please.Location, "please")))
    67  	// Panics because it's not a valid .tar.gz
    68  	c.Please.Version.UnmarshalFlag("1.0.0")
    69  	assert.Panics(t, func() { downloadPlease(c, false) })
    70  	// Panics because it doesn't exist
    71  	c.Please.Version.UnmarshalFlag("2.0.0")
    72  	assert.Panics(t, func() { downloadPlease(c, false) })
    73  	// Panics because invalid URL
    74  	c.Please.DownloadLocation = "notaurl"
    75  	assert.Panics(t, func() { downloadPlease(c, false) })
    76  }
    77  
    78  func TestShouldUpdateVersionsMatch(t *testing.T) {
    79  	c := makeConfig("shouldupdate")
    80  	c.Please.Version.Set(core.PleaseVersion.String())
    81  	// Versions match, update is never needed
    82  	assert.False(t, shouldUpdate(c, false, false))
    83  	assert.False(t, shouldUpdate(c, true, true))
    84  }
    85  
    86  func TestShouldUpdateVersionsDontMatch(t *testing.T) {
    87  	c := makeConfig("shouldupdate")
    88  	c.Please.Version.UnmarshalFlag("2.0.0")
    89  	// Versions don't match but update is skipped
    90  	assert.False(t, shouldUpdate(c, false, false))
    91  	// Versions don't match, update is not skipped.
    92  	assert.True(t, shouldUpdate(c, true, false))
    93  	// Updates are off in config.
    94  	c.Please.SelfUpdate = false
    95  	assert.False(t, shouldUpdate(c, true, false))
    96  }
    97  
    98  func TestShouldUpdateGTEVersion(t *testing.T) {
    99  	c := makeConfig("shouldupdate")
   100  	c.Please.Version.UnmarshalFlag(">=2.0.0")
   101  	assert.False(t, shouldUpdate(c, true, false))
   102  	assert.True(t, shouldUpdate(c, true, true))
   103  }
   104  
   105  func TestShouldUpdateNoDownloadLocation(t *testing.T) {
   106  	c := makeConfig("shouldupdate")
   107  	// Download location isn't set
   108  	c.Please.DownloadLocation = ""
   109  	assert.False(t, shouldUpdate(c, true, true))
   110  }
   111  
   112  func TestShouldUpdateNoPleaseLocation(t *testing.T) {
   113  	c := makeConfig("shouldupdate")
   114  	// Please location isn't set
   115  	c.Please.Location = ""
   116  	assert.False(t, shouldUpdate(c, true, true))
   117  }
   118  
   119  func TestShouldUpdateNoVersion(t *testing.T) {
   120  	c := makeConfig("shouldupdate")
   121  	// No version is set, shouldn't update unless we force
   122  	c.Please.Version = cli.Version{}
   123  	assert.False(t, shouldUpdate(c, true, false))
   124  	assert.Equal(t, core.PleaseVersion, c.Please.Version.Semver())
   125  	c.Please.Version = cli.Version{}
   126  	assert.True(t, shouldUpdate(c, true, true))
   127  }
   128  
   129  func TestDownloadAndLinkPlease(t *testing.T) {
   130  	c := makeConfig("downloadandlink")
   131  	c.Please.Version.UnmarshalFlag(core.PleaseVersion.String())
   132  	newPlease := downloadAndLinkPlease(c, false)
   133  	assert.True(t, core.PathExists(newPlease))
   134  }
   135  
   136  func TestDownloadAndLinkPleaseBadVersion(t *testing.T) {
   137  	c := makeConfig("downloadandlink")
   138  	assert.Panics(t, func() { downloadAndLinkPlease(c, false) })
   139  	// Should have deleted the thing it downloaded.
   140  	assert.False(t, core.PathExists(path.Join(c.Please.Location, c.Please.Version.String())))
   141  }
   142  
   143  func TestFilterArgs(t *testing.T) {
   144  	assert.Equal(t, []string{"plz", "update"}, filterArgs(false, []string{"plz", "update"}))
   145  	assert.Equal(t, []string{"plz", "update", "--force"}, filterArgs(false, []string{"plz", "update", "--force"}))
   146  	assert.Equal(t, []string{"plz", "update"}, filterArgs(true, []string{"plz", "update", "--force"}))
   147  }
   148  
   149  func TestXZVersions(t *testing.T) {
   150  	var v cli.Version
   151  	v.UnmarshalFlag("13.1.9")
   152  	assert.False(t, shouldUseXZ(v))
   153  	v.UnmarshalFlag("13.2.0")
   154  	assert.True(t, shouldUseXZ(v))
   155  }
   156  
   157  func handler(w http.ResponseWriter, r *http.Request) {
   158  	vCurrent := fmt.Sprintf("/%s_%s/%s/please_%s.tar.xz", runtime.GOOS, runtime.GOARCH, core.PleaseVersion, core.PleaseVersion)
   159  	v42 := fmt.Sprintf("/%s_%s/42.0.0/please_42.0.0.tar.xz", runtime.GOOS, runtime.GOARCH)
   160  	if r.URL.Path == "/latest_version" {
   161  		w.Write([]byte("42.0.0"))
   162  	} else if r.URL.Path == vCurrent || r.URL.Path == v42 {
   163  		b, err := ioutil.ReadFile("src/update/please_test.tar.xz")
   164  		if err != nil {
   165  			panic(err)
   166  		}
   167  		w.Write(b)
   168  	} else if r.URL.Path == fmt.Sprintf("/%s_%s/1.0.0/please_1.0.0.tar.gz", runtime.GOOS, runtime.GOARCH) {
   169  		w.Write([]byte("notatarball"))
   170  	} else {
   171  		w.WriteHeader(http.StatusNotFound)
   172  	}
   173  }
   174  
   175  func makeConfig(dir string) *core.Configuration {
   176  	c := core.DefaultConfiguration()
   177  	wd, _ := os.Getwd()
   178  	c.Please.Location = path.Join(wd, dir)
   179  	c.Please.DownloadLocation.UnmarshalFlag(server.URL)
   180  	c.Please.Version.UnmarshalFlag("42.0.0")
   181  	return c
   182  }
   183  
   184  func TestMain(m *testing.M) {
   185  	// Reset this so it panics instead of exiting on Fatal messages
   186  	logging.SetBackend(&fakeLogBackend{})
   187  	server = httptest.NewServer(http.HandlerFunc(handler))
   188  	defer server.Close()
   189  	os.Exit(m.Run())
   190  }