Repository navigation
Expand file tree
/
Copy pathcommand_test.go
More file actions
86 lines (69 loc) · 2.78 KB
/
Copy pathcommand_test.go
File metadata and controls
86 lines (69 loc) · 2.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package git
import (
"bytes"
"os/exec"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestRunPreservesGitErrorDiagnostics(t *testing.T) {
// Given a command that writes output before failing with a diagnostic on stderr
const stdout = "partial output\n"
const stderr = "fatal: test transport failed\n"
execCmd := createCommand(t, 128, stdout, stderr)
stdoutBuffer := &bytes.Buffer{}
execCmd.Stdout = stdoutBuffer
cmd := Command{execCmd}
// When the command is run
err := cmd.Run()
// Then stdout is written and the Git error preserves the diagnostic and exit code
require.Error(t, err)
var gitErr *GitError
require.ErrorAs(t, err, &gitErr)
assert.Equal(t, stdout, stdoutBuffer.String())
assert.Equal(t, 128, gitErr.ExitCode)
assert.Equal(t, stderr, gitErr.Stderr)
require.EqualError(t, err, "failed to run git: fatal: test transport failed\n")
}
func TestOutputReturnsCommandOutput(t *testing.T) {
// Given a command that writes to standard output
cmd := createCommand(t, 0, "hello world", "")
command := Command{cmd}
// When its output is captured
out, err := command.Output()
// Then the output is returned without an error
require.NoError(t, err)
assert.Equal(t, "hello world", string(out))
}
func TestOutputReturnsGitError(t *testing.T) {
// Given a command that fails with Git's not-a-repository diagnostic
stderr := "fatal: not a git repository (or any of the parent directories): .git"
cmd := createCommand(t, 128, "", stderr)
command := Command{cmd}
// When its output is captured
out, err := command.Output()
// Then the exit status and diagnostic are preserved in a Git error
require.Error(t, err)
var gitErr *GitError
require.ErrorAs(t, err, &gitErr)
assert.Equal(t, 128, gitErr.ExitCode)
assert.Equal(t, stderr, gitErr.Stderr)
require.EqualError(t, gitErr, "failed to run git: "+stderr)
assert.Empty(t, out)
}
func TestWithRepoDirInsertsRepositoryBeforeGitArguments(t *testing.T) {
// Given a Git command with a final -C operand after the argument separator
command := &Command{exec.Command("git", "rev-parse", "--", "-C")}
// When its repository directory is changed
WithRepoDir("path/to/repo")(command)
// Then the repository option is inserted before the Git subcommand without moving the separator
assert.Equal(t, []string{"git", "-C", "path/to/repo", "rev-parse", "--", "-C"}, command.Args)
}
func TestWithRepoDirReplacesExistingRepository(t *testing.T) {
// Given a Git command already scoped to a repository
command := &Command{exec.Command("git", "-C", "old/repo", "status")}
// When its repository directory is changed
WithRepoDir("new/repo")(command)
// Then the existing repository value is replaced without adding another option
assert.Equal(t, []string{"git", "-C", "new/repo", "status"}, command.Args)
}