-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup_and_teardown_test.go
94 lines (78 loc) · 1.98 KB
/
setup_and_teardown_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
// Copyright 2012, The gohg Authors. All rights reserved.
// Use of this source code is governed by a BSD style license
// that can be found in the LICENSE.md file.
package gohg
import (
"io/ioutil"
"os"
"os/exec"
"testing"
)
var testdir string
func setup(t *testing.T) (hct *HgClient) {
// Set var hgexe to whatever is appropriate for your situation.
// You can also change it to test with different versions of Mercurial.
hgexe := "hg"
var err error
testdir, err = createTempdir(t)
if err != nil {
t.Fatal(err)
}
var cmd *exec.Cmd
cmd = exec.Command(hgexe, "--cwd", testdir, "init")
if err = cmd.Run(); err != nil {
t.Fatal(err)
}
repo := testdir
// let's have some config for the new repo
hct = NewHgClient()
err = createFile(".hg/hgrc", "[ui]\nusername=me-myself\n", hct.RepoRoot())
cfg := make([]string, 0)
err = hct.Connect(hgexe, repo, cfg, false)
if err != nil {
t.Fatal(err)
}
return hct
}
func teardown(t *testing.T, hct *HgClient) {
err := hct.Disconnect()
if err != nil {
t.Errorf("teardown(): %s", err.Error())
}
err = os.RemoveAll(testdir)
if err != nil {
t.Errorf("teardown(): %s", err.Error())
}
}
func createFile(file string, data string, basefolder string) error {
f, err := os.Create(basefolder + "/" + file)
if err != nil {
return err
}
_, err = f.Write([]byte(data))
f.Sync()
f.Close()
return err
}
func createAndCommitFile(t *testing.T, hct *HgClient, testfile string, filecontent string) error {
err := createFile(testfile, filecontent, hct.RepoRoot())
if err != nil {
t.Fatal(err)
}
// add all there is to add to the repo,
_, err = hct.Add(nil, nil)
// commit it
var cmd *exec.Cmd
cmd = exec.Command(hct.HgExe(), "--cwd", hct.RepoRoot(), "commit", "-Am\"first commit\"")
if err = cmd.Run(); err != nil {
t.Fatal(err)
}
return nil
}
func createTempdir(t *testing.T) (string, error) {
tempdir, err := ioutil.TempDir("", "gohg_temp_")
return tempdir, err
}
func destroyTempdir(tempDir string) {
_ = os.RemoveAll(tempDir)
}