Apache Solr open-source search software
LUCENE-4308: don't delete a test's files if the test fails
Robert Muir04df51f90df6
diff --git a/lucene/core/src/test/org/apache/lucene/util/junitcompat/TestLeaveFilesIfTestFails.java b/lucene/core/src/test/org/apache/lucene/util/junitcompat/TestLeaveFilesIfTestFails.javanew file mode 100644index 00000000000..6be8aee640f--- /dev/null+++ b/lucene/core/src/test/org/apache/lucene/util/junitcompat/TestLeaveFilesIfTestFails.java@@ -0,0 +1,51 @@+package org.apache.lucene.util.junitcompat;++import java.io.File;++import org.apache.lucene.store.Directory;+import org.apache.lucene.util._TestUtil;+import org.apache.lucene.util.junitcompat.TestFailIfDirectoryNotClosed.Nested1;+import org.junit.Assert;+import org.junit.Test;+import org.junit.runner.JUnitCore;+import org.junit.runner.Result;++/*+ * Licensed to the Apache Software Foundation (ASF) under one or more+ * contributor license agreements. See the NOTICE file distributed with+ * this work for additional information regarding copyright ownership.+ * The ASF licenses this file to You under the Apache License, Version 2.0+ * (the "License"); you may not use this file except in compliance with+ * the License. You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */++public class TestLeaveFilesIfTestFails extends WithNestedTests {+ public TestLeaveFilesIfTestFails() {+ super(true);+ }++ public static class Nested1 extends WithNestedTests.AbstractNestedTest {+ static File file;+ public void testDummy() {+ file = _TestUtil.getTempDir("leftover");+ file.mkdirs();+ fail();+ }+ }++ @Test+ public void testLeaveFilesIfTestFails() {+ Result r = JUnitCore.runClasses(Nested1.class);+ Assert.assertEquals(1, r.getFailureCount());+ Assert.assertTrue(Nested1.file.exists());+ Nested1.file.delete();+ }+}diff --git a/lucene/test-framework/src/java/org/apache/lucene/util/CloseableFile.java b/lucene/test-framework/src/java/org/apache/lucene/util/CloseableFile.javaindex aa44cfbdf41..7aedb3fec35 100644--- a/lucene/test-framework/src/java/org/apache/lucene/util/CloseableFile.java+++ b/lucene/test-framework/src/java/org/apache/lucene/util/CloseableFile.java@@ -24,25 +24,30 @@ import java.io.*;*/final class CloseableFile implements Closeable {private final File file;+ private final TestRuleMarkFailure failureMarker;- public CloseableFile(File file) {+ public CloseableFile(File file, TestRuleMarkFailure failureMarker) {this.file = file;+ this.failureMarker = failureMarker;}@Overridepublic void close() throws IOException {- if (file.exists()) {- try {- _TestUtil.rmDir(file);- } catch (IOException e) {- // Ignore the exception from rmDir.- }-- // Re-check.+ // only if there were no other test failures.+ if (failureMarker.wasSuccessful()) {if (file.exists()) {- throw new IOException(+ try {+ _TestUtil.rmDir(file);+ } catch (IOException e) {+ // Ignore the exception from rmDir.+ }++ // Re-check.+ if (file.exists()) {+ throw new IOException("Could not remove: " + file.getAbsolutePath());- }+ }+ }}}}\ No newline at end of filediff --git a/lucene/test-framework/src/java/org/apache/lucene/util/_TestUtil.java b/lucene/test-framework/src/java/org/apache/lucene/util/_TestUtil.javaindex 0a0fd27f3f6..22837ba6cc8 100644--- a/lucene/test-framework/src/java/org/apache/lucene/util/_TestUtil.java+++ b/lucene/test-framework/src/java/org/apache/lucene/util/_TestUtil.java@@ -94,7 +94,7 @@ public class _TestUtil {try {File f = createTempFile(desc, "tmp", LuceneTestCase.TEMP_DIR);f.delete();- LuceneTestCase.closeAfterSuite(new CloseableFile(f));+ LuceneTestCase.closeAfterSuite(new CloseableFile(f, LuceneTestCase.suiteFailureMarker));return f;} catch (IOException e) {throw new RuntimeException(e);@@ -136,7 +136,7 @@ public class _TestUtil {rmDir(destDir);destDir.mkdir();- LuceneTestCase.closeAfterSuite(new CloseableFile(destDir));+ LuceneTestCase.closeAfterSuite(new CloseableFile(destDir, LuceneTestCase.suiteFailureMarker));while (entries.hasMoreElements()) {ZipEntry entry = entries.nextElement();