Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Normalize file names even more #648

Merged
merged 2 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
package nl.avisi.structurizr.site.generatr

fun String.normalize(): String = lowercase().replace("\\s+".toRegex(), "-")
// based on https://stackoverflow.com/questions/1976007/what-characters-are-forbidden-in-windows-and-linux-directory-names
private const val reservedChars = "|\\?*<\":>+[]/'"
private val reservedNames = setOf(
"CON", "PRN", "AUX", "NUL",
"COM1", "COM2", "COM3", "COM4", "COM5", "COM6", "COM7", "COM8", "COM9",
"LPT1", "LPT2", "LPT3", "LPT4", "LPT5", "LPT6", "LPT7", "LPT8", "LPT9"
)

fun String.normalize(): String =
lowercase()
.replace("\\s+".toRegex(), "-")
.filterNot { reservedChars.contains(it) }
.trim()
.let {
if (reservedNames.contains(it.uppercase()))
"${it}-"
else
it
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package nl.avisi.structurizr.site.generatr.site

import assertk.assertThat
import assertk.assertions.isEqualTo
import nl.avisi.structurizr.site.generatr.normalize
import org.junit.jupiter.api.DynamicTest
import org.junit.jupiter.api.TestFactory

class StringUtilitiesTest {

@TestFactory
fun `normalize strips invalid chars`() = listOf(
listOf("doc", "doc"),
listOf("d c", "d-c"),
listOf("doc:", "doc"),
listOf(" doc ", "-doc-"),
listOf("aux", "aux-"),
).map { (actual, expected) ->
DynamicTest.dynamicTest("normalize replaces $actual with $expected") {
assertThat(actual.normalize()).isEqualTo(expected)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package nl.avisi.structurizr.site.generatr.site

import assertk.assertThat
import assertk.assertions.isEqualTo
import com.structurizr.Workspace
import nl.avisi.structurizr.site.generatr.includedProperties
import kotlin.test.Test
import kotlin.test.assertEquals

class StructurizrUtilitiesTest {

protected val svgFactory = { _: String, _: String -> "" }
private val svgFactory = { _: String, _: String -> "" }

protected fun generatorContext(
private fun generatorContext(
workspaceName: String = "Workspace name",
branches: List<String> = listOf("main"),
currentBranch: String = "main",
Expand All @@ -27,8 +28,8 @@ class StructurizrUtilitiesTest {

val includedProperties = element.includedProperties

assertEquals(3, includedProperties.size)
assertEquals(includedProperties.keys, setOf("structurizrnotquite.key", "generatrbut.not.key", "other.key"))
assertThat(includedProperties.size).isEqualTo(3)
assertThat(includedProperties.keys).isEqualTo(setOf("structurizrnotquite.key", "generatrbut.not.key", "other.key"))
}

@Test
Expand All @@ -39,15 +40,15 @@ class StructurizrUtilitiesTest {

val includedProperties = element.includedProperties

assertEquals(2, includedProperties.size)
assertEquals("key1", includedProperties.keys.first())
assertEquals("key2", includedProperties.keys.last())
assertThat(includedProperties.size).isEqualTo(2)
assertThat(includedProperties.keys.first()).isEqualTo("key1")
assertThat(includedProperties.keys.last()).isEqualTo("key2")
}

@Test
fun `includedProperties returns empty map when no properties are set`() {
val element = generatorContext().workspace.model.addSoftwareSystem("System 1")
val includedProperties = element.includedProperties
assertEquals(0, includedProperties.size)
assertThat(includedProperties.size).isEqualTo(0)
}
}
Loading