Files
tailscale/util/httpm/httpm_test.go
T
Brad FitzpatrickandBrad Fitzpatrick de0553be66 util/httpm: exempt tempfork from TestUsedConsistently
Files under tempfork are vendored copies of upstream code that we
want to keep as close to upstream as possible, so don't require them
to use httpm constants. An upcoming tempfork/acme sync brings in
upstream test files using net/http's method constants.

Updates tailscale/corp#45167

Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
Change-Id: If2a90b1d7c5e8f3a6b4d0c9e2a7f5b8d1c4e6a3f
2026-07-20 11:37:37 -07:00

51 lines
1.4 KiB
Go

// Copyright (c) Tailscale Inc & contributors
// SPDX-License-Identifier: BSD-3-Clause
package httpm
import (
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
)
func TestUsedConsistently(t *testing.T) {
dir, err := os.Getwd()
if err != nil {
t.Fatal(err)
}
rootDir := filepath.Join(dir, "../..")
// If we don't have a .git directory, we're not in a git checkout (e.g.
// a downstream package); skip this test.
if _, err := os.Stat(filepath.Join(rootDir, ".git")); err != nil {
t.Skipf("skipping test since .git doesn't exist: %v", err)
}
// Open .git/index so Go's test cache tracks it as an input.
// The index file changes on git reset, checkout, pull, etc.,
// so the cache is properly invalidated when moving between commits.
if f, err := os.Open(filepath.Join(rootDir, ".git", "index")); err == nil {
f.Close()
}
cmd := exec.Command("git", "grep", "-l", "-F", "http.Method")
cmd.Dir = rootDir
matches, _ := cmd.Output()
for fn := range strings.SplitSeq(strings.TrimSpace(string(matches)), "\n") {
if strings.HasPrefix(fn, "tempfork/") {
// Files under tempfork are vendored copies of upstream
// code that we want to keep as close to upstream as
// possible, so don't hold them to this rule.
continue
}
switch fn {
case "util/httpm/httpm.go", "util/httpm/httpm_test.go":
continue
}
t.Errorf("http.MethodFoo constant used in %s; use httpm.FOO instead", fn)
}
}