-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathebox_test.go
128 lines (102 loc) · 3.42 KB
/
ebox_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
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
* Copyright (c) 2018 lærling
* For details see ./LICENSE
*/
package main
import (
"fmt"
"os"
"os/exec"
"os/user"
"strconv"
"strings"
"testing"
)
const programName string = "ebox"
// N as a suffix for a variable or a constant stands for Negative
const distroName string = "ohai-emacs"
const distroNameN string = "nonexistent"
const remoteUser string = "bodil"
const remoteUserN string = "nonexistent"
var distroDir string
var distroDirN string
func TestMain(m *testing.M) {
// set global variables
usr, err := user.Current()
if err != nil {
panic("Error getting current user: " + err.Error())
}
distroDir = usr.HomeDir + PATHSEP + "emacs" + PATHSEP + distroName
distroDirN = usr.HomeDir + PATHSEP + "emacs" + PATHSEP + distroNameN
// run tests
exitState := m.Run()
// clean up
if err := ensureDirectoryExistsNot(distroDirN); err != nil {
fmt.Fprintln(os.Stderr, "Cannot delete directory "+distroDirN)
os.Exit(1)
}
// exit
os.Exit(exitState)
}
// runs main and checks if distroDir exists
func runMain(t *testing.T, expectSuccess, expectDistroDir bool, distroName, arg string) {
fmt.Println("Running with argument '" + arg + "'")
// make sure distro for positive tests does not exist
t.Log("Making sure distribution directory does not exist: '" + distroDir + "'")
if err := ensureDirectoryExistsNot(distroDir); err != nil {
t.Fatal("Cannot remove distribution " + distroName + ": " + err.Error())
}
// make sure distro for negative tests does not exist
t.Log("Making sure distribution directory does not exist: '" + distroDirN + "'")
if err := ensureDirectoryExistsNot(distroDirN); err != nil {
t.Fatal("Cannot remove distribution " + distroNameN + ": " + err.Error())
}
// set command line argument
os.Args = []string{"", arg}
t.Log("Program arguments: '" + strings.Join(os.Args, "', '") + "'")
t.Log("Expect distribution directory to exist after run: " + strconv.FormatBool(expectDistroDir))
// run program
cmd := exec.Command(programName, arg)
err := cmd.Run()
t.Log("Program finished")
if cmd.ProcessState.Success() != expectSuccess {
t.Fatal("Wrong exit status: ", err)
}
// check that distro dir exists
_, err = os.Stat(distroDir)
distroDirExists := !os.IsNotExist(err)
if distroDirExists != expectDistroDir {
t.Error("Distribution directory exists, expected " +
strconv.FormatBool(expectDistroDir) + ", found " +
strconv.FormatBool(distroDirExists) + ".")
}
}
/*
* negative tests
*/
// nonexisting repo of the form 'nonexistent'
func TestDownloadNameN(t *testing.T) {
runMain(t, false, false, distroNameN, distroNameN)
}
// there is no test for the remote 'nonexisting/nonexisting', because git would just assume
// the repo to be a private one on Github, thus asking for a username and password
// nonexisting remote of the form 'domain.tld/foo'
func TestDownloadDomainN(t *testing.T) {
runMain(t, false, false, distroNameN, "domain.tld/"+remoteUserN+"/"+distroNameN)
}
/*
* positive tests
* the positive tests run last, because I want to keep the distro
*/
// existing repo of the form foo
func TestDownloadName(t *testing.T) {
runMain(t, true, true, distroName, distroName)
}
// existing remote of the form foo/bar
func TestDownloadSlash(t *testing.T) {
runMain(t, true, true, distroName, remoteUser+"/"+distroName)
}
// existing remote of the form 'domain.tld/foo'
func TestDownloadDomain(t *testing.T) {
runMain(t, true, true, distroName, "github.com/"+remoteUser+"/"+distroName)
}