You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

141 regels
3.8 KiB

  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"
  21. "io/ioutil"
  22. "os"
  23. "os/exec"
  24. "regexp"
  25. "testing"
  26. )
  27. // Diff compares two byte arrays using the diff tool to highlight the
  28. // differences. It is meant for testing purposes to display the
  29. // differences between expected and actual output.
  30. func Diff(b1, b2 []byte) (data []byte, err error) {
  31. f1, err := ioutil.TempFile("", "proto_test")
  32. if err != nil {
  33. return nil, err
  34. }
  35. defer os.Remove(f1.Name())
  36. defer f1.Close()
  37. f2, err := ioutil.TempFile("", "proto_test")
  38. if err != nil {
  39. return nil, err
  40. }
  41. defer os.Remove(f2.Name())
  42. defer f2.Close()
  43. f1.Write(b1)
  44. f2.Write(b2)
  45. data, err = exec.Command("diff", "-u", f1.Name(), f2.Name()).CombinedOutput()
  46. if len(data) > 0 {
  47. // diff exits with a non-zero status when the files don't match.
  48. // Ignore that failure as long as we get output.
  49. err = nil
  50. }
  51. if err != nil {
  52. data = []byte(fmt.Sprintf("diff failed: %v\nb1: %q\nb2: %q\n", err, b1, b2))
  53. err = nil
  54. }
  55. return
  56. }
  57. // EncodeJSON encodes a value into a byte array. This is intended for
  58. // testing purposes.
  59. func EncodeJSON(x interface{}) []byte {
  60. data, err := json.MarshalIndent(x, "", " ")
  61. if err != nil {
  62. panic(err)
  63. }
  64. data = append(data, '\n')
  65. return data
  66. }
  67. // TestUI implements the plugin.UI interface, triggering test failures
  68. // if more than Ignore errors not matching AllowRx are printed.
  69. // Also tracks the number of times the error matches AllowRx in
  70. // NumAllowRxMatches.
  71. type TestUI struct {
  72. T *testing.T
  73. Ignore int
  74. AllowRx string
  75. NumAllowRxMatches int
  76. Input []string
  77. index int
  78. }
  79. // ReadLine returns no input, as no input is expected during testing.
  80. func (ui *TestUI) ReadLine(_ string) (string, error) {
  81. if ui.index >= len(ui.Input) {
  82. return "", io.EOF
  83. }
  84. input := ui.Input[ui.index]
  85. ui.index++
  86. if input == "**error**" {
  87. return "", fmt.Errorf("error: %s", input)
  88. }
  89. return input, nil
  90. }
  91. // Print messages are discarded by the test UI.
  92. func (ui *TestUI) Print(args ...interface{}) {
  93. }
  94. // PrintErr messages may trigger an error failure. A fixed number of
  95. // error messages are permitted when appropriate.
  96. func (ui *TestUI) PrintErr(args ...interface{}) {
  97. if ui.AllowRx != "" {
  98. if matched, err := regexp.MatchString(ui.AllowRx, fmt.Sprint(args...)); matched || err != nil {
  99. if err != nil {
  100. ui.T.Errorf("failed to match against regex %q: %v", ui.AllowRx, err)
  101. }
  102. ui.NumAllowRxMatches++
  103. return
  104. }
  105. }
  106. if ui.Ignore > 0 {
  107. ui.Ignore--
  108. return
  109. }
  110. // Stringify arguments with fmt.Sprint() to match what default UI
  111. // implementation does. Without this Error() calls fmt.Sprintln() which
  112. // _always_ adds spaces between arguments, unlike fmt.Sprint() which only
  113. // adds them between arguments if neither is string.
  114. ui.T.Error("unexpected error: " + fmt.Sprint(args...))
  115. }
  116. // IsTerminal indicates if the UI is an interactive terminal.
  117. func (ui *TestUI) IsTerminal() bool {
  118. return false
  119. }
  120. // WantBrowser indicates whether a browser should be opened with the -http option.
  121. func (ui *TestUI) WantBrowser() bool {
  122. return false
  123. }
  124. // SetAutoComplete is not supported by the test UI.
  125. func (ui *TestUI) SetAutoComplete(_ func(string) string) {
  126. }