github.com/swaros/contxt/module/taskrun@v0.0.0-20240305083542-3dbd4436ac40/cnmagic_test.go (about) 1 package taskrun_test 2 3 import ( 4 "fmt" 5 "os" 6 "strconv" 7 "testing" 8 "time" 9 10 "github.com/swaros/contxt/module/configure" 11 "github.com/swaros/contxt/module/taskrun" 12 ) 13 14 func TestDecicePath(t *testing.T) { 15 paths := []string{ 16 "/home/user/project/testing", 17 "/home/user/project/testing/source/backend/server", 18 "/home/user/project/testing/source/backend/server/build", 19 "/home/user/project/testing/source/frontend/website", 20 "/home/user/project/testing/source/frontend/website/build", 21 "/home/user/project/testing/source/toolset/someelse/build", 22 } 23 24 pathsReordered := []string{ 25 "/home/user/project/testing/source/toolset/someelse/build", 26 "/home/user/project/testing/source/frontend/website/build", 27 "/home/user/project/testing/source/backend/server", 28 "/home/user/project/testing/source/frontend/website", 29 "/home/user/project/testing", 30 "/home/user/project/testing/source/backend/server/build", 31 } 32 if decidedPath, ok := taskrun.DecidePath([]string{"website"}, paths); ok { 33 assertStringEquals(t, decidedPath, "/home/user/project/testing/source/frontend/website") 34 } else { 35 t.Error("they shoud find a path.") 36 } 37 38 if decidedPath, ok := taskrun.DecidePath([]string{"website"}, pathsReordered); ok { 39 assertStringEquals(t, decidedPath, "/home/user/project/testing/source/frontend/website") 40 } else { 41 t.Error("they shoud find a path.") 42 } 43 44 if decidedPath, ok := taskrun.DecidePath([]string{"server"}, paths); ok { 45 assertStringEquals(t, decidedPath, "/home/user/project/testing/source/backend/server") 46 } else { 47 t.Error("they shoud find a path.") 48 } 49 50 if decidedPath, ok := taskrun.DecidePath([]string{"server"}, pathsReordered); ok { 51 assertStringEquals(t, decidedPath, "/home/user/project/testing/source/backend/server") 52 } else { 53 t.Error("they shoud find a path.") 54 } 55 // looking for build in booth path slices 56 if decidedPath, ok := taskrun.DecidePath([]string{"build"}, paths); ok { 57 assertStringEquals(t, decidedPath, "/home/user/project/testing/source/frontend/website/build") 58 } else { 59 t.Error("they shoud find a path.") 60 } 61 62 if decidedPath, ok := taskrun.DecidePath([]string{"build"}, pathsReordered); ok { 63 assertStringEquals(t, decidedPath, "/home/user/project/testing/source/frontend/website/build") 64 } else { 65 t.Error("they shoud find a path.") 66 } 67 68 // looking for a specific path by more arguments 69 if decidedPath, ok := taskrun.DecidePath([]string{"web", "build"}, paths); ok { 70 assertStringEquals(t, decidedPath, "/home/user/project/testing/source/frontend/website/build") 71 } else { 72 t.Error("they shoud find a path.") 73 } 74 75 if decidedPath, ok := taskrun.DecidePath([]string{"server", "build"}, paths); ok { 76 assertStringEquals(t, decidedPath, "/home/user/project/testing/source/backend/server/build") 77 } else { 78 t.Error("they shoud find a path.") 79 } 80 81 if decidedPath, ok := taskrun.DecidePath([]string{"build", "server"}, paths); ok { 82 assertStringEquals(t, decidedPath, "/home/user/project/testing/source/backend/server/build") 83 } else { 84 t.Error("they shoud find a path.") 85 } 86 87 if decidedPath, ok := taskrun.DecidePath([]string{"build", "tool"}, paths); ok { 88 assertStringEquals(t, decidedPath, "/home/user/project/testing/source/toolset/someelse/build") 89 } else { 90 t.Error("they shoud find a path.") 91 } 92 93 if decidedPath, ok := taskrun.DecidePath([]string{"noin", "eventhis"}, paths); ok { 94 t.Error("is is impossible to find something that matches with the arguments") 95 } else { 96 assertStringEquals(t, decidedPath, ".") 97 } 98 } 99 100 func TestCnHandle(t *testing.T) { 101 // setup the temp folder for the test 102 rendomTimeBasedName := fmt.Sprintf("test-%d", time.Now().UnixNano()) 103 configure.USE_SPECIAL_DIR = false 104 configure.CONTEXT_DIR = "temp" 105 configure.CONTXT_FILE = rendomTimeBasedName + "fake_contxt.yml" 106 configure.MIGRATION_ENABLED = false 107 108 err := os.MkdirAll(configure.CONTEXT_DIR, 0777) 109 if err != nil { 110 t.Error(err) 111 } 112 defer os.RemoveAll(configure.CONTEXT_DIR) 113 114 // create some dirs 115 createTestDirs := []string{"project1", "project2", "project3", "project4"} 116 createDirsInProject := []string{"role1", "role2", "role3", "role4", "frontend/build", "backend/build", "toolset/assets/build"} 117 118 for _, project := range createTestDirs { 119 for _, role := range createDirsInProject { 120 err = os.MkdirAll(configure.CONTEXT_DIR+"/"+project+"/"+role, 0777) 121 if err != nil { 122 t.Error(err) 123 } 124 } 125 } 126 127 // init the config 128 conf := configure.NewContxtConfig() 129 // add the projects 130 for id, project := range createTestDirs { 131 strId := strconv.Itoa(id) 132 if cerr := conf.AddWorkSpace("project_"+strId, func(s string) bool { return true }, func(s string) {}); cerr != nil { 133 t.Error(cerr) 134 } else { 135 // add the paths 136 for _, role := range createDirsInProject { 137 if err := conf.AddPath(configure.CONTEXT_DIR + "/" + project + "/" + role); err != nil { 138 t.Error(err) 139 } 140 } 141 } 142 } 143 if cerr := conf.SaveConfiguration(); cerr != nil { 144 t.Error(cerr) 145 } 146 147 // change the workspace to project 0 148 berr := conf.ChangeWorkspace("project_0", func(s1 string) bool { 149 return true 150 }, func(origin string) { 151 if origin != "project_0" { 152 t.Error("unexpected workspace", origin) 153 } 154 }) 155 if berr != nil { 156 t.Error(berr) 157 } 158 159 // check the paths in a loop with the DirFind function and using a slice of strings 160 // the first element is the path to find and the second is the expected result 161 // the result is the path relative to the project 162 // the first project is project1 (id -1) 163 // the second project is project2 (id 0) 164 // the third project is project3 (id 1) 165 // the fourth project is project4 (id 2) 166 167 sliceList := [][]string{ 168 {"le1", "temp/project1/role1"}, 169 {"le2", "temp/project1/role2"}, 170 {"le3", "temp/project1/role3"}, 171 {"le4", "temp/project1/role4"}, 172 {"frontend", "temp/project1/frontend/build"}, 173 {"backend", "temp/project1/backend/build"}, 174 {"toolset", "temp/project1/toolset/assets/build"}, 175 } 176 177 for _, slice := range sliceList { 178 check := taskrun.DirFind([]string{slice[0]}) 179 if check != slice[1] { 180 t.Error("DirFind failed. got: " + check + " expected: " + slice[1]) 181 } 182 } 183 184 check := taskrun.DirFind([]string{"le2"}) 185 if check != "temp/project1/role2" { // the path for project 0 is project1 (id -1) 186 t.Error("DirFind failed. got: " + check) 187 } 188 189 check = taskrun.DirFind([]string{"build"}) 190 if check != "temp/project1/toolset/assets/build" { // the path should used where the search word should stay at least in the path 191 t.Error("DirFind failed. got: " + check) 192 } 193 194 check = taskrun.DirFind([]string{"back", "build"}) 195 if check != "temp/project1/backend/build" { // backend/build is the only one they matches to the search words 196 t.Error("DirFind failed. got: " + check) 197 } 198 199 check = taskrun.DirFind([]string{"2"}) 200 if check != "temp/project1/role3" { // backend/build is the only one they matches to the search words 201 t.Error("DirFind failed. got: " + check) 202 } 203 }