-
-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathplugin_test.go
More file actions
104 lines (96 loc) · 2.2 KB
/
plugin_test.go
File metadata and controls
104 lines (96 loc) · 2.2 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
package main
import (
"context"
"os"
"os/exec"
"testing"
)
func TestPlugin_HandleRemote(t *testing.T) {
type fields struct {
Netrc Netrc
Commit Commit
Config Config
}
tests := []struct {
name string
fields fields
wantErr bool
}{
{
name: "vaild git URL",
fields: fields{
Config: Config{
RemoteName: "deploy",
Remote: "git@github.com:appleboy/drone-git-push.git",
},
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
p := Plugin{
Netrc: tt.fields.Netrc,
Commit: tt.fields.Commit,
Config: tt.fields.Config,
}
if err := p.HandleRemote(context.Background()); (err != nil) != tt.wantErr {
t.Errorf("Plugin.HandleRemote() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func TestPlugin_HandleRemote_ExistingRemote(t *testing.T) {
// Create a temporary git repo
tmpDir, err := os.MkdirTemp("", "drone-git-push-test")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tmpDir)
// Save current dir and change to temp dir
origDir, err := os.Getwd()
if err != nil {
t.Fatal(err)
}
defer func() {
_ = os.Chdir(origDir)
}()
if err := os.Chdir(tmpDir); err != nil {
t.Fatal(err)
}
// Initialize a git repo and add a remote
ctx := context.Background()
if out, err := exec.CommandContext(
ctx, "git", "init",
).CombinedOutput(); err != nil {
t.Fatalf("git init failed: %s, %v", out, err)
}
if out, err := exec.CommandContext(
ctx, "git", "remote", "add", "origin",
"git@github.com:old/repo.git",
).CombinedOutput(); err != nil {
t.Fatalf("git remote add failed: %s, %v", out, err)
}
// HandleRemote should succeed even though "origin" already exists
p := Plugin{
Config: Config{
RemoteName: "origin",
Remote: "git@github.com:new/repo.git",
},
}
if err := p.HandleRemote(ctx); err != nil {
t.Errorf("HandleRemote() with existing remote should not fail, got: %v", err)
}
// Verify the remote URL was updated
out, err := exec.CommandContext(
ctx, "git", "remote", "get-url", "origin",
).Output()
if err != nil {
t.Fatal(err)
}
got := string(out)
want := "git@github.com:new/repo.git\n"
if got != want {
t.Errorf("remote URL = %q, want %q", got, want)
}
}