Ei kuvausta

proftest.go 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. // Package proftest provides some utility routines to test other
  15. // packages related to profiles.
  16. package proftest
  17. import (
  18. "encoding/json"
  19. "fmt"
  20. "io/ioutil"
  21. "os"
  22. "os/exec"
  23. "testing"
  24. )
  25. // Diff compares two byte arrays using the diff tool to highlight the
  26. // differences. It is meant for testing purposes to display the
  27. // differences between expected and actual output.
  28. func Diff(b1, b2 []byte) (data []byte, err error) {
  29. f1, err := ioutil.TempFile("", "proto_test")
  30. if err != nil {
  31. return nil, err
  32. }
  33. defer os.Remove(f1.Name())
  34. defer f1.Close()
  35. f2, err := ioutil.TempFile("", "proto_test")
  36. if err != nil {
  37. return nil, err
  38. }
  39. defer os.Remove(f2.Name())
  40. defer f2.Close()
  41. f1.Write(b1)
  42. f2.Write(b2)
  43. data, err = exec.Command("diff", "-u", f1.Name(), f2.Name()).CombinedOutput()
  44. if len(data) > 0 {
  45. // diff exits with a non-zero status when the files don't match.
  46. // Ignore that failure as long as we get output.
  47. err = nil
  48. }
  49. if err != nil {
  50. data = []byte(fmt.Sprintf("diff failed: %v\nb1: %q\nb2: %q\n", err, b1, b2))
  51. err = nil
  52. }
  53. return
  54. }
  55. // EncodeJSON encodes a value into a byte array. This is intended for
  56. // testing purposes.
  57. func EncodeJSON(x interface{}) []byte {
  58. data, err := json.MarshalIndent(x, "", " ")
  59. if err != nil {
  60. panic(err)
  61. }
  62. data = append(data, '\n')
  63. return data
  64. }
  65. // TestUI implements the plugin.UI interface, triggering test failures
  66. // if more than Ignore errors are printed.
  67. type TestUI struct {
  68. T *testing.T
  69. Ignore int
  70. }
  71. // ReadLine returns no input, as no input is expected during testing.
  72. func (ui *TestUI) ReadLine(_ string) (string, error) {
  73. return "", fmt.Errorf("no input")
  74. }
  75. // Print messages are discarded by the test UI.
  76. func (ui *TestUI) Print(args ...interface{}) {
  77. }
  78. // PrintErr messages may trigger an error failure. A fixed number of
  79. // error messages are permitted when appropriate.
  80. func (ui *TestUI) PrintErr(args ...interface{}) {
  81. if ui.Ignore > 0 {
  82. ui.Ignore--
  83. return
  84. }
  85. ui.T.Error(args)
  86. }
  87. // IsTerminal indicates if the UI is an interactive terminal.
  88. func (ui *TestUI) IsTerminal() bool {
  89. return false
  90. }
  91. // SetAutoComplete is not supported by the test UI.
  92. func (ui *TestUI) SetAutoComplete(_ func(string) string) {
  93. }