Debian Codegraph @

gopls/internal/test/integration/fake/proxy.go

raw symbols SCIP
1 // Copyright 2020 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 fake
6
7 import (
8 "fmt"
9 "strings"
10
11 "golang.org/x/tools/internal/proxydir"
12 )
13
14 // WriteProxy creates a new proxy file tree using the txtar-encoded content,
15 // and returns its URL.
16 func WriteProxy(tmpdir string, files map[string][]byte) (string, error) {
17 type moduleVersion struct {
18 modulePath, version string
19 }
20 // Transform into the format expected by the proxydir package.
21 filesByModule := make(map[moduleVersion]map[string][]byte)
22 for name, data := range files {
23 modulePath, version, suffix := splitModuleVersionPath(name)
24 mv := moduleVersion{modulePath, version}
25 if _, ok := filesByModule[mv]; !ok {
26 filesByModule[mv] = make(map[string][]byte)
27 }
28 filesByModule[mv][suffix] = data
29 }
30 for mv, files := range filesByModule {
31 // Don't hoist this check out of the loop:
32 // the problem is benign if filesByModule is empty.
33 if strings.Contains(tmpdir, "#") {
34 return "", fmt.Errorf("WriteProxy's tmpdir contains '#', which is unsuitable for GOPROXY. (If tmpdir was derived from testing.T.Name, use t.Run to ensure that each subtest has a unique name.)")
35 }
36 if err := proxydir.WriteModuleVersion(tmpdir, mv.modulePath, mv.version, files); err != nil {
37 return "", fmt.Errorf("error writing %s@%s: %v", mv.modulePath, mv.version, err)
38 }
39 }
40 return proxydir.ToURL(tmpdir), nil
41 }