Debian Codegraph @

debian/go/src/golang.org/x/telemetry/internal/configtest/configtest.go

raw symbols SCIP
1 // Copyright 2024 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 // Package configtest provides a helper for testing using a local proxy
6 // containing a fake upload config.
7 package configtest
8
9 import (
10 "encoding/json"
11 "fmt"
12 "os/exec"
13 "path/filepath"
14 "testing"
15
16 "golang.org/x/telemetry/internal/configstore"
17 "golang.org/x/telemetry/internal/proxy"
18 "golang.org/x/telemetry/internal/telemetry"
19 )
20
21 // LocalProxyEnv writes a proxy directory for the given upload config, and
22 // returns a go environment to use for fetching that config from a local
23 // file-based proxy.
24 //
25 // This environment should be passed to [configstore.Download].
26 func LocalProxyEnv(t *testing.T, cfg *telemetry.UploadConfig, version string) []string {
27 t.Helper()
28
29 dir := t.TempDir()
30
31 encoded, err := json.Marshal(cfg)
32 if err != nil {
33 t.Fatalf("marshaling config failed: %v", err)
34 }
35 dirPath := fmt.Sprintf("%v@%v/", configstore.ModulePath, version)
36 files := map[string][]byte{
37 dirPath + "go.mod": []byte("module " + configstore.ModulePath + "\n\ngo 1.20\n"),
38 dirPath + "config.json": encoded,
39 }
40 proxyURI, err := proxy.WriteProxy(filepath.Join(dir, "proxy"), files)
41 if err != nil {
42 t.Fatalf("writing proxy failed: %v", err)
43 }
44
45 env := []string{
46 "GOPROXY=" + proxyURI, // Use the fake proxy.
47 "GONOSUMDB=*", // Skip verifying checksum against sum.golang.org.
48 "GOMODCACHE=" + filepath.Join(dir, "modcache"), // Don't pollute system module cache.
49 }
50 t.Cleanup(func() {
51 cmd := exec.Command("go", "clean", "-modcache")
52 cmd.Env = append(cmd.Environ(), env...)
53 out, err := cmd.CombinedOutput()
54 if err != nil {
55 t.Errorf("go clean -modcache failed: %v\n%s", err, out)
56 }
57 })
58 return env
59 }