Source file src/cmd/compile/internal/test/clobberdead_test.go

     1  // Copyright 2021 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  	"internal/testenv"
     9  	"os"
    10  	"path/filepath"
    11  	"testing"
    12  )
    13  
    14  const helloSrc = `
    15  package main
    16  import "fmt"
    17  func main() { fmt.Println("hello") }
    18  `
    19  
    20  func TestClobberDead(t *testing.T) {
    21  	// Test that clobberdead mode generates correct program.
    22  	runHello(t, "-clobberdead")
    23  }
    24  
    25  func TestClobberDeadReg(t *testing.T) {
    26  	// Test that clobberdeadreg mode generates correct program.
    27  	runHello(t, "-clobberdeadreg")
    28  }
    29  
    30  func runHello(t *testing.T, flag string) {
    31  	if testing.Short() {
    32  		// This test rebuilds the runtime with a special flag, which
    33  		// takes a while.
    34  		t.Skip("skip in short mode")
    35  	}
    36  	testenv.MustHaveGoRun(t)
    37  	t.Parallel()
    38  
    39  	tmpdir := t.TempDir()
    40  	src := filepath.Join(tmpdir, "x.go")
    41  	err := os.WriteFile(src, []byte(helloSrc), 0644)
    42  	if err != nil {
    43  		t.Fatalf("write file failed: %v", err)
    44  	}
    45  
    46  	cmd := testenv.Command(t, testenv.GoToolPath(t), "run", "-gcflags=all="+flag, src)
    47  	out, err := cmd.CombinedOutput()
    48  	if err != nil {
    49  		t.Fatalf("go run failed: %v\n%s", err, out)
    50  	}
    51  	if string(out) != "hello\n" {
    52  		t.Errorf("wrong output: got %q, want %q", out, "hello\n")
    53  	}
    54  }
    55  

View as plain text