toTestCase converts *TCase to *TestCase
()
| 139 | |
| 140 | // toTestCase converts *TCase to *TestCase |
| 141 | func (tc *TCase) toTestCase() (*TestCase, error) { |
| 142 | testCase := &TestCase{ |
| 143 | Config: tc.Config, |
| 144 | } |
| 145 | |
| 146 | err := tc.MakeCompat() |
| 147 | if err != nil { |
| 148 | return nil, err |
| 149 | } |
| 150 | |
| 151 | // locate project root dir by plugin path |
| 152 | projectRootDir, err := GetProjectRootDirPath(tc.Config.Path) |
| 153 | if err != nil { |
| 154 | return nil, errors.Wrap(err, "failed to get project root dir") |
| 155 | } |
| 156 | |
| 157 | // load .env file |
| 158 | dotEnvPath := filepath.Join(projectRootDir, ".env") |
| 159 | if builtin.IsFilePathExists(dotEnvPath) { |
| 160 | envVars := make(map[string]string) |
| 161 | err = builtin.LoadFile(dotEnvPath, envVars) |
| 162 | if err != nil { |
| 163 | return nil, errors.Wrap(err, "failed to load .env file") |
| 164 | } |
| 165 | |
| 166 | // override testcase config env with variables loaded from .env file |
| 167 | // priority: .env file > testcase config env |
| 168 | if testCase.Config.Environs == nil { |
| 169 | testCase.Config.Environs = make(map[string]string) |
| 170 | } |
| 171 | for key, value := range envVars { |
| 172 | testCase.Config.Environs[key] = value |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | for _, step := range tc.TestSteps { |
| 177 | if step.API != nil { |
| 178 | apiPath, ok := step.API.(string) |
| 179 | if ok { |
| 180 | path := filepath.Join(projectRootDir, apiPath) |
| 181 | if !builtin.IsFilePathExists(path) { |
| 182 | return nil, errors.Wrap(code.ReferencedFileNotFound, |
| 183 | fmt.Sprintf("referenced api file not found: %s", path)) |
| 184 | } |
| 185 | |
| 186 | refAPI := APIPath(path) |
| 187 | apiContent, err := refAPI.ToAPI() |
| 188 | if err != nil { |
| 189 | return nil, err |
| 190 | } |
| 191 | step.API = apiContent |
| 192 | } else { |
| 193 | apiMap, ok := step.API.(map[string]interface{}) |
| 194 | if !ok { |
| 195 | return nil, errors.Wrap(code.InvalidCaseFormat, |
| 196 | fmt.Sprintf("referenced api should be map or path(string), got %v", step.API)) |
| 197 | } |
| 198 | api := &API{} |
no test coverage detected