-
Notifications
You must be signed in to change notification settings - Fork 4
/
main_test.go
64 lines (49 loc) · 1.34 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
package main
import (
"bytes"
"io/ioutil"
"log"
"os"
"strconv"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestSnagInit(t *testing.T) {
buf := bytes.NewBuffer(nil)
log.SetOutput(buf)
defer log.SetOutput(os.Stdout)
wd, tmpDir := tmpDirectory(t)
defer os.RemoveAll(tmpDir)
chdir(t, tmpDir)
defer os.Chdir(wd)
_, err := os.Stat(SnagFile)
assert.True(t, os.IsNotExist(err))
err = initSnag()
require.NoError(t, err)
assert.Contains(t, buf.String(), "Success")
_, err = os.Stat(SnagFile)
require.NoError(t, err)
err = initSnag()
require.Error(t, err)
assert.Contains(t, err.Error(), "file already exists")
}
func chdir(t *testing.T, path string) {
err := os.Chdir(path)
require.NoError(t, err, "could not change directories")
}
func tmpDirectory(t *testing.T) (string, string) {
wd, err := os.Getwd()
require.NoError(t, err, "could not get working directory")
tmpDir, err := ioutil.TempDir("", strconv.FormatInt(time.Now().UnixNano(), 10))
require.NoError(t, err, "could not create tmp directory")
return wd, tmpDir
}
func writeSnagFile(t *testing.T, content string) {
f, err := os.Create(".snag.yml")
require.NoError(t, err, "could not create snag.yml")
defer f.Close()
_, err = f.WriteString(content)
require.NoError(t, err, "could not write content to snag.yml")
}