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