|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "os" |
| 6 | + "os/exec" |
| 7 | + "path/filepath" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/stretchr/testify/require" |
| 11 | +) |
| 12 | + |
| 13 | +func TestVersionOutsideRepository(t *testing.T) { |
| 14 | + binary := filepath.Join(t.TempDir(), "git-drs") |
| 15 | + build := exec.Command("go", "build", "-o", binary, ".") |
| 16 | + output, err := build.CombinedOutput() |
| 17 | + require.NoError(t, err, "%s", output) |
| 18 | + |
| 19 | + dir := t.TempDir() |
| 20 | + command := exec.Command(binary, "version") |
| 21 | + command.Dir = dir |
| 22 | + // Prevent an inherited Git environment from pointing at a repository. |
| 23 | + command.Env = append(os.Environ(), "GIT_DIR=", "GIT_WORK_TREE=", "GIT_CEILING_DIRECTORIES="+filepath.Dir(dir)) |
| 24 | + var stderr bytes.Buffer |
| 25 | + command.Stderr = &stderr |
| 26 | + output, err = command.Output() |
| 27 | + require.NoError(t, err, "%s", stderr.String()) |
| 28 | + require.Empty(t, stderr.String()) |
| 29 | + require.Regexp(t, `^git-drs \S+\n$`, string(output)) |
| 30 | + entries, err := os.ReadDir(dir) |
| 31 | + require.NoError(t, err) |
| 32 | + require.Empty(t, entries, "version should not create repository or log files") |
| 33 | +} |
0 commit comments