Skip to content

Commit 05554b7

Browse files
Fix: Keep dots in directory names (#3274)
Previously, `nameWithoutExtension()` would incorrectly strip parts of a directory name if it contained a dot. This change ensures that for directories, the full name is returned, while for files, the extension is correctly removed. Fixes #3240
1 parent 5fd5653 commit 05554b7

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

core/documentfile/src/main/kotlin/voice/core/documentfile/CachedDocumentFile.kt

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,12 @@ fun CachedDocumentFile.nameWithoutExtension(): String {
2121
?.takeUnless { it.isBlank() }
2222
?: uri.toString()
2323
} else {
24-
name.substringBeforeLast(".")
25-
.takeUnless { it.isEmpty() }
26-
?: name
24+
if (isFile) {
25+
name.substringBeforeLast(".")
26+
.takeUnless { it.isEmpty() }
27+
?: name
28+
} else {
29+
name
30+
}
2731
}
2832
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package voice.core.documentfile
2+
3+
import io.kotest.matchers.shouldBe
4+
import org.junit.Rule
5+
import org.junit.Test
6+
import org.junit.rules.TemporaryFolder
7+
8+
class NameWithoutExtensionTest {
9+
10+
@get:Rule
11+
val testFolder = TemporaryFolder()
12+
13+
@Test
14+
fun keepsDotsForDirectoryNames() {
15+
val folder = testFolder.newFolder("Author.Name")
16+
17+
FileBasedDocumentFile(folder).nameWithoutExtension() shouldBe "Author.Name"
18+
}
19+
20+
@Test
21+
fun stripsExtensionForFileNames() {
22+
val file = testFolder.newFile("Chapter.01.m4b")
23+
24+
FileBasedDocumentFile(file).nameWithoutExtension() shouldBe "Chapter.01"
25+
}
26+
}

0 commit comments

Comments
 (0)