summaryrefslogtreecommitdiffstats
path: root/pkg
diff options
context:
space:
mode:
authorCI <CI@example.com>2020-10-06 09:23:09 +1100
committerJesse Duffield <jessedduffield@gmail.com>2020-10-10 00:23:01 +1100
commita9049b4a82e733521a0249c458eee0e9fdd1295f (patch)
treed444285e2c8589667b5632fc9b1aa53a9462b4e6 /pkg
parentae352a5d8c29755f2b10c710b3338ab7ce10cb70 (diff)
stop using snapshots
Diffstat (limited to 'pkg')
-rw-r--r--pkg/commands/oscommands/copy.go7
-rw-r--r--pkg/gui/gui_test.go87
2 files changed, 77 insertions, 17 deletions
diff --git a/pkg/commands/oscommands/copy.go b/pkg/commands/oscommands/copy.go
index 2681441c5..131e9bc6b 100644
--- a/pkg/commands/oscommands/copy.go
+++ b/pkg/commands/oscommands/copy.go
@@ -76,7 +76,7 @@ func CopyFile(src, dst string) (err error) {
}
// CopyDir recursively copies a directory tree, attempting to preserve permissions.
-// Source directory must exist, destination directory must *not* exist.
+// Source directory must exist. If destination already exists we'll clobber it.
// Symlinks are ignored and skipped.
func CopyDir(src string, dst string) (err error) {
src = filepath.Clean(src)
@@ -95,7 +95,10 @@ func CopyDir(src string, dst string) (err error) {
return
}
if err == nil {
- return fmt.Errorf("destination already exists")
+ // it exists so let's remove it
+ if err := os.RemoveAll(dst); err != nil {
+ return err
+ }
}
err = os.MkdirAll(dst, si.Mode())
diff --git a/pkg/gui/gui_test.go b/pkg/gui/gui_test.go
index ef8ad6dd9..e11151020 100644
--- a/pkg/gui/gui_test.go
+++ b/pkg/gui/gui_test.go
@@ -81,13 +81,45 @@ func tests() []integrationTest {
}
}
-func generateSnapshot(t *testing.T, actualDir string) string {
+func generateSnapshot(t *testing.T, dir string) string {
osCommand := oscommands.NewDummyOSCommand()
- cmd := fmt.Sprintf(`bash -c "cd %s && git status; cat ./*; git log --pretty=%%B -p"`, actualDir)
- // need to copy from current directory to
+ _, err := os.Stat(filepath.Join(dir, ".git"))
+ if err != nil {
+ return "git directory not found"
+ }
+
+ snapshot := ""
+
+ statusCmd := fmt.Sprintf(`git -C %s status`, dir)
+ statusCmdOutput, err := osCommand.RunCommandWithOutput(statusCmd)
+ assert.NoError(t, err)
+
+ snapshot += statusCmdOutput + "\n"
+
+ logCmd := fmt.Sprintf(`git -C %s log --pretty=%%B -p -1`, dir)
+ logCmdOutput, err := osCommand.RunCommandWithOutput(logCmd)
+ assert.NoError(t, err)
+
+ snapshot += logCmdOutput + "\n"
+
+ err = filepath.Walk(dir, func(path string, f os.FileInfo, err error) error {
+ assert.NoError(t, err)
+
+ if f.IsDir() {
+ if f.Name() == ".git" {
+ return filepath.SkipDir
+ }
+ return nil
+ }
+
+ bytes, err := ioutil.ReadFile(path)
+ assert.NoError(t, err)
+ snapshot += string(bytes) + "\n"
+
+ return nil
+ })
- snapshot, err := osCommand.RunCommandWithOutput(cmd)
assert.NoError(t, err)
return snapshot
@@ -160,23 +192,46 @@ func Test(t *testing.T) {
testPath := filepath.Join(rootDir, "test", "integration", test.name)
actualDir := filepath.Join(testPath, "actual")
expectedDir := filepath.Join(testPath, "expected")
+ t.Logf("testPath: %s, actualDir: %s, expectedDir: %s", testPath, actualDir, expectedDir)
findOrCreateDir(testPath)
- prepareIntegrationTestDir(testPath)
+ prepareIntegrationTestDir(actualDir)
err := createFixture(rootDir, test.fixture, actualDir)
assert.NoError(t, err)
runLazygit(t, testPath, rootDir, record, speed)
- actual := generateSnapshot(t, actualDir)
-
if updateSnapshots {
err = oscommands.CopyDir(actualDir, expectedDir)
assert.NoError(t, err)
}
- expected := generateSnapshot(t, expectedDir)
+ actual := generateSnapshot(t, actualDir)
+
+ expected := ""
+
+ func() {
+ // git refuses to track .git folders in subdirectories so we need to rename it
+ // to git_keep after running a test
+
+ defer func() {
+ err = os.Rename(
+ filepath.Join(expectedDir, ".git"),
+ filepath.Join(expectedDir, ".git_keep"),
+ )
+
+ assert.NoError(t, err)
+ }()
+
+ // ignoring this error because we might not have a .git_keep file here yet.
+ _ = os.Rename(
+ filepath.Join(expectedDir, ".git_keep"),
+ filepath.Join(expectedDir, ".git"),
+ )
+
+ expected = generateSnapshot(t, expectedDir)
+ }()
if expected == actual {
t.Logf("%s: success at speed %d\n", test.name, speed)
@@ -268,7 +323,7 @@ func runLazygit(t *testing.T, testPath string, rootDir string, record bool, spee
}
// if we're on CI we'll need to use a PTY. We can work that out by seeing if the 'TERM' env is defined.
- if runInParallel() {
+ if runInPTY() {
cmd.Env = append(cmd.Env, "TERM=xterm")
f, err := pty.StartWithSize(cmd, &pty.Winsize{Rows: 100, Cols: 100})
@@ -286,17 +341,19 @@ func runLazygit(t *testing.T, testPath string, rootDir string, record bool, spee
}
func runInParallel() bool {
- return os.Getenv("PARALLEL") != "" || os.Getenv("TERM") == ""
+ return os.Getenv("PARALLEL") != ""
}
-func prepareIntegrationTestDir(testPath string) {
- path := filepath.Join(testPath, "actual")
+func runInPTY() bool {
+ return runInParallel() || os.Getenv("TERM") == ""
+}
+func prepareIntegrationTestDir(actualDir string) {
// remove contents of integration test directory
- dir, err := ioutil.ReadDir(path)
+ dir, err := ioutil.ReadDir(actualDir)
if err != nil {
if os.IsNotExist(err) {
- err = os.Mkdir(path, 0777)
+ err = os.Mkdir(actualDir, 0777)
if err != nil {
panic(err)
}
@@ -305,6 +362,6 @@ func prepareIntegrationTestDir(testPath string) {
}
}
for _, d := range dir {
- os.RemoveAll(filepath.Join(path, d.Name()))
+ os.RemoveAll(filepath.Join(actualDir, d.Name()))
}
}