goproxytest

package
v0.15.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 17, 2026 License: BSD-3-Clause Imports: 23 Imported by: 0

Documentation

Overview

Package goproxytest serves Go modules from a proxy server designed to run on localhost during tests, both to make tests avoid requiring specific network servers and also to make them significantly faster.

Each module archive is either a file named path_vers.txtar or path_vers.txt, or a directory named path_vers, where slashes in path have been replaced with underscores. The archive or directory may contain files ".info" and ".mod", to be served as the info and mod files in the proxy protocol (see https://research.swtch.com/vgo-module). If ".mod" is not present, the content of the "go.mod" file is used. If ".info" is not present, it is synthesized from the version with the Unix epoch as timestamp.

The remaining files are served as the content of the module zip file. The path@vers prefix required of files in the zip file is added automatically by the proxy: the files in the archive have names without the prefix, like plain "go.mod", "x.go", and so on.

See ../cmd/txtar-addmod and ../cmd/txtar-c for tools generate txtar files, although it's fine to write them by hand.

Index

Constants

View Source
const GoModProxyDir = ".gomodproxy"

GoModProxyDir is the name of the special subdirectory in a txtar script's supporting files within which module archives for goproxytest are expected to be found.

Variables

This section is empty.

Functions

func Setup

func Setup(p *testscript.Params)

Setup sets up the given test parameters so that test scripts that contain a GoModProxyDir directory will have GOPROXY set to serve modules from that directory using a localhost proxy server.

It wraps p.Setup to start a proxy server when the directory is present, set GOPROXY and GONOSUMDB appropriately, and shuts down the server when the test completes.

If a file exists in the top level of the GoModProxyDir directory named "_enable_overlay", then instead of replacing the Go proxy entirely, the existing Go proxy is used to serve modules that are not present in the test script. In this case it uses the existing GOMODPROXY download cache to avoid network downloads if it can, but takes care not to pollute it with the localhost proxy server content.

Types

type Server

type Server struct {
	URL string
	// contains filtered or unexported fields
}

func NewServer

func NewServer(dir, addr string) (*Server, error)

NewServer starts the Go module proxy listening on the given network address. It serves modules taken from the given directory name. If addr is empty, it will listen on an arbitrary localhost port. If dir is empty, "testmod" will be used.

The returned Server should be closed after use.

func NewTestServer

func NewTestServer(tb testing.TB, dir, addr string) *Server

NewTestServer is a wrapper around NewServer for use in Go tests. Failure to start the server stops the test via testing.TB.Fatalf, all server logs go through testing.TB.Logf, and the server is closed when the test finishes via testing.TB.Cleanup.

func (*Server) Close

func (srv *Server) Close()

Close shuts down the proxy.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL