MCPcopy Index your code
hub / github.com/tailscale/tailscale / testsForShard

Function testsForShard

cmd/testwrapper/testwrapper.go:176–230  ·  view source on GitHub ↗

testsForShard returns the test names in pkg that belong to the given shard spec (e.g. "2/3"). It uses "go list -json" to find test source files (no compilation) and scans them for top-level test function names, assigning each to a shard by hashing. Returns nil if the spec is invalid or if listing fa

(ctx context.Context, pkg, shardSpec string)

Source from the content-addressed store, hash-verified

174// each to a shard by hashing. Returns nil if the spec is invalid or if
175// listing fails (the main run will surface the error).
176func testsForShard(ctx context.Context, pkg, shardSpec string) ([]string, error) {
177 a, b, ok := strings.Cut(shardSpec, "/")
178 if !ok {
179 return nil, nil
180 }
181 wantShard, err := strconv.Atoi(a)
182 if err != nil || wantShard < 1 {
183 return nil, nil
184 }
185 shards, err := strconv.Atoi(b)
186 if err != nil || shards < 1 {
187 return nil, nil
188 }
189
190 out, err := exec.CommandContext(ctx, "go", "list", "-json", pkg).Output()
191 if err != nil {
192 // Errors will be surfaced by the main test run.
193 return nil, nil
194 }
195
196 type pkgJSON struct {
197 Dir string
198 TestGoFiles []string
199 XTestGoFiles []string
200 }
201
202 seen := map[string]bool{}
203 var result []string
204
205 dec := json.NewDecoder(bytes.NewReader(out))
206 for dec.More() {
207 var p pkgJSON
208 if err := dec.Decode(&p); err != nil {
209 break
210 }
211 for _, f := range append(p.TestGoFiles, p.XTestGoFiles...) {
212 names, err := testFuncNames(filepath.Join(p.Dir, f))
213 if err != nil {
214 continue
215 }
216 for _, name := range names {
217 if seen[name] {
218 continue
219 }
220 seen[name] = true
221 h := fnv.New32a()
222 io.WriteString(h, name)
223 if int(h.Sum32()%uint32(shards)) == wantShard-1 {
224 result = append(result, name)
225 }
226 }
227 }
228 }
229 return result, nil
230}
231
232// testFuncNames scans a Go source file and returns the names of all top-level
233// test functions (Test*, Benchmark*, Example*, Fuzz*).

Callers 1

runTestsFunction · 0.85

Calls 4

testFuncNamesFunction · 0.85
WriteStringMethod · 0.80
OutputMethod · 0.45
DecodeMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…