-
Notifications
You must be signed in to change notification settings - Fork 72
/
process_test.go
152 lines (126 loc) · 4.65 KB
/
process_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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package scipipe
import (
"io/ioutil"
"os"
"strconv"
"testing"
)
func TestNewProc(t *testing.T) {
wf := NewWorkflow("test_wf", 16)
p1 := NewProc(wf, "echo", "echo {p:text}")
if p1.InParam("text") == nil {
t.Error(`p.InParam("text") = nil. want: not nil`)
}
p2 := NewProc(wf, "cat", "cat {i:infile} > {o:outfile}")
if p2.In("infile") == nil {
t.Error(`p.In("infile") = nil. want: not nil`)
}
if p2.Out("outfile") == nil {
t.Error(`p.Out("outfile") = nil. want: not nil`)
}
}
func TestSetOut(t *testing.T) {
wf := NewWorkflow("test_wf", 16)
p := wf.NewProc("cat_foo", "cat {i:foo} > {o:bar} # {p:p1}")
p.InParam("p1").FromStr("p1val")
newIP, err := NewFileIP("data/foo.txt")
Check(err)
mockTask := NewTask(wf, p, "echo_foo_task", "", map[string]*FileIP{"foo": newIP},
nil, nil, map[string]string{"p1": "p1val"}, nil, "", nil, 1)
inputsAndOutputs := map[string]string{
"{i:foo}": "data/foo.txt",
"{i:foo}.bar.{p:p1}.txt": "data/foo.txt.bar.p1val.txt",
"{i:foo|s/.txt//}_bar.txt": "data/foo_bar.txt",
"{i:foo|s/.txt/.bar/}.txt": "data/foo.bar.txt",
"{i:foo|%.txt}.bar.txt": "data/foo.bar.txt", // Bash style strip from end of string
"{i:foo|%oo.txt}.txt": "data/f.txt", // Bash style strip from end of string
"{i:foo|basename}": "foo.txt",
}
for pathPattern, expectedPath := range inputsAndOutputs {
// Set a path format for the "bar" out-port
p.SetOut("bar", pathPattern)
// Check that the result from running the configured path func (stored in
// p.PathFuncs) is as expected
actualPath := p.PathFuncs["bar"](mockTask)
if actualPath != expectedPath {
t.Errorf(`Wrong path in SetOut. Got:%v Expected:%v`, actualPath, expectedPath)
}
}
}
func TestDefaultPattern(t *testing.T) {
wf := NewWorkflow("test_wf", 16)
p := wf.NewProc("cat_foo", "cat {i:foo} > {o:bar|.txt} # {p:p1}")
p.InParam("p1").FromStr("p1val")
newIP, err := NewFileIP("foo.txt")
Check(err)
mockTask := NewTask(wf, p, "echo_foo_task", "", map[string]*FileIP{"foo": newIP}, nil, nil, map[string]string{"p1": "p1val"}, nil, "", nil, 1)
// We expact a filename on the form: input filename . procname . paramname _ val . outport . extension
expected := "foo.txt.cat_foo.p1_p1val.bar.txt"
if p.PathFuncs["bar"](mockTask) != expected {
t.Errorf(`Did not get expected path in SetOut. Got:%v Expected:%v`, p.PathFuncs["bar"](mockTask), expected)
}
}
func TestDontCreatePortInShellCommand(t *testing.T) {
wf := NewWorkflow("test_wf", 4)
ef := wf.NewProc("echo_foo", "echo foo > .tmp/foo.txt")
ef.SetOut("foo", ".tmp/foo.txt")
cf := wf.NewProc("cat_foo", "cat {i:foo} > {o:footoo}")
cf.In("foo").From(ef.Out("foo"))
cf.SetOut("footoo", ".tmp/footoo.txt")
wf.Run()
fileName := ".tmp/footoo.txt"
f, openErr := os.Open(fileName)
if openErr != nil {
t.Errorf("Could not open file: %s\n", fileName)
}
b, readErr := ioutil.ReadAll(f)
if readErr != nil {
t.Errorf("Could not read file: %s\n", fileName)
}
expected := "foo\n"
if string(b) != expected {
t.Errorf("File %s did not contain %s as expected, but %s\n", fileName, expected, string(b))
}
cleanFiles(".tmp/foo.txt", ".tmp/footoo.txt")
}
func TestProcTaskBuffering(t *testing.T) {
// An attempt to test the issue in #80.
numbers := []string{}
for i := 0; i < getBufsize()+1; i++ {
numbers = append(numbers, strconv.Itoa(i))
}
// Since it's dependent on map iteration order, we run the test
// multiple times in an attempt to trigger.
for range [10]int{} {
wf := NewWorkflow("mwe", 4)
nSource := NewParamSource(wf, "numbers", numbers...)
// Make files from params
stuffMaker := wf.NewProc("make_stuff", "echo testing_{p:number} > {o:out}")
stuffMaker.InParam("number").From(nSource.Out())
stuffMaker.SetOut("out", "out/{p:number}.txt")
// Second Step
second := wf.NewProc("second_step", "cat {i:in} > {o:out}")
second.In("in").From(stuffMaker.Out("out"))
second.SetOut("out", "out/second_{i:in|basename}")
// Dummy Consumer
echo := wf.NewProc("echo", "echo {i:first} {i:second}")
echo.In("first").From(stuffMaker.Out("out"))
echo.In("second").From(second.Out("out"))
wf.Run()
}
}
func TestInitPortsFromCommandPattern(t *testing.T) {
wf := NewWorkflow("testwf", 4)
p1 := wf.NewProc("p1", "echo hej > {o:out}")
p2 := wf.NewProc("p2", "echo {i:in} > {o:out} && cat {i:in} >> {o:out}")
p2.In("in").From(p1.Out("out"))
// The above should not crash
}
func TestEnsureFailOnSamePortNameMultipleTypes(t *testing.T) {
ensureFailsProgram("TestEnsureFailOnSamePortNameMultipleTypes", func() {
wf := NewWorkflow("testwf", 4)
p1 := wf.NewProc("p1", "echo hej > {o:out}")
p2 := wf.NewProc("p2", "cat {i:foo} > {o:foo}")
p2.In("foo").From(p1.Out("out"))
}, t)
}