2021-04-05 20:37:18 +10:00
|
|
|
package integration
|
|
|
|
|
|
|
|
import (
|
2022-01-26 16:46:54 +11:00
|
|
|
"errors"
|
2021-04-05 20:37:18 +10:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
2022-08-09 21:27:12 +10:00
|
|
|
"log"
|
2021-04-05 20:37:18 +10:00
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"path/filepath"
|
2022-08-09 21:27:12 +10:00
|
|
|
"strings"
|
2021-04-05 20:37:18 +10:00
|
|
|
|
2022-08-09 21:27:12 +10:00
|
|
|
"github.com/jesseduffield/generics/slices"
|
2021-04-05 20:37:18 +10:00
|
|
|
"github.com/jesseduffield/lazygit/pkg/commands/oscommands"
|
2022-08-12 09:19:39 +10:00
|
|
|
"github.com/jesseduffield/lazygit/pkg/integration/components"
|
2022-08-09 21:27:12 +10:00
|
|
|
"github.com/jesseduffield/lazygit/pkg/integration/tests"
|
|
|
|
"github.com/stretchr/testify/assert"
|
2021-04-05 20:37:18 +10:00
|
|
|
)
|
|
|
|
|
2022-08-07 22:09:39 +10:00
|
|
|
// this is the integration runner for the new and improved integration interface
|
2022-05-07 15:42:36 +10:00
|
|
|
|
2022-08-09 21:27:12 +10:00
|
|
|
var Tests = tests.Tests
|
|
|
|
|
|
|
|
type Mode int
|
|
|
|
|
|
|
|
const (
|
|
|
|
// Default: if a snapshot test fails, the we'll be asked whether we want to update it
|
|
|
|
ASK_TO_UPDATE_SNAPSHOT = iota
|
|
|
|
// fails the test if the snapshots don't match
|
|
|
|
CHECK_SNAPSHOT
|
|
|
|
// runs the test and updates the snapshot
|
|
|
|
UPDATE_SNAPSHOT
|
|
|
|
// This just makes use of the setup step of the test to get you into
|
|
|
|
// a lazygit session. Then you'll be able to do whatever you want. Useful
|
|
|
|
// when you want to test certain things without needing to manually set
|
|
|
|
// up the situation yourself.
|
|
|
|
// fails the test if the snapshots don't match
|
|
|
|
SANDBOX
|
|
|
|
)
|
|
|
|
|
2022-08-13 14:12:35 +10:00
|
|
|
const LAZYGIT_TEST_NAME_ENV_VAR = "LAZYGIT_TEST_NAME"
|
|
|
|
|
2022-08-09 21:27:12 +10:00
|
|
|
type (
|
|
|
|
logf func(format string, formatArgs ...interface{})
|
|
|
|
)
|
|
|
|
|
2022-08-11 21:21:46 +10:00
|
|
|
func RunTests(
|
2022-08-09 21:27:12 +10:00
|
|
|
logf logf,
|
2021-04-06 09:02:01 +10:00
|
|
|
runCmd func(cmd *exec.Cmd) error,
|
2022-08-12 09:19:39 +10:00
|
|
|
fnWrapper func(test *components.IntegrationTest, f func() error),
|
2022-01-15 20:24:19 +11:00
|
|
|
mode Mode,
|
2021-04-06 18:20:34 +10:00
|
|
|
includeSkipped bool,
|
2021-04-06 09:02:01 +10:00
|
|
|
) error {
|
|
|
|
rootDir := GetRootDirectory()
|
|
|
|
err := os.Chdir(rootDir)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-08-07 22:09:39 +10:00
|
|
|
testDir := filepath.Join(rootDir, "test", "integration_new")
|
2021-04-06 09:02:01 +10:00
|
|
|
|
|
|
|
osCommand := oscommands.NewDummyOSCommand()
|
2022-08-13 14:12:35 +10:00
|
|
|
err = osCommand.Cmd.New(fmt.Sprintf("go build -o %s pkg/integration/cmd/injector/main.go", tempLazygitPath())).Run()
|
2021-04-06 09:02:01 +10:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-08-09 21:27:12 +10:00
|
|
|
for _, test := range Tests {
|
2021-04-06 09:02:01 +10:00
|
|
|
test := test
|
|
|
|
|
2022-08-09 21:11:41 +10:00
|
|
|
fnWrapper(test, func() error { //nolint: thelper
|
2022-08-07 22:09:39 +10:00
|
|
|
if test.Skip() && !includeSkipped {
|
|
|
|
logf("skipping test: %s", test.Name())
|
2022-03-26 21:18:10 +11:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-08-07 22:09:39 +10:00
|
|
|
testPath := filepath.Join(testDir, test.Name())
|
|
|
|
|
2022-03-27 11:47:07 +11:00
|
|
|
actualDir := filepath.Join(testPath, "actual")
|
|
|
|
expectedDir := filepath.Join(testPath, "expected")
|
|
|
|
actualRepoDir := filepath.Join(actualDir, "repo")
|
2021-04-06 11:17:32 +10:00
|
|
|
logf("path: %s", testPath)
|
2021-04-06 09:02:01 +10:00
|
|
|
|
2022-08-07 22:09:39 +10:00
|
|
|
findOrCreateDir(testPath)
|
|
|
|
prepareIntegrationTestDir(actualDir)
|
|
|
|
findOrCreateDir(actualRepoDir)
|
2022-08-11 21:21:46 +10:00
|
|
|
err := createFixture(test, actualRepoDir, rootDir)
|
2022-08-07 22:09:39 +10:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
configDir := filepath.Join(testPath, "used_config")
|
2021-04-06 09:02:01 +10:00
|
|
|
|
2022-08-11 21:21:46 +10:00
|
|
|
cmd, err := getLazygitCommand(test, testPath, rootDir)
|
2022-08-07 22:09:39 +10:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-08-09 21:27:12 +10:00
|
|
|
err = runCmd(cmd)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-04-06 09:02:01 +10:00
|
|
|
|
2022-08-09 21:27:12 +10:00
|
|
|
switch mode {
|
|
|
|
case UPDATE_SNAPSHOT:
|
2022-08-12 09:24:39 +10:00
|
|
|
if err := updateSnapshot(actualDir, expectedDir); err != nil {
|
2021-04-06 09:02:01 +10:00
|
|
|
return err
|
|
|
|
}
|
2022-08-09 21:27:12 +10:00
|
|
|
logf("Test passed: %s", test.Name())
|
|
|
|
case CHECK_SNAPSHOT:
|
|
|
|
if err := compareSnapshots(logf, configDir, actualDir, expectedDir, test.Name()); err != nil {
|
2021-04-06 09:02:01 +10:00
|
|
|
return err
|
|
|
|
}
|
2022-08-09 21:27:12 +10:00
|
|
|
logf("Test passed: %s", test.Name())
|
|
|
|
case ASK_TO_UPDATE_SNAPSHOT:
|
|
|
|
if _, err := os.Stat(expectedDir); os.IsNotExist(err) {
|
2022-08-12 09:24:39 +10:00
|
|
|
if err := updateSnapshot(actualDir, expectedDir); err != nil {
|
2022-08-09 21:27:12 +10:00
|
|
|
return err
|
2021-04-06 09:02:01 +10:00
|
|
|
}
|
2022-08-09 21:27:12 +10:00
|
|
|
logf("No existing snapshot found for %s. Created snapshot.", test.Name())
|
2022-01-28 21:56:01 +11:00
|
|
|
|
2022-08-09 21:27:12 +10:00
|
|
|
return nil
|
|
|
|
}
|
2021-10-22 20:18:40 +11:00
|
|
|
|
2022-08-09 21:27:12 +10:00
|
|
|
if err := compareSnapshots(logf, configDir, actualDir, expectedDir, test.Name()); err != nil {
|
|
|
|
logf("%s", err)
|
2021-10-22 20:18:40 +11:00
|
|
|
|
2022-08-09 21:27:12 +10:00
|
|
|
// prompt user whether to update the snapshot (Y/N)
|
|
|
|
if promptUserToUpdateSnapshot() {
|
2022-08-12 09:24:39 +10:00
|
|
|
if err := updateSnapshot(actualDir, expectedDir); err != nil {
|
2022-01-15 20:24:19 +11:00
|
|
|
return err
|
|
|
|
}
|
2022-08-09 21:27:12 +10:00
|
|
|
logf("Snapshot updated: %s", test.Name())
|
|
|
|
} else {
|
|
|
|
return err
|
2022-03-27 11:47:07 +11:00
|
|
|
}
|
2021-04-06 09:02:01 +10:00
|
|
|
}
|
|
|
|
|
2022-08-09 21:27:12 +10:00
|
|
|
logf("Test passed: %s", test.Name())
|
|
|
|
case SANDBOX:
|
|
|
|
logf("Session exited")
|
|
|
|
}
|
2022-08-07 22:09:39 +10:00
|
|
|
|
2021-04-06 09:02:01 +10:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-08-09 21:27:12 +10:00
|
|
|
func promptUserToUpdateSnapshot() bool {
|
|
|
|
fmt.Println("Test failed. Update snapshot? (y/n)")
|
|
|
|
var input string
|
|
|
|
fmt.Scanln(&input)
|
|
|
|
return input == "y"
|
|
|
|
}
|
|
|
|
|
2022-08-12 09:24:39 +10:00
|
|
|
func updateSnapshot(actualDir string, expectedDir string) error {
|
2022-08-09 21:27:12 +10:00
|
|
|
// create/update snapshot
|
|
|
|
err := oscommands.CopyDir(actualDir, expectedDir)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := renameSpecialPaths(expectedDir); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func compareSnapshots(logf logf, configDir string, actualDir string, expectedDir string, testName string) error {
|
|
|
|
// there are a couple of reasons we're not generating the snapshot in expectedDir directly:
|
|
|
|
// Firstly we don't want to have to revert our .git file back to .git_keep.
|
|
|
|
// Secondly, the act of calling git commands like 'git status' actually changes the index
|
|
|
|
// for some reason, and we don't want to leave your lazygit working tree dirty as a result.
|
|
|
|
expectedDirCopy := filepath.Join(os.TempDir(), "expected_dir_test", testName)
|
|
|
|
err := oscommands.CopyDir(expectedDir, expectedDirCopy)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
err := os.RemoveAll(expectedDirCopy)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
if err := restoreSpecialPaths(expectedDirCopy); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = validateSameRepos(expectedDirCopy, actualDir)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// iterate through each repo in the expected dir and comparet to the corresponding repo in the actual dir
|
|
|
|
expectedFiles, err := ioutil.ReadDir(expectedDirCopy)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, f := range expectedFiles {
|
|
|
|
if !f.IsDir() {
|
|
|
|
return errors.New("unexpected file (as opposed to directory) in integration test 'expected' directory")
|
|
|
|
}
|
|
|
|
|
|
|
|
// get corresponding file name from actual dir
|
|
|
|
actualRepoPath := filepath.Join(actualDir, f.Name())
|
|
|
|
expectedRepoPath := filepath.Join(expectedDirCopy, f.Name())
|
|
|
|
|
|
|
|
actualRepo, expectedRepo, err := generateSnapshots(actualRepoPath, expectedRepoPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if expectedRepo != actualRepo {
|
|
|
|
// get the log file and print it
|
|
|
|
bytes, err := ioutil.ReadFile(filepath.Join(configDir, "development.log"))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
logf("%s", string(bytes))
|
|
|
|
|
|
|
|
return errors.New(getDiff(f.Name(), actualRepo, expectedRepo))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-08-12 09:19:39 +10:00
|
|
|
func createFixture(test *components.IntegrationTest, actualDir string, rootDir string) error {
|
2022-08-07 22:09:39 +10:00
|
|
|
if err := os.Chdir(actualDir); err != nil {
|
2021-04-05 20:37:18 +10:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2022-08-12 09:19:39 +10:00
|
|
|
shell := components.NewShell()
|
2022-08-07 22:09:39 +10:00
|
|
|
shell.RunCommand("git init")
|
|
|
|
shell.RunCommand(`git config user.email "CI@example.com"`)
|
|
|
|
shell.RunCommand(`git config user.name "CI"`)
|
2021-04-05 20:37:18 +10:00
|
|
|
|
2022-08-07 22:09:39 +10:00
|
|
|
test.SetupRepo(shell)
|
2021-04-05 20:37:18 +10:00
|
|
|
|
2022-08-07 22:09:39 +10:00
|
|
|
// changing directory back to rootDir after the setup is done
|
|
|
|
if err := os.Chdir(rootDir); err != nil {
|
2022-01-28 21:56:01 +11:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-08-12 09:19:39 +10:00
|
|
|
func getLazygitCommand(test *components.IntegrationTest, testPath string, rootDir string) (*exec.Cmd, error) {
|
2022-08-09 21:27:12 +10:00
|
|
|
osCommand := oscommands.NewDummyOSCommand()
|
|
|
|
|
2021-04-05 20:37:18 +10:00
|
|
|
templateConfigDir := filepath.Join(rootDir, "test", "default_test_config")
|
2022-03-27 11:47:07 +11:00
|
|
|
actualRepoDir := filepath.Join(testPath, "actual", "repo")
|
2021-04-05 20:37:18 +10:00
|
|
|
|
|
|
|
configDir := filepath.Join(testPath, "used_config")
|
|
|
|
|
2022-08-07 22:09:39 +10:00
|
|
|
err := os.RemoveAll(configDir)
|
2021-04-05 20:37:18 +10:00
|
|
|
if err != nil {
|
2022-08-09 21:27:12 +10:00
|
|
|
return nil, err
|
2021-04-05 20:37:18 +10:00
|
|
|
}
|
|
|
|
err = oscommands.CopyDir(templateConfigDir, configDir)
|
|
|
|
if err != nil {
|
2022-08-09 21:27:12 +10:00
|
|
|
return nil, err
|
2021-04-05 20:37:18 +10:00
|
|
|
}
|
|
|
|
|
2022-08-09 21:27:12 +10:00
|
|
|
cmdStr := fmt.Sprintf("%s -debug --use-config-dir=%s --path=%s %s", tempLazygitPath(), configDir, actualRepoDir, test.ExtraCmdArgs())
|
|
|
|
|
|
|
|
cmdObj := osCommand.Cmd.New(cmdStr)
|
|
|
|
|
2022-08-13 14:12:35 +10:00
|
|
|
cmdObj.AddEnvVars(fmt.Sprintf("%s=%s", LAZYGIT_TEST_NAME_ENV_VAR, test.Name()))
|
2022-08-09 21:27:12 +10:00
|
|
|
|
|
|
|
return cmdObj.GetCmd(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetModeFromEnv() Mode {
|
|
|
|
switch os.Getenv("MODE") {
|
|
|
|
case "", "ask":
|
|
|
|
return ASK_TO_UPDATE_SNAPSHOT
|
|
|
|
case "check":
|
|
|
|
return CHECK_SNAPSHOT
|
|
|
|
case "updateSnapshot":
|
|
|
|
return UPDATE_SNAPSHOT
|
|
|
|
case "sandbox":
|
|
|
|
return SANDBOX
|
|
|
|
default:
|
|
|
|
log.Fatalf("unknown test mode: %s, must be one of [test, record, updateSnapshot, sandbox]", os.Getenv("MODE"))
|
|
|
|
panic("unreachable")
|
2022-08-09 21:11:41 +10:00
|
|
|
}
|
2022-08-09 21:27:12 +10:00
|
|
|
}
|
2021-04-05 20:37:18 +10:00
|
|
|
|
2022-08-09 21:27:12 +10:00
|
|
|
func GetRootDirectory() string {
|
|
|
|
path, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
2022-08-09 21:11:41 +10:00
|
|
|
}
|
2021-04-05 20:37:18 +10:00
|
|
|
|
2022-08-09 21:27:12 +10:00
|
|
|
for {
|
|
|
|
_, err := os.Stat(filepath.Join(path, ".git"))
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
return path
|
|
|
|
}
|
|
|
|
|
|
|
|
if !os.IsNotExist(err) {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
path = filepath.Dir(path)
|
|
|
|
|
|
|
|
if path == "/" {
|
|
|
|
log.Fatal("must run in lazygit folder or child folder")
|
|
|
|
}
|
|
|
|
}
|
2022-08-09 21:11:41 +10:00
|
|
|
}
|
2021-04-05 20:37:18 +10:00
|
|
|
|
2022-08-09 21:27:12 +10:00
|
|
|
func tempLazygitPath() string {
|
|
|
|
return filepath.Join("/tmp", "lazygit", "test_lazygit")
|
|
|
|
}
|
|
|
|
|
|
|
|
func generateSnapshots(actualDir string, expectedDir string) (string, string, error) {
|
|
|
|
actual, err := generateSnapshot(actualDir)
|
|
|
|
if err != nil {
|
|
|
|
return "", "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
expected, err := generateSnapshot(expectedDir)
|
|
|
|
if err != nil {
|
|
|
|
return "", "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return actual, expected, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// note that we don't actually store this snapshot in the lazygit repo.
|
|
|
|
// Instead we store the whole expected git repo of our test, so that
|
|
|
|
// we can easily change what we want to compare without needing to regenerate
|
|
|
|
// snapshots for each test.
|
|
|
|
func generateSnapshot(dir string) (string, error) {
|
|
|
|
osCommand := oscommands.NewDummyOSCommand()
|
|
|
|
|
|
|
|
_, err := os.Stat(filepath.Join(dir, ".git"))
|
|
|
|
if err != nil {
|
|
|
|
return "git directory not found", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
snapshot := ""
|
|
|
|
|
|
|
|
cmdStrs := []string{
|
|
|
|
`remote show -n origin`, // remote branches
|
|
|
|
// TODO: find a way to bring this back without breaking tests
|
|
|
|
// `ls-remote origin`,
|
|
|
|
`status`, // file tree
|
|
|
|
`log --pretty=%B|%an|%ae -p -1`, // log
|
|
|
|
`tag -n`, // tags
|
|
|
|
`stash list`, // stash
|
|
|
|
`submodule foreach 'git status'`, // submodule status
|
|
|
|
`submodule foreach 'git log --pretty=%B -p -1'`, // submodule log
|
|
|
|
`submodule foreach 'git tag -n'`, // submodule tags
|
|
|
|
`submodule foreach 'git stash list'`, // submodule stash
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, cmdStr := range cmdStrs {
|
|
|
|
// ignoring error for now. If there's an error it could be that there are no results
|
|
|
|
output, _ := osCommand.Cmd.New(fmt.Sprintf("git -C %s %s", dir, cmdStr)).RunWithOutput()
|
|
|
|
|
|
|
|
snapshot += fmt.Sprintf("git %s:\n%s\n", cmdStr, output)
|
|
|
|
}
|
|
|
|
|
|
|
|
snapshot += "files in repo:\n"
|
|
|
|
err = filepath.Walk(dir, func(path string, f os.FileInfo, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2022-08-09 21:11:41 +10:00
|
|
|
}
|
|
|
|
|
2022-08-09 21:27:12 +10:00
|
|
|
if f.IsDir() {
|
|
|
|
if f.Name() == ".git" {
|
|
|
|
return filepath.SkipDir
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
bytes, err := ioutil.ReadFile(path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
relativePath, err := filepath.Rel(dir, path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
snapshot += fmt.Sprintf("path: %s\ncontent:\n%s\n", relativePath, string(bytes))
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return snapshot, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getPathsToRename(dir string, needle string, contains string) []string {
|
|
|
|
pathsToRename := []string{}
|
|
|
|
|
|
|
|
err := filepath.Walk(dir, func(path string, f os.FileInfo, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if f.Name() == needle && (contains == "" || strings.Contains(path, contains)) {
|
|
|
|
pathsToRename = append(pathsToRename, path)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return pathsToRename
|
|
|
|
}
|
|
|
|
|
|
|
|
var specialPathMappings = []struct{ original, new, contains string }{
|
|
|
|
// git refuses to track .git or .gitmodules in subdirectories so we need to rename them
|
|
|
|
{".git", ".git_keep", ""},
|
|
|
|
{".gitmodules", ".gitmodules_keep", ""},
|
|
|
|
// we also need git to ignore the contents of our test gitignore files so that
|
|
|
|
// we actually commit files that are ignored within the test.
|
|
|
|
{".gitignore", "lg_ignore_file", ""},
|
|
|
|
// this is the .git/info/exclude file. We're being a little more specific here
|
|
|
|
// so that we don't accidentally mess with some other file named 'exclude' in the test.
|
|
|
|
{"exclude", "lg_exclude_file", ".git/info/exclude"},
|
|
|
|
}
|
|
|
|
|
|
|
|
func renameSpecialPaths(dir string) error {
|
|
|
|
for _, specialPath := range specialPathMappings {
|
|
|
|
for _, path := range getPathsToRename(dir, specialPath.original, specialPath.contains) {
|
|
|
|
err := os.Rename(path, filepath.Join(filepath.Dir(path), specialPath.new))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func restoreSpecialPaths(dir string) error {
|
|
|
|
for _, specialPath := range specialPathMappings {
|
|
|
|
for _, path := range getPathsToRename(dir, specialPath.new, specialPath.contains) {
|
|
|
|
err := os.Rename(path, filepath.Join(filepath.Dir(path), specialPath.original))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// validates that the actual and expected dirs have the same repo names (doesn't actually check the contents of the repos)
|
|
|
|
func validateSameRepos(expectedDir string, actualDir string) error {
|
|
|
|
// iterate through each repo in the expected dir and compare to the corresponding repo in the actual dir
|
|
|
|
expectedFiles, err := ioutil.ReadDir(expectedDir)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var actualFiles []os.FileInfo
|
|
|
|
actualFiles, err = ioutil.ReadDir(actualDir)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
expectedFileNames := slices.Map(expectedFiles, getFileName)
|
|
|
|
actualFileNames := slices.Map(actualFiles, getFileName)
|
|
|
|
if !slices.Equal(expectedFileNames, actualFileNames) {
|
|
|
|
return fmt.Errorf("expected and actual repo dirs do not match: expected: %s, actual: %s", expectedFileNames, actualFileNames)
|
|
|
|
}
|
2022-08-09 21:11:41 +10:00
|
|
|
|
|
|
|
return nil
|
2021-04-05 20:37:18 +10:00
|
|
|
}
|
2022-08-09 21:27:12 +10:00
|
|
|
|
|
|
|
func getFileName(f os.FileInfo) string {
|
|
|
|
return f.Name()
|
|
|
|
}
|
|
|
|
|
|
|
|
func findOrCreateDir(path string) {
|
|
|
|
_, err := os.Stat(path)
|
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
err = os.MkdirAll(path, 0o777)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func prepareIntegrationTestDir(actualDir string) {
|
|
|
|
// remove contents of integration test directory
|
|
|
|
dir, err := ioutil.ReadDir(actualDir)
|
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
err = os.Mkdir(actualDir, 0o777)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, d := range dir {
|
|
|
|
os.RemoveAll(filepath.Join(actualDir, d.Name()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func getDiff(prefix string, expected string, actual string) string {
|
|
|
|
mockT := &MockTestingT{}
|
|
|
|
assert.Equal(mockT, expected, actual, fmt.Sprintf("Unexpected %s. Expected:\n%s\nActual:\n%s\n", prefix, expected, actual))
|
|
|
|
return mockT.err
|
|
|
|
}
|
|
|
|
|
|
|
|
type MockTestingT struct {
|
|
|
|
err string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *MockTestingT) Errorf(format string, args ...interface{}) {
|
|
|
|
self.err += fmt.Sprintf(format, args...)
|
|
|
|
}
|