This repository has been archived by the owner on Jun 21, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathplugin_test.go
115 lines (101 loc) · 2.29 KB
/
plugin_test.go
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
114
115
package main
import (
"io/ioutil"
"os"
"path/filepath"
"testing"
)
var commits = []struct {
path string
clone string
event string
branch string
commit string
file string
data string
}{
// first commit
{
path: "cedk/drone-hg-test",
clone: "https://bitbucket.org/cedk/drone-hg-test",
event: "push",
branch: "default",
commit: "37526193d0139f188b20e5c8bed8fc0640c38627",
file: "test",
data: "",
},
// head commit
{
path: "cedk/drone-hg-test",
clone: "https://bitbucket.org/cedk/drone-hg-test",
event: "push",
branch: "default",
commit: "6e4637b5ef305536115bd05e97c913fc7cdcc69d",
file: "test",
data: "Test\n",
},
}
// TestClone tests the ability to clone a specific commit into
// a fresh, empty directory every time.
func TestClone(t *testing.T) {
for _, c := range commits {
dir := setup()
defer teardown(dir)
plugin := Plugin{
Repo: Repo{
Clone: c.clone,
},
Build: Build{
Path: filepath.Join(dir, c.path),
Commit: c.commit,
Event: c.event,
},
}
if err := plugin.Exec(); err != nil {
t.Errorf("Expected successful clone. Got error. %s.", err)
}
data := readFile(plugin.Build.Path, c.file)
if data != c.data {
t.Errorf("Expected %s to contain [%s]. Got [%s].", c.file, c.data, data)
}
}
}
// TestCloneNonEmpty tests the ability to clone a specific commit into
// a non-empty directory. This is useful if the mercurial workspace is cached
// and re-stored for every build.
func TestCloneNonEmpty(t *testing.T) {
dir := setup()
defer teardown(dir)
for _, c := range commits {
plugin := Plugin{
Repo: Repo{
Clone: c.clone,
},
Build: Build{
Path: filepath.Join(dir, c.path),
Commit: c.commit,
Event: c.event,
},
}
if err := plugin.Exec(); err != nil {
t.Errorf("Expected successful clone. Got error. %s.", err)
}
data := readFile(plugin.Build.Path, c.file)
if data != c.data {
t.Errorf("Expected %s to contain [%s]. Got [%s].", c.file, c.data, data)
}
}
}
func setup() string {
dir, _ := ioutil.TempDir("/tmp", "drone_hg_test_")
os.Mkdir(dir, 0777)
return dir
}
func teardown(dir string) {
os.RemoveAll(dir)
}
func readFile(dir, file string) string {
filename := filepath.Join(dir, file)
data, _ := ioutil.ReadFile(filename)
return string(data)
}