golang.org/x/tools/gopls@v0.15.3/internal/vulncheck/vulntest/report_test.go (about)

     1  // Copyright 2022 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  //go:build go1.18
     6  // +build go1.18
     7  
     8  package vulntest
     9  
    10  import (
    11  	"bytes"
    12  	"io"
    13  	"os"
    14  	"path/filepath"
    15  	"testing"
    16  
    17  	"github.com/google/go-cmp/cmp"
    18  )
    19  
    20  func readAll(t *testing.T, filename string) io.Reader {
    21  	d, err := os.ReadFile(filename)
    22  	if err != nil {
    23  		t.Fatal(err)
    24  	}
    25  	return bytes.NewReader(d)
    26  }
    27  
    28  func TestRoundTrip(t *testing.T) {
    29  	// A report shouldn't change after being read and then written.
    30  	in := filepath.Join("testdata", "report.yaml")
    31  	r, err := readReport(readAll(t, in))
    32  	if err != nil {
    33  		t.Fatal(err)
    34  	}
    35  	out := filepath.Join(t.TempDir(), "report.yaml")
    36  	if err := r.Write(out); err != nil {
    37  		t.Fatal(err)
    38  	}
    39  
    40  	want, err := os.ReadFile(in)
    41  	if err != nil {
    42  		t.Fatal(err)
    43  	}
    44  	got, err := os.ReadFile(out)
    45  	if err != nil {
    46  		t.Fatal(err)
    47  	}
    48  	if diff := cmp.Diff(want, got); diff != "" {
    49  		t.Errorf("mismatch (-want, +got):\n%s", diff)
    50  	}
    51  }