diff --git a/src/main/kotlin/nl/avisi/structurizr/site/generatr/StringUtilities.kt b/src/main/kotlin/nl/avisi/structurizr/site/generatr/StringUtilities.kt index 1afba68e..6599720d 100644 --- a/src/main/kotlin/nl/avisi/structurizr/site/generatr/StringUtilities.kt +++ b/src/main/kotlin/nl/avisi/structurizr/site/generatr/StringUtilities.kt @@ -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 + } diff --git a/src/test/kotlin/nl/avisi/structurizr/site/generatr/site/StringUtilitiesTest.kt b/src/test/kotlin/nl/avisi/structurizr/site/generatr/site/StringUtilitiesTest.kt new file mode 100644 index 00000000..0b959d21 --- /dev/null +++ b/src/test/kotlin/nl/avisi/structurizr/site/generatr/site/StringUtilitiesTest.kt @@ -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) + } + } +} diff --git a/src/test/kotlin/nl/avisi/structurizr/site/generatr/site/StructurizrUtilitiesTest.kt b/src/test/kotlin/nl/avisi/structurizr/site/generatr/site/StructurizrUtilitiesTest.kt index 7ebb8900..8657d901 100644 --- a/src/test/kotlin/nl/avisi/structurizr/site/generatr/site/StructurizrUtilitiesTest.kt +++ b/src/test/kotlin/nl/avisi/structurizr/site/generatr/site/StructurizrUtilitiesTest.kt @@ -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 = listOf("main"), currentBranch: String = "main", @@ -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 @@ -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) } }