| 6 | // |
| 7 | // https://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 ar |
| 16 |
| 17 | import ( |
| 18 | "bytes" |
| 19 | "flag" |
| 20 | "os" |
| 21 | "path/filepath" |
| 22 | "testing" |
| 23 | ) |
| 24 |
| 25 | var testDataDir = flag.String("testdata", "testdata", "The path to the test data directory.") |
| 26 |
| 27 | type arTest struct { |
| 28 | name string |
| 29 | in string |
| 30 | out map[string]string |
| 31 | // allowPadding is true if the contents may have trailing newlines at end. |
| 32 | // On macOS, ar calls ranlib which pads all inputs up to eight bytes with |
| 33 | // newlines. Unlike ar's native padding up to two bytes, this padding is |
| 34 | // included in the size field, so it is not removed when decoding. |
| 35 | allowPadding bool |
| 36 | } |
| 37 |
| 38 | func (test *arTest) Path(file string) string { |
| 39 | return filepath.Join(*testDataDir, test.name, file) |
| 40 | } |
| 41 |
| 42 | func removeTrailingNewlines(in []byte) []byte { |
| 43 | for len(in) > 0 && in[len(in)-1] == '\n' { |
| 44 | in = in[:len(in)-1] |
| 45 | } |
| 46 | return in |
| 47 | } |
| 48 |
| 49 | var arTests = []arTest{ |
| 50 | { |
| 51 | "linux", |
| 52 | "libsample.a", |
| 53 | map[string]string{ |
| 54 | "foo.c.o": "foo.c.o", |
| 55 | "bar.cc.o": "bar.cc.o", |
| 56 | }, |
| 57 | false, |
| 58 | }, |
| 59 | { |
| 60 | "mac", |
| 61 | "libsample.a", |
| 62 | map[string]string{ |
| 63 | "foo.c.o": "foo.c.o", |
| 64 | "bar.cc.o": "bar.cc.o", |
| 65 | }, |
| 66 | true, |
| 67 | }, |
| 68 | { |
| 69 | "windows", |
| 70 | "sample.lib", |
| 71 | map[string]string{ |
| 72 | "CMakeFiles\\sample.dir\\foo.c.obj": "foo.c.obj", |
| 73 | "CMakeFiles\\sample.dir\\bar.cc.obj": "bar.cc.obj", |
| 74 | }, |
| 75 | false, |
| 76 | }, |
| 77 | } |
| 78 |
| 79 | func TestAR(t *testing.T) { |
| 80 | for _, test := range arTests { |
| 81 | t.Run(test.name, func(t *testing.T) { |
| 82 | in, err := os.Open(test.Path(test.in)) |
| 83 | if err != nil { |
| 84 | t.Fatalf("opening input failed: %s", err) |
| 85 | } |
| 86 | defer in.Close() |
| 87 |
| 88 | ret, err := ParseAR(in) |
| 89 | if err != nil { |
| 90 | t.Fatalf("reading input failed: %s", err) |
| 91 | } |
| 92 |
| 93 | for file, contentsPath := range test.out { |
| 94 | expected, err := os.ReadFile(test.Path(contentsPath)) |
| 95 | if err != nil { |
| 96 | t.Fatalf("error reading %s: %s", contentsPath, err) |
| 97 | } |
| 98 | got, ok := ret[file] |
| 99 | if test.allowPadding { |
| 100 | got = removeTrailingNewlines(got) |
| 101 | expected = removeTrailingNewlines(got) |
| 102 | } |
| 103 | if !ok { |
| 104 | t.Errorf("file %s missing from output", file) |
| 105 | } else if !bytes.Equal(got, expected) { |
| 106 | t.Errorf("contents for file %s did not match", file) |
| 107 | } |
| 108 | } |
| 109 |
| 110 | for file, _ := range ret { |
| 111 | if _, ok := test.out[file]; !ok { |
| 112 | t.Errorf("output contained unexpected file %q", file) |
| 113 | } |
| 114 | } |
| 115 | }) |
| 116 | } |
| 117 | } |
| 118 |