Skip to content

Commit

Permalink
Fix new temp dirs not different on native (#1550)
Browse files Browse the repository at this point in the history
  • Loading branch information
clementetb authored Oct 30, 2023
1 parent 93a05a1 commit be153b6
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright 2023 Realm Inc.
*
* Licensed 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.
*/
package io.realm.kotlin.test.common.utils

import io.realm.kotlin.internal.platform.directoryExists
import io.realm.kotlin.test.platform.PlatformUtils
import kotlin.test.Test
import kotlin.test.assertFalse
import kotlin.test.assertNotEquals
import kotlin.test.assertTrue

class PlatformUtilsTests {
@Test
fun createTempDir_createDifferentDirs() {
val testDir1 = PlatformUtils.createTempDir("test-dir")
val testDir2 = PlatformUtils.createTempDir("test-dir")

assertTrue(directoryExists(testDir1))
assertTrue(directoryExists(testDir2))

assertNotEquals(testDir1, testDir2)
}

@Test
fun createTempDir_deleteDifferentDirs() {
val testDir = PlatformUtils.createTempDir("test-dir")
assertTrue(directoryExists(testDir))
PlatformUtils.deleteTempDir(testDir)
assertFalse(directoryExists(testDir))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,17 @@

package io.realm.kotlin.test.platform

import io.realm.kotlin.test.util.Utils
import kotlinx.cinterop.ULongVar
import kotlinx.cinterop.alloc
import kotlinx.cinterop.cValue
import kotlinx.cinterop.cstr
import kotlinx.cinterop.memScoped
import kotlinx.cinterop.ptr
import kotlinx.cinterop.value
import platform.posix.S_IRGRP
import platform.posix.S_IROTH
import platform.posix.S_IRUSR
import platform.posix.S_IRWXU
import platform.posix.nanosleep
import platform.posix.pthread_threadid_np
import platform.posix.timespec
Expand All @@ -34,10 +35,12 @@ import kotlin.time.Duration

actual object PlatformUtils {
actual fun createTempDir(prefix: String, readOnly: Boolean): String {
// X is a special char which will be replace by mkdtemp template
val mask = prefix.replace('X', 'Z', ignoreCase = true)
val path = "${platform.Foundation.NSTemporaryDirectory()}$mask"
platform.posix.mkdtemp(path.cstr)
// Currently we cannot template using platform.posix.mkdtemp
// the return value is not of use.
val suffix = "-${Utils.createRandomString(6)}"
val path = "${platform.Foundation.NSTemporaryDirectory()}$prefix$suffix"
platform.posix.mkdir(path, S_IRWXU.toUShort())

if (readOnly) {
platform.posix.chmod(path, (S_IRUSR or S_IRGRP or S_IROTH).toUShort())
}
Expand Down

0 comments on commit be153b6

Please sign in to comment.