@@ -1092,4 +1092,113 @@ public void testAddToPathOnLinuxRespectsNoAutoPathInProfile() throws IOException
10921092 assertTrue (bashrcContent .contains (binDir .getAbsolutePath ()),
10931093 ".bashrc should contain the PATH export" );
10941094 }
1095+
1096+ // ==================== Blank Line Cleanup Tests ====================
1097+
1098+ @ Test
1099+ public void testRemovePathCollapsesConsecutiveBlankLines () throws IOException {
1100+ File bashrc = new File (homeDir , ".bashrc" );
1101+ String existingPath = binDir .getAbsolutePath ();
1102+ // Simulate a file with blank lines around jDeploy entries (from repeated install/uninstall)
1103+ String originalContent = "# Some config\n export FOO=bar\n \n \n \n \n # Added by jDeploy installer\n export PATH=\" " + existingPath + ":$PATH\" \n \n \n \n \n export BAZ=qux\n " ;
1104+ Files .write (bashrc .toPath (), originalContent .getBytes (StandardCharsets .UTF_8 ));
1105+
1106+ boolean removed = UnixPathManager .removePathFromConfigFile (bashrc , binDir , homeDir );
1107+
1108+ assertTrue (removed , "Should return true when entry was removed" );
1109+ String content = IOUtil .readToString (new FileInputStream (bashrc ));
1110+ assertFalse (content .contains (existingPath ), "PATH entry should be removed" );
1111+ assertFalse (content .contains ("\n \n \n " ), "Should not have more than one consecutive blank line" );
1112+ assertTrue (content .contains ("export FOO=bar" ), "Other content should be preserved" );
1113+ assertTrue (content .contains ("export BAZ=qux" ), "Other content should be preserved" );
1114+ }
1115+
1116+ @ Test
1117+ public void testRemovePathRemovesOrphanedComments () throws IOException {
1118+ File bashrc = new File (homeDir , ".bashrc" );
1119+ String existingPath = binDir .getAbsolutePath ();
1120+ // Simulate orphaned comments (comment not followed by export PATH line)
1121+ String originalContent = "# Some config\n export FOO=bar\n # Added by jDeploy installer\n \n # Added by jDeploy installer\n # Added by jDeploy installer\n export PATH=\" " + existingPath + ":$PATH\" \n export BAZ=qux\n " ;
1122+ Files .write (bashrc .toPath (), originalContent .getBytes (StandardCharsets .UTF_8 ));
1123+
1124+ boolean removed = UnixPathManager .removePathFromConfigFile (bashrc , binDir , homeDir );
1125+
1126+ assertTrue (removed , "Should return true when entries were removed" );
1127+ String content = IOUtil .readToString (new FileInputStream (bashrc ));
1128+ assertFalse (content .contains ("# Added by jDeploy installer" ), "All jDeploy comments should be removed" );
1129+ assertFalse (content .contains (existingPath ), "PATH entry should be removed" );
1130+ assertTrue (content .contains ("export FOO=bar" ), "Other content should be preserved" );
1131+ assertTrue (content .contains ("export BAZ=qux" ), "Other content should be preserved" );
1132+ }
1133+
1134+ @ Test
1135+ public void testRemovePathRemovesOrphanedCommentAtEndOfFile () throws IOException {
1136+ File bashrc = new File (homeDir , ".bashrc" );
1137+ String existingPath = binDir .getAbsolutePath ();
1138+ // Orphaned comment at end of file
1139+ String originalContent = "# Some config\n export FOO=bar\n # Added by jDeploy installer\n export PATH=\" " + existingPath + ":$PATH\" \n # Added by jDeploy installer\n " ;
1140+ Files .write (bashrc .toPath (), originalContent .getBytes (StandardCharsets .UTF_8 ));
1141+
1142+ boolean removed = UnixPathManager .removePathFromConfigFile (bashrc , binDir , homeDir );
1143+
1144+ assertTrue (removed , "Should return true when entries were removed" );
1145+ String content = IOUtil .readToString (new FileInputStream (bashrc ));
1146+ assertFalse (content .contains ("# Added by jDeploy installer" ), "All jDeploy comments should be removed" );
1147+ assertFalse (content .contains (existingPath ), "PATH entry should be removed" );
1148+ assertTrue (content .contains ("export FOO=bar" ), "Other content should be preserved" );
1149+ }
1150+
1151+ @ Test
1152+ public void testRemovePathPreservesCommentsForOtherApps () throws IOException {
1153+ File bashrc = new File (homeDir , ".bashrc" );
1154+ String existingPath = binDir .getAbsolutePath ();
1155+ // Comment+export for a different app should be preserved
1156+ String originalContent = "# Some config\n # Added by jDeploy installer\n export PATH=\" " + existingPath + ":$PATH\" \n # Added by jDeploy installer\n export PATH=\" /some/other/app:$PATH\" \n export FOO=bar\n " ;
1157+ Files .write (bashrc .toPath (), originalContent .getBytes (StandardCharsets .UTF_8 ));
1158+
1159+ boolean removed = UnixPathManager .removePathFromConfigFile (bashrc , binDir , homeDir );
1160+
1161+ assertTrue (removed , "Should return true when entry was removed" );
1162+ String content = IOUtil .readToString (new FileInputStream (bashrc ));
1163+ assertFalse (content .contains (existingPath ), "Our PATH entry should be removed" );
1164+ assertTrue (content .contains ("# Added by jDeploy installer" ), "Comment for other app should be preserved" );
1165+ assertTrue (content .contains ("/some/other/app" ), "Other app's PATH should be preserved" );
1166+ assertTrue (content .contains ("export FOO=bar" ), "Other content should be preserved" );
1167+ }
1168+
1169+ @ Test
1170+ public void testRemovePathTrimsTrailingBlankLines () throws IOException {
1171+ File bashrc = new File (homeDir , ".bashrc" );
1172+ String existingPath = binDir .getAbsolutePath ();
1173+ // Entry at end of file followed by blank lines
1174+ String originalContent = "# Some config\n export FOO=bar\n # Added by jDeploy installer\n export PATH=\" " + existingPath + ":$PATH\" \n \n \n \n " ;
1175+ Files .write (bashrc .toPath (), originalContent .getBytes (StandardCharsets .UTF_8 ));
1176+
1177+ boolean removed = UnixPathManager .removePathFromConfigFile (bashrc , binDir , homeDir );
1178+
1179+ assertTrue (removed , "Should return true when entry was removed" );
1180+ String content = IOUtil .readToString (new FileInputStream (bashrc ));
1181+ assertFalse (content .contains (existingPath ), "PATH entry should be removed" );
1182+ assertTrue (content .endsWith ("\n " ), "Should end with a single newline" );
1183+ assertFalse (content .endsWith ("\n \n " ), "Should not end with multiple newlines" );
1184+ }
1185+
1186+ @ Test
1187+ public void testAddPathDoesNotAccumulateBlankLines () throws IOException {
1188+ File bashrc = new File (homeDir , ".bashrc" );
1189+ String existingPath = binDir .getAbsolutePath ();
1190+ // Simulate existing content with trailing blank lines
1191+ String originalContent = "# Some config\n export FOO=bar\n \n \n \n " ;
1192+ Files .write (bashrc .toPath (), originalContent .getBytes (StandardCharsets .UTF_8 ));
1193+
1194+ // Add path entry
1195+ String shell = "/bin/bash" ;
1196+ String pathEnv = "/usr/bin:/bin" ;
1197+ UnixPathManager .addToPath (binDir , shell , pathEnv , homeDir );
1198+
1199+ String content = IOUtil .readToString (new FileInputStream (bashrc ));
1200+ assertFalse (content .contains ("\n \n \n " ), "Should not have more than one consecutive blank line" );
1201+ assertTrue (content .contains ("# Added by jDeploy installer" ), "Should have jDeploy comment" );
1202+ assertTrue (content .contains (existingPath ), "Should have PATH entry" );
1203+ }
10951204}
0 commit comments