Nav apraksta

proftest.go 3.1KB

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