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

View as plain text