github.com/informationsea/shellflow@v0.1.3/shellflow_shelltask_test.go (about) 1 package main 2 3 import ( 4 "reflect" 5 "testing" 6 7 "github.com/informationsea/shellflow/flowscript" 8 ) 9 10 func TestShellTask(t *testing.T) { 11 builder, err := NewShellTaskBuilder() 12 if err != nil { 13 t.Fatalf("error: %s", err.Error()) 14 } 15 16 { 17 shellTask, err := builder.CreateShellTask(1, "java float command") 18 if err != nil { 19 t.Fatalf("Failed to create shell task %s", err.Error()) 20 } 21 22 if !reflect.DeepEqual(shellTask, &ShellTask{ 23 LineNum: 1, 24 ID: 1, 25 ShellScript: "java float command", 26 DependentFiles: flowscript.NewStringSet(), 27 CreatingFiles: flowscript.NewStringSet(), 28 DependentTaskID: []int{}, 29 CommandConfiguration: CommandConfiguration{RegExp: "java .*", SGEOption: []string{"-l", "s_vmem=40G,mem_req=40G"}}, 30 }) { 31 t.Fatalf("Invalid shell task: %s", shellTask) 32 } 33 } 34 35 if len(builder.Tasks) != 1 { 36 t.Fatalf("Invalid missing task list: %s", builder.Tasks) 37 } 38 if !reflect.DeepEqual(builder.MissingCreatorFiles.Array(), []string{}) { 39 t.Fatalf("Invalid missing creator files: %s", builder.MissingCreatorFiles.Array()) 40 } 41 42 { 43 shellTask, err := builder.CreateShellTask(2, "mid1 ((input1)) [[middle1]]") 44 if err != nil { 45 t.Fatalf("Failed to create shell task %s", err.Error()) 46 } 47 48 expectedx := &ShellTask{ 49 LineNum: 2, 50 ID: 2, 51 ShellScript: "mid1 input1 middle1", 52 DependentFiles: flowscript.NewStringSetWithValues("input1"), 53 CreatingFiles: flowscript.NewStringSetWithValues("middle1"), 54 DependentTaskID: []int{}, 55 CommandConfiguration: CommandConfiguration{SGEOption: []string{}}, 56 } 57 58 if !reflect.DeepEqual(shellTask, expectedx) { 59 t.Fatalf("Invalid shell task: %s / expected: %s / %t, %t, %t, %t", shellTask, expectedx, shellTask.ShellScript == expectedx.ShellScript, reflect.DeepEqual(shellTask.DependentFiles, expectedx.DependentFiles), reflect.DeepEqual(shellTask.CreatingFiles, expectedx.CreatingFiles), reflect.DeepEqual(shellTask.DependentTaskID, expectedx.DependentTaskID)) 60 } 61 } 62 63 if len(builder.Tasks) != 2 { 64 t.Fatalf("Invalid missing task list: %s", builder.Tasks) 65 } 66 if !reflect.DeepEqual(builder.MissingCreatorFiles.Array(), []string{"input1"}) { 67 t.Fatalf("Invalid missing creator files: %s", builder.MissingCreatorFiles.Array()) 68 } 69 70 { 71 shellTask, err := builder.CreateShellTask(5, "mid2 ((input1)) ((middle1)) [[middle2]]") 72 if err != nil { 73 t.Fatalf("Failed to create shell task %s", err.Error()) 74 } 75 76 if !reflect.DeepEqual(shellTask, &ShellTask{ 77 LineNum: 5, 78 ID: 3, 79 ShellScript: "mid2 input1 middle1 middle2", 80 DependentFiles: flowscript.NewStringSetWithValues("input1", "middle1"), 81 CreatingFiles: flowscript.NewStringSetWithValues("middle2"), 82 DependentTaskID: []int{2}, 83 CommandConfiguration: CommandConfiguration{SGEOption: []string{}}, 84 }) { 85 t.Fatalf("Invalid shell task: %s", shellTask) 86 } 87 } 88 89 if len(builder.Tasks) != 3 { 90 t.Fatalf("Invalid missing task list: %s", builder.Tasks) 91 } 92 if !reflect.DeepEqual(builder.MissingCreatorFiles.Array(), []string{"input1"}) { 93 t.Fatalf("Invalid missing creator files: %s", builder.MissingCreatorFiles.Array()) 94 } 95 96 { 97 shellTask, err := builder.CreateShellTask(10, "mid3 ((input2)) ((middle1)) ((middle2)) [[output]]") 98 if err != nil { 99 t.Fatalf("Failed to create shell task %s", err.Error()) 100 } 101 102 if !reflect.DeepEqual(shellTask, &ShellTask{ 103 LineNum: 10, 104 ID: 4, 105 ShellScript: "mid3 input2 middle1 middle2 output", 106 DependentFiles: flowscript.NewStringSetWithValues("input2", "middle1", "middle2"), 107 CreatingFiles: flowscript.NewStringSetWithValues("output"), 108 DependentTaskID: []int{2, 3}, 109 CommandConfiguration: CommandConfiguration{SGEOption: []string{}}, 110 }) { 111 t.Fatalf("Invalid shell task: %s", shellTask) 112 } 113 114 } 115 116 if s := builder.CreateDag(); s != `digraph shelltask { 117 node [shape=box]; 118 task1 [label="java float command"]; 119 task2 [label="mid1 input1 middle1"]; 120 task3 [label="mid2 input1 middle1 middle2"]; 121 task4 [label="mid3 input2 middle1 middle2 output"]; 122 input0 [label="input1", color=red]; 123 input0 -> task2; 124 input0 -> task3; 125 input1 [label="input2", color=red]; 126 input1 -> task4; 127 task2 -> task3 [label="middle1"]; 128 task2 -> task4 [label="middle1"]; 129 task3 -> task4 [label="middle2"]; 130 output1 [label="output", color=blue]; 131 task4 -> output1; 132 } 133 ` { 134 t.Fatalf("bad dag: %s", s) 135 } 136 137 if _, err := builder.CreateShellTask(1, "echo hello, ((world"); err == nil || err.Error() != "Closing bracket is not found: ))" { 138 t.Fatalf("Invalid error: %s", err) 139 } 140 141 if _, err := builder.CreateShellTask(1, "echo hello, [[world"); err == nil || err.Error() != "Closing bracket is not found: ]]" { 142 t.Fatalf("Invalid error: %s", err) 143 } 144 145 if _, err := builder.CreateShellTask(1, "echo ((hello)., ((world"); err == nil || err.Error() != "Closing bracket is not found: ))" { 146 t.Fatalf("Invalid error: %s", err) 147 } 148 149 if _, err := builder.CreateShellTask(1, "echo [[hello]], [[world"); err == nil || err.Error() != "Closing bracket is not found: ]]" { 150 t.Fatalf("Invalid error: %s", err) 151 } 152 }