|
| 1 | +// Copyright 2022 The Bazel Authors. 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 | + |
| 15 | +package com.google.devtools.build.lib.bazel.repository; |
| 16 | + |
| 17 | +import static com.google.common.truth.Truth.assertThat; |
| 18 | + |
| 19 | +import com.google.devtools.build.lib.testutil.TestConstants; |
| 20 | +import com.google.devtools.build.lib.testutil.TestUtils; |
| 21 | +import com.google.devtools.build.lib.unix.UnixFileSystem; |
| 22 | +import com.google.devtools.build.lib.util.OS; |
| 23 | +import com.google.devtools.build.lib.vfs.DigestHashFunction; |
| 24 | +import com.google.devtools.build.lib.vfs.FileSystem; |
| 25 | +import com.google.devtools.build.lib.vfs.JavaIoFileSystem; |
| 26 | +import com.google.devtools.build.lib.vfs.Path; |
| 27 | +import com.google.devtools.build.runfiles.Runfiles; |
| 28 | +import java.io.File; |
| 29 | +import java.io.IOException; |
| 30 | +import org.junit.Test; |
| 31 | +import org.junit.runner.RunWith; |
| 32 | +import org.junit.runners.JUnit4; |
| 33 | + |
| 34 | +/** Tests decompressing archives. */ |
| 35 | +@RunWith(JUnit4.class) |
| 36 | +public class ArFunctionTest { |
| 37 | + /* |
| 38 | + * .ar archive created with ar cr test_files.ar archived_first.txt archived_second.md |
| 39 | + * The files contain short UTF-8 encoded strings. |
| 40 | + */ |
| 41 | + private static final String ARCHIVE_NAME = "test_files.ar"; |
| 42 | + private static final String PATH_TO_TEST_ARCHIVE = |
| 43 | + "/com/google/devtools/build/lib/bazel/repository/"; |
| 44 | + private static final String FIRST_FILE_NAME = "archived_first.txt"; |
| 45 | + private static final String SECOND_FILE_NAME = "archived_second.md"; |
| 46 | + |
| 47 | + @Test |
| 48 | + public void testDecompress() throws Exception { |
| 49 | + Path outputDir = decompress(createDescriptorBuilder()); |
| 50 | + |
| 51 | + assertThat(outputDir.exists()).isTrue(); |
| 52 | + Path firstFile = outputDir.getRelative(FIRST_FILE_NAME); |
| 53 | + assertThat(firstFile.exists()).isTrue(); |
| 54 | + // There are 20 bytes in the content "this is test file 1" |
| 55 | + assertThat(firstFile.getFileSize()).isEqualTo(20); |
| 56 | + assertThat(firstFile.isSymbolicLink()).isFalse(); |
| 57 | + |
| 58 | + Path secondFile = outputDir.getRelative(SECOND_FILE_NAME); |
| 59 | + assertThat(secondFile.exists()).isTrue(); |
| 60 | + // There are 20 bytes in the content "this is the second test file" |
| 61 | + assertThat(secondFile.getFileSize()).isEqualTo(29); |
| 62 | + assertThat(secondFile.isSymbolicLink()).isFalse(); |
| 63 | + } |
| 64 | + |
| 65 | + private Path decompress(DecompressorDescriptor.Builder descriptorBuilder) throws Exception { |
| 66 | + descriptorBuilder.setDecompressor(ArFunction.INSTANCE); |
| 67 | + return new ArFunction().decompress(descriptorBuilder.build()); |
| 68 | + } |
| 69 | + |
| 70 | + private DecompressorDescriptor.Builder createDescriptorBuilder() throws IOException { |
| 71 | + // This was cribbed from TestArchiveDescriptor |
| 72 | + FileSystem testFS = |
| 73 | + OS.getCurrent() == OS.WINDOWS |
| 74 | + ? new JavaIoFileSystem(DigestHashFunction.SHA256) |
| 75 | + : new UnixFileSystem(DigestHashFunction.SHA256, /*hashAttributeName=*/ ""); |
| 76 | + |
| 77 | + // do not rely on TestConstants.JAVATESTS_ROOT end with slash, but ensure separators |
| 78 | + // are not duplicated |
| 79 | + String path = |
| 80 | + (TestConstants.JAVATESTS_ROOT + PATH_TO_TEST_ARCHIVE + ARCHIVE_NAME).replace("//", "/"); |
| 81 | + Path tarballPath = testFS.getPath(Runfiles.create().rlocation(path)); |
| 82 | + |
| 83 | + Path workingDir = testFS.getPath(new File(TestUtils.tmpDir()).getCanonicalPath()); |
| 84 | + Path outDir = workingDir.getRelative("out"); |
| 85 | + |
| 86 | + return DecompressorDescriptor.builder().setRepositoryPath(outDir).setArchivePath(tarballPath); |
| 87 | + } |
| 88 | +} |
0 commit comments