github.com/Iosif02/cli@v1.0.1/api/create_test.go (about) 1 package api 2 3 import ( 4 "fmt" 5 "io/ioutil" 6 "os" 7 "path/filepath" 8 "testing" 9 10 "github.com/stretchr/testify/assert" 11 ) 12 13 var jsonString = `{ 14 "name": "_APP_NAME_", 15 "type": "flogo:app", 16 "version": "0.0.1", 17 "description": "My flogo application description", 18 "appModel": "1.0.0", 19 "imports": [ 20 "github.com/TIBCOSoftware/flogo-contrib/activity/log", 21 "github.com/Iosif02/contrib/trigger/rest", 22 "github.com/Iosif02/flow" 23 ], 24 "triggers": [ 25 { 26 "id": "my_rest_trigger", 27 "type": "rest", 28 "settings": { 29 "port": "8888" 30 }, 31 "handlers": [ 32 { 33 "settings": { 34 "method": "GET", 35 "path": "/test/:val" 36 }, 37 "actions": [ 38 { 39 "type": "flow", 40 "settings": { 41 "flowURI": "res://flow:simple_flow" 42 }, 43 "input": { 44 "in": "=$.pathParams.val" 45 } 46 } 47 ] 48 } 49 ] 50 } 51 ], 52 "resources": [ 53 { 54 "id": "flow:simple_flow", 55 "data": { 56 "name": "simple_flow", 57 "metadata": { 58 "input": [ 59 { "name": "in", "type": "string", "value": "test" } 60 ], 61 "output": [ 62 { "name": "out", "type": "string" } 63 ] 64 }, 65 "tasks": [ 66 { 67 "id": "log", 68 "name": "Log Message", 69 "activity": { 70 "type": "log", 71 "input": { 72 "message": "=$flow.in", 73 "flowInfo": "false", 74 "addToFlow": "false" 75 } 76 } 77 } 78 ], 79 "links": [] 80 } 81 } 82 ] 83 } 84 ` 85 86 type TestEnv struct { 87 currentDir string 88 } 89 90 func (t *TestEnv) getTestwd() (dir string, err error) { 91 return t.currentDir, nil 92 } 93 94 func (t *TestEnv) cleanup() { 95 96 os.RemoveAll(t.currentDir) 97 } 98 99 func TestCmdCreate_noflag(t *testing.T) { 100 t.Log("Testing simple creation of project") 101 102 tempDir, _ := GetTempDir() 103 testEnv := &TestEnv{currentDir: tempDir} 104 105 defer testEnv.cleanup() 106 107 t.Logf("Current dir '%s'", testEnv.currentDir) 108 _, err := CreateProject(testEnv.currentDir, "myApp", "", "") 109 assert.Equal(t, nil, err) 110 111 _, err = os.Stat(filepath.Join(tempDir, "myApp", "src", "go.mod")) 112 113 assert.Equal(t, nil, err) 114 _, err = os.Stat(filepath.Join(tempDir, "myApp", "flogo.json")) 115 116 assert.Equal(t, nil, err) 117 118 _, err = os.Stat(filepath.Join(tempDir, "myApp", "src", "main.go")) 119 assert.Equal(t, nil, err) 120 } 121 122 func TestCmdCreate_flag(t *testing.T) { 123 t.Log("Testing creation of project while the file is provided") 124 125 tempDir, err := ioutil.TempDir("", "test") 126 if err != nil { 127 t.Fatal(err) 128 } 129 130 testEnv := &TestEnv{currentDir: tempDir} 131 132 defer testEnv.cleanup() 133 134 t.Logf("Current dir '%s'", testEnv.currentDir) 135 os.Chdir(testEnv.currentDir) 136 file, err := os.Create("flogo.json") 137 if err != nil { 138 t.Fatal(err) 139 assert.Equal(t, true, false) 140 } 141 defer file.Close() 142 fmt.Fprintf(file, jsonString) 143 _, err = CreateProject(testEnv.currentDir, "flogo", "flogo.json", "") 144 assert.Equal(t, nil, err) 145 146 _, err = os.Stat(filepath.Join(tempDir, "flogo", "src", "go.mod")) 147 148 assert.Equal(t, nil, err) 149 _, err = os.Stat(filepath.Join(tempDir, "flogo", "flogo.json")) 150 151 assert.Equal(t, nil, err) 152 153 _, err = os.Stat(filepath.Join(tempDir, "flogo", "src", "main.go")) 154 assert.Equal(t, nil, err) 155 } 156 157 func TestCmdCreate_masterCore(t *testing.T) { 158 t.Log("Testing creation of project when the version of core is provided `master`") 159 160 tempDir, err := ioutil.TempDir("", "test") 161 if err != nil { 162 t.Fatal(err) 163 } 164 165 testEnv := &TestEnv{currentDir: tempDir} 166 167 defer testEnv.cleanup() 168 169 t.Logf("Current dir '%s'", testEnv.currentDir) 170 os.Chdir(testEnv.currentDir) 171 172 _, err = CreateProject(testEnv.currentDir, "myApp", "", "master") 173 assert.Equal(t, nil, err) 174 } 175 176 //todo fix this test, unreliable 177 //func TestCmdCreate_versionCore(t *testing.T) { 178 // t.Log("Testing creation of project when the version of core is provided `v0.9.0-alpha.4`") 179 // 180 // tempDir, err := ioutil.TempDir("", "test") 181 // if err != nil { 182 // t.Fatal(err) 183 // } 184 // 185 // testEnv := &TestEnv{currentDir: tempDir} 186 // 187 // defer testEnv.cleanup() 188 // 189 // t.Logf("Current dir '%s'", testEnv.currentDir) 190 // os.Chdir(testEnv.currentDir) 191 // 192 // _, err = CreateProject(testEnv.currentDir, "myApp", "", "v0.9.0-alpha.4") 193 // assert.Equal(t, nil, err) 194 // 195 // _, err = os.Stat(filepath.Join(tempDir, "myApp", "src", "go.mod")) 196 // 197 // assert.Equal(t, nil, err) 198 // _, err = os.Stat(filepath.Join(tempDir, "myApp", "flogo.json")) 199 // 200 // assert.Equal(t, nil, err) 201 // 202 // _, err = os.Stat(filepath.Join(tempDir, "myApp", "src", "main.go")) 203 // assert.Equal(t, nil, err) 204 // 205 // data, err1 := ioutil.ReadFile(filepath.Join(tempDir, "myApp", "src", "go.mod")) 206 // assert.Equal(t, nil, err1) 207 // 208 // //todo fix, not a reliable test giving that importing latest of flow which will affect this import 209 // assert.Equal(t, true, strings.Contains(string(data), "v0.9.0-alpha.4")) 210 // fmt.Println(string(data)) 211 // 212 // appProject := NewAppProject(filepath.Join(testEnv.currentDir, "myApp")) 213 // 214 // err = appProject.Validate() 215 // assert.Nil(t, err) 216 // 217 // common.SetCurrentProject(appProject) 218 // 219 // err = BuildProject(common.CurrentProject(), BuildOptions{}) 220 // assert.Nil(t, err) 221 //}