File: drv_test.go

package info (click to toggle)
golang-golang-x-tools 1%3A0.5.0%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bookworm-backports
  • size: 16,592 kB
  • sloc: javascript: 2,011; asm: 1,635; sh: 192; yacc: 155; makefile: 52; ansic: 8
file content (73 lines) | stat: -rw-r--r-- 1,943 bytes parent folder | download | duplicates (3)
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
// Copyright 2021 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 main

import (
	"os"
	"os/exec"
	"path/filepath"
	"runtime"
	"testing"

	"golang.org/x/tools/internal/testenv"
)

// buildDriver builds the fuzz-driver executable, returning its path.
func buildDriver(t *testing.T) string {
	t.Helper()
	if runtime.GOOS == "android" {
		t.Skipf("the dependencies are not available on android")
		return ""
	}
	bindir := filepath.Join(t.TempDir(), "bin")
	err := os.Mkdir(bindir, os.ModePerm)
	if err != nil {
		t.Fatal(err)
	}
	binary := filepath.Join(bindir, "driver")
	if runtime.GOOS == "windows" {
		binary += ".exe"
	}
	cmd := exec.Command("go", "build", "-o", binary)
	if err := cmd.Run(); err != nil {
		t.Fatalf("Building fuzz-driver: %v", err)
	}
	return binary
}

func TestEndToEndIntegration(t *testing.T) {
	testenv.NeedsTool(t, "go")
	td := t.TempDir()

	// Build the fuzz-driver binary.
	// Note: if more tests are added to this package, move this to single setup fcn, so
	// that we don't have to redo the build each time.
	binary := buildDriver(t)

	// Kick off a run.
	gendir := filepath.Join(td, "gen")
	args := []string{"-numfcns", "3", "-numpkgs", "1", "-seed", "101", "-outdir", gendir}
	c := exec.Command(binary, args...)
	b, err := c.CombinedOutput()
	if err != nil {
		t.Fatalf("error invoking fuzz-driver: %v\n%s", err, b)
	}

	found := ""
	walker := func(path string, info os.FileInfo, err error) error {
		found = found + ":" + info.Name()
		return nil
	}

	// Make sure it emits something.
	err2 := filepath.Walk(gendir, walker)
	if err2 != nil {
		t.Fatalf("error from filepath.Walk: %v", err2)
	}
	const expected = ":gen:genCaller0:genCaller0.go:genChecker0:genChecker0.go:genMain.go:genUtils:genUtils.go:go.mod"
	if found != expected {
		t.Errorf("walk of generated code: got %s want %s", found, expected)
	}
}