From 17b93f51cccdcda7be36a3c6fc599e80580e850e Mon Sep 17 00:00:00 2001 From: Daniel McCoy Stephenson Date: Mon, 16 Nov 2026 19:32:41 -0700 Subject: [PATCH] Test UsageReportingService's value handling and no-home path Lock in that only "false" turns reporting off (any case, trimmed) while values such as "no", "0" and "off" leave it on, as COMMANDS.md documents; that the version is trimmed and a blank or missing one becomes "unknown"; that the installation ID file sits next to the notice marker; and that without a usable home directory no marker, installation ID or first-run notice exists while the startup event is still sent. Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 1 + .../backup/UsageReportingServiceTest.java | 53 +++++++++++++++++++ 2 files changed, 54 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index fdc7f39..f2cbdb7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,6 +20,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). - 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. +- Unit tests for `UsageReportingService`'s value handling and no-home path: only `false` (in any case, with surrounding spaces) turns reporting off, while values such as `no`, `0` and `off` leave it on, as `COMMANDS.md` documents for `USAGE_REPORTING_ENABLED`; the version is trimmed and a missing or blank one is sent as `unknown`; the installation ID file sits next to the notice marker; and without a usable home directory there is no notice marker, no installation ID and no first-run notice, while the `startup` event is still sent. Before these, only the `false`/blank cases and the unfiltered `@project.version@` placeholder were tested. ### Changed diff --git a/src/test/java/com/github/backup/UsageReportingServiceTest.java b/src/test/java/com/github/backup/UsageReportingServiceTest.java index 8952483..0dbc0f3 100644 --- a/src/test/java/com/github/backup/UsageReportingServiceTest.java +++ b/src/test/java/com/github/backup/UsageReportingServiceTest.java @@ -22,6 +22,7 @@ import java.util.concurrent.TimeUnit; import static org.junit.jupiter.api.Assertions.*; +import static org.junit.jupiter.api.Assumptions.assumeTrue; /** * Drives the usage-reporting wiring against a stub trace server on a loopback @@ -199,6 +200,58 @@ void defaultMarkerLivesUnderTheUsersConfigDirectory() { assertTrue(marker.endsWith(Path.of(".config", "gh-backup", UsageReportingService.NOTICE_MARKER_FILE)), marker.toString()); } + @Test + void onlyFalseTurnsReportingOffWhateverItsCaseOrSurroundingSpace() { + assertFalse(new UsageReportingService(" False ", endpoint(), "test-key", "1", marker()).isEnabled()); + // COMMANDS.md: any value of USAGE_REPORTING_ENABLED other than false leaves reporting on + for (String other : new String[] {"no", "0", "off", "disabled", "true"}) { + assertTrue(new UsageReportingService(other, endpoint(), "test-key", "1", marker()).isEnabled(), + "'" + other + "' should leave reporting on"); + } + } + + @Test + void versionIsTrimmedAndAMissingOneIsSentAsUnknown() { + assertEquals("2.0.0", UsageReportingService.versionOrUnknown(" 2.0.0 ")); + assertEquals(UsageReportingService.UNKNOWN_VERSION, UsageReportingService.versionOrUnknown(null)); + assertEquals(UsageReportingService.UNKNOWN_VERSION, UsageReportingService.versionOrUnknown("")); + assertEquals(UsageReportingService.UNKNOWN_VERSION, UsageReportingService.versionOrUnknown(" ")); + assertEquals(UsageReportingService.UNKNOWN_VERSION, UsageReportingService.versionOrUnknown(" @project.version@ ")); + } + + @Test + void installationIdFileIsKeptNextToTheNoticeMarkerAndAbsentWithoutOne() { + assertEquals(marker().resolveSibling("trace-install-id").toFile(), UsageReportingService.installIdFile(marker())); + assertNull(UsageReportingService.installIdFile(null)); + } + + @Test + void noDefaultMarkerWithoutAUsableHomeDirectory() { + String home = System.getProperty("user.home"); + try { + System.setProperty("user.home", " "); + assertNull(UsageReportingService.defaultNoticeMarker()); + System.clearProperty("user.home"); + assertNull(UsageReportingService.defaultNoticeMarker()); + } finally { + System.setProperty("user.home", home); + } + } + + @Test + void withoutAHomeDirectoryTheStartupEventIsStillSentButNoNoticeOrInstallationId() throws Exception { + assumeTrue(System.getenv("TRACE_INSTALL_ID") == null, "TRACE_INSTALL_ID would pin an ID"); + UsageReportingService service = new UsageReportingService("true", endpoint(), "test-key", "2.0.0-TEST", null); + assertNull(service.installId(), "with nowhere to keep it, no installation ID is made up"); + + service.start(); + assertTrue(arrived.await(5, TimeUnit.SECONDS), "startup event should arrive"); + service.close(); + + assertEquals("{\"application\":\"gh-backup\",\"name\":\"startup\",\"tags\":{\"version\":\"2.0.0-TEST\"}}", bodies.get(0)); + assertEquals(0, noticesLogged(), "with nowhere to record it, the notice is not shown"); + } + private static byte[] readAll(InputStream in) throws java.io.IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); byte[] buffer = new byte[1024];