Source file test/linkmain_run.go

     1  // +build !nacl,!js
     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  	"io/ioutil"
    15  	"os"
    16  	"os/exec"
    17  	"path/filepath"
    18  	"strings"
    19  )
    20  
    21  var tmpDir string
    22  
    23  func cleanup() {
    24  	os.RemoveAll(tmpDir)
    25  }
    26  
    27  func run(cmdline ...string) {
    28  	args := strings.Fields(strings.Join(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(strings.Join(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  	var err error
    55  	tmpDir, err = ioutil.TempDir("", "")
    56  	if err != nil {
    57  		fmt.Println(err)
    58  		os.Exit(1)
    59  	}
    60  	tmp := func(name string) string {
    61  		return filepath.Join(tmpDir, name)
    62  	}
    63  
    64  	// helloworld.go is package main
    65  	run("go tool compile -p=main -o", tmp("linkmain.o"), "helloworld.go")
    66  	run("go tool compile -p=main -pack -o", tmp("linkmain.a"), "helloworld.go")
    67  	run("go tool link -o", tmp("linkmain.exe"), tmp("linkmain.o"))
    68  	run("go tool link -o", tmp("linkmain.exe"), tmp("linkmain.a"))
    69  
    70  	// linkmain.go is not
    71  	run("go tool compile -p=notmain -o", tmp("linkmain1.o"), "linkmain.go")
    72  	run("go tool compile -p=notmain -pack -o", tmp("linkmain1.a"), "linkmain.go")
    73  	runFail("go tool link -o", tmp("linkmain.exe"), tmp("linkmain1.o"))
    74  	runFail("go tool link -o", tmp("linkmain.exe"), tmp("linkmain1.a"))
    75  	cleanup()
    76  }
    77  

View as plain text