github.com/distbuild/reclient@v0.0.0-20240401075343-3de72e395564/internal/pkg/inputprocessor/action/clanglink/ar_reader_test.go (about) 1 // Copyright 2023 Google LLC 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 15 package clanglink 16 17 import ( 18 "runtime" 19 "testing" 20 21 "github.com/bazelbuild/rules_go/go/tools/bazel" 22 "github.com/google/go-cmp/cmp" 23 ) 24 25 func TestReadFileNamesCalledMultipleTimes(t *testing.T) { 26 test := []struct { 27 name string 28 file string 29 want []string 30 wantErr bool 31 }{ 32 { 33 name: "Reading archive file.", 34 file: "testdata/testarchive.a", 35 want: []string{ 36 "foo.o", 37 "bar.o", 38 "baz.o", 39 }, 40 wantErr: false, 41 }, 42 { 43 name: "Reading extended file names with BSD file format.", 44 file: "testdata/bsd.a", 45 want: []string{ 46 "bsd_extended_file_name.txt", 47 "another_bsd_extended_file_name.txt", 48 }, 49 wantErr: false, 50 }, 51 { 52 name: "Reading extended file names with GNU file format.", 53 file: "testdata/gnu.a", 54 want: []string{ 55 "gnu_extended_file_name.txt", 56 "another_gnu_extended_file_name.txt", 57 }, 58 wantErr: false, 59 }, 60 { 61 name: "2 byte alignment in data section.", 62 file: "testdata/byte_alignment.a", 63 want: []string{ 64 "odd", 65 "even", 66 "new_file", 67 }, 68 wantErr: false, 69 }, 70 { 71 name: "malformed archive file.", 72 file: "testdata/malformed.a", 73 want: []string{}, 74 wantErr: true, 75 }, 76 { 77 name: "thin archive file.", 78 file: "testdata/thinarchive.a", 79 want: map[string][]string{ 80 "darwin": []string{ 81 "DirA/foo.o", 82 "DirB/bar.o", 83 }, 84 "linux": []string{ 85 "DirA/foo.o", 86 "DirB/bar.o", 87 }, 88 "windows": []string{ 89 "DirA\\foo.o", 90 "DirB\\bar.o", 91 }, 92 }[runtime.GOOS], 93 wantErr: false, 94 }, 95 } 96 97 for _, test := range test { 98 t.Run(test.name, func(t *testing.T) { 99 file, err := bazel.Runfile(test.file) 100 if err != nil { 101 t.Fatalf("Failed to find archive: %v err: %v", test.file, err) 102 } 103 arReader, err := newArReader(file, "") 104 if arReader != nil { 105 defer arReader.Close() 106 } 107 if !test.wantErr && err != nil { 108 t.Errorf("newArReader(f) returned error: %v", err) 109 } 110 111 for i := 0; i < 5; i++ { 112 if arReader != nil { 113 got, err := arReader.ReadFileNames() 114 if err != nil { 115 t.Errorf("arReader.ReadFileNames() returned error: %v", err) 116 } 117 118 if diff := cmp.Diff(test.want, got); diff != "" { 119 t.Errorf("arReader.ReadFileNames() failed on run %v returned diff, (-want +got): %s", i+1, diff) 120 } 121 } 122 } 123 124 }) 125 } 126 127 }