Skip to content

Commit

Permalink
Add exception when work and cache directories are the same (#521)
Browse files Browse the repository at this point in the history
  • Loading branch information
05nelsonm authored Aug 24, 2024
1 parent feec32c commit dbd993b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -339,9 +339,6 @@ public sealed interface TorRuntime:
* identical (e.g. `torservice`), especially when creating multiple
* instances of [Environment].
*
* **NOTE:** If the same directory is utilized for both [workDirectory]
* and [cacheDirectory], tor may fail to start; they **must** be different.
*
* **NOTE:** If an [Environment] already exists for the provided [workDirectory]
* **or** [cacheDirectory], that instance will be returned.
*
Expand All @@ -350,7 +347,8 @@ public sealed interface TorRuntime:
* @param [cacheDirectory] tor's cache directory (e.g. `$HOME/.my_application/cache/torservice`).
* @param [installer] lambda for creating [ResourceInstaller] using the configured
* [BuilderScope.installationDirectory]. See [kmp-tor-resource](https://github.com/05nelsonm/kmp-tor-resource)
* @see [io.matthewnelson.kmp.tor.runtime.service.TorServiceConfig]
* @throws [IllegalArgumentException] when [workDirectory] and [cacheDirectory] are
* the same.
* */
@JvmStatic
public fun Builder(
Expand All @@ -373,9 +371,6 @@ public sealed interface TorRuntime:
* identical (e.g. `torservice`), especially when creating multiple
* instances of [Environment].
*
* **NOTE:** If the same directory is utilized for both [workDirectory]
* and [cacheDirectory], tor may fail to start; they **must** be different.
*
* **NOTE:** If an [Environment] already exists for the provided [workDirectory]
* **or** [cacheDirectory], that instance will be returned.
*
Expand All @@ -385,7 +380,8 @@ public sealed interface TorRuntime:
* @param [installer] lambda for creating [ResourceInstaller] using the configured
* [BuilderScope.installationDirectory]. See [kmp-tor-resource](https://github.com/05nelsonm/kmp-tor-resource)
* @param [block] optional lambda for modifying default parameters.
* @see [io.matthewnelson.kmp.tor.runtime.service.TorServiceConfig]
* @throws [IllegalArgumentException] when [workDirectory] and [cacheDirectory] are
* the same.
* */
@JvmStatic
public fun Builder(
Expand Down Expand Up @@ -492,6 +488,11 @@ public sealed interface TorRuntime:
block: ThisBlock<BuilderScope>?,
): Environment {
val b = BuilderScope(workDirectory.absoluteFile.normalize(), cacheDirectory.absoluteFile.normalize())

require(b.workDirectory != b.cacheDirectory) {
"workDirectory and cacheDirectory cannot be the same locations"
}

// Apply block outside getOrCreateInstance call to
// prevent double instance creation
if (block != null) b.apply(block)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,7 @@ package io.matthewnelson.kmp.tor.runtime
import io.matthewnelson.kmp.file.*
import io.matthewnelson.kmp.tor.core.api.ResourceInstaller
import io.matthewnelson.kmp.tor.core.api.annotation.ExperimentalKmpTorApi
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertNotEquals
import kotlin.test.fail
import kotlin.test.*

@OptIn(ExperimentalKmpTorApi::class)
class TorRuntimeEnvironmentUnitTest {
Expand Down Expand Up @@ -93,4 +90,11 @@ class TorRuntimeEnvironmentUnitTest {
assertNotEquals(env1, env2)
assertEquals(env1.torResource, env2.torResource)
}

@Test
fun givenBuilder_whenWorkDirectorySameAsCache_thenThrowsException() {
assertFailsWith<IllegalArgumentException> {
TorRuntime.Environment.Builder("".toFile(), "".toFile()) { torResource(it) }
}
}
}

0 comments on commit dbd993b

Please sign in to comment.