(m *testing.M)
| 29 | } |
| 30 | |
| 31 | func TestMain(m *testing.M) { |
| 32 | tmpDir, err := os.MkdirTemp("", "integration-doctl") |
| 33 | if err != nil { |
| 34 | panic("failed to create temp dir") |
| 35 | } |
| 36 | defer os.RemoveAll(tmpDir) // yes, this is the best effort only |
| 37 | |
| 38 | builtBinaryPath = filepath.Join(tmpDir, path.Base(packagePath)) |
| 39 | if runtime.GOOS == "windows" { |
| 40 | builtBinaryPath += ".exe" |
| 41 | } |
| 42 | |
| 43 | // tried to use -mod=vendor but it blew up |
| 44 | cmd := exec.Command("go", "build", "-o", builtBinaryPath, packagePath) |
| 45 | cmd.Env = append(os.Environ(), "CGO_ENABLED=0") |
| 46 | output, err := cmd.CombinedOutput() |
| 47 | if err != nil { |
| 48 | panic(fmt.Sprintf("failed to build doctl: %s", output)) |
| 49 | } |
| 50 | |
| 51 | location, err := getDefaultConfigLocation() |
| 52 | if err != nil { |
| 53 | panic(fmt.Sprintf("failed to get config location: %s", err)) |
| 54 | } |
| 55 | |
| 56 | var contents []byte |
| 57 | if _, err := os.Stat(location); !os.IsNotExist(err) { |
| 58 | contents, err = os.ReadFile(location) |
| 59 | if err != nil { |
| 60 | panic("failed to copy config") |
| 61 | } |
| 62 | |
| 63 | err = os.Remove(location) |
| 64 | if err != nil { |
| 65 | panic("failed to delete initial config") |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | code := m.Run() |
| 70 | |
| 71 | if len(contents) != 0 { |
| 72 | err = os.WriteFile(location, contents, 0644) |
| 73 | if err != nil { |
| 74 | panic("failed to restore contents of config") |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | os.Exit(code) |
| 79 | } |
| 80 | |
| 81 | func getDefaultConfigLocation() (string, error) { |
| 82 | configDir, err := os.UserConfigDir() |
nothing calls this directly
no test coverage detected