github.com/bazelbuild/bazel-gazelle@v0.36.1-0.20240520142334-61b277ba6fed/repo/stubs_test.go (about)

     1  /* Copyright 2018 The Bazel Authors. All rights reserved.
     2  
     3  Licensed under the Apache License, Version 2.0 (the "License");
     4  you may not use this file except in compliance with the License.
     5  You may obtain a copy of the License at
     6  
     7     http://www.apache.org/licenses/LICENSE-2.0
     8  
     9  Unless required by applicable law or agreed to in writing, software
    10  distributed under the License is distributed on an "AS IS" BASIS,
    11  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  See the License for the specific language governing permissions and
    13  limitations under the License.
    14  */
    15  
    16  package repo
    17  
    18  import (
    19  	"errors"
    20  	"fmt"
    21  	"os"
    22  
    23  	"github.com/bazelbuild/bazel-gazelle/pathtools"
    24  	"golang.org/x/tools/go/vcs"
    25  )
    26  
    27  func NewStubRemoteCache(rs []Repo) *RemoteCache {
    28  	rc, _ := NewRemoteCache(rs)
    29  	rc.tmpDir = os.DevNull
    30  	rc.tmpErr = errors.New("stub remote cache cannot use temp dir")
    31  	rc.RepoRootForImportPath = stubRepoRootForImportPath
    32  	rc.HeadCmd = stubHeadCmd
    33  	rc.ModInfo = stubModInfo
    34  	rc.ModVersionInfo = stubModVersionInfo
    35  	return rc
    36  }
    37  
    38  // stubRepoRootForImportPath is a stub implementation of vcs.RepoRootForImportPath
    39  func stubRepoRootForImportPath(importPath string, verbose bool) (*vcs.RepoRoot, error) {
    40  	if pathtools.HasPrefix(importPath, "example.com/repo.git") {
    41  		return &vcs.RepoRoot{
    42  			VCS:  vcs.ByCmd("git"),
    43  			Repo: "https://example.com/repo.git",
    44  			Root: "example.com/repo.git",
    45  		}, nil
    46  	}
    47  
    48  	if pathtools.HasPrefix(importPath, "example.com/repo") {
    49  		return &vcs.RepoRoot{
    50  			VCS:  vcs.ByCmd("git"),
    51  			Repo: "https://example.com/repo.git",
    52  			Root: "example.com/repo",
    53  		}, nil
    54  	}
    55  
    56  	if pathtools.HasPrefix(importPath, "example.com") {
    57  		return &vcs.RepoRoot{
    58  			VCS:  vcs.ByCmd("git"),
    59  			Repo: "https://example.com",
    60  			Root: "example.com",
    61  		}, nil
    62  	}
    63  
    64  	return nil, fmt.Errorf("could not resolve import path: %q", importPath)
    65  }
    66  
    67  func stubHeadCmd(remote, vcs string) (string, error) {
    68  	if vcs == "git" && remote == "https://example.com/repo" {
    69  		return "abcdef", nil
    70  	}
    71  	return "", fmt.Errorf("could not resolve remote: %q", remote)
    72  }
    73  
    74  func stubModInfo(importPath string) (string, error) {
    75  	if pathtools.HasPrefix(importPath, "example.com/stub/v2") {
    76  		return "example.com/stub/v2", nil
    77  	}
    78  	if pathtools.HasPrefix(importPath, "example.com/stub") {
    79  		return "example.com/stub", nil
    80  	}
    81  	return "", fmt.Errorf("could not find module path for %s", importPath)
    82  }
    83  
    84  func stubModVersionInfo(modPath, query string) (version, sum string, err error) {
    85  	if modPath == "example.com/known" || modPath == "example.com/unknown" {
    86  		return "v1.2.3", "h1:abcdef", nil
    87  	}
    88  	return "", "", fmt.Errorf("no such module: %s", modPath)
    89  }