golang.org/toolchain@v0.0.1-go1.9rc2.windows-amd64/src/cmd/vendor/github.com/google/pprof/internal/proftest/proftest.go (about) 1 // Copyright 2014 Google Inc. All Rights Reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 // Package proftest provides some utility routines to test other 16 // packages related to profiles. 17 package proftest 18 19 import ( 20 "encoding/json" 21 "fmt" 22 "io/ioutil" 23 "os" 24 "os/exec" 25 "testing" 26 ) 27 28 // Diff compares two byte arrays using the diff tool to highlight the 29 // differences. It is meant for testing purposes to display the 30 // differences between expected and actual output. 31 func Diff(b1, b2 []byte) (data []byte, err error) { 32 f1, err := ioutil.TempFile("", "proto_test") 33 if err != nil { 34 return nil, err 35 } 36 defer os.Remove(f1.Name()) 37 defer f1.Close() 38 39 f2, err := ioutil.TempFile("", "proto_test") 40 if err != nil { 41 return nil, err 42 } 43 defer os.Remove(f2.Name()) 44 defer f2.Close() 45 46 f1.Write(b1) 47 f2.Write(b2) 48 49 data, err = exec.Command("diff", "-u", f1.Name(), f2.Name()).CombinedOutput() 50 if len(data) > 0 { 51 // diff exits with a non-zero status when the files don't match. 52 // Ignore that failure as long as we get output. 53 err = nil 54 } 55 if err != nil { 56 data = []byte(fmt.Sprintf("diff failed: %v\nb1: %q\nb2: %q\n", err, b1, b2)) 57 err = nil 58 } 59 return 60 } 61 62 // EncodeJSON encodes a value into a byte array. This is intended for 63 // testing purposes. 64 func EncodeJSON(x interface{}) []byte { 65 data, err := json.MarshalIndent(x, "", " ") 66 if err != nil { 67 panic(err) 68 } 69 data = append(data, '\n') 70 return data 71 } 72 73 // TestUI implements the plugin.UI interface, triggering test failures 74 // if more than Ignore errors are printed. 75 type TestUI struct { 76 T *testing.T 77 Ignore int 78 } 79 80 // ReadLine returns no input, as no input is expected during testing. 81 func (ui *TestUI) ReadLine(_ string) (string, error) { 82 return "", fmt.Errorf("no input") 83 } 84 85 // Print messages are discarded by the test UI. 86 func (ui *TestUI) Print(args ...interface{}) { 87 } 88 89 // PrintErr messages may trigger an error failure. A fixed number of 90 // error messages are permitted when appropriate. 91 func (ui *TestUI) PrintErr(args ...interface{}) { 92 if ui.Ignore > 0 { 93 ui.Ignore-- 94 return 95 } 96 ui.T.Error(args) 97 } 98 99 // IsTerminal indicates if the UI is an interactive terminal. 100 func (ui *TestUI) IsTerminal() bool { 101 return false 102 } 103 104 // SetAutoComplete is not supported by the test UI. 105 func (ui *TestUI) SetAutoComplete(_ func(string) string) { 106 }