golden_test.go 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. // Go support for Protocol Buffers - Google's data interchange format
  2. //
  3. // Copyright 2012 The Go Authors. All rights reserved.
  4. // http://code.google.com/p/goprotobuf/
  5. //
  6. // Redistribution and use in source and binary forms, with or without
  7. // modification, are permitted provided that the following conditions are
  8. // met:
  9. //
  10. // * Redistributions of source code must retain the above copyright
  11. // notice, this list of conditions and the following disclaimer.
  12. // * Redistributions in binary form must reproduce the above
  13. // copyright notice, this list of conditions and the following disclaimer
  14. // in the documentation and/or other materials provided with the
  15. // distribution.
  16. // * Neither the name of Google Inc. nor the names of its
  17. // contributors may be used to endorse or promote products derived from
  18. // this software without specific prior written permission.
  19. //
  20. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. // Verify that the compiler output for test.proto is unchanged.
  32. package testdata
  33. import (
  34. "crypto/sha1"
  35. "fmt"
  36. "io/ioutil"
  37. "os"
  38. "os/exec"
  39. "path/filepath"
  40. "testing"
  41. )
  42. // sum returns in string form (for easy comparison) the SHA-1 hash of the named file.
  43. func sum(t *testing.T, name string) string {
  44. data, err := ioutil.ReadFile(name)
  45. if err != nil {
  46. t.Fatal(err)
  47. }
  48. t.Logf("sum(%q): length is %d", name, len(data))
  49. hash := sha1.New()
  50. _, err = hash.Write(data)
  51. if err != nil {
  52. t.Fatal(err)
  53. }
  54. return fmt.Sprintf("% x", hash.Sum(nil))
  55. }
  56. func run(t *testing.T, name string, args ...string) {
  57. cmd := exec.Command(name, args...)
  58. cmd.Stdin = os.Stdin
  59. cmd.Stdout = os.Stdout
  60. cmd.Stderr = os.Stderr
  61. err := cmd.Run()
  62. if err != nil {
  63. t.Fatal(err)
  64. }
  65. }
  66. func TestGolden(t *testing.T) {
  67. // Compute the original checksum.
  68. goldenSum := sum(t, "test.pb.go")
  69. // Run the proto compiler.
  70. run(t, "protoc", "--go_out="+os.TempDir(), "test.proto")
  71. newFile := filepath.Join(os.TempDir(), "test.pb.go")
  72. defer os.Remove(newFile)
  73. // Compute the new checksum.
  74. newSum := sum(t, newFile)
  75. // Verify
  76. if newSum != goldenSum {
  77. run(t, "diff", "-u", "test.pb.go", newFile)
  78. t.Fatal("Code generated by protoc-gen-go has changed; update test.pb.go")
  79. }
  80. }