Skip to content

Commit

Permalink
Merge pull request #5572 from seadowg/flakey-test
Browse files Browse the repository at this point in the history
Improve temp file utility to try and prevent flakey tests
  • Loading branch information
grzesiek2010 authored May 9, 2023
2 parents 44a50c0 + dfdd358 commit 8dfcf80
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class FormMediaDownloaderTest {
}

@Test
fun `returns false when there is an existing copy of a media file and an older one and server hash doesn't match existing copy`() {
fun `returns false when there is an existing copy of a media file and an older one and media file list hash doesn't match existing copy`() {
// Save forms
val formsRepository = InMemFormsRepository()
val form1 = FormFixtures.form(
Expand Down
45 changes: 33 additions & 12 deletions shared/src/main/java/org/odk/collect/shared/TempFiles.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@ object TempFiles {

@JvmStatic
fun createTempFile(name: String, extension: String): File {
val tempFile = File.createTempFile(name, extension)
tempFile.deleteOnExit()
return tempFile
val tmpDir = getTempDir()
return File(tmpDir, name + getRandomName(tmpDir) + extension).also {
it.createNewFile()
it.deleteOnExit()
}
}

@JvmStatic
fun createTempFile(parent: File, name: String): File {
return File(parent, name).also {
return File(parent, name + getRandomName(parent)).also {
it.createNewFile()
it.deleteOnExit()
}
Expand All @@ -30,30 +32,31 @@ object TempFiles {

@JvmStatic
fun createTempFile(parent: File): File {
val tempFile = File.createTempFile(getRandomString(), ".temp", parent)
tempFile.deleteOnExit()
return tempFile
return File(parent, getRandomName(parent)).also {
it.createNewFile()
it.deleteOnExit()
}
}

@JvmStatic
fun getPathInTempDir(name: String, extension: String): String {
val tmpDir = System.getProperty("java.io.tmpdir", ".")
val tmpDir = getTempDir()
val file = File(tmpDir, name + extension)
file.deleteOnExit()
return file.absolutePath
}

@JvmStatic
fun getPathInTempDir(): String {
val tmpDir = System.getProperty("java.io.tmpdir", ".")
return File(tmpDir, getRandomString()).absolutePath
val tmpDir = getTempDir()
return File(tmpDir, getRandomName(tmpDir)).absolutePath
}

@JvmStatic
@JvmOverloads
fun createTempDir(parent: File? = null): File {
val dir = if (parent != null) {
File(parent, getRandomString())
File(parent, getRandomName(parent))
} else {
File(getPathInTempDir())
}
Expand All @@ -62,5 +65,23 @@ object TempFiles {
return dir
}

private fun getRandomString() = RandomString.randomString(16)
private fun getTempDir(): File {
val tmpDir = File(System.getProperty("java.io.tmpdir", "."), " org.odk.collect.shared.TempFiles")
if (!tmpDir.exists()) {
tmpDir.mkdir()
}

return tmpDir
}

private fun getRandomName(parent: File): String {
val existing = parent.listFiles()

var candiate = RandomString.randomString(16)
while (existing!!.any { it.name.contains(candiate) }) {
candiate = RandomString.randomString(16)
}

return candiate
}
}

0 comments on commit 8dfcf80

Please sign in to comment.