@@ -15,6 +15,8 @@ import java.util.jar.JarOutputStream
1515import org.junit.jupiter.api.Assertions.*
1616import org.junit.jupiter.api.Test
1717import org.junit.jupiter.api.io.TempDir
18+ import org.junit.jupiter.params.ParameterizedTest
19+ import org.junit.jupiter.params.provider.ValueSource
1820
1921class AddonInstallerTest {
2022 @TempDir lateinit var tempDir: Path
@@ -66,15 +68,37 @@ class AddonInstallerTest {
6668 assertThrows(IOException ::class .java) { AddonInstaller .sortModules(listOf (first, second.copy(dependsOn = listOf (" A" )))) }
6769 }
6870
69- @Test
70- fun `dependencies retain template entries and do not duplicate selections` () {
71+ @ParameterizedTest
72+ @ValueSource(strings = [" \n " , " \r\n " ])
73+ fun `dependencies start the main block and do not duplicate selections` (newline : String ) {
7174 val build = tempDir.resolve(" build.gradle" )
72- Files .writeString(build, " dependencies { implementation 'demo:existing' }\n " )
75+ val buildscript = """
76+ buildscript {
77+ dependencies { classpath 'demo:plugin' }
78+ }
79+
80+ """ .trimIndent().replace(" \n " , newline)
81+ Files .writeString(build, buildscript + " dependencies {${newline} implementation 'demo:existing'${newline} }$newline " )
7382 AddonInstaller .appendDependencies(info(), build)
7483 AddonInstaller .appendDependencies(info(), build)
7584 val text = Files .readString(build)
85+ assertTrue(text.startsWith(buildscript))
86+ assertTrue(text.contains(" dependencies {$newline // Selected Jmix add-ons$newline implementation 'demo:sample-starter:3.0.1'" ))
87+ assertEquals(1 , Regex (" (?m)^dependencies" ).findAll(text).count())
7688 assertTrue(text.contains(" demo:existing" ))
89+ assertTrue(text.indexOf(" demo:sample-starter" ) < text.indexOf(" demo:existing" ))
7790 assertEquals(1 , Regex (" demo:sample-starter:3.0.1" ).findAll(text).count())
91+ if (newline == " \r\n " ) assertFalse(text.replace(newline, " " ).contains(' \n ' ))
92+ }
93+
94+ @Test
95+ fun `missing main dependencies block leaves the build file unchanged` () {
96+ val build = tempDir.resolve(" build.gradle" )
97+ val text = " buildscript {\n dependencies { classpath 'demo:plugin' }\n }\n "
98+ Files .writeString(build, text)
99+
100+ assertThrows(IOException ::class .java) { AddonInstaller .appendDependencies(info(), build) }
101+ assertEquals(text, Files .readString(build))
78102 }
79103
80104 @Test
@@ -178,6 +202,7 @@ class AddonInstallerTest {
178202 assertFalse(Files .readString(build).contains(" security-starter" ))
179203 repeat(2 ) { AddonInstaller .configureTestSecurity(build, listOf (security)) }
180204 assertEquals(1 , Regex (" testImplementation 'io.jmix.security:jmix-security-starter'" ).findAll(Files .readString(build)).count())
205+ assertEquals(1 , Regex (" (?m)^dependencies" ).findAll(Files .readString(build)).count())
181206 assertEquals(if (repository.isEmpty()) 1 else 0 ,
182207 Regex (" addonTestUserRepository" ).findAll(Files .readString(config)).count())
183208 if (extension == " kt" ) assertTrue(Files .readString(config).contains(" open fun addonTestUserRepository" ))
0 commit comments