-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathmain_test.go
More file actions
113 lines (101 loc) · 2.59 KB
/
main_test.go
File metadata and controls
113 lines (101 loc) · 2.59 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package main
import (
"bytes"
"os"
"os/exec"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestWriteScriptsToTemp(t *testing.T) {
// Create a temporary directory for testing
tmpDir, err := os.MkdirTemp("", "drone-git-test-*")
require.NoError(t, err)
defer os.RemoveAll(tmpDir)
// Test writing scripts
err = writeScriptsToTemp(tmpDir)
require.NoError(t, err)
// Verify posix scripts are written
posixFiles := []string{"script", "clone", "common"}
for _, file := range posixFiles {
path := filepath.Join(tmpDir, "posix", file)
_, err := os.Stat(path)
assert.NoError(t, err, "posix file should exist: %s", file)
// Verify file permissions
info, err := os.Stat(path)
assert.NoError(t, err)
assert.Equal(t, mode, info.Mode().Perm(), "file should have correct permissions: %s", file)
}
// Verify windows scripts are written
windowsFiles := []string{"clone.ps1", "common.ps1"}
for _, file := range windowsFiles {
path := filepath.Join(tmpDir, "windows", file)
_, err := os.Stat(path)
assert.NoError(t, err, "windows file should exist: %s", file)
// Verify file permissions
info, err := os.Stat(path)
assert.NoError(t, err)
assert.Equal(t, mode, info.Mode().Perm(), "file should have correct permissions: %s", file)
}
}
func TestRunCmds(t *testing.T) {
tests := []struct {
name string
cmds []*exec.Cmd
env []string
workdir string
wantErr bool
wantOutput string
wantErrText string
}{
{
name: "successful command",
cmds: []*exec.Cmd{
exec.Command("echo", "hello"),
},
env: []string{"TEST=true"},
workdir: ".",
wantErr: false,
wantOutput: "hello\n",
},
{
name: "failed command",
cmds: []*exec.Cmd{
exec.Command("nonexistent-command"),
},
env: []string{"TEST=true"},
workdir: ".",
wantErr: true,
},
{
name: "multiple commands",
cmds: []*exec.Cmd{
exec.Command("echo", "first"),
exec.Command("echo", "second"),
},
env: []string{"TEST=true"},
workdir: ".",
wantErr: false,
wantOutput: "first\nsecond\n",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
stdout := &bytes.Buffer{}
stderr := &bytes.Buffer{}
err := runCmds(tt.cmds, tt.env, tt.workdir, stdout, stderr)
if tt.wantErr {
assert.Error(t, err)
if tt.wantErrText != "" {
assert.Contains(t, stderr.String(), tt.wantErrText)
}
} else {
assert.NoError(t, err)
if tt.wantOutput != "" {
assert.Equal(t, tt.wantOutput, stdout.String())
}
}
})
}
}