github.com/swaros/contxt/module/taskrun@v0.0.0-20240305083542-3dbd4436ac40/testhelper_test.go (about) 1 package taskrun_test 2 3 import ( 4 "os" 5 "testing" 6 7 "github.com/swaros/contxt/module/dirhandle" 8 "github.com/swaros/contxt/module/taskrun" 9 ) 10 11 // caseRunner helps to switch a testrunn in testcase directory to this 12 // this folder. and go back after the test is done 13 // it also resets all variables 14 // the id is just the number of the test/case folder (postfix) 15 func caseRunner(id string, t *testing.T, testFunc func(t *testing.T)) { 16 taskrun.ClearAll() 17 old, derr := dirhandle.Current() 18 if derr != nil { 19 t.Error(derr) 20 } 21 dir := "./../../docs/test/case" + id 22 taskrun.GetLogger().Debug("--- [CR] TESTING FILE " + dir) 23 if err := os.Chdir(dir); err == nil { 24 testFunc(t) 25 os.Chdir(old) 26 } else { 27 t.Error(err) 28 } 29 30 } 31 32 // folderRunner takes a path relative to the current module path (most starts with "./../docs/") 33 // and executes the the function. 34 // if the folder is not valid, a error will be returned instead 35 func folderRunner(folder string, t *testing.T, testFunc func(t *testing.T)) error { 36 taskrun.ClearAll() 37 taskrun.ResetAllTaskInfos() 38 taskrun.InitDefaultVars() 39 old, derr := dirhandle.Current() 40 if derr != nil { 41 t.Error(derr) 42 return derr 43 } 44 if err := os.Chdir(folder); err != nil { 45 t.Error(err) 46 return err 47 } 48 testFunc(t) 49 if err := os.Chdir(old); err != nil { 50 t.Error(err) 51 return err 52 } 53 return nil 54 55 } 56 57 func eqCheckVarMapValue(keyname, jsonPath, expected string, success, fail, notfound func(result string)) { 58 if result, ok := taskrun.GetJSONPathResult(keyname, jsonPath); ok { 59 if result.Str == expected { 60 success(result.Str) 61 } else { 62 fail(result.Str) 63 } 64 } else { 65 notfound(result.Str) 66 } 67 } 68 69 // assertVarStrEquals is testing a contxt variable content against the expected value 70 func assertVarStrEquals(t *testing.T, keyname, expected string) bool { 71 check := clearStrings(taskrun.GetPH(keyname)) 72 73 if check != clearStrings(expected) { 74 t.Error("expected " + expected + " as variable. but got <" + check + ">") 75 return false 76 } 77 return true 78 } 79 80 // assertVarStrEquals is testing a contxt variable content against the expected value 81 func assertVarStrNotEquals(t *testing.T, keyname, unExpected string) bool { 82 check := clearStrings(taskrun.GetPH(keyname)) 83 84 if check == clearStrings(unExpected) { 85 t.Error("unexpected [" + unExpected + "] is present ") 86 return false 87 } 88 return true 89 } 90 91 func assertStringEquals(t *testing.T, actual, expected string) bool { 92 check := clearStrings(actual) 93 94 if check != clearStrings(expected) { 95 t.Error("expected " + expected + " as variable. but got <" + check + ">") 96 return false 97 } 98 return true 99 } 100 101 // assertCaseLogLastEquals tests for a case (docs/tests/case<nr>) if the last output 102 // is equasl to the expected. 103 // depending on the executed targetName 104 func assertCaseLogLastEquals(t *testing.T, caseNr, targetName, expected string) { 105 caseRunner(caseNr, t, func(t *testing.T) { 106 taskrun.RunTargets(targetName, true) 107 log := taskrun.GetPH("RUN." + targetName + ".LOG.LAST") 108 if log != expected { 109 t.Error("the last executed script should be "+expected+". not ", log) 110 } 111 }) 112 } 113 114 func assertTestFolderVarFn(t *testing.T, folder, targetRun string, fn func()) { 115 if runError := folderRunner("./../../docs/test/"+folder, t, func(t *testing.T) { 116 taskrun.RunTargets(targetRun, false) 117 fn() 118 }); runError != nil { 119 t.Error(runError) 120 } 121 }