randomize tests and fix some edge cases

Signed-off-by: alperozturk <alper_ozturk@proton.me>
This commit is contained in:
alperozturk 2024-10-28 15:22:08 +01:00
parent 939388ffe4
commit f54eb094d6
No known key found for this signature in database
GPG key ID: 4E577DC593B59BDF
2 changed files with 38 additions and 51 deletions

View file

@ -26,7 +26,16 @@ class AutoRenameTests : AbstractOnServerIT() {
testOnlyOnServer(NextcloudVersion.nextcloud_30)
capability = capability.apply {
forbiddenFilenameExtensionJson = """[" ",".",".part",".part"]"""
forbiddenFilenameExtensionJson = listOf(
"""[" ",".",".part",".part"]""",
"""[".",".part",".part"," "]""",
"""[".",".part"," ", ".part"]""",
"""[".part"," ", ".part","."]""",
"""[" ",".",".PART",".PART"]""",
"""[".",".PART",".PART"," "]""",
"""[".",".PART"," ", ".PART"]""",
"""[".PART"," ", ".PART","."]"""
).random()
forbiddenFilenameCharactersJson = """["<", ">", ":", "\\\\", "/", "|", "?", "*", "&"]"""
}
}
@ -55,18 +64,6 @@ class AutoRenameTests : AbstractOnServerIT() {
assert(result == expectedFilename) { "Expected $expectedFilename but got $result" }
}
@Test
fun testStartEndInvalidExtensionsIfSpaceCharInTheEnd() {
capability = capability.apply {
forbiddenFilenameExtensionJson = """[".",".part",".part"," "]"""
}
val filename = " .file.part "
val result = AutoRename.rename(filename, capability)
val expectedFilename = "_file_part"
assert(result == expectedFilename) { "Expected $expectedFilename but got $result" }
}
@Test
fun testStartEndInvalidExtensions() {
val filename = " .file.part "
@ -75,18 +72,6 @@ class AutoRenameTests : AbstractOnServerIT() {
assert(result == expectedFilename) { "Expected $expectedFilename but got $result" }
}
@Test
fun testStartInvalidExtensionIfSpaceCharInTheEnd() {
capability = capability.apply {
forbiddenFilenameExtensionJson = """[".",".part",".part"," "]"""
}
val filename = " .file.part"
val result = AutoRename.rename(filename, capability)
val expectedFilename = "_file_part"
assert(result == expectedFilename) { "Expected $expectedFilename but got $result" }
}
@Test
fun testStartInvalidExtension() {
val filename = " .file.part"
@ -95,18 +80,6 @@ class AutoRenameTests : AbstractOnServerIT() {
assert(result == expectedFilename) { "Expected $expectedFilename but got $result" }
}
@Test
fun testEndInvalidExtensionIfSpaceCharInTheEnd() {
capability = capability.apply {
forbiddenFilenameExtensionJson = """[".",".part",".part"," "]"""
}
val filename = ".file.part "
val result = AutoRename.rename(filename, capability)
val expectedFilename = "_file_part"
assert(result == expectedFilename) { "Expected $expectedFilename but got $result" }
}
@Test
fun testEndInvalidExtension() {
val filename = ".file.part "

View file

@ -28,44 +28,48 @@ object AutoRename {
val pathSegments = filename.split(OCFile.PATH_SEPARATOR).toMutableList()
capability.run {
forbiddenFilenameCharactersJson?.let {
if (forbiddenFilenameCharactersJson != null) {
var forbiddenFilenameCharacters = capability.forbiddenFilenameCharacters()
if (isFolderPath) {
forbiddenFilenameCharacters = forbiddenFilenameCharacters.filter { it != OCFile.PATH_SEPARATOR }
}
pathSegments.replaceAll { segment ->
var modifiedSegment = segment
forbiddenFilenameCharacters.forEach { forbiddenChar ->
if (modifiedSegment.contains(forbiddenChar)) {
modifiedSegment = modifiedSegment.replace(forbiddenChar, REPLACEMENT)
}
}
modifiedSegment
}
}
forbiddenFilenameExtensionJson?.let {
// First handle if space char exist
forbiddenFilenameExtensions().find { it == StringConstants.SPACE }?.let {
if (forbiddenFilenameExtensionJson != null) {
val forbiddenFilenameExtensions = forbiddenFilenameExtensions()
forbiddenFilenameExtensions.find { it == StringConstants.SPACE }?.let {
pathSegments.replaceAll { segment ->
segment.trim()
}
}
forbiddenFilenameExtensions().forEach { forbiddenExtension ->
forbiddenFilenameExtensions.find { it == StringConstants.DOT }?.let { forbiddenExtension ->
pathSegments.replaceAll { segment ->
var modifiedSegment = segment
if (modifiedSegment.endsWith(forbiddenExtension, ignoreCase = true) ||
modifiedSegment.startsWith(forbiddenExtension, ignoreCase = true)
) {
modifiedSegment = modifiedSegment.replace(forbiddenExtension, REPLACEMENT)
}
modifiedSegment
replacePathSegment(forbiddenExtension, segment)
}
}
forbiddenFilenameExtensions
.filter { it != StringConstants.SPACE && it != StringConstants.DOT }
.forEach { forbiddenExtension ->
pathSegments.replaceAll { segment ->
replacePathSegment(forbiddenExtension, segment)
}
}
}
}
@ -78,6 +82,16 @@ object AutoRename {
}
}
private fun replacePathSegment(forbiddenExtension: String, segment: String): String {
return if (segment.endsWith(forbiddenExtension, ignoreCase = true) ||
segment.startsWith(forbiddenExtension, ignoreCase = true)
) {
segment.replace(forbiddenExtension, REPLACEMENT)
} else {
segment
}
}
private fun convertToUTF8(filename: String): String {
return String(filename.toByteArray(), Charsets.UTF_8)
}