compare_test.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. // Copyright 2019 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 main
  15. import (
  16. "archive/zip"
  17. "reflect"
  18. "testing"
  19. )
  20. func TestDiffTargetFilesLists(t *testing.T) {
  21. zipArtifactFile := func(name string, crc32 uint32, size uint64) *ZipArtifactFile {
  22. return &ZipArtifactFile{
  23. File: &zip.File{
  24. FileHeader: zip.FileHeader{
  25. Name: name,
  26. CRC32: crc32,
  27. UncompressedSize64: size,
  28. },
  29. },
  30. }
  31. }
  32. x0 := zipArtifactFile("x", 0, 0)
  33. x1 := zipArtifactFile("x", 1, 0)
  34. x2 := zipArtifactFile("x", 0, 2)
  35. y0 := zipArtifactFile("y", 0, 0)
  36. //y1 := zipArtifactFile("y", 1, 0)
  37. //y2 := zipArtifactFile("y", 1, 2)
  38. z0 := zipArtifactFile("z", 0, 0)
  39. z1 := zipArtifactFile("z", 1, 0)
  40. //z2 := zipArtifactFile("z", 1, 2)
  41. testCases := []struct {
  42. name string
  43. a, b []*ZipArtifactFile
  44. diff zipDiff
  45. }{
  46. {
  47. name: "same",
  48. a: []*ZipArtifactFile{x0, y0, z0},
  49. b: []*ZipArtifactFile{x0, y0, z0},
  50. diff: zipDiff{nil, nil, nil},
  51. },
  52. {
  53. name: "first only in a",
  54. a: []*ZipArtifactFile{x0, y0, z0},
  55. b: []*ZipArtifactFile{y0, z0},
  56. diff: zipDiff{nil, []*ZipArtifactFile{x0}, nil},
  57. },
  58. {
  59. name: "middle only in a",
  60. a: []*ZipArtifactFile{x0, y0, z0},
  61. b: []*ZipArtifactFile{x0, z0},
  62. diff: zipDiff{nil, []*ZipArtifactFile{y0}, nil},
  63. },
  64. {
  65. name: "last only in a",
  66. a: []*ZipArtifactFile{x0, y0, z0},
  67. b: []*ZipArtifactFile{x0, y0},
  68. diff: zipDiff{nil, []*ZipArtifactFile{z0}, nil},
  69. },
  70. {
  71. name: "first only in b",
  72. a: []*ZipArtifactFile{y0, z0},
  73. b: []*ZipArtifactFile{x0, y0, z0},
  74. diff: zipDiff{nil, nil, []*ZipArtifactFile{x0}},
  75. },
  76. {
  77. name: "middle only in b",
  78. a: []*ZipArtifactFile{x0, z0},
  79. b: []*ZipArtifactFile{x0, y0, z0},
  80. diff: zipDiff{nil, nil, []*ZipArtifactFile{y0}},
  81. },
  82. {
  83. name: "last only in b",
  84. a: []*ZipArtifactFile{x0, y0},
  85. b: []*ZipArtifactFile{x0, y0, z0},
  86. diff: zipDiff{nil, nil, []*ZipArtifactFile{z0}},
  87. },
  88. {
  89. name: "diff",
  90. a: []*ZipArtifactFile{x0},
  91. b: []*ZipArtifactFile{x1},
  92. diff: zipDiff{[][2]*ZipArtifactFile{{x0, x1}}, nil, nil},
  93. },
  94. {
  95. name: "diff plus unique last",
  96. a: []*ZipArtifactFile{x0, y0},
  97. b: []*ZipArtifactFile{x1, z0},
  98. diff: zipDiff{[][2]*ZipArtifactFile{{x0, x1}}, []*ZipArtifactFile{y0}, []*ZipArtifactFile{z0}},
  99. },
  100. {
  101. name: "diff plus unique first",
  102. a: []*ZipArtifactFile{x0, z0},
  103. b: []*ZipArtifactFile{y0, z1},
  104. diff: zipDiff{[][2]*ZipArtifactFile{{z0, z1}}, []*ZipArtifactFile{x0}, []*ZipArtifactFile{y0}},
  105. },
  106. {
  107. name: "diff size",
  108. a: []*ZipArtifactFile{x0},
  109. b: []*ZipArtifactFile{x2},
  110. diff: zipDiff{[][2]*ZipArtifactFile{{x0, x2}}, nil, nil},
  111. },
  112. }
  113. for _, test := range testCases {
  114. t.Run(test.name, func(t *testing.T) {
  115. diff := diffTargetFilesLists(test.a, test.b)
  116. if !reflect.DeepEqual(diff, test.diff) {
  117. t.Errorf("diffTargetFilesLists = %v, %v, %v", diff.modified, diff.onlyInA, diff.onlyInB)
  118. t.Errorf(" want %v, %v, %v", test.diff.modified, test.diff.onlyInA, test.diff.onlyInB)
  119. }
  120. })
  121. }
  122. }