Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

ar_test.go 2.9 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. // Copyright (c) 2018, Google Inc.
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  8. // WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  9. // MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  10. // SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  11. // WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  12. // OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  13. // CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
  14. package ar
  15. import (
  16. "bytes"
  17. "flag"
  18. "io/ioutil"
  19. "os"
  20. "path/filepath"
  21. "testing"
  22. )
  23. var testDataDir = flag.String("testdata", "testdata", "The path to the test data directory.")
  24. type arTest struct {
  25. name string
  26. in string
  27. out map[string]string
  28. // allowPadding is true if the contents may have trailing newlines at end.
  29. // On macOS, ar calls ranlib which pads all inputs up to eight bytes with
  30. // newlines. Unlike ar's native padding up to two bytes, this padding is
  31. // included in the size field, so it is not removed when decoding.
  32. allowPadding bool
  33. }
  34. func (test *arTest) Path(file string) string {
  35. return filepath.Join(*testDataDir, test.name, file)
  36. }
  37. func removeTrailingNewlines(in []byte) []byte {
  38. for len(in) > 0 && in[len(in)-1] == '\n' {
  39. in = in[:len(in)-1]
  40. }
  41. return in
  42. }
  43. var arTests = []arTest{
  44. {
  45. "linux",
  46. "libsample.a",
  47. map[string]string{
  48. "foo.c.o": "foo.c.o",
  49. "bar.cc.o": "bar.cc.o",
  50. },
  51. false,
  52. },
  53. {
  54. "mac",
  55. "libsample.a",
  56. map[string]string{
  57. "foo.c.o": "foo.c.o",
  58. "bar.cc.o": "bar.cc.o",
  59. },
  60. true,
  61. },
  62. {
  63. "windows",
  64. "sample.lib",
  65. map[string]string{
  66. "CMakeFiles\\sample.dir\\foo.c.obj": "foo.c.obj",
  67. "CMakeFiles\\sample.dir\\bar.cc.obj": "bar.cc.obj",
  68. },
  69. false,
  70. },
  71. }
  72. func TestAR(t *testing.T) {
  73. for _, test := range arTests {
  74. t.Run(test.name, func(t *testing.T) {
  75. in, err := os.Open(test.Path(test.in))
  76. if err != nil {
  77. t.Fatalf("opening input failed: %s", err)
  78. }
  79. defer in.Close()
  80. ret, err := ParseAR(in)
  81. if err != nil {
  82. t.Fatalf("reading input failed: %s", err)
  83. }
  84. for file, contentsPath := range test.out {
  85. expected, err := ioutil.ReadFile(test.Path(contentsPath))
  86. if err != nil {
  87. t.Fatalf("error reading %s: %s", contentsPath, err)
  88. }
  89. got, ok := ret[file]
  90. if test.allowPadding {
  91. got = removeTrailingNewlines(got)
  92. expected = removeTrailingNewlines(got)
  93. }
  94. if !ok {
  95. t.Errorf("file %s missing from output", file)
  96. } else if !bytes.Equal(got, expected) {
  97. t.Errorf("contents for file %s did not match", file)
  98. }
  99. }
  100. for file, _ := range ret {
  101. if _, ok := test.out[file]; !ok {
  102. t.Errorf("output contained unexpected file %q", file)
  103. }
  104. }
  105. })
  106. }
  107. }