(root fs.StatFS, path string)
| 116 | } |
| 117 | |
| 118 | func LoadTestModules(root fs.StatFS, path string) (map[string]*ast.Module, []File, error) { |
| 119 | path = filepath.ToSlash(path) |
| 120 | path = strings.TrimSuffix(path, "/") |
| 121 | if path == "" { |
| 122 | path = "." |
| 123 | } |
| 124 | info, err := root.Stat(path) |
| 125 | if err != nil { |
| 126 | return nil, nil, errors.Wrapf(err, "stat %s", path) |
| 127 | } |
| 128 | |
| 129 | var files []string |
| 130 | if info.IsDir() { |
| 131 | entries, err := fs.ReadDir(root, path) |
| 132 | if err != nil { |
| 133 | return nil, nil, errors.Wrapf(err, "read dir %s", path) |
| 134 | } |
| 135 | for _, entry := range entries { |
| 136 | if entry.IsDir() { |
| 137 | continue |
| 138 | } |
| 139 | name := entry.Name() |
| 140 | if !strings.HasSuffix(name, "_test.rego") { |
| 141 | continue |
| 142 | } |
| 143 | files = append(files, filepath.ToSlash(filepath.Join(path, name))) |
| 144 | } |
| 145 | } else { |
| 146 | if !strings.HasSuffix(path, "_test.rego") { |
| 147 | return nil, nil, errors.Errorf("test file must have _test.rego suffix: %s", path) |
| 148 | } |
| 149 | files = append(files, filepath.ToSlash(path)) |
| 150 | } |
| 151 | |
| 152 | if len(files) == 0 { |
| 153 | return nil, nil, errors.New("no policy tests found") |
| 154 | } |
| 155 | |
| 156 | sort.Strings(files) |
| 157 | modules := make(map[string]*ast.Module, len(files)) |
| 158 | entries := make([]File, 0, len(files)) |
| 159 | for _, file := range files { |
| 160 | dt, err := fs.ReadFile(root, file) |
| 161 | if err != nil { |
| 162 | return nil, nil, errors.Wrapf(err, "read policy test module %s", file) |
| 163 | } |
| 164 | mod, err := ast.ParseModuleWithOpts(file, string(dt), ast.ParserOptions{ |
| 165 | RegoVersion: ast.RegoV1, |
| 166 | }) |
| 167 | if err != nil { |
| 168 | return nil, nil, errors.Wrapf(err, "parse policy test module %s", file) |
| 169 | } |
| 170 | modules[file] = mod |
| 171 | entries = append(entries, File{ |
| 172 | Filename: file, |
| 173 | Data: dt, |
| 174 | }) |
| 175 | } |
no test coverage detected
searching dependent graphs…