| 
									
										
										
										
											2014-11-09 21:10:49 -05:00
										 |  |  | // +build !nacl | 
					
						
							| 
									
										
										
										
											2014-11-06 15:14:08 -05:00
										 |  |  | // run | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Copyright 2014 The Go Authors. All rights reserved. | 
					
						
							|  |  |  | // Use of this source code is governed by a BSD-style | 
					
						
							|  |  |  | // license that can be found in the LICENSE file. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Run the linkx test. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | package main | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2015-05-21 14:35:02 -04:00
										 |  |  | 	"bytes" | 
					
						
							| 
									
										
										
										
											2014-11-06 15:14:08 -05:00
										 |  |  | 	"fmt" | 
					
						
							|  |  |  | 	"os" | 
					
						
							|  |  |  | 	"os/exec" | 
					
						
							| 
									
										
										
										
											2015-06-29 13:03:11 -04:00
										 |  |  | 	"strings" | 
					
						
							| 
									
										
										
										
											2014-11-06 15:14:08 -05:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func main() { | 
					
						
							| 
									
										
										
										
											2016-01-28 22:50:03 -05:00
										 |  |  | 	// test(" ") // old deprecated & removed syntax | 
					
						
							| 
									
										
										
										
											2015-05-21 14:35:02 -04:00
										 |  |  | 	test("=") // new syntax | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func test(sep string) { | 
					
						
							| 
									
										
										
										
											2014-12-31 13:25:52 -08:00
										 |  |  | 	// Successful run | 
					
						
							| 
									
										
										
										
											2015-05-21 14:35:02 -04:00
										 |  |  | 	cmd := exec.Command("go", "run", "-ldflags=-X main.tbd"+sep+"hello -X main.overwrite"+sep+"trumped -X main.nosuchsymbol"+sep+"neverseen", "linkx.go") | 
					
						
							|  |  |  | 	var out, errbuf bytes.Buffer | 
					
						
							|  |  |  | 	cmd.Stdout = &out | 
					
						
							|  |  |  | 	cmd.Stderr = &errbuf | 
					
						
							|  |  |  | 	err := cmd.Run() | 
					
						
							| 
									
										
										
										
											2014-11-06 15:14:08 -05:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2015-05-21 14:35:02 -04:00
										 |  |  | 		fmt.Println(errbuf.String()) | 
					
						
							|  |  |  | 		fmt.Println(out.String()) | 
					
						
							| 
									
										
										
										
											2014-11-06 15:14:08 -05:00
										 |  |  | 		fmt.Println(err) | 
					
						
							|  |  |  | 		os.Exit(1) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	want := "hello\ntrumped\n" | 
					
						
							| 
									
										
										
										
											2015-05-21 14:35:02 -04:00
										 |  |  | 	got := out.String() | 
					
						
							| 
									
										
										
										
											2014-11-06 15:14:08 -05:00
										 |  |  | 	if got != want { | 
					
						
							|  |  |  | 		fmt.Printf("got %q want %q\n", got, want) | 
					
						
							|  |  |  | 		os.Exit(1) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-12-31 13:25:52 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// Issue 8810 | 
					
						
							|  |  |  | 	cmd = exec.Command("go", "run", "-ldflags=-X main.tbd", "linkx.go") | 
					
						
							|  |  |  | 	_, err = cmd.CombinedOutput() | 
					
						
							|  |  |  | 	if err == nil { | 
					
						
							|  |  |  | 		fmt.Println("-X linker flag should not accept keys without values") | 
					
						
							|  |  |  | 		os.Exit(1) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-06-29 13:03:11 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// Issue 9621 | 
					
						
							|  |  |  | 	cmd = exec.Command("go", "run", "-ldflags=-X main.b=false -X main.x=42", "linkx.go") | 
					
						
							|  |  |  | 	outx, err := cmd.CombinedOutput() | 
					
						
							|  |  |  | 	if err == nil { | 
					
						
							|  |  |  | 		fmt.Println("-X linker flag should not overwrite non-strings") | 
					
						
							|  |  |  | 		os.Exit(1) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	outstr := string(outx) | 
					
						
							|  |  |  | 	if !strings.Contains(outstr, "main.b") { | 
					
						
							| 
									
										
										
										
											2016-04-20 10:29:43 -04:00
										 |  |  | 		fmt.Printf("-X linker flag did not diagnose overwrite of main.b:\n%s\n", outstr) | 
					
						
							| 
									
										
										
										
											2015-06-29 13:03:11 -04:00
										 |  |  | 		os.Exit(1) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if !strings.Contains(outstr, "main.x") { | 
					
						
							| 
									
										
										
										
											2016-04-20 10:29:43 -04:00
										 |  |  | 		fmt.Printf("-X linker flag did not diagnose overwrite of main.x:\n%s\n", outstr) | 
					
						
							| 
									
										
										
										
											2015-06-29 13:03:11 -04:00
										 |  |  | 		os.Exit(1) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-11-06 15:14:08 -05:00
										 |  |  | } |