Skip to content

Commit

Permalink
fix(core): update file path validation regex and related tests
Browse files Browse the repository at this point in the history
Updated the regular expression used for file path validation in SaveFileProcessor.kt to improve accuracy. Also, adjusted the corresponding tests in SaveFileProcessorTest.kt to reflect these changes.
  • Loading branch information
phodal committed Sep 14, 2024
1 parent 6c2401f commit 3ab4e9f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ class SaveFileProcessor : PostProcessor, Disposable {
}

fun getValidFilePath(filePath: String, ext: String): String {
val pathRegex = """^([a-zA-Z]:\\(?:[^<>:"/\\|?*]+\\)*[^<>:"/\\|?*]*)|(/[^<>:"/\\|?*]+(/[^<>:"/\\|?*]*)*)$""".toRegex()
val pathRegex = """^([a-zA-Z]:\\|\\\\|/|)([a-zA-Z0-9_\-\\/.]+)$""".toRegex()

if (filePath.isBlank()) {
return "${System.currentTimeMillis()}.$ext"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package com.phodal.shirecore.middleware.builtin

import com.intellij.testFramework.fixtures.BasePlatformTestCase
import junit.framework.TestCase

class FileNameTest : BasePlatformTestCase() {

fun testReturnTimestampWithExtensionWhenFilePathIsBlank() {
val ext = "txt"
val filePath = ""
Expand All @@ -23,13 +21,22 @@ class FileNameTest : BasePlatformTestCase() {
assertEquals(filePath, result)
}

// "docs/api.yml"
fun testReturnValidFilePathIfItMatchesRegexForLinux() {
val ext = "yml"
val filePath = "docs/api.yml"
val result = getValidFilePath(filePath, ext)

// Since the path is valid, it should return the same file path
assertEquals(filePath, result)
}

fun testReturnTimestampWithExtensionIfPathIsInvalid() {
val ext = "txt"
val filePath = "Invalid\\Path\\test"
val result = getValidFilePath(filePath, ext)

// If the file path is invalid, it should return a timestamp followed by the extension
assertTrue(result.matches(Regex("""\d+\.txt""")))
assertEquals(result, filePath)
}

fun testReturnTimestampWithExtensionIfPathIsInvalidForLinux() {
Expand Down

0 comments on commit 3ab4e9f

Please sign in to comment.