-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathutil_test.go
More file actions
99 lines (85 loc) · 2.34 KB
/
Copy pathutil_test.go
File metadata and controls
99 lines (85 loc) · 2.34 KB
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
package sshooks
import (
"io/ioutil"
"os"
"testing"
)
type TestDataUIntToStr struct {
in uint
out string
}
func TestUIntToStr(t *testing.T) {
tests := []TestDataUIntToStr{
{0, "0"},
{10, "10"},
{65535, "65535"},
}
for i, test := range tests {
actual := UIntToStr(test.in)
if test.out != actual {
t.Errorf("#%d: UIntToStr(%d)=%s; expected %s", i, test.in, actual, test.out)
}
}
}
type TestDataFileExists struct {
in string
out bool
}
func TestFileExists(t *testing.T) {
existingDir, err := ioutil.TempDir("", "tempdir")
if err != nil {
t.Errorf("Couldn't create temp directory: %s. Tests not run", existingDir)
}
existingFile, err := ioutil.TempFile("", "tempfile")
if err != nil {
t.Errorf("Couldn't create temp file: %s. Tests not run", existingFile.Name())
}
defer os.RemoveAll(existingDir)
defer os.Remove(existingFile.Name())
tests := []TestDataFileExists{
{"missing_directory", false},
{existingDir, true},
{"missing_file", false},
{existingFile.Name(), true},
}
for i, test := range tests {
actual := FileExists(test.in)
if test.out != actual {
t.Errorf("#%d: FileExists(%s)=%t; expected %t", i, test.in, actual, test.out)
}
}
}
type TestDataExecCmd struct {
in string
inArgs []string
stdout string
stderr string
err string
}
func TestExecCmd(t *testing.T) {
failScript := []byte("#!/bin/sh\n" +
"echo my awesome error 1>&2\n" +
"exit 1\n")
err := ioutil.WriteFile("failScript.sh", failScript, 0777)
defer os.Remove("failScript.sh")
if err != nil {
t.Error("Couldn't create file needed by tests: failScript.sh")
}
tests := []TestDataExecCmd{
{"", []string{}, "", "", "fork/exec : no such file or directory"},
{"echo", []string{"hello", "you"}, "hello you\n", "", ""},
{"sh", []string{"failScript.sh"}, "", "my awesome error\n", "exit status 1"},
}
for i, test := range tests {
stdout, stderr, err := ExecCmd(test.in, test.inArgs...)
if test.stdout != stdout {
t.Errorf("#%d: stdout, _, _ := ExecCmd(%s) == %s; expected %s", i, test.in, stdout, test.stdout)
}
if test.stderr != stderr {
t.Errorf("#%d: _, stderr, _ := ExecCmd(%s) == %s; expected %s", i, test.in, stderr, test.stderr)
}
if (err == nil && test.err != "") || (err != nil && err.Error() != test.err) {
t.Errorf("#%d: _, _, err := ExecCmd(%s) == %v; expected %v", i, test.in, err, test.err)
}
}
}