Skip to content

Commit 31dfcd0

Browse files
strzhaoclaude
andcommitted
fix(harmonyos): verify a bundle record is gone before reporting uninstall success
bm prints its verdict into stdout at host exit code 0 on 7.0.0.111, so the uninstall command result alone cannot separate a hit from a miss. A second bm dump now decides: a surviving record refuses with the bm output attached, and a missing record needs the same live-transport proof as the pre-probe. Co-Authored-By: Claude Code <[email protected]>
1 parent 6b5b601 commit 31dfcd0

2 files changed

Lines changed: 78 additions & 3 deletions

File tree

‎packages/platform-harmonyos/src/deployment/runtime.test.ts‎

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,14 @@ const installedDump: HostCommandResult = {
106106
};
107107

108108
test('uninstall removes an installed HarmonyOS bundle through bm uninstall', async () => {
109-
const run = vi.fn(async () => installedDump);
109+
const uninstallAnswer: HostCommandResult = {
110+
stdout: 'uninstall bundle successfully.',
111+
stderr: '',
112+
exitCode: 0,
113+
};
114+
const run = vi.fn(
115+
scriptAnswers([installedDump, uninstallAnswer, absentBundleAnswer, emptyAnswer]),
116+
);
110117
const commands = { which: async () => undefined, run } as HostCommandRunner;
111118
const signal = new AbortController().signal;
112119
const operations = createHarmonyAppDeploymentOperations({ commands, device, signal });
@@ -115,7 +122,7 @@ test('uninstall removes an installed HarmonyOS bundle through bm uninstall', asy
115122
wasInstalled: true,
116123
packageName: 'com.example.app',
117124
});
118-
expect(run).toHaveBeenCalledTimes(2);
125+
expect(run).toHaveBeenCalledTimes(4);
119126
expect(run).toHaveBeenNthCalledWith(
120127
1,
121128
expect.objectContaining({
@@ -132,8 +139,50 @@ test('uninstall removes an installed HarmonyOS bundle through bm uninstall', asy
132139
}),
133140
signal,
134141
);
142+
expect(run).toHaveBeenNthCalledWith(
143+
3,
144+
expect.objectContaining({
145+
executable: 'hdc',
146+
args: ['-t', device.id, 'shell', 'bm', 'dump', '-n', 'com.example.app'],
147+
}),
148+
signal,
149+
);
150+
expect(run).toHaveBeenNthCalledWith(
151+
4,
152+
expect.objectContaining({ args: ['-t', device.id, 'shell', 'true'] }),
153+
signal,
154+
);
135155
});
136156

157+
test('uninstall refuses to claim removal when the bundle record survives bm uninstall', async () => {
158+
const lyingUninstallAnswer: HostCommandResult = {
159+
stdout: 'uninstall bundle successfully.',
160+
stderr: '',
161+
exitCode: 0,
162+
};
163+
const run = vi.fn(scriptAnswers([installedDump, lyingUninstallAnswer, installedDump]));
164+
const commands = { which: async () => undefined, run } as HostCommandRunner;
165+
const signal = new AbortController().signal;
166+
const operations = createHarmonyAppDeploymentOperations({ commands, device, signal });
167+
168+
await expect(operations.uninstallApp?.({ app: 'com.example.app' })).rejects.toMatchObject({
169+
code: 'COMMAND_FAILED',
170+
message: 'com.example.app is still installed after bm uninstall',
171+
details: { stdout: 'uninstall bundle successfully.' },
172+
});
173+
expect(run).toHaveBeenCalledTimes(3);
174+
expect(run).not.toHaveBeenCalledWith(
175+
expect.objectContaining({ args: ['-t', device.id, 'shell', 'true'] }),
176+
signal,
177+
);
178+
});
179+
180+
/** Answers each call in order; the last answer repeats when a script runs past its end. */
181+
function scriptAnswers(answers: readonly HostCommandResult[]) {
182+
let next = 0;
183+
return async (): Promise<HostCommandResult> => answers[Math.min(next++, answers.length - 1)]!;
184+
}
185+
137186
const emptyAnswer: HostCommandResult = { stdout: '', stderr: '', exitCode: 0 };
138187

139188
/**

‎packages/platform-harmonyos/src/deployment/runtime.ts‎

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,33 @@ async function uninstallHarmonyApp(
9090
}
9191
return { wasInstalled: false };
9292
}
93-
await runHdcShell(commands, device, ['bm', 'uninstall', '-n', bundleId], signal);
93+
const uninstall = await runHdcShell(
94+
commands,
95+
device,
96+
['bm', 'uninstall', '-n', bundleId],
97+
signal,
98+
);
99+
const after = await runHdcShell(commands, device, ['bm', 'dump', '-n', bundleId], signal, {
100+
allowFailure: true,
101+
});
102+
if (parseBundleDump(after.stdout)) {
103+
// bm prints its verdict into stdout at exit code 0, so only a second dump that no
104+
// longer finds the record proves the removal.
105+
throw new AppError('COMMAND_FAILED', `${bundleId} is still installed after bm uninstall`, {
106+
exitCode: uninstall.exitCode,
107+
stdout: uninstall.stdout,
108+
stderr: uninstall.stderr,
109+
hint: 'Read the bm uninstall output above for the bm reason; system and shared bundles refuse removal.',
110+
});
111+
}
112+
if (after.exitCode === null || !(await isHarmonyShellTransportAlive(commands, device, signal))) {
113+
throw new AppError('COMMAND_FAILED', `Could not verify the uninstall of ${bundleId}`, {
114+
exitCode: after.exitCode,
115+
stdout: after.stdout,
116+
stderr: after.stderr,
117+
hint: 'Confirm the device connection, then check the bundle name through bm dump -a.',
118+
});
119+
}
94120
return { wasInstalled: true, packageName: bundleId };
95121
}
96122

0 commit comments

Comments
 (0)