CodeSampleX

示例

golang.org/x/tools v0.6.0: cover.ParseProfilesFromReader

已验证示例 — golang golang.org/x/tools v0.6.0: cover.ParseProfilesFromReader. contract 在 go 1.26 · linux debian/x64 · docker 上运行并通过.

sha256:b7d0303667efdf2aed2d76f41af98f078f27b59f17a781e26487afcc75510d4f

本网络只提供一件事:能构建的样本。它在沙箱中运行并保留签名回执。它不评级、不担保——同样的代码能否在你的环境构建,它没有测量过。 提交了通过的契约回执的不同签名密钥数量。为 1 表示只有作者;大于 1 表示还有其他人构建过。密钥是自行生成的,背后没有注册身份,因此计的是密钥而非人。 MIT-0

执行证据

声明的环境与签名的运行分开呈现,你可以看到这个样本究竟运行了什么、在哪里运行。

证据依据
签名契约通过
验证回执
1
构建过它的签名密钥
1
声明的环境 linux 24 · ubuntu · glibc 2.39 x64 go

验证运行环境

环境 契约 阶段 运行日期
go 1.26 · linux debian/x64 · docker ed25519:d91480838ac982c9 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · golang@1golang:1.26@sha256:e30143be198a…
2026-09-16

案例

HOW
目标
verify golang.org/x/tools/cover.ParseProfilesFromReader in pkg:golang/golang.org/x/tools@v0.6.0
包
符号
  • golang.org/x/tools/cover.ParseProfilesFromReader
创建时间
2026-09-16T10:16:33Z

契约

  1. ParseProfilesFromReader parses coverage profile lines and returns Profile with correct FileName, Mode, and Blocks
  2. ParseProfilesFromReader parses multiple source files and returns Profiles sorted alphabetically by FileName
  3. ParseProfilesFromReader merges identical block ranges by summing counts in count mode and bitwise OR in set mode
  4. ParseProfilesFromReader returns an error when the mode header is missing or malformed
  5. ParseProfilesFromReader returns an error when block lines have invalid formatting

文件

  • PROMPT.md
  • csx.json
  • go.mod
  • go.sum
  • main.go
  • spec.json
  • test/contract.go

下载源代码构件 (tar.gz)

源代码

PROMPT.md
Clean-room public code sample — generation instructions

Write a brand-new, minimal, self-contained code sample in this clean-room directory.
Do not copy, paraphrase, or reference any existing project source. Work only from this spec.

A csx.json manifest scaffold already exists. Do not recreate it from memory. Preserve its case.goal, packages and symbols; fill its empty case.contract with exact assertions and correct its environment, commands and verifierAdapter for the files you generate.

Goal: verify golang.org/x/tools/cover.ParseProfilesFromReader in pkg:golang/golang.org/x/tools@v0.6.0
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:golang/golang.org/x/tools@v0.6.0
Demonstrate these symbols/APIs:
  - golang.org/x/tools/cover.ParseProfilesFromReader

Rules:
  - One focused purpose; the smallest project that proves the goal.
  - Include a contract test (test/contract.*) that runs OFFLINE and exits 0 exactly when the goal behavior works.
  - Pin every dependency with a lockfile so resolution is reproducible.
  - No secrets, credentials, or tokens. No real URLs (only example.com or localhost). No absolute paths.
  - No personal names, emails, company names, or project identifiers of any kind.
  - No binaries and no generated output (node_modules, dist, target, venv, .git, .env).
  - Keep it under 200 files and 256KB packed.
