-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdigital_output_test.go
108 lines (96 loc) · 2.36 KB
/
digital_output_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
package unipitt
import (
"bytes"
"io/ioutil"
"os"
"path/filepath"
"testing"
)
func TestNewDigitalOutputWriter(t *testing.T) {
folder := "foo/bar"
d := NewDigitalOutputWriter(folder)
if d.Name != "bar" {
t.Fatalf("Expected name %s, got %s\n", "bar", d.Name)
}
if d.Path != folder {
t.Fatalf("Expected path %s, got %s\n", folder, d.Path)
}
}
func TestUpdateDigitalOutputWriter(t *testing.T) {
folder := "do_2_01"
// Create temporary folder, only if it does not exist already
sysFsRoot, err := ioutil.TempDir("", "unipitt")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(sysFsRoot)
doFolder := filepath.Join(sysFsRoot, folder)
if _, pathErr := os.Stat(doFolder); pathErr != nil {
err := os.Mkdir(doFolder, os.ModePerm)
if err != nil {
t.Fatal(err)
}
}
fname := filepath.Join(doFolder, DoFilename)
d := NewDigitalOutputWriter(doFolder)
cases := []struct {
Given bool
Expected string
}{
{Given: false, Expected: DoFalseValue},
{Given: true, Expected: DoTrueValue},
}
for _, testCase := range cases {
err := d.Update(testCase.Given)
if err != nil {
t.Fatal(err)
}
f, err := os.Open(fname)
if err != nil {
t.Fatal(err)
}
b := make([]byte, 2)
f.Read(b)
if !bytes.Equal(b, []byte(testCase.Expected)) {
t.Fatalf("Expected %s, got %s\n", testCase.Expected, string(b))
}
}
}
func TestUpdateDigitalOutputWriterBogusFolder(t *testing.T) {
folder := "/foo/bar"
d := NewDigitalOutputWriter(folder)
err := d.Update(false)
if err == nil {
t.Fatal("Expected an error, found none")
}
}
func TestFindDigitalOutputWriters(t *testing.T) {
folder := "do_2_01"
// Create temporary folder, only if it does not exist already
sysFsRoot, err := ioutil.TempDir("", "unipitt")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(sysFsRoot)
doFolder := filepath.Join(sysFsRoot, folder)
if _, pathErr := os.Stat(doFolder); pathErr != nil {
err := os.Mkdir(doFolder, os.ModePerm)
if err != nil {
t.Fatal(err)
}
}
writerMap, err := FindDigitalOutputWriters(sysFsRoot)
if err != nil {
t.Fatal(err)
}
if writer, ok := writerMap[folder]; !ok {
t.Fatalf("Expected to find writer with name %s in map for name %s\n", writer.Name, folder)
}
}
func TestFindDigitalOutputWritersNoFolder(t *testing.T) {
folder := "foo"
_, err := FindDigitalOutputWriters(folder)
if err == nil {
t.Fatal("Expected no mapping to be found")
}
}