Skip to content

Commit 3524622

Browse files
committed
Normalize backward slashes in preferences file
Added logic to replace backward slashes with forward slashes in the preferences file to ensure consistent path formatting. Updated tests to verify the normalization behavior.
1 parent ba82cfd commit 3524622

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

app/src/processing/app/Preferences.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,14 @@ fun PreferencesProvider(content: @Composable () -> Unit) {
8787
preferencesFile.createNewFile()
8888
}
8989

90+
remember {
91+
// check if the file has backward slashes
92+
if (preferencesFile.readText().contains("\\")) {
93+
val correctedText = preferencesFile.readText().replace("\\", "/")
94+
preferencesFile.writeText(correctedText)
95+
}
96+
}
97+
9098
val update = watchFile(preferencesFile)
9199

92100

app/test/processing/app/PreferencesKtTest.kt

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import androidx.compose.material.Text
55
import androidx.compose.ui.Modifier
66
import androidx.compose.ui.platform.testTag
77
import androidx.compose.ui.test.*
8-
import java.util.Properties
8+
import java.util.*
99
import kotlin.io.path.createFile
1010
import kotlin.io.path.createTempDirectory
1111
import kotlin.test.Test
@@ -58,4 +58,31 @@ class PreferencesKtTest{
5858

5959
onNodeWithTag("text").assertTextEquals(nextValue)
6060
}
61+
62+
@OptIn(ExperimentalTestApi::class)
63+
@Test
64+
fun testWithBackwardSlashes() = runComposeUiTest {
65+
val directory = createTempDirectory("preferences")
66+
val tempPreferences = directory
67+
.resolve("preferences.txt")
68+
.createFile()
69+
.toFile()
70+
71+
System.setProperty("processing.app.preferences.file", tempPreferences.absolutePath)
72+
System.setProperty("processing.app.preferences.debounce", "0")
73+
System.setProperty("processing.app.watchfile.forced", "true")
74+
val testKey = "test.preferences.backward.slash"
75+
76+
val value = "C:\\Users\\Test\\Documents"
77+
tempPreferences.writeText("$testKey=$value")
78+
79+
setContent {
80+
PreferencesProvider {
81+
val preferences = LocalPreferences.current
82+
Text(preferences[testKey] ?: "default", modifier = Modifier.testTag("text"))
83+
}
84+
}
85+
86+
onNodeWithTag("text").assertTextEquals(value.replace("\\", "/"))
87+
}
6188
}

0 commit comments

Comments
 (0)