Help us shape the future of Go! Take the 2026 Go Developer Survey

Source file src/cmd/go/internal/test/inputs_test.go

     1  // Copyright 2026 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package test
     6  
     7  import (
     8  	"bytes"
     9  	"cmd/go/internal/load"
    10  	"cmd/go/internal/work"
    11  	"os"
    12  	"path/filepath"
    13  	"testing"
    14  	"time"
    15  )
    16  
    17  func TestComputeTestInputsIDSavesInputs(t *testing.T) {
    18  	root, outside := t.TempDir(), t.TempDir()
    19  	old := time.Now().Add(-time.Hour)
    20  	for _, f := range []string{filepath.Join(root, "a.txt"), filepath.Join(outside, "b.txt")} {
    21  		if err := os.WriteFile(f, nil, 0666); err != nil {
    22  			t.Fatal(err)
    23  		}
    24  		if err := os.Chtimes(f, old, old); err != nil {
    25  			t.Fatal(err)
    26  		}
    27  	}
    28  	a := &work.Action{Package: &load.Package{PackagePublic: load.PackagePublic{Dir: root, Root: root}}}
    29  
    30  	in := []string{
    31  		"getenv HOME",
    32  		"chdir " + outside,
    33  		"open b.txt",
    34  		"stat " + filepath.Join(outside, "missing"),
    35  		"chdir " + root,
    36  		"open a.txt",
    37  		"stat " + filepath.Join(root, "missing"),
    38  		"open " + filepath.Join(outside, "b.txt"),
    39  	}
    40  	var log bytes.Buffer
    41  	log.Write(testlogMagic)
    42  	for _, line := range in {
    43  		log.WriteString(line + "\n")
    44  	}
    45  	var inputs bytes.Buffer
    46  	id, err := computeTestInputsID(a, log.Bytes(), &inputs)
    47  	if err != nil {
    48  		t.Fatal(err)
    49  	}
    50  
    51  	// Saved with the names the ID was computed from, made absolute.
    52  	want := string(testlogMagic) + in[0] + "\n" + in[1] + "\n" + in[4] + "\n" +
    53  		"open " + filepath.Join(root, "a.txt") + "\n" + in[6] + "\n"
    54  	if got := inputs.String(); got != want {
    55  		t.Errorf("saved test log:\n%s\nwant:\n%s", got, want)
    56  	}
    57  	id2, err := computeTestInputsID(a, inputs.Bytes(), nil)
    58  	if err != nil {
    59  		t.Fatal(err)
    60  	}
    61  	if id2 != id {
    62  		t.Errorf("inputs ID of the saved log = %x, want %x", id2, id)
    63  	}
    64  }
    65  

View as plain text