Skip to content

Commit d936456

Browse files
Try renaming rather than deleting the directory on Windows.
1 parent 77b01cc commit d936456

2 files changed

Lines changed: 32 additions & 9 deletions

File tree

src/org/labkey/test/TestFileUtils.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import org.bouncycastle.openpgp.operator.jcajce.JcePBEDataDecryptorFactoryBuilder;
3939
import org.bouncycastle.util.io.Streams;
4040
import org.jetbrains.annotations.NotNull;
41+
import org.junit.Assert;
4142
import org.labkey.api.util.FileUtil;
4243
import org.jetbrains.annotations.Nullable;
4344
import org.openqa.selenium.NotFoundException;
@@ -445,6 +446,25 @@ public static void deleteDir(File dir)
445446
}
446447
}
447448

449+
public static void renameDir(Path sourceDir, String newDirName)
450+
{
451+
LOG.info("Renaming " + sourceDir + " to " + newDirName);
452+
checkFileLocation(sourceDir.toFile());
453+
Assert.assertTrue(sourceDir + " does not exists.", sourceDir.toFile().exists());
454+
455+
try
456+
{
457+
String separator = File.separator;
458+
Path newDir = Paths.get(sourceDir.toString().substring(0, sourceDir.toString().lastIndexOf(separator)) + separator + newDirName);
459+
Files.move(sourceDir, newDir);
460+
LOG.info("Rename successful.");
461+
}
462+
catch (IOException e)
463+
{
464+
LOG.info("WARNING: Exception renaming directory -- " + e.getMessage());
465+
}
466+
}
467+
448468
private static void checkFileLocation(File file)
449469
{
450470
try

src/org/labkey/test/tests/assay/AssayTransformMissingParentDirTest.java

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
package org.labkey.test.tests.assay;
22

33
import org.apache.commons.io.FileUtils;
4+
import org.apache.commons.lang3.SystemUtils;
45
import org.junit.Assert;
56
import org.junit.Test;
67
import org.junit.experimental.categories.Category;
78
import org.labkey.api.util.FileUtil;
8-
import org.labkey.test.BaseWebDriverTest;
99
import org.labkey.test.Locator;
1010
import org.labkey.test.TestFileUtils;
1111
import org.labkey.test.categories.Assays;
@@ -19,6 +19,8 @@
1919
import java.io.File;
2020
import java.nio.file.Files;
2121
import java.nio.file.Path;
22+
import java.nio.file.Paths;
23+
import java.time.Instant;
2224

2325
/**
2426
* Issue 54156: Regression test to ensure a reasonable error message is shown when an assay design references
@@ -49,18 +51,19 @@ public void testMissingParentDirectoryRegression() throws Exception
4951
getArtifactCollector().dumpPageSnapshot("TransformScript_Added");
5052
assayDesignerPage.clickSave();
5153

52-
// Now delete the parent dir to ensure we handle it reasonably
53-
TestFileUtils.deleteDir(parentDir.toFile());
54-
55-
int count = 1;
56-
while (!FileUtils.isDirectory(parentDir.toFile()) && count <= 5)
54+
// Now delete, or rename, the parent dir to ensure we handle it reasonably
55+
if (SystemUtils.IS_OS_WINDOWS)
56+
{
57+
// Deleting directories on Windows is not always reliable, try renaming as an alternative.
58+
String newName = "Not-Here-" + Instant.now().getEpochSecond();
59+
TestFileUtils.renameDir(parentDir, newName);
60+
}
61+
else
5762
{
58-
sleep(1_000);
5963
TestFileUtils.deleteDir(parentDir.toFile());
60-
count++;
6164
}
6265

63-
Assert.assertFalse(String.format("Directory %s not deleted.", parentDir.toString()),
66+
Assert.assertFalse(String.format("Directory %s is still present.", parentDir),
6467
FileUtils.isDirectory(parentDir.toFile()));
6568

6669
// Attempt to import data and verify a reasonable error message is shown

0 commit comments

Comments
 (0)