Running Tests for Sourcegraph/Zoekt

This document provides a comprehensive guide on running tests for the Sourcegraph/Zoekt project.

Pre-requisites

  • You should have Go installed and configured on your system.
  • You should have nix installed, following the instructions in Nix.
  • You should be familiar with basic shell commands and have a basic understanding of how to run Go tests.

Running Tests

  1. Using nix:

    This method utilizes a Nix shell to create an environment with all dependencies pre-installed. This offers a more reliable and consistent testing environment.

    nix-shell -p golang
    go test ./...
    

    This will create a Nix shell with the necessary Go tools and run all tests under the project root directory.

  2. Directly with Go:

    You can also directly execute Go tests with go test command. This requires that you have all the dependencies installed on your system.

    go test ./...
    

    This will run all tests under the project root directory.

    To run tests for a specific package, use the following:

    go test github.com/sourcegraph/zoekt/... 
    

    This will run all tests in the github.com/sourcegraph/zoekt module.

  3. Using the cmd/zoekt-test utility:

    The cmd/zoekt-test utility is a command-line tool for comparing search results with expected output. This is a convenient tool for testing specific search scenarios.

    To use this utility, you need to provide the following flags:

    • -repo: The repository to search.
    • -indexDir: The index directory to load and exit.
    • -case: Whether to perform case-sensitive search.
    go run cmd/zoekt-test/main.go -repo "github.com/sourcegraph/zoekt" -indexDir /path/to/index -case true pattern.txt
    

    This command will run the search using the specified repository and index directory with case-sensitive search, and compare the results with the patterns in pattern.txt.

Running Tests with Docker

  1. Building a Docker Image:

    FROM golang:1.18
    
    WORKDIR /app
    COPY . .
    
    RUN go mod download
    
    RUN go test ./...
    

    This Dockerfile uses a Go image and copies the project code to a container. Then it downloads the dependencies and executes the tests.

  2. Running the Docker Image:

    docker build -t zoekt-tests .
    docker run zoekt-tests
    

    This command builds a Docker image named zoekt-tests and runs it, executing the tests within the container.

Test Coverage

You can obtain a report of test coverage using the go test command with -coverprofile flag:

go test -coverprofile=coverage.out ./...

This will generate a coverage.out file, which can be used to view test coverage details.

Example Test Code

// File: cmd/zoekt-dynamic-indexserver/main_test.go

func TestLoggedRun(t *testing.T) {
    ctx, cancel := context.WithTimeout(context.Background(), cmdTimeout)
    defer cancel()

    cmd := exec.CommandContext(ctx, "echo", "-n", "1")

    stdout := captureOutput(func() {
        loggedRun(cmd)
    })

    if !strings.Contains(stdout, "run [echo -n 1]") {
        t.Errorf("loggedRun output is incorrect: %v", stdout)
    }
}

This test checks the output of the loggedRun function, which is used to log commands before executing them. The test ensures that the output contains the expected command string.

Note: This documentation is based on the provided information and code snippets, it may not cover all aspects of testing for the Sourcegraph/Zoekt project. It is recommended to refer to the project’s source code and documentation for further details.