This repository has been archived by the owner on Jul 14, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpackage_test.go
74 lines (57 loc) · 1.9 KB
/
package_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
package main
import (
. "github.com/smartystreets/goconvey/convey"
"os"
"testing"
"os/exec"
"io/ioutil"
"bytes"
)
func RunCommand(name string, arg ...string) (bytes.Buffer, bytes.Buffer, error) {
cmd := exec.Command(name, arg...)
var stdout bytes.Buffer
var stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
err := cmd.Run()
return stdout, stderr, err
}
const PackageName = "summon-file"
func TestPackage(t *testing.T) {
Convey("Given a compiled summon-file package", t, func() {
Convey("Given summon-file is run with no arguments", func() {
_, stderr, err := RunCommand(PackageName)
Convey("Returns with error", func() {
So(err, ShouldNotBeNil)
So(stderr.String(), ShouldEqual, "A variable identifier must be given as the first and only argument!")
})
})
Convey("Given summon-file is run with too many arguments", func() {
_, stderr, err := RunCommand(PackageName, "one", "two")
Convey("Returns with error", func() {
So(err, ShouldNotBeNil)
So(stderr.String(), ShouldEqual, "A variable identifier must be given as the first and only argument!")
})
})
Convey("Given summon-file is run with an existent file path", func() {
content := []byte("existent-file-content")
tmpfile, err := ioutil.TempFile("", "existent-file")
tmpfileName := tmpfile.Name()
defer os.Remove(tmpfileName)
tmpfile.Write(content)
tmpfile.Close()
stdout, _, err := RunCommand(PackageName, tmpfileName)
Convey("Prints the contents of the file on stdout", func() {
So(err, ShouldBeNil)
So(stdout.String(), ShouldEqual, "existent-file-content")
})
})
Convey("Given summon-file is run with a non-existent file path", func() {
_, stderr, err := RunCommand(PackageName, "non-existent-file")
Convey("Returns with error", func() {
So(err, ShouldNotBeNil)
So(stderr.String(), ShouldContainSubstring, "no such file or directory")
})
})
})
}