Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve temp file utility to try and prevent flakey tests #5572

Merged
merged 2 commits into from
May 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
}