diff options
Diffstat (limited to 'client/go/cmd/clone_test.go')
-rw-r--r-- | client/go/cmd/clone_test.go | 67 |
1 files changed, 51 insertions, 16 deletions
diff --git a/client/go/cmd/clone_test.go b/client/go/cmd/clone_test.go index 9587a1435d3..1971e0032a4 100644 --- a/client/go/cmd/clone_test.go +++ b/client/go/cmd/clone_test.go @@ -5,9 +5,10 @@ package cmd import ( - "io/ioutil" + "net/http" "os" "path/filepath" + "strings" "testing" "github.com/stretchr/testify/assert" @@ -21,28 +22,60 @@ func TestClone(t *testing.T) { } func assertCreated(sampleAppName string, app string, t *testing.T) { - appCached := app + "-cache" + tempDir := t.TempDir() + app1 := filepath.Join(tempDir, "app1") defer os.RemoveAll(app) - defer os.RemoveAll(appCached) httpClient := &mock.HTTPClient{} - testdata, err := ioutil.ReadFile(filepath.Join("testdata", "sample-apps-master.zip")) + cli, stdout, stderr := newTestCLI(t) + cli.httpClient = httpClient + testdata, err := os.ReadFile(filepath.Join("testdata", "sample-apps-master.zip")) require.Nil(t, err) + + // Initial cloning. GitHub includes the ETag header, but we don't require it httpClient.NextResponseBytes(200, testdata) + require.Nil(t, cli.Run("clone", sampleAppName, app1)) + assert.Equal(t, "Created "+app1+"\n", stdout.String()) + assertFiles(t, app1) - cli, stdout, _ := newTestCLI(t) - cli.httpClient = httpClient - err = cli.Run("clone", sampleAppName, app) - assert.Nil(t, err) + // Clone with cache hit + httpClient.NextStatus(http.StatusNotModified) + stdout.Reset() + app2 := filepath.Join(tempDir, "app2") + require.Nil(t, cli.Run("clone", sampleAppName, app2)) + assert.Equal(t, "Using cached sample apps ...\nCreated "+app2+"\n", stdout.String()) + assertFiles(t, app2) - assert.Equal(t, "Created "+app+"\n", stdout.String()) - assertFiles(t, app) + // Clone while ignoring cache + headers := make(http.Header) + headers.Set("etag", `W/"id1"`) + httpClient.NextResponse(mock.HTTPResponse{Status: 200, Body: testdata, Header: headers}) + stdout.Reset() + app3 := filepath.Join(tempDir, "app3") + require.Nil(t, cli.Run("clone", "-f", sampleAppName, app3)) + assert.Equal(t, "Created "+app3+"\n", stdout.String()) + assertFiles(t, app3) + // Cloning falls back to cached copy if GitHub is unavailable + httpClient.NextStatus(500) stdout.Reset() - err = cli.Run("clone", sampleAppName, appCached) - assert.Nil(t, err) - assert.Equal(t, "Using cached sample apps ...\nCreated "+appCached+"\n", stdout.String()) - assertFiles(t, appCached) + app4 := filepath.Join(tempDir, "app4") + require.Nil(t, cli.Run("clone", "-f=false", sampleAppName, app4)) + assert.Equal(t, "Warning: could not download sample apps: github returned status 500\n", stderr.String()) + assert.Equal(t, "Using cached sample apps ...\nCreated "+app4+"\n", stdout.String()) + assertFiles(t, app4) + + // The only cached file is the latest one + dirEntries, err := os.ReadDir(cli.config.cacheDir) + require.Nil(t, err) + var zipFiles []string + for _, de := range dirEntries { + name := de.Name() + if strings.HasPrefix(name, sampleAppsNamePrefix) { + zipFiles = append(zipFiles, name) + } + } + assert.Equal(t, []string{"sample-apps-master_id1.zip"}, zipFiles) } func assertFiles(t *testing.T, app string) { @@ -50,10 +83,12 @@ func assertFiles(t *testing.T, app string) { assert.True(t, util.PathExists(filepath.Join(app, "src", "main", "application"))) assert.True(t, util.IsDirectory(filepath.Join(app, "src", "main", "application"))) - servicesStat, _ := os.Stat(filepath.Join(app, "src", "main", "application", "services.xml")) + servicesStat, err := os.Stat(filepath.Join(app, "src", "main", "application", "services.xml")) + require.Nil(t, err) servicesSize := int64(1772) assert.Equal(t, servicesSize, servicesStat.Size()) - scriptStat, _ := os.Stat(filepath.Join(app, "bin", "convert-msmarco.sh")) + scriptStat, err := os.Stat(filepath.Join(app, "bin", "convert-msmarco.sh")) + require.Nil(t, err) assert.Equal(t, os.FileMode(0755), scriptStat.Mode()) } |