< Summary - Repometa Coverage

Information
Class: goDetector
Assembly: repometa
File(s): /home/runner/work/repometa/repometa/detect_go.go
Line coverage
100%
Covered lines: 30
Uncovered lines: 0
Coverable lines: 30
Total lines: 83
Line coverage: 100%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
detect0%00100%

File(s)

/home/runner/work/repometa/repometa/detect_go.go

#LineLine coverage
 1package repometa
 2
 3import (
 4  "path/filepath"
 5  "strings"
 6)
 7
 8type goDetector struct{}
 9
 8410func (goDetector) detect(dv dirVisit, cfg options) []finding {
 8411  var out []finding
 8412
 8413  // go.work — the whole directory is a Go workspace. Members are the
 8414  // module paths listed in the `use` directive.
 215  if hasFile(dv.files, "go.work") {
 216    members := parseGoWorkUse(filepath.Join(dv.abs, "go.work"), cfg)
 217    expanded := make([]string, 0, len(members))
 218    for _, m := range members {
 519      expanded = append(expanded, joinRel(dv.rel, m))
 520    }
 221    out = append(out, finding{
 222      Kind:       KindGoModule,
 223      Confidence: 1.0,
 224      Evidence: []Evidence{
 225        {Path: relJoin(dv.rel, "go.work"), Reason: "go.work at directory root"},
 226      },
 227      Workspaces: []Workspace{{Kind: WorkspaceGo, Members: expanded}},
 228    })
 229    return out
 30  }
 31
 932  if hasFile(dv.files, "go.mod") {
 933    out = append(out, finding{
 934      Kind:       KindGoModule,
 935      Confidence: 1.0,
 936      Evidence: []Evidence{
 937        {Path: relJoin(dv.rel, "go.mod"), Reason: "go.mod at directory root"},
 938      },
 939    })
 940  }
 8241  return out
 42}
 43
 44// parseGoWorkUse returns the module directories listed in a go.work file.
 45// It handles both `use ./mod` and `use ( ... )` block forms. On any read
 46// error it returns nil.
 47func parseGoWorkUse(path string, cfg options) []string {
 48  data := readManifestOrNil(path, cfg)
 49  if data == nil {
 50    return nil
 51  }
 52  var members []string
 53  inBlock := false
 54  for line := range strings.SplitSeq(string(data), "\n") {
 55    trim := strings.TrimSpace(line)
 56    if trim == "" || strings.HasPrefix(trim, "//") {
 57      continue
 58    }
 59    if idx := strings.Index(trim, "//"); idx >= 0 {
 60      trim = strings.TrimSpace(trim[:idx])
 61    }
 62    switch {
 63    case strings.HasPrefix(trim, "use ("):
 64      inBlock = true
 65    case inBlock && trim == ")":
 66      inBlock = false
 67    case inBlock:
 68      m := strings.Trim(trim, "\"'")
 69      m = strings.TrimPrefix(m, "./")
 70      if m != "" {
 71        members = append(members, m)
 72      }
 73    case strings.HasPrefix(trim, "use "):
 74      m := strings.TrimSpace(strings.TrimPrefix(trim, "use "))
 75      m = strings.Trim(m, "\"'")
 76      m = strings.TrimPrefix(m, "./")
 77      if m != "" {
 78        members = append(members, m)
 79      }
 80    }
 81  }
 82  return members
 83}

Methods/Properties

detect