Nessuna descrizione

proftest.go 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. return
  50. }
  51. // EncodeJSON encodes a value into a byte array. This is intended for
  52. // testing purposes.
  53. func EncodeJSON(x interface{}) []byte {
  54. data, err := json.MarshalIndent(x, "", " ")
  55. if err != nil {
  56. panic(err)
  57. }
  58. data = append(data, '\n')
  59. return data
  60. }
  61. // TestUI implements the plugin.UI interface, triggering test failures
  62. // if more than Ignore errors are printed.
  63. type TestUI struct {
  64. T *testing.T
  65. Ignore int
  66. }
  67. // ReadLine returns no input, as no input is expected during testing.
  68. func (ui *TestUI) ReadLine(_ string) (string, error) {
  69. return "", fmt.Errorf("no input")
  70. }
  71. // Print messages are discarded by the test UI.
  72. func (ui *TestUI) Print(args ...interface{}) {
  73. }
  74. // PrintErr messages may trigger an error failure. A fixed number of
  75. // error messages are permitted when appropriate.
  76. func (ui *TestUI) PrintErr(args ...interface{}) {
  77. if ui.Ignore > 0 {
  78. ui.Ignore--
  79. return
  80. }
  81. ui.T.Error(args)
  82. }
  83. // IsTerminal indicates if the UI is an interactive terminal.
  84. func (ui *TestUI) IsTerminal() bool {
  85. return false
  86. }
  87. // SetAutoComplete is not supported by the test UI.
  88. func (ui *TestUI) SetAutoComplete(_ func(string) string) {
  89. }