Skip to content

Commit 8b14110

Browse files
authored
fix(cli-repl): clear update notification cache when source URL changes (#1587)
This should remove flakiness from a recently introduced test and should also just generally be the right thing to do.
1 parent cefa8c7 commit 8b14110

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

packages/cli-repl/src/update-notification-manager.spec.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,10 @@ describe('UpdateNotificationManager', function () {
4545
expect(await manager.getLatestVersionIfMoreRecent('')).to.equal(null);
4646
expect(reqHandler).to.have.been.calledOnce;
4747
const fileContents = JSON.parse(await fs.readFile(filename, 'utf-8'));
48-
expect(Object.keys(fileContents)).to.deep.equal(['lastChecked']);
48+
expect(Object.keys(fileContents)).to.deep.equal([
49+
'updateURL',
50+
'lastChecked',
51+
]);
4952
expect(fileContents.lastChecked).to.be.a('number');
5053
});
5154

@@ -56,6 +59,13 @@ describe('UpdateNotificationManager', function () {
5659
expect(reqHandler).to.have.been.calledOnce;
5760
});
5861

62+
it('does not re-use existing data if the updateURL value has changed', async function () {
63+
const manager = new UpdateNotificationManager();
64+
await manager.fetchUpdateMetadata(httpServerUrl, filename);
65+
await manager.fetchUpdateMetadata(httpServerUrl + '/?foo=bar', filename);
66+
expect(reqHandler).to.have.been.calledTwice;
67+
});
68+
5969
it('caches 304 responses if the server supports ETag-based caching', async function () {
6070
let cacheHits = 0;
6171
reqHandler.callsFake((req, res) => {

packages/cli-repl/src/update-notification-manager.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ interface MongoshUpdateLocalFileContents {
66
lastChecked?: number;
77
latestKnownMongoshVersion?: string;
88
etag?: string;
9+
updateURL?: string;
910
}
1011

1112
// Utility for fetching metadata about potentially available newer versions
@@ -64,6 +65,11 @@ export class UpdateNotificationManager {
6465
// ignore possibly corrupted file contents
6566
}
6667

68+
if (localFileContents?.updateURL !== updateURL) {
69+
// Invalidate local cache if the source URL has changed.
70+
localFileContents = undefined;
71+
}
72+
6773
if (localFileContents?.latestKnownMongoshVersion) {
6874
this.latestKnownMongoshVersion =
6975
localFileContents.latestKnownMongoshVersion;
@@ -109,6 +115,7 @@ export class UpdateNotificationManager {
109115
?.sort(semver.rcompare)?.[0];
110116

111117
localFileContents = {
118+
updateURL,
112119
lastChecked: Date.now(),
113120
etag: response.headers.get('etag') ?? undefined,
114121
latestKnownMongoshVersion: this.latestKnownMongoshVersion,

0 commit comments

Comments
 (0)