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

Code before:
  public void testWriteLocalConfig() throws IOException, URISyntaxException, DigestException {
    ContainerConfigurationTemplate containerConfigurationTemplate =
        loadJsonResource("core/json/containerconfig.json", ContainerConfigurationTemplate.class);

    cacheStorageWriter.writeLocalConfig(
        DescriptorDigest.fromHash(
            "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"),
        containerConfigurationTemplate);

    Path savedConfigPath =
        cacheStorageFiles
            .getLocalDirectory()
            .resolve("config")
            .resolve("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
    Assert.assertTrue(Files.exists(savedConfigPath));
    ContainerConfigurationTemplate savedContainerConfig =
        JsonTemplateMapper.readJsonFromFile(savedConfigPath, ContainerConfigurationTemplate.class);
    Assert.assertEquals("wasm", savedContainerConfig.getArchitecture());
  }

  @Test
  public void testMoveIfDoesNotExist_exceptionAfterFailure() {
    Exception exception =
        assertThrows(
            IOException.class,
            () -> CacheStorageWriter.moveIfDoesNotExist(Paths.get("/foo"), Paths.get("/bar")));
    assertThat(exception)
        .hasMessageThat()
        .isEqualTo(
            "unable to move: /foo to /bar; such failures are often caused by interference from "
                + "antivirus (https://github.com/GoogleContainerTools/jib/issues/3127#issuecomment-796838294), "
                + "or rarely if the operation is not supported by the file system (for example: "
                + "special non-local file system)");
    assertThat(exception).hasCauseThat().isInstanceOf(NoSuchFileException.class);
    assertThat(exception.getCause()).hasMessageThat().isEqualTo("/foo");
  }

  private void verifyCachedLayer(CachedLayer cachedLayer, Blob uncompressedLayerBlob)
      throws IOException {
    BlobDescriptor layerBlobDescriptor = getDigest(compress(uncompressedLayerBlob));
    DescriptorDigest layerDiffId = getDigest(uncompressedLayerBlob).getDigest();

    // Verifies cachedLayer is correct.
    Assert.assertEquals(layerBlobDescriptor.getDigest(), cachedLayer.getDigest());
    Assert.assertEquals(layerDiffId, cachedLayer.getDiffId());
    Assert.assertEquals(layerBlobDescriptor.getSize(), cachedLayer.getSize());
    Assert.assertArrayEquals(
        Blobs.writeToByteArray(uncompressedLayerBlob),
        Blobs.writeToByteArray(decompress(cachedLayer.getBlob())));

    // Verifies that the files are present.

Review comment (performance):
```suggestion
            () -> CacheStorageWriter.moveIfDoesNotExist(Paths.get("foo"), Paths.get("bar")));
```

Code after:
  public void testWriteLocalConfig() throws IOException, URISyntaxException, DigestException {
    ContainerConfigurationTemplate containerConfigurationTemplate =
        loadJsonResource("core/json/containerconfig.json", ContainerConfigurationTemplate.class);

    cacheStorageWriter.writeLocalConfig(
        DescriptorDigest.fromHash(
            "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"),
        containerConfigurationTemplate);

    Path savedConfigPath =
        cacheStorageFiles
            .getLocalDirectory()
            .resolve("config")
            .resolve("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
    Assert.assertTrue(Files.exists(savedConfigPath));
    ContainerConfigurationTemplate savedContainerConfig =
        JsonTemplateMapper.readJsonFromFile(savedConfigPath, ContainerConfigurationTemplate.class);
    Assert.assertEquals("wasm", savedContainerConfig.getArchitecture());
  }

  @Test
  public void testMoveIfDoesNotExist_exceptionAfterFailure() {
    Exception exception =
        assertThrows(
            IOException.class,
            () -> CacheStorageWriter.moveIfDoesNotExist(Paths.get("foo"), Paths.get("bar")));
    assertThat(exception)
        .hasMessageThat()
        .contains(
            "unable to move: foo to bar; such failures are often caused by interference from "
                + "antivirus");
    assertThat(exception).hasCauseThat().isInstanceOf(NoSuchFileException.class);
    assertThat(exception.getCause()).hasMessageThat().isEqualTo("foo");
  }

  private void verifyCachedLayer(CachedLayer cachedLayer, Blob uncompressedLayerBlob)
      throws IOException {
    BlobDescriptor layerBlobDescriptor = getDigest(compress(uncompressedLayerBlob));
    DescriptorDigest layerDiffId = getDigest(uncompressedLayerBlob).getDigest();

    // Verifies cachedLayer is correct.
    Assert.assertEquals(layerBlobDescriptor.getDigest(), cachedLayer.getDigest());
    Assert.assertEquals(layerDiffId, cachedLayer.getDiffId());
    Assert.assertEquals(layerBlobDescriptor.getSize(), cachedLayer.getSize());
    Assert.assertArrayEquals(
        Blobs.writeToByteArray(uncompressedLayerBlob),
        Blobs.writeToByteArray(decompress(cachedLayer.getBlob())));

    // Verifies that the files are present.
    Assert.assertTrue(
        Files.exists(