File: testfiles_test.go

package info (click to toggle)
golang-golang-x-tools 1%3A0.25.0%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental, forky, sid, trixie
  • size: 22,724 kB
  • sloc: javascript: 2,027; asm: 1,645; sh: 166; yacc: 155; makefile: 49; ansic: 8
file content (95 lines) | stat: -rw-r--r-- 2,625 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// Copyright 2024 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package testfiles_test

import (
	"fmt"
	"os"
	"path/filepath"
	"testing"

	"golang.org/x/tools/go/analysis"
	"golang.org/x/tools/go/analysis/analysistest"
	"golang.org/x/tools/internal/testenv"
	"golang.org/x/tools/internal/testfiles"
	"golang.org/x/tools/internal/versions"
	"golang.org/x/tools/txtar"
)

func TestTestDir(t *testing.T) {
	testenv.NeedsGo1Point(t, 23)

	// Files are initially {go.mod.test,sub.test/sub.go.test}.
	fs := os.DirFS(filepath.Join(analysistest.TestData(), "versions"))
	tmpdir := testfiles.CopyToTmp(t, fs,
		"go.mod.test,go.mod",                // After: {go.mod,sub.test/sub.go.test}
		"sub.test/sub.go.test,sub.test/abc", // After: {go.mod,sub.test/abc}
		"sub.test,sub",                      // After: {go.mod,sub/abc}
		"sub/abc,sub/sub.go",                // After: {go.mod,sub/sub.go}
	)

	filever := &analysis.Analyzer{
		Name: "filever",
		Doc:  "reports file go versions",
		Run: func(pass *analysis.Pass) (any, error) {
			for _, file := range pass.Files {
				ver := versions.FileVersion(pass.TypesInfo, file)
				name := filepath.Base(pass.Fset.Position(file.Package).Filename)
				pass.Reportf(file.Package, "%s@%s", name, ver)
			}
			return nil, nil
		},
	}
	res := analysistest.Run(t, tmpdir, filever, "golang.org/fake/versions", "golang.org/fake/versions/sub")
	got := 0
	for _, r := range res {
		got += len(r.Diagnostics)
	}

	if want := 4; got != want {
		t.Errorf("Got %d diagnostics. wanted %d", got, want)
	}
}

func TestTestDirErrors(t *testing.T) {
	const input = `
-- one.txt --
one
`
	// Files are initially {go.mod.test,sub.test/sub.go.test}.
	fs, err := txtar.FS(txtar.Parse([]byte(input)))
	if err != nil {
		t.Fatal(err)
	}

	directive := "no comma to split on"
	intercept := &fatalIntercept{t, nil}
	func() {
		defer func() { // swallow panics from fatalIntercept.Fatal
			if r := recover(); r != intercept {
				panic(r)
			}
		}()
		testfiles.CopyToTmp(intercept, fs, directive)
	}()

	got := fmt.Sprint(intercept.fatalfs)
	want := `[rename directive "no comma to split on" does not contain delimiter ","]`
	if got != want {
		t.Errorf("CopyToTmp(%q) had the Fatal messages %q. wanted %q", directive, got, want)
	}
}

// helper for TestTestDirErrors
type fatalIntercept struct {
	testing.TB
	fatalfs []string
}

func (i *fatalIntercept) Fatalf(format string, args ...any) {
	i.fatalfs = append(i.fatalfs, fmt.Sprintf(format, args...))
	// Do not mark the test as failing, but fail early.
	panic(i)
}