Repository: GoogleContainerTools/jib
File: jib-core/src/test/java/com/google/cloud/tools/jib/tar/TarExtractorTest.java (Java)

Code before:

    Assert.assertTrue(Files.isDirectory(destination.resolve("directory1")));
    Assert.assertTrue(Files.isDirectory(destination.resolve("directory2")));
    Assert.assertTrue(Files.isRegularFile(destination.resolve("directory2/regular")));
    Assert.assertTrue(Files.isSymbolicLink(destination.resolve("directory-symlink")));
    Assert.assertTrue(Files.isSymbolicLink(destination.resolve("directory1/file-symlink")));
  }

  @Test
  public void testExtract_modificationTimePreserved() throws URISyntaxException, IOException {
    Path source = Paths.get(Resources.getResource("core/extract.tar").toURI());
    Path destination = temporaryFolder.getRoot().toPath();

    TarExtractor.extract(source, destination);

    assertThat(Files.getLastModifiedTime(destination.resolve("file A")))
        .isEqualTo(FileTime.from(Instant.parse("2019-08-01T16:13:09Z")));
    assertThat(Files.getLastModifiedTime(destination.resolve("file B")))
        .isEqualTo(FileTime.from(Instant.parse("2019-08-01T16:12:00Z")));
    assertThat(Files.getLastModifiedTime(destination.resolve("folder")))
        .isEqualTo(FileTime.from(Instant.parse("2019-08-01T16:12:33Z")));
    assertThat(Files.getLastModifiedTime(destination.resolve("folder").resolve("nested folder")))
        .isEqualTo(FileTime.from(Instant.parse("2019-08-01T16:13:30Z")));
    assertThat(
            Files.getLastModifiedTime(
                destination.resolve("folder").resolve("nested folder").resolve("file C")))
        .isEqualTo(FileTime.from(Instant.parse("2019-08-01T16:12:21Z")));
  }

  @Test
  public void testExtract_modificationTimePreserved_onlyFilePackaged()
      throws URISyntaxException, IOException {
    // The tarfile has only level1/level2/level3/file.txt packaged
    Path source = Paths.get(Resources.getResource("core/tarfile-only-file-packaged.tar").toURI());

    Path destination = temporaryFolder.getRoot().toPath();

    TarExtractor.extract(source, destination);

    assertThat(Files.getLastModifiedTime(destination.resolve("level-1")))
        .isEqualTo(FileTime.from(Instant.ofEpochSecond(1L)));
    assertThat(Files.getLastModifiedTime(destination.resolve("level-1").resolve("level-2")))
        .isEqualTo(FileTime.from(Instant.ofEpochSecond(1L)));
    assertThat(
            Files.getLastModifiedTime(
                destination.resolve("level-1").resolve("level-2").resolve("level-3")))
        .isEqualTo(FileTime.from(Instant.ofEpochSecond(1L)));
    assertThat(
            Files.getLastModifiedTime(
                destination
                    .resolve("level-1")

Review comment (refactor):
Just FYI, no need to change: in *tests*, we're tolerant in writing `destination.resolve("folder/nested folder/file C"`) if it can simplify code or improve readability.

Code after:
    TarExtractor.extract(source, destination);

    Assert.assertTrue(Files.isDirectory(destination.resolve("directory1")));
    Assert.assertTrue(Files.isDirectory(destination.resolve("directory2")));
    Assert.assertTrue(Files.isRegularFile(destination.resolve("directory2/regular")));
    Assert.assertTrue(Files.isSymbolicLink(destination.resolve("directory-symlink")));
    Assert.assertTrue(Files.isSymbolicLink(destination.resolve("directory1/file-symlink")));
  }

  @Test
  public void testExtract_modificationTimePreserved() throws URISyntaxException, IOException {
    Path source = Paths.get(Resources.getResource("core/extract.tar").toURI());
    Path destination = temporaryFolder.getRoot().toPath();

    TarExtractor.extract(source, destination);

    assertThat(Files.getLastModifiedTime(destination.resolve("file A")))
        .isEqualTo(FileTime.from(Instant.parse("2019-08-01T16:13:09Z")));
    assertThat(Files.getLastModifiedTime(destination.resolve("file B")))
        .isEqualTo(FileTime.from(Instant.parse("2019-08-01T16:12:00Z")));
    assertThat(Files.getLastModifiedTime(destination.resolve("folder")))
        .isEqualTo(FileTime.from(Instant.parse("2019-08-01T16:12:33Z")));
    assertThat(Files.getLastModifiedTime(destination.resolve("folder/nested folder")))
        .isEqualTo(FileTime.from(Instant.parse("2019-08-01T16:13:30Z")));
    assertThat(Files.getLastModifiedTime(destination.resolve("folder/nested folder/file C")))
        .isEqualTo(FileTime.from(Instant.parse("2019-08-01T16:12:21Z")));
  }

  @Test
  public void testExtract_reproducibleTimestampsEnabled() throws URISyntaxException, IOException {
    // The tarfile has only level1/level2/level3/file.txt packaged
    Path source = Paths.get(Resources.getResource("core/tarfile-only-file-packaged.tar").toURI());

    Path destination = temporaryFolder.getRoot().toPath();

    TarExtractor.extract(source, destination, true);

    assertThat(Files.getLastModifiedTime(destination.resolve("level-1")))
        .isEqualTo(FileTime.fromMillis(1000L));
    assertThat(Files.getLastModifiedTime(destination.resolve("level-1/level-2")))
        .isEqualTo(FileTime.fromMillis(1000L));
    assertThat(Files.getLastModifiedTime(destination.resolve("level-1/level-2/level-3")))
        .isEqualTo(FileTime.fromMillis(1000L));
    assertThat(Files.getLastModifiedTime(destination.resolve("level-1/level-2/level-3/file.txt")))
        .isEqualTo(FileTime.from(Instant.parse("2021-01-29T21:10:02Z")));
  }

  @Test
  public void testExtract_reproducibleTimestampsEnabled_destinationNotEmpty() throws IOException {
    Path destination = temporaryFolder.getRoot().toPath();
    temporaryFolder.newFile();