Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue discovered on src/test/resources/dataflow-functional-tests/ArchiveAnalyzer.java with Use Files#createTempDirectory`` #4437

Open
kmccarp opened this issue Aug 22, 2024 · 0 comments
Labels
bug Something isn't working

Comments

@kmccarp
Copy link
Contributor

kmccarp commented Aug 22, 2024

Problem

The recipe org.openrewrite.java.security.UseFilesCreateTempDirectory made a change that deletes a bunch of code that may or may not be needed.

Expected behavior

I expected the recipe to only make the required set of changes. The result I got changes the underlying behavior.

Example diff

From: src/test/resources/dataflow-functional-tests/ArchiveAnalyzer.java

 
import javax.annotation.concurrent.ThreadSafe;
import java.io.*;
+import java.nio.file.Files;
import java.nio.file.Path;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
public void prepareFileTypeAnalyzer(Engine engine) throws InitializationException {
try {
  final File baseDir = getSettings().getTempDirectory();
-            tempFileLocation = File.createTempFile("check", "tmp", baseDir);
-            if (!tempFileLocation.delete()) {
-                setEnabled(false);
-                final String msg = String.format("Unable to delete temporary file '%s'.", tempFileLocation.getAbsolutePath());
-                throw new InitializationException(msg);
-            }
-            if (!tempFileLocation.mkdirs()) {
-                setEnabled(false);
-                final String msg = String.format("Unable to create directory '%s'.", tempFileLocation.getAbsolutePath());
-                throw new InitializationException(msg);
-            }
+            tempFileLocation = Files.createTempDirectory(baseDir.toPath(), "check" + "tmp").toFile();
} catch (IOException ex) {
  setEnabled(false);
  throw new InitializationException("Unable to create a temporary file", ex);

Recipes in example diff:

  • org.openrewrite.java.security.UseFilesCreateTempDirectory

References:

  • View original result
  • Recipe ID: org.openrewrite.java.security.UseFilesCreateTempDirectory
  • Recipe Name: Use Files#createTempDirectory``
  • Repository: openrewrite/rewrite-analysis/main
  • Created at Thu Aug 22 2024 09:49:22 GMT-0300 (Horário Padrão da Argentina)
@kmccarp kmccarp added the bug Something isn't working label Aug 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
Status: No status
Development

No branches or pull requests

1 participant