From f372f9e35aec4df007e7a9497550dd38eedc811f Mon Sep 17 00:00:00 2001 From: Daniel McCoy Stephenson Date: Sun, 15 Nov 2026 19:52:05 -0700 Subject: [PATCH] Test BackupService's failed-update and relative-directory paths A repository directory that exists but is not a Git repository is reported as a failed update without being cloned over, and a relative backup.directory is resolved and normalized. Neither was asserted by any test before. Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 1 + .../com/github/backup/BackupServiceTest.java | 54 +++++++++++++++++++ 2 files changed, 55 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0ad152e..fdc7f39 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). - Unit tests for `BackupCommandLineRunner`'s interactive mode, driven through a stubbed `System.in`: `-i` and `--interactive` start it only as the first argument, `backup ` backs up the trimmed name and reports one completed run per command (including a failed one), `backup` with no target prints its usage, `status`, `help`, `quit`, unknown commands and blank lines behave as documented in `COMMANDS.md`, and command names are matched case-insensitively while the user/org name keeps its case. Before these, interactive mode was covered only by a placeholder test that never ran it. - Unit tests for `POST /api/backups` request validation: a missing or blank `userOrOrg`, a leading, trailing or doubled hyphen, characters outside letters, digits and hyphens, and a name longer than 39 characters each return `400 Bad Request` without starting a backup or reporting a completed one, while a 39-character name and a single inner hyphen are accepted; a successful backup reports exactly one `backup-completed` event and a failed one reports none. Before these, the `400` behavior documented in `COMMANDS.md` was untested. - Unit tests for the clone/fetch progress monitor's `backup.progress.overwrite` handling and update edge cases: overwrite mode is the default when the property is unset, `false` (or any value other than `true`) selects one line per update, progress reported before any task has begun is ignored, an update reaching or overshooting the task's total leaves the `100%` line to the end of the task, and in overwrite mode each update rewrites the current line. Before these, the system property documented in `COMMANDS.md` and `CONFIG.md` was not read by any test. +- Unit tests for two `BackupService` paths that had no assertions: a repository whose directory already exists but is not a Git repository is reported as a failed update (`✗ Failed to update`, then named in the failure summary) without being cloned over or having its contents touched, while the other repositories are still backed up; and a relative `backup.directory` is resolved against the working directory and normalized, as the path printed by `status` shows. Before these, the update-failure branch was never run by a test and the relative-path test only checked that nothing was thrown. ### Changed diff --git a/src/test/java/com/github/backup/BackupServiceTest.java b/src/test/java/com/github/backup/BackupServiceTest.java index 1c7ec62..9544cb0 100644 --- a/src/test/java/com/github/backup/BackupServiceTest.java +++ b/src/test/java/com/github/backup/BackupServiceTest.java @@ -184,6 +184,33 @@ void testBackupUserRepositories_AllRepositoriesFailing_AreAllNamed() throws Exce failure.get().getMessage()); } + @Test + void testBackupUserRepositories_ExistingDirectoryThatIsNotARepositoryFailsToUpdate() throws Exception { + Path notARepository = Files.createDirectories(tempDir.resolve("testuser").resolve("repo1")); + Path keptFile = Files.writeString(notARepository.resolve("notes.txt"), "kept"); + Path source = createSourceRepository("source-repo2"); + when(mockRepo1.getName()).thenReturn("repo1"); + when(mockRepo2.getName()).thenReturn("repo2"); + when(mockRepo2.getHttpTransportUrl()).thenReturn(source.toUri().toString()); + when(gitHubService.getPublicRepositories("testuser")) + .thenReturn(Arrays.asList(mockRepo1, mockRepo2)); + + AtomicReference failure = new AtomicReference<>(); + AtomicReference stdout = new AtomicReference<>(); + String stderr = captureStderr(() -> stdout.set(captureStdout(() -> failure.set(assertThrows( + IOException.class, () -> backupService.backupUserRepositories("testuser")))))); + + // The directory is neither cloned over nor fetched into; it is reported as a failed update + assertTrue(stderr.contains(" ✗ Failed to update: "), stderr); + assertTrue(stderr.contains("Failed to backup repo1: Failed to update repository"), stderr); + assertFalse(stdout.get().contains("(repository exists, updating)"), stdout.get()); + verify(mockRepo1, never()).getHttpTransportUrl(); + assertEquals("kept", Files.readString(keptFile)); + assertFalse(Files.exists(notARepository.resolve(".git"))); + assertTrue(Files.isDirectory(tempDir.resolve("testuser").resolve("repo2").resolve(".git"))); + assertEquals("Failed to back up 1 of 2 repositories for testuser: repo1", failure.get().getMessage()); + } + @Test void testBackupUserRepositories_BackupDirectoryCannotBeCreated() throws IOException { Path blocker = Files.createFile(tempDir.resolve("blocker")); @@ -225,6 +252,19 @@ void testBackupDirectory_IsAbsolute() throws IOException { assertDoesNotThrow(() -> service.showBackupStatus()); } + @Test + void testBackupDirectory_RelativePathIsResolvedAgainstWorkingDirectoryAndNormalized() { + String relative = "gh-backup-test-missing/../gh-backup-test-missing-" + System.nanoTime(); + BackupService service = new BackupService(gitHubService, relative); + String expected = Path.of(relative).toAbsolutePath().normalize().toString(); + + String output = captureStdout(service::showBackupStatus); + + assertEquals("No backups found. Backup directory does not exist: " + expected + System.lineSeparator(), + output); + assertFalse(output.contains(".."), output); + } + @Test void testBackupUserRepositories_HandlesException() throws IOException { when(gitHubService.getPublicRepositories("erroruser")) @@ -561,6 +601,20 @@ private static String captureStdout(ThrowingRunnable action) { return buffer.toString(StandardCharsets.UTF_8); } + private static String captureStderr(ThrowingRunnable action) { + PrintStream original = System.err; + ByteArrayOutputStream buffer = new ByteArrayOutputStream(); + System.setErr(new PrintStream(buffer, true, StandardCharsets.UTF_8)); + try { + action.run(); + } catch (Exception e) { + throw new AssertionError(e); + } finally { + System.setErr(original); + } + return buffer.toString(StandardCharsets.UTF_8); + } + @FunctionalInterface private interface ThrowingRunnable { void run() throws Exception;