Skip to content

Commit 5c77363

Browse files
shannahclaude
andauthored
fix: clean up blank lines and orphaned comments in shell config files (#454)
When jDeploy installs/uninstalls CLI commands, it adds and removes PATH entries from .zshrc/.bashrc. Over repeated cycles, this left behind accumulating blank lines and orphaned "# Added by jDeploy installer" comments (not followed by an export line). Three fixes in UnixPathManager: - removePathFromConfigFile: collapse 3+ consecutive blank lines to one - removePathFromConfigFile: remove orphaned jDeploy comments (not followed by an export PATH line) - addPathToConfigFile: trim trailing whitespace before appending new entry, preventing blank line accumulation https://claude.ai/code/session_01D3Pey4PcNjGphHHbbADRdC Co-authored-by: Claude <noreply@anthropic.com>
1 parent c91133f commit 5c77363

2 files changed

Lines changed: 132 additions & 5 deletions

File tree

‎installer/src/main/java/ca/weblite/jdeploy/installer/cli/UnixPathManager.java‎

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -187,10 +187,15 @@ private static boolean addPathToConfigFile(File configFile, File binDir, File ho
187187
}
188188

189189
// Append PATH export to the config file (always at the end)
190+
// Read current content to ensure clean trailing whitespace before appending
190191
DebugLogger.log("Writing PATH export to: " + configFile.getAbsolutePath());
191-
String pathExport = "\n# Added by jDeploy installer\nexport PATH=\"" + pathExportString + ":$PATH\"\n";
192-
try (FileOutputStream fos = new FileOutputStream(configFile, true)) {
193-
fos.write(pathExport.getBytes(StandardCharsets.UTF_8));
192+
String currentContent = IOUtil.readToString(new FileInputStream(configFile));
193+
String trimmedContent = currentContent.replaceAll("\\s+$", "");
194+
// Use a blank line separator only when there's existing content
195+
String separator = trimmedContent.isEmpty() ? "" : "\n\n";
196+
String pathExport = separator + "# Added by jDeploy installer\nexport PATH=\"" + pathExportString + ":$PATH\"\n";
197+
try (FileOutputStream fos = new FileOutputStream(configFile)) {
198+
fos.write((trimmedContent + pathExport).getBytes(StandardCharsets.UTF_8));
194199
}
195200

196201
System.out.println("Added " + displayPath + " to PATH in " + configFile.getName());
@@ -299,13 +304,22 @@ public static boolean removePathFromConfigFile(File configFile, File binDir, Fil
299304
// Look ahead to see if the next line is an export for our binDir
300305
if (i + 1 < lines.length) {
301306
String nextLine = lines[i + 1].trim();
302-
if (nextLine.startsWith("export PATH=\"") &&
307+
if (nextLine.startsWith("export PATH=\"") &&
303308
(nextLine.contains(pathExportString) || nextLine.contains(absolutePath))) {
304309
// Skip this comment line and mark to skip the export line
305310
skipNextExport = true;
306311
removed = true;
307312
continue;
308313
}
314+
// Orphaned comment: not followed by any export PATH line
315+
if (!nextLine.startsWith("export PATH=\"")) {
316+
removed = true;
317+
continue;
318+
}
319+
} else {
320+
// Comment at end of file with no following line - orphaned
321+
removed = true;
322+
continue;
309323
}
310324
}
311325

@@ -335,8 +349,12 @@ public static boolean removePathFromConfigFile(File configFile, File binDir, Fil
335349

336350
if (removed) {
337351
// Write back the modified content
338-
// Preserve trailing newline if original had one
339352
String newContent = result.toString();
353+
// Collapse multiple consecutive blank lines into at most one
354+
newContent = newContent.replaceAll("\n{3,}", "\n\n");
355+
// Trim trailing blank lines, keeping at most one trailing newline
356+
newContent = newContent.replaceAll("\n{2,}$", "\n");
357+
// Preserve trailing newline if original had one
340358
if (content.endsWith("\n") && !newContent.endsWith("\n")) {
341359
newContent += "\n";
342360
}

‎installer/src/test/java/ca/weblite/jdeploy/installer/cli/UnixPathManagerTest.java‎

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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\nexport FOO=bar\n\n\n\n\n# Added by jDeploy installer\nexport PATH=\"" + existingPath + ":$PATH\"\n\n\n\n\nexport 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\nexport FOO=bar\n# Added by jDeploy installer\n\n# Added by jDeploy installer\n# Added by jDeploy installer\nexport PATH=\"" + existingPath + ":$PATH\"\nexport 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\nexport FOO=bar\n# Added by jDeploy installer\nexport 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\nexport PATH=\"" + existingPath + ":$PATH\"\n# Added by jDeploy installer\nexport PATH=\"/some/other/app:$PATH\"\nexport 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\nexport FOO=bar\n# Added by jDeploy installer\nexport 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\nexport 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

Comments
 (0)