Skip to content
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
Expand Up @@ -10,18 +10,25 @@ class GraphWriter(private val nodes: List<ProjectGraph>, val path: String) {
println("Creating graph file")
val file = File("$path/graph.dot")
file.createNewFile()
file.writeText(render(nodes))
}

val content = StringBuilder()
content.appendLine("digraph G {")
companion object {
fun render(nodes: List<ProjectGraph>): String {
val content = StringBuilder()
content.appendLine("digraph G {")

for (nodeGraph in nodes) {
val node = "${NameMappings.layerName(nodeGraph.layer)}:${NameMappings.moduleName(nodeGraph.id)}"
for (dep in nodeGraph.nodes) {
content.appendLine("\"$node\" -> \"${NameMappings.layerName(dep.layer)}:${NameMappings.moduleName(dep.id)}\";")
for (nodeGraph in nodes) {
val node = "${NameMappings.layerName(nodeGraph.layer)}:${NameMappings.moduleName(nodeGraph.id)}"
for (dep in nodeGraph.nodes) {
content.appendLine(
"\"$node\" -> \"${NameMappings.layerName(dep.layer)}:${NameMappings.moduleName(dep.id)}\";"
)
}
}
}

content.appendLine("}")
file.writeText(content.toString())
content.appendLine("}")
return content.toString()
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package io.github.cdsap.projectgenerator.writer

import io.github.cdsap.projectgenerator.NameMappings
import io.github.cdsap.projectgenerator.model.ProjectGraph
import io.github.cdsap.projectgenerator.model.TypeProject
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.io.TempDir
import java.io.File
import java.nio.file.Path

class GraphWriterTest {

@TempDir
lateinit var tempDir: Path

@Test
fun `render builds digraph edges with layer module labels from NameMappings`() {
val previousLayerNames = NameMappings.layerNames
val previousModuleNames = NameMappings.moduleNames
NameMappings.layerNames = mapOf(1 to "layer_1", 2 to "app")
NameMappings.moduleNames = mapOf("module_1_1" to "sample-lib", "module_2_1" to "app")
try {
val lib = ProjectGraph("module_1_1", 1, emptyList(), TypeProject.LIB, 1)
val app = ProjectGraph("module_2_1", 2, listOf(lib), TypeProject.ANDROID_APP, 1)

val dot = GraphWriter.render(listOf(lib, app))

assertEquals(
"""
digraph G {
"app:app" -> "layer_1:sample-lib";
}

""".trimIndent(),
dot
)
} finally {
NameMappings.layerNames = previousLayerNames
NameMappings.moduleNames = previousModuleNames
}
}

@Test
fun `write writes render output to graph dot`() {
val previousLayerNames = NameMappings.layerNames
val previousModuleNames = NameMappings.moduleNames
NameMappings.layerNames = mapOf(1 to "layer_1", 2 to "app")
NameMappings.moduleNames = mapOf("module_1_1" to "sample-lib", "module_2_1" to "app")
try {
val lib = ProjectGraph("module_1_1", 1, emptyList(), TypeProject.LIB, 1)
val app = ProjectGraph("module_2_1", 2, listOf(lib), TypeProject.ANDROID_APP, 1)
val nodes = listOf(lib, app)
val outDir = tempDir.resolve("out").toFile().also { it.mkdirs() }

GraphWriter(nodes, outDir.path).write()

val graphFile = File(outDir, "graph.dot")
assertTrue(graphFile.exists())
assertEquals(GraphWriter.render(nodes), graphFile.readText())
} finally {
NameMappings.layerNames = previousLayerNames
NameMappings.moduleNames = previousModuleNames
}
}
}
Loading