modernc.org/gc@v1.0.1-0.20240304020402-f0dba7c97c2b/testdata/errchk/test/linkmain_run.go (about)

     1  // +build !nacl
     2  // run
     3  
     4  // Copyright 2014 The Go Authors. All rights reserved.
     5  // Use of this source code is governed by a BSD-style
     6  // license that can be found in the LICENSE file.
     7  
     8  // Run the sinit test.
     9  
    10  package main
    11  
    12  import (
    13  	"fmt"
    14  	"os"
    15  	"os/exec"
    16  	"strings"
    17  )
    18  
    19  func cleanup() {
    20  	os.Remove("linkmain.o")
    21  	os.Remove("linkmain.a")
    22  	os.Remove("linkmain1.o")
    23  	os.Remove("linkmain1.a")
    24  	os.Remove("linkmain.exe")
    25  }
    26  
    27  func run(cmdline string) {
    28  	args := strings.Fields(cmdline)
    29  	cmd := exec.Command(args[0], args[1:]...)
    30  	out, err := cmd.CombinedOutput()
    31  	if err != nil {
    32  		fmt.Printf("$ %s\n", cmdline)
    33  		fmt.Println(string(out))
    34  		fmt.Println(err)
    35  		cleanup()
    36  		os.Exit(1)
    37  	}
    38  }
    39  
    40  func runFail(cmdline string) {
    41  	args := strings.Fields(cmdline)
    42  	cmd := exec.Command(args[0], args[1:]...)
    43  	out, err := cmd.CombinedOutput()
    44  	if err == nil {
    45  		fmt.Printf("$ %s\n", cmdline)
    46  		fmt.Println(string(out))
    47  		fmt.Println("SHOULD HAVE FAILED!")
    48  		cleanup()
    49  		os.Exit(1)
    50  	}
    51  }
    52  
    53  func main() {
    54  	// helloworld.go is package main
    55  	run("go tool compile -o linkmain.o helloworld.go")
    56  	run("go tool compile -pack -o linkmain.a helloworld.go")
    57  	run("go tool link -o linkmain.exe linkmain.o")
    58  	run("go tool link -o linkmain.exe linkmain.a")
    59  
    60  	// linkmain.go is not
    61  	run("go tool compile -o linkmain1.o linkmain.go")
    62  	run("go tool compile -pack -o linkmain1.a linkmain.go")
    63  	runFail("go tool link -o linkmain.exe linkmain1.o")
    64  	runFail("go tool link -o linkmain.exe linkmain1.a")
    65  	cleanup()
    66  }