github.com/keybase/client/go@v0.0.0-20240309051027-028f7c731f8b/updater/test/test.go (about) 1 // Copyright 2016 Keybase, Inc. All rights reserved. Use of 2 // this source code is governed by the included BSD license. 3 4 package main 5 6 import ( 7 "flag" 8 "fmt" 9 "log" 10 "os" 11 "os/signal" 12 "syscall" 13 "time" 14 ) 15 16 type flags struct { 17 out string 18 outPath string 19 } 20 21 // This is a test executable built and installed prior to test run, which is 22 // useful for testing some command.go functions. 23 func main() { 24 f := flags{} 25 flag.StringVar(&f.out, "out", "", "Output") 26 flag.StringVar(&f.outPath, "outPath", "", "Output path") 27 flag.Parse() 28 var arg = flag.Arg(0) 29 30 switch arg { 31 case "noexit": 32 noexit() 33 case "output": 34 output() 35 case "echo": 36 echo(flag.Arg(1)) 37 case "writeToFile": 38 writeToFile(f.out, f.outPath) 39 case "version": 40 echo("1.2.3-400+cafebeef") 41 case "err": 42 log.Fatal("Error") 43 case "sleep": 44 time.Sleep(10 * time.Second) 45 case "/layout": 46 if flag.NArg() < 4 { 47 log.Fatal("Error in /layout command: requires \"/layout /quiet /log filename\"") 48 } 49 copyFakeLayout(flag.Arg(3)) 50 default: 51 log.Printf("test") 52 } 53 } 54 55 func noexit() { 56 c := make(chan os.Signal, 1) 57 signal.Notify(c, syscall.SIGTERM) 58 go func() { 59 <-c 60 fmt.Printf("Got SIGTERM, not exiting on purpose") 61 // Don't exit on SIGTERM, so we can test timeout with SIGKILL 62 }() 63 fmt.Printf("Waiting for 10 seconds...") 64 time.Sleep(10 * time.Second) 65 } 66 67 func output() { 68 fmt.Fprintln(os.Stdout, "stdout output") 69 fmt.Fprintln(os.Stderr, "stderr output") 70 } 71 72 func echo(s string) { 73 fmt.Fprintln(os.Stdout, s) 74 } 75 76 func writeToFile(s string, path string) { 77 err := os.WriteFile(path, []byte(s), 0700) 78 if err != nil { 79 log.Fatalf("Error writing to file: %s", err) 80 } 81 } 82 83 func copyFakeLayout(dst string) { 84 // Read all content of src to data 85 data, err := os.ReadFile("winlayout.log") 86 if err != nil { 87 log.Fatalf("Error reading winlayout.log: %s", err) 88 } 89 // Write data to dst 90 err = os.WriteFile(dst, data, 0644) 91 if err != nil { 92 log.Fatalf("Error writing to %s: %s", dst, err) 93 } 94 }