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