csx.json
{"case":{"caseId":"case:sha256:c0ded8ec8006c4b65d0486b4d268e7045de35b0ef36a7b2595496498e8c98584","contract":["ParseProfilesFromReader parses coverage profile lines and returns Profile with correct FileName, Mode, and Blocks","ParseProfilesFromReader parses multiple source files and returns Profiles sorted alphabetically by FileName","ParseProfilesFromReader merges identical block ranges by summing counts in count mode and bitwise OR in set mode","ParseProfilesFromReader returns an error when the mode header is missing or malformed","ParseProfilesFromReader returns an error when block lines have invalid formatting"],"goal":"verify golang.org/x/tools/cover.ParseProfilesFromReader in pkg:golang/golang.org/x/tools@v0.6.0","kind":"HOW","packages":["pkg:golang/golang.org/x/tools@v0.6.0"],"schemaVersion":1,"symbols":["golang.org/x/tools/cover.ParseProfilesFromReader"]},"contractCommand":["go","run","./test"],"environment":{"arch":"x64","distro":"ubuntu","ecosystem":"golang","libc":"glibc","libcVersion":"2.39","os":"linux","osVersionBucket":"24","packageManager":"go","schemaVersion":1},"license":"MIT-0","packages":["pkg:golang/golang.org/x/tools@v0.6.0"],"schemaVersion":1,"subject":"pkg:golang/golang.org/x/tools@v0.6.0","symbols":["golang.org/x/tools/cover.ParseProfilesFromReader"],"verifierAdapter":"golang@1"}
go.mod
module example.com/sample

go 1.26.6

require golang.org/x/tools v0.6.0
go.sum
golang.org/x/tools v0.6.0 h1:BOw41kyTf3PuCW1pVQf8+Cyg8pMlkYB1oo9iJ6D/lKM=
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
main.go
package main

import (
	"fmt"
	"os"
	"strings"

	"golang.org/x/tools/cover"
)

func main() {
	sampleProfile := `mode: set
example.com/sample/main.go:10.1,15.20 2 1
example.com/sample/main.go:20.1,25.20 1 0
`
	profiles, err := cover.ParseProfilesFromReader(strings.NewReader(sampleProfile))
	if err != nil {
		fmt.Fprintf(os.Stderr, "ParseProfilesFromReader failed: %v\n", err)
		os.Exit(1)
	}

	for _, p := range profiles {
		fmt.Printf("File: %s (Mode: %s), Blocks: %d\n", p.FileName, p.Mode, len(p.Blocks))
	}
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify golang.org/x/tools/cover.ParseProfilesFromReader in pkg:golang/golang.org/x/tools@v0.6.0",
  "kind": "HOW",
  "packages": [
    "pkg:golang/golang.org/x/tools@v0.6.0"
  ],
  "symbols": [
    "golang.org/x/tools/cover.ParseProfilesFromReader"
  ]
}
test/contract.go
package main

import (
	"fmt"
	"os"
	"strings"

	"golang.org/x/tools/cover"
)

func main() {
	if err := runTests(); err != nil {
		fmt.Fprintf(os.Stderr, "Contract test failed: %v\n", err)
		os.Exit(1)
	}
	fmt.Println("All contract assertions passed.")
}

