| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 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 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 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, |
| 47 |
"GONOSUMDB=*", |
| 48 |
"GOMODCACHE=" + filepath.Join(dir, "modcache"), |
| 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 |
} |