Без опису

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 AllowRx are printed.
  68. // Also tracks the number of times the error matches AllowRx in
  69. // NumAllowRxMatches.
  70. type TestUI struct {
  71. T *testing.T
  72. Ignore int
  73. AllowRx string
  74. NumAllowRxMatches int
  75. }
  76. // ReadLine returns no input, as no input is expected during testing.
  77. func (ui *TestUI) ReadLine(_ string) (string, error) {
  78. return "", fmt.Errorf("no input")
  79. }
  80. // Print messages are discarded by the test UI.
  81. func (ui *TestUI) Print(args ...interface{}) {
  82. }
  83. // PrintErr messages may trigger an error failure. A fixed number of
  84. // error messages are permitted when appropriate.
  85. func (ui *TestUI) PrintErr(args ...interface{}) {
  86. if ui.AllowRx != "" {
  87. if matched, err := regexp.MatchString(ui.AllowRx, fmt.Sprint(args...)); matched || err != nil {
  88. if err != nil {
  89. ui.T.Errorf("failed to match against regex %q: %v", ui.AllowRx, err)
  90. }
  91. ui.NumAllowRxMatches++
  92. return
  93. }
  94. }
  95. if ui.Ignore > 0 {
  96. ui.Ignore--
  97. return
  98. }
  99. // Stringify arguments with fmt.Sprint() to match what default UI
  100. // implementation does. Without this Error() calls fmt.Sprintln() which
  101. // _always_ adds spaces between arguments, unlike fmt.Sprint() which only
  102. // adds them between arguments if neither is string.
  103. ui.T.Error(fmt.Sprint(args...))
  104. }
  105. // IsTerminal indicates if the UI is an interactive terminal.
  106. func (ui *TestUI) IsTerminal() bool {
  107. return false
  108. }
  109. // SetAutoComplete is not supported by the test UI.
  110. func (ui *TestUI) SetAutoComplete(_ func(string) string) {
  111. }