### Go Coverage Profile Input Example Source: https://context7.com/jandelgado/gcov2lcov/llms.txt Example of the input format for Go coverage profiles, showing module path, line ranges, statement count, and execution count. ```text mode: set github.com/jandelgado/gcov2lcov/main.go:45.44,49.38 4 1 github.com/jandelgado/gcov2lcov/main.go:46.44,50.38 4 0 github.com/jandelgado/gcov2lcov/main.go:58.2,58.32 1 1 ``` -------------------------------- ### Install gcov2lcov from Source Source: https://github.com/jandelgado/gcov2lcov/blob/master/README.md Installs the gcov2lcov tool using Go modules. Ensure your Go environment is set up correctly. ```bash go install github.com/jandelgado/gcov2lcov@latest ``` -------------------------------- ### GitHub Actions Integration with `gcov2lcov-action` Source: https://context7.com/jandelgado/gcov2lcov/llms.txt Demonstrates how to integrate the gcov2lcov binary with GitHub Actions using the dedicated `gcov2lcov-action` or by installing the binary directly. This setup is for running tests with coverage and converting the output to lcov format for upload. ```yaml # .github/workflows/coverage.yml name: coverage on: [push, pull_request] jobs: coverage: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - uses: actions/setup-go@v5 with: go-version: '1.22' - name: Run tests with coverage run: go test -coverprofile=coverage.out ./... # Option A: use the dedicated action - name: Convert coverage to lcov uses: jandelgado/gcov2lcov-action@v1 with: infile: coverage.out outfile: coverage.lcov # Option B: install binary directly and run manually - name: Install and run gcov2lcov run: | go install github.com/jandelgado/gcov2lcov@latest GOROOT=$(go env GOROOT) gcov2lcov -infile=coverage.out -outfile=coverage.lcov - name: Upload to Coveralls uses: coverallsapp/github-action@v2 with: file: coverage.lcov format: lcov ``` -------------------------------- ### LCOV Tracefile Output Example Source: https://context7.com/jandelgado/gcov2lcov/llms.txt Example of the generated lcov tracefile format, including test name, source file path, line execution counts, and total/hit line counts. ```text TN: SF:main.go DA:45,1 DA:46,1 DA:47,1 DA:48,1 DA:49,1 DA:50,0 DA:58,1 LF:7 LH:6 end_of_record ``` -------------------------------- ### Parse Go Coverage Line Source: https://context7.com/jandelgado/gcov2lcov/llms.txt Demonstrates parsing a single line from a Go coverage profile into file path and block data. Shows successful parsing and error cases for malformed input. ```go // Input line format: // github.com/jandelgado/gcov2lcov/main.go:6.14,8.3 2 1 // └─ file path ─────────────────────────┘ └start┘└end┘ stmts covered line := "github.com/jandelgado/gcov2lcov/main.go:6.14,8.3 2 1" file, b, err := parseCoverageLine(line) // file => "github.com/jandelgado/gcov2lcov/main.go" // b.startLine => 6, b.startChar => 14 // b.endLine => 8, b.endChar => 3 // b.statements => 2, b.covered => 1 // err => nil // Error cases: _, _, err = parseCoverageLine("main.go") // missing colon separator → error _, _, err = parseCoverageLine("main.go:A B") // wrong number of space-separated parts → error _, _, err = parseCoverageLine("main.go:6.14,8.3 X 1") // non-numeric statement count → error ``` -------------------------------- ### Resolve Repo-Relative Path with `getCoverallsSourceFileName` Source: https://context7.com/jandelgado/gcov2lcov/llms.txt Walks up the directory tree from a given absolute path to find the VCS root (e.g., .git, .hg) and strips that prefix to produce a repository-relative path. This is the default path resolver for Coveralls compatibility. Falls back to the original path if no VCS root is found. ```go // Assuming cwd is /home/user/projects/gcov2lcov (which contains .git) name := getCoverallsSourceFileName("/home/user/projects/gcov2lcov/main.go") // => "main.go" name = getCoverallsSourceFileName("/home/user/projects/gcov2lcov/pkg/foo/bar.go") // => "pkg/foo/bar.go" // Falls back to original path if no VCS root found name = getCoverallsSourceFileName("/tmp/standalone/main.go") // => "/tmp/standalone/main.go" ``` -------------------------------- ### Parse Go Coverage Profile with `parseCoverage` Source: https://context7.com/jandelgado/gcov2lcov/llms.txt Reads a Go coverage profile from an io.Reader, resolves file paths using go/build, and applies a path resolver function. Lines that cannot be parsed or whose packages cannot be resolved will emit a warning log and be skipped. ```go import ( "strings" ) cov := `mode: set github.com/jandelgado/gcov2lcov/main.go:6.14,8.3 2 1 github.com/jandelgado/gcov2lcov/main.go:7.14,9.3 2 0 github.com/jandelgado/gcov2lcov/main.go:10.1,11.10 2 2` reader := strings.NewReader(cov) // getCoverallsSourceFileName: strips repo root prefix → relative path (e.g. "main.go") blocks, err := parseCoverage(reader, getCoverallsSourceFileName) // blocks["main.go"] => []*block{ {6,14,8,3,2,1}, {7,14,9,3,2,0}, {10,1,11,10,2,2} } // getSourceFileName: identity function → keeps absolute path as-is blocks, err = parseCoverage(reader, getSourceFileName) // blocks["/abs/path/to/main.go"] => []*block{...} ``` -------------------------------- ### Pipe Coverage Data via Stdin/Stdout Source: https://context7.com/jandelgado/gcov2lcov/llms.txt Convert coverage data by piping the output of 'go test' directly to gcov2lcov's stdin. ```sh # Pipe via stdin/stdout go test -coverprofile=coverage.out ./... && cat coverage.out | gcov2lcov > coverage.lcov ``` -------------------------------- ### Fix GOROOT Warnings Source: https://context7.com/jandelgado/gcov2lcov/llms.txt Prepend the GOROOT environment variable to resolve 'cannot find GOROOT directory' warnings, often encountered in CI environments. ```sh # Fix "cannot find GOROOT directory" warnings in CI environments GOROOT=$(go env GOROOT) gcov2lcov -infile=coverage.out -outfile=coverage.lcov ``` -------------------------------- ### getCoverallsSourceFileName Source: https://context7.com/jandelgado/gcov2lcov/llms.txt Resolves a repository-relative path by walking up the directory tree to find the VCS root (e.g., .git) and stripping that prefix. This is the default path resolver for Coveralls compatibility. ```APIDOC ## `getCoverallsSourceFileName` — Resolve Repo-Relative Path Walks up the directory tree from a given absolute path to find the VCS root (detected by the presence of `.git`, `.hg`, `.bzr`, or `.svn`), then strips that prefix to produce a repository-relative path. This is the default path resolver and produces paths compatible with Coveralls. ```go // Assuming cwd is /home/user/projects/gcov2lcov (which contains .git) name := getCoverallsSourceFileName("/home/user/projects/gcov2lcov/main.go") // => "main.go" name = getCoverallsSourceFileName("/home/user/projects/gcov2lcov/pkg/foo/bar.go") // => "pkg/foo/bar.go" // Falls back to original path if no VCS root found name = getCoverallsSourceFileName("/tmp/standalone/main.go") // => "/tmp/standalone/main.go" ``` ``` -------------------------------- ### Use Absolute Source Paths Source: https://context7.com/jandelgado/gcov2lcov/llms.txt Configure gcov2lcov to output absolute file paths in the lcov tracefile, which can be useful when the repository root is not consistently determined. ```sh # Use absolute source paths (useful when repo root is ambiguous) gcov2lcov -infile=coverage.out -outfile=coverage.lcov -use-absolute-source-path ``` -------------------------------- ### parseCoverage Source: https://context7.com/jandelgado/gcov2lcov/llms.txt Parses a Go coverage profile from an io.Reader, resolves file paths, and returns a map of file paths to coverage blocks. Handles warnings for unparseable lines or unresolved packages. ```APIDOC ## `parseCoverage` — Parse a Full Coverage Profile Reads an entire Go coverage profile from an `io.Reader`, resolves each file path using `go/build`, applies a path resolver function, and returns a map of resolved file paths to their coverage blocks. Lines that cannot be parsed or whose packages cannot be resolved emit a warning log and are skipped. ```go import ( "strings" ) cov := `mode: set github.com/jandelgado/gcov2lcov/main.go:6.14,8.3 2 1 github.com/jandelgado/gcov2lcov/main.go:7.14,9.3 2 0 github.com/jandelgado/gcov2lcov/main.go:10.1,11.10 2 2` reader := strings.NewReader(cov) // getCoverallsSourceFileName: strips repo root prefix → relative path (e.g. "main.go") blocks, err := parseCoverage(reader, getCoverallsSourceFileName) // blocks["main.go"] => []*block{ {6,14,8,3,2,1}, {7,14,9,3,2,0}, {10,1,11,10,2,2} } // getSourceFileName: identity function → keeps absolute path as-is blocks, err = parseCoverage(reader, getSourceFileName) // blocks["/abs/path/to/main.go"] => []*block{...} ``` ``` -------------------------------- ### Convert Go Coverage to Lcov Format Source: https://github.com/jandelgado/gcov2lcov/blob/master/README.md Converts a Go coverage file to the lcov format. This is useful for uploading coverage reports to services like Coveralls. The output can be directed to a file or stdout. ```sh go test -coverprofile=coverage.out && \ gcov2lcov -infile=coverage.out -outfile=coverage.lcov ``` -------------------------------- ### Basic gcov2lcov Usage Source: https://context7.com/jandelgado/gcov2lcov/llms.txt Generate a coverage profile with 'go test' and then convert it to lcov format using gcov2lcov. ```sh # Install from source go install github.com/jandelgado/gcov2lcov@latest # Basic usage: generate coverage profile then convert go test -coverprofile=coverage.out ./... gcov2lcov -infile=coverage.out -outfile=coverage.lcov ``` -------------------------------- ### Lcov Tracefile Format Reference Source: https://github.com/jandelgado/gcov2lcov/blob/master/README.md Describes the structure of an lcov tracefile, including test name, source file information, function details, branch coverage, line execution counts, and summaries. This format is generated by tools like geninfo. ```text TN: SF: FN:, FNDA:, FNF: FNH: BRDA:,,, BRF: BRH: DA:,[,] LH: LF: end_of_record ``` -------------------------------- ### convertCoverage Source: https://context7.com/jandelgado/gcov2lcov/llms.txt The top-level conversion function that reads Go coverage data, converts it to lcov format, and writes the output. It combines parsing and writing into a single operation. ```APIDOC ## `convertCoverage` — End-to-End Conversion The top-level conversion function. Reads a Go coverage profile from `io.Reader`, converts it to lcov format, and writes the result to `io.Writer`. Combines `parseCoverage` and `writeLcov` in a single call. ```go import ( "bytes" "strings" ) cov := `mode: set github.com/jandelgado/gcov2lcov/main.go:6.14,8.3 2 1 github.com/jandelgado/gcov2lcov/main.go:7.14,9.3 2 0 github.com/jandelgado/gcov2lcov/main.go:10.1,11.10 2 2` in := strings.NewReader(cov) out := bytes.NewBufferString("") err := convertCoverage(in, out, getCoverallsSourceFileName) // out.String() => // TN: // SF:main.go // DA:6,1 // DA:7,1 // DA:8,1 // DA:9,0 // DA:10,2 // DA:11,2 // LF:6 // LH:5 // end_of_record ``` ``` -------------------------------- ### End-to-End Conversion with `convertCoverage` Source: https://context7.com/jandelgado/gcov2lcov/llms.txt The top-level conversion function that reads a Go coverage profile, converts it to lcov format, and writes the result to an io.Writer. It combines `parseCoverage` and `writeLcov` in a single call. ```go import ( "bytes" "strings" ) cov := `mode: set github.com/jandelgado/gcov2lcov/main.go:6.14,8.3 2 1 github.com/jandelgado/gcov2lcov/main.go:7.14,9.3 2 0 github.com/jandelgado/gcov2lcov/main.go:10.1,11.10 2 2` in := strings.NewReader(cov) out := bytes.NewBufferString("") err := convertCoverage(in, out, getCoverallsSourceFileName) // out.String() => // TN: // SF:main.go // DA:6,1 // DA:7,1 // DA:8,1 // DA:9,0 // DA:10,2 // DA:11,2 // LF:6 // LH:5 // end_of_record ``` -------------------------------- ### Write lcov Record with `writeLcovRecord` Source: https://context7.com/jandelgado/gcov2lcov/llms.txt Writes a single lcov record for a source file to an io.StringWriter. It aggregates per-block covered counts into per-line counts and emits sorted `DA` entries followed by `LF`/`LH` summary lines. ```go import ( "bytes" "bufio" ) blocks := []*block{ {startLine: 6, endLine: 8, covered: 1}, // lines 6,7,8 each get +1 {startLine: 7, endLine: 9, covered: 0}, // lines 7,8,9 each get +0 } buf := bytes.NewBufferString("") w := bufio.NewWriter(buf) err := writeLcovRecord("main.go", blocks, w) w.Flush() // Output: // TN: // SF:main.go // DA:6,1 // DA:7,1 // DA:8,1 // DA:9,0 // LF:4 // LH:3 // end_of_record ``` -------------------------------- ### Set GOROOT for gcov2lcov Source: https://github.com/jandelgado/gcov2lcov/blob/master/README.md If you encounter 'cannot find GOROOT directory' errors, explicitly set the GOROOT environment variable before running gcov2lcov. This ensures the tool can locate necessary Go build information. ```sh GOROOT=$(go env GOROOT) gcov2lcov -infile=coverage.out -outfile=coverage.lcov ``` -------------------------------- ### writeLcovRecord Source: https://context7.com/jandelgado/gcov2lcov/llms.txt Writes a single lcov record for a source file to an io.StringWriter. It aggregates block coverage counts into line counts and emits sorted DA entries with LF/LH summary lines. ```APIDOC ## `writeLcovRecord` — Write a Single File's lcov Record Writes one lcov record for a single source file to an `io.StringWriter`. Aggregates per-block covered counts into per-line counts (a line covered by multiple overlapping blocks sums their counts), then emits sorted `DA` entries followed by `LF`/`LH` summary lines. ```go import ( "bytes" "bufio" ) blocks := []*block{ {startLine: 6, endLine: 8, covered: 1}, // lines 6,7,8 each get +1 {startLine: 7, endLine: 9, covered: 0}, // lines 7,8,9 each get +0 } buf := bytes.NewBufferString("") w := bufio.NewWriter(buf) err := writeLcovRecord("main.go", blocks, w) w.Flush() // Output: // TN: // SF:main.go // DA:6,1 // DA:7,1 // DA:8,1 // DA:9,0 // LF:4 // LH:3 // end_of_record ``` ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.