openrewrite / rewrite-java-security

OpenRewrite recipes for patching Java security vulnerabilities.
Apache License 2.0
20 stars 15 forks source link

Issue discovered with UseFilesCreateTempDirectory #40

Closed kmccarp closed 2 years ago

kmccarp commented 2 years ago

Problem

The recipe Use Files#createTempDirectory, we are replacing assertTrue(dir.delete()) and assertTrue(dir.mkdir()) with assertTrue(true).

Expected behavior

I'm not sure exactly what the behavior should be, only that we should never end up with assertTrue(true). Maybe we could start by just having this call a doNext that removes any assertTrue(true)?

Example diff

 import java.io.File;
 import java.io.IOException;
 import java.io.PrintStream;
+import java.nio.file.Files;
 import java.util.Collections;
 import java.util.concurrent.atomic.AtomicReference;
    @Test
    public void testLocateReports() throws Exception {
        // Create a temporary workspace in the system
-       File w = File.createTempFile("workspace", ".test");
-       assertTrue(w.delete());
-       assertTrue(w.mkdir());
+       File w = Files.createTempDirectory("workspace" + ".test").toFile();
+       assertTrue(true);
+       assertTrue(true);
        w.deleteOnExit();
        FilePath workspace = new FilePath(w);
            return null;
        });

-        File dir = File.createTempFile("JaCoCoPublisherTest", ".tst");
-        assertTrue(dir.delete());
-        assertTrue(dir.mkdirs());
+        File dir = Files.createTempDirectory("JaCoCoPublisherTest" + ".tst").toFile();
+        assertTrue(true);
+        assertTrue(true);

         try {
            assertTrue(new File(dir, "jacoco/classes").mkdirs());
            return null;
        });

-       File dir = File.createTempFile("JaCoCoPublisherTest", ".tst");
-       assertTrue(dir.delete());
-       assertTrue(dir.mkdirs());
+       File dir = Files.createTempDirectory("JaCoCoPublisherTest" + ".tst").toFile();
+       assertTrue(true);
+       assertTrue(true);
        assertTrue(new File(dir, "jacoco/classes").mkdirs());
        FilePath filePath = new FilePath(dir);

Recipes in example diff:

References:

kmccarp commented 2 years ago

Duplicate of https://github.com/openrewrite/rewrite-java-security/issues/33