func runTests() error {
	// 1. ParseProfilesFromReader parses coverage profile lines and returns Profile with correct FileName, Mode, and Blocks
	{
		data := "mode: set\nexample.com/sample/pkg.go:10.1,15.20 2 1\nexample.com/sample/pkg.go:20.1,25.20 1 0\n"
		profiles, err := cover.ParseProfilesFromReader(strings.NewReader(data))
		if err != nil {
			return fmt.Errorf("assertion 1 failed: %w", err)
		}
		if len(profiles) != 1 {
			return fmt.Errorf("assertion 1: expected 1 profile, got %d", len(profiles))
		}
		p := profiles[0]
		if p.FileName != "example.com/sample/pkg.go" {
			return fmt.Errorf("assertion 1: expected FileName example.com/sample/pkg.go, got %s", p.FileName)
		}
		if p.Mode != "set" {
			return fmt.Errorf("assertion 1: expected Mode set, got %s", p.Mode)
		}
		if len(p.Blocks) != 2 {
			return fmt.Errorf("assertion 1: expected 2 blocks, got %d", len(p.Blocks))
		}
		b0 := p.Blocks[0]
		if b0.StartLine != 10 || b0.StartCol != 1 || b0.EndLine != 15 || b0.EndCol != 20 || b0.NumStmt != 2 || b0.Count != 1 {
			return fmt.Errorf("assertion 1: unexpected block 0: %+v", b0)
		}
		b1 := p.Blocks[1]
		if b1.StartLine != 20 || b1.StartCol != 1 || b1.EndLine != 25 || b1.EndCol != 20 || b1.NumStmt != 1 || b1.Count != 0 {
			return fmt.Errorf("assertion 1: unexpected block 1: %+v", b1)
		}
	}

	// 2. ParseProfilesFromReader parses multiple source files and returns Profiles sorted alphabetically by FileName
	{
		data := "mode: count\nz.go:1.1,2.1 1 5\na.go:1.1,2.1 1 2\nm.go:1.1,2.1 1 1\n"
		profiles, err := cover.ParseProfilesFromReader(strings.NewReader(data))
		if err != nil {
			return fmt.Errorf("assertion 2 failed: %w", err)
		}
		if len(profiles) != 3 {
			return fmt.Errorf("assertion 2: expected 3 profiles, got %d", len(profiles))
		}
		if profiles[0].FileName != "a.go" || profiles[1].FileName != "m.go" || profiles[2].FileName != "z.go" {
			return fmt.Errorf("assertion 2: expected sorted files [a.go, m.go, z.go], got [%s, %s, %s]",
				profiles[0].FileName, profiles[1].FileName, profiles[2].FileName)
		}
	}

	// 3. ParseProfilesFromReader merges identical block ranges by summing counts in count mode and bitwise OR in set mode
	{
		// Test count mode (summing)
		countData := "mode: count\nmain.go:5.1,10.1 2 3\nmain.go:5.1,10.1 2 4\n"
		countProfiles, err := cover.ParseProfilesFromReader(strings.NewReader(countData))
		if err != nil {
			return fmt.Errorf("assertion 3 (count mode) failed: %w", err)
		}
		if len(countProfiles) != 1 || len(countProfiles[0].Blocks) != 1 {
			return fmt.Errorf("assertion 3: expected 1 merged block, got profiles: %d", len(countProfiles))
		}
		if countProfiles[0].Blocks[0].Count != 7 {
			return fmt.Errorf("assertion 3: expected summed count 7, got %d", countProfiles[0].Blocks[0].Count)
		}

		// Test set mode (bitwise OR)
		setData := "mode: set\nmain.go:5.1,10.1 2 1\nmain.go:5.1,10.1 2 1\n"
		setProfiles, err := cover.ParseProfilesFromReader(strings.NewReader(setData))
		if err != nil {
			return fmt.Errorf("assertion 3 (set mode) failed: %w", err)
		}
		if len(setProfiles) != 1 || len(setProfiles[0].Blocks) != 1 {
			return fmt.Errorf("assertion 3: expected 1 merged block, got profiles: %d", len(setProfiles))
		}
		if setProfiles[0].Blocks[0].Count != 1 {
			return fmt.Errorf("assertion 3: expected bitwise OR count 1, got %d", setProfiles[0].Blocks[0].Count)
		}
	}

	// 4. ParseProfilesFromReader returns an error when the mode header is missing or malformed
	{
		invalidModes := []string{
			"bad header without mode\nmain.go:1.1,2.1 1 1\n",
			"mode: \nmain.go:1.1,2.1 1 1\n",
			"mode\nmain.go:1.1,2.1 1 1\n",
		}
		for _, inv := range invalidModes {
			_, err := cover.ParseProfilesFromReader(strings.NewReader(inv))
			if err == nil {
				return fmt.Errorf("assertion 4: expected error for malformed mode header %q, got nil", inv)
			}
		}
	}

	// 5. ParseProfilesFromReader returns an error when block lines have invalid formatting
	{
		invalidLines := []string{
			"mode: set\nnot-a-valid-coverage-line\n",
			"mode: set\nmain.go:1.1,2.1 bad 1\n",
			"mode: set\nmain.go:1.1,2.1 1 -5\n",
		}
		for _, inv := range invalidLines {
			_, err := cover.ParseProfilesFromReader(strings.NewReader(inv))
			if err == nil {
				return fmt.Errorf("assertion 5: expected error for invalid line %q, got nil", inv)
			}
		}
	}

	return nil
}

原始种子者

匿名