diff --git a/src/org/labkey/test/TestFileUtils.java b/src/org/labkey/test/TestFileUtils.java index d73ad973a1..892ae635b8 100644 --- a/src/org/labkey/test/TestFileUtils.java +++ b/src/org/labkey/test/TestFileUtils.java @@ -38,6 +38,7 @@ import org.bouncycastle.openpgp.operator.jcajce.JcePBEDataDecryptorFactoryBuilder; import org.bouncycastle.util.io.Streams; import org.jetbrains.annotations.NotNull; +import org.junit.Assert; import org.labkey.api.util.FileUtil; import org.jetbrains.annotations.Nullable; import org.openqa.selenium.NotFoundException; @@ -445,6 +446,31 @@ public static void deleteDir(File dir) } } + /** + * Rename a directory. + * @param sourceDir the directory to rename + * @param newDirName the new name for the directory + * @return return the path to the renamed directory + */ + public static Path renameDir(Path sourceDir, String newDirName) throws IOException + { + LOG.info("Renaming {} to {}", sourceDir, newDirName); + + // Check if the directory is outside the test enlistment. + checkFileLocation(sourceDir.toFile()); + + Assert.assertTrue("Directory does not exist: " + sourceDir, sourceDir.toFile().exists()); + + Path newDir = sourceDir.getParent() != null + ? sourceDir.getParent().resolve(newDirName) + : Paths.get(newDirName); + Files.move(sourceDir, newDir); + + LOG.info("Renamed {} to {}", sourceDir, newDir); + + return newDir; + } + private static void checkFileLocation(File file) { try @@ -452,7 +478,8 @@ private static void checkFileLocation(File file) if (!FileUtils.directoryContains(getLabKeyRoot(), file)) { // TODO: Consider throwing IllegalArgumentException - LOG.info("DEBUG: Attempting to delete a file outside of test enlistment: " + getLabKeyRoot()); + LOG.info("DEBUG: Attempting to delete / rename a file, {}, outside of test enlistment {}.", + file, getLabKeyRoot()); } } catch (IOException ignore) { } diff --git a/src/org/labkey/test/tests/assay/AssayTransformMissingParentDirTest.java b/src/org/labkey/test/tests/assay/AssayTransformMissingParentDirTest.java index 69c8a94f29..137b6844a1 100644 --- a/src/org/labkey/test/tests/assay/AssayTransformMissingParentDirTest.java +++ b/src/org/labkey/test/tests/assay/AssayTransformMissingParentDirTest.java @@ -1,5 +1,7 @@ package org.labkey.test.tests.assay; +import org.apache.commons.io.FileUtils; +import org.junit.Assert; import org.junit.Test; import org.junit.experimental.categories.Category; import org.labkey.api.util.FileUtil; @@ -16,6 +18,7 @@ import java.io.File; import java.nio.file.Files; import java.nio.file.Path; +import java.util.UUID; /** * Issue 54156: Regression test to ensure a reasonable error message is shown when an assay design references @@ -29,7 +32,7 @@ public void testMissingParentDirectoryRegression() throws Exception { // Create a nested directory and an R transform script within it String assayName = "missingParentDirAssay"; - Path parentDir = Files.createTempDirectory("assay-transform-parent-"); + Path parentDir = Files.createTempDirectory("assay-transform-missing-parent-"); Path nestedDir = FileUtil.createDirectories(parentDir.resolve("child"), false); String scriptName = "transformMissingParent.R"; String transformContent = "library(Rlabkey);"; @@ -45,8 +48,17 @@ public void testMissingParentDirectoryRegression() throws Exception assayDesignerPage.addTransformScript(transformFile); assayDesignerPage.clickSave(); - // Now delete the parent dir to ensure we handle it reasonably - TestFileUtils.deleteDir(parentDir.toFile()); + // Rename the parent dir to ensure we handle it reasonably. Deleting directories on Windows is not always + // timely, renaming the directory will have the same effect. + String newName = "Not-Here-" + UUID.randomUUID(); + Path renamedDir = TestFileUtils.renameDir(parentDir, newName); + + Assert.assertFalse(String.format("Directory %s is still present.", parentDir), + FileUtils.isDirectory(parentDir.toFile())); + + // Delete the renamed directory as a cleanup step. Since the directory has been renamed, the slow delete on Windows + // will not cause the test to fail. + TestFileUtils.deleteDir(renamedDir.toFile()); // Attempt to import data and verify a reasonable error message is shown String importData = """