fix(auto-update): handle null JSON.parse and restore mocks on test failure

This commit is contained in:
acamq
2026-03-11 08:08:30 -06:00
parent 9cdd2a11ec
commit 66cb16c8d2
2 changed files with 41 additions and 37 deletions
@@ -241,6 +241,7 @@ describe("syncCachePackageJsonToIntent", () => {
renameSync: fs.renameSync, renameSync: fs.renameSync,
})) }))
try {
const { syncCachePackageJsonToIntent } = await import("./sync-package-json") const { syncCachePackageJsonToIntent } = await import("./sync-package-json")
const pluginInfo: PluginEntryInfo = { const pluginInfo: PluginEntryInfo = {
@@ -254,12 +255,13 @@ describe("syncCachePackageJsonToIntent", () => {
expect(result.synced).toBe(false) expect(result.synced).toBe(false)
expect(result.error).toBe("write_error") expect(result.error).toBe("write_error")
} finally {
mock.module("node:fs", () => ({ mock.module("node:fs", () => ({
...fs, ...fs,
writeFileSync: originalWriteFileSync, writeFileSync: originalWriteFileSync,
renameSync: originalRenameSync, renameSync: originalRenameSync,
})) }))
}
}) })
}) })
@@ -289,6 +291,7 @@ describe("syncCachePackageJsonToIntent", () => {
}), }),
})) }))
try {
const { syncCachePackageJsonToIntent } = await import("./sync-package-json") const { syncCachePackageJsonToIntent } = await import("./sync-package-json")
const pluginInfo: PluginEntryInfo = { const pluginInfo: PluginEntryInfo = {
@@ -304,12 +307,13 @@ describe("syncCachePackageJsonToIntent", () => {
expect(result.error).toBe("write_error") expect(result.error).toBe("write_error")
expect(tempFilePath).not.toBeNull() expect(tempFilePath).not.toBeNull()
expect(existsSync(tempFilePath!)).toBe(false) expect(existsSync(tempFilePath!)).toBe(false)
} finally {
mock.module("node:fs", () => ({ mock.module("node:fs", () => ({
...fs, ...fs,
writeFileSync: originalWriteFileSync, writeFileSync: originalWriteFileSync,
renameSync: originalRenameSync, renameSync: originalRenameSync,
})) }))
}
}) })
}) })
}) })
@@ -57,7 +57,7 @@ export function syncCachePackageJsonToIntent(pluginInfo: PluginEntryInfo): SyncR
return { synced: false, error: "parse_error", message: "Failed to parse cache package.json (malformed JSON)" } return { synced: false, error: "parse_error", message: "Failed to parse cache package.json (malformed JSON)" }
} }
if (!pkgJson.dependencies?.[PACKAGE_NAME]) { if (!pkgJson || !pkgJson.dependencies?.[PACKAGE_NAME]) {
log("[auto-update-checker] Plugin not in cache package.json dependencies, nothing to sync") log("[auto-update-checker] Plugin not in cache package.json dependencies, nothing to sync")
return { synced: false, error: "plugin_not_in_deps", message: "Plugin not in cache package.json dependencies" } return { synced: false, error: "plugin_not_in_deps", message: "Plugin not in cache package.json dependencies" }
} }