Black Lives Matter. Support the Equal Justice Initiative.

Source file test/linkx_run.go

Documentation: test

     1  // +build !nacl,!js,gc
     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 linkx test.
     9  
    10  package main
    11  
    12  import (
    13  	"bytes"
    14  	"fmt"
    15  	"os"
    16  	"os/exec"
    17  	"strings"
    18  )
    19  
    20  func main() {
    21  	// test(" ") // old deprecated & removed syntax
    22  	test("=") // new syntax
    23  }
    24  
    25  func test(sep string) {
    26  	// Successful run
    27  	cmd := exec.Command("go", "run", "-ldflags=-X main.tbd"+sep+"hello -X main.overwrite"+sep+"trumped -X main.nosuchsymbol"+sep+"neverseen", "linkx.go")
    28  	var out, errbuf bytes.Buffer
    29  	cmd.Stdout = &out
    30  	cmd.Stderr = &errbuf
    31  	err := cmd.Run()
    32  	if err != nil {
    33  		fmt.Println(errbuf.String())
    34  		fmt.Println(out.String())
    35  		fmt.Println(err)
    36  		os.Exit(1)
    37  	}
    38  
    39  	want := "hello\nhello\nhello\ntrumped\ntrumped\ntrumped\n"
    40  	got := out.String()
    41  	if got != want {
    42  		fmt.Printf("got %q want %q\n", got, want)
    43  		os.Exit(1)
    44  	}
    45  
    46  	// Issue 8810
    47  	cmd = exec.Command("go", "run", "-ldflags=-X main.tbd", "linkx.go")
    48  	_, err = cmd.CombinedOutput()
    49  	if err == nil {
    50  		fmt.Println("-X linker flag should not accept keys without values")
    51  		os.Exit(1)
    52  	}
    53  
    54  	// Issue 9621
    55  	cmd = exec.Command("go", "run", "-ldflags=-X main.b=false -X main.x=42", "linkx.go")
    56  	outx, err := cmd.CombinedOutput()
    57  	if err == nil {
    58  		fmt.Println("-X linker flag should not overwrite non-strings")
    59  		os.Exit(1)
    60  	}
    61  	outstr := string(outx)
    62  	if !strings.Contains(outstr, "main.b") {
    63  		fmt.Printf("-X linker flag did not diagnose overwrite of main.b:\n%s\n", outstr)
    64  		os.Exit(1)
    65  	}
    66  	if !strings.Contains(outstr, "main.x") {
    67  		fmt.Printf("-X linker flag did not diagnose overwrite of main.x:\n%s\n", outstr)
    68  		os.Exit(1)
    69  	}
    70  }
    71  

View as plain text