-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
main_test.go
70 lines (52 loc) · 1.38 KB
/
main_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
package main
import (
"bytes"
"fmt"
"io"
"os"
"testing"
"github.com/fatih/color"
"github.com/stretchr/testify/assert"
)
func Test_DeletingBranchesWhenDryRunOptionIsFalse(t *testing.T) {
onlyCI(t)
results := captureOutput(func() { runMain(false, false) })
expected := fmt.Sprintf("%s %s", green("✔"), "Deleting branches...")
assert.Contains(t, results, expected)
}
func Test_DoNotDeleteBranchesWhenDryRunOptionIsTrue(t *testing.T) {
onlyCI(t)
results := captureOutput(func() { runMain(true, false) })
expected := fmt.Sprintf("%s %s", hiBlack("-"), "Deleting branches...")
assert.Contains(t, results, expected)
}
func Test_ProtectAndUnprotect(t *testing.T) {
onlyCI(t)
runProtect([]string{"main"}, false)
protectResults := captureOutput(func() { runMain(true, false) })
expected := fmt.Sprintf("main %s", hiBlack("[protected]"))
assert.Contains(t, protectResults, expected)
runUnprotect([]string{"main"}, false)
unprotectResults := captureOutput(func() { runMain(true, false) })
assert.NotContains(t, unprotectResults, expected)
}
func onlyCI(t *testing.T) {
if os.Getenv("CI") == "" {
t.Skip("skipping test in local")
}
os.Chdir("ci-test")
}
func captureOutput(f func()) string {
org := os.Stdout
defer func() {
os.Stdout = org
}()
r, w, _ := os.Pipe()
os.Stdout = w
color.Output = w
f()
w.Close()
var buf bytes.Buffer
io.Copy(&buf, r)
return buf.String()
}