Hacktoberfest 2026: le issue che i maintainer hanno segnato per ottobre, aperte e adatte ai principianti. Sfoglia le issue Hacktoberfest

[Duplicate Code] Extract repeated CloudHypervisorManager setup in manager-cleanup tests

Aperta
#9,274 0 commenti 1 reazione 2 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

@lpcox ci sta già lavorando.

Dal 30/9/2026.

  • #9276 di @copilot-swe-agent — aperta

Valutazione

Questa issue non è ancora stata valutata.

Descrizione

code-quality refactoring

Duplicate Code Opportunity

Summary
  • Pattern: Two CloudHypervisorManager tests repeat the same process mock, dependency wiring, manager construction, and start() bootstrap before exercising different stop() behavior.
  • Locations: src/cloud-hypervisor/manager-cleanup.test.ts lines 167-206 and 209-230.
  • Impact: Removes about 22 duplicated lines and keeps the lifecycle setup consistent across stop-path assertions.
Evidence

Block A (src/cloud-hypervisor/manager-cleanup.test.ts:167-206)

it('invokes a beforeCleanup hook after process termination but before run-directory removal', async () => {
  // Regression test: Cloud Hypervisor does not flush buffered guest
  // serial console output until its process actually exits, so
  // diagnostics collection must happen after process termination is
  // confirmed but before stop() removes the run directory those
  // diagnostic files live in. Discovered via live-KVM validation: a
  // guest boot failure produced a completely empty serial console log
  // when diagnostics were collected any earlier (e.g. before
  // vmm.shutdown()/process termination).
  const child = processMock();
  const deps = dependencies({
    launch: jest.fn().mockReturnValue(child),
  });
  const manager = new CloudHypervisorManager(
    config(),
    '/tmp/awf',
    deps,
    'keep',
    networkConfig(),
    guestConfig(),
  );
  await manager.start();

  const beforeCleanup = jest.fn(async () => {});

  await manager.stop({ beforeCleanup });

  expect(beforeCleanup).toHaveBeenCalledTimes(1);
  expect(deps.rm).toHaveBeenCalledWith(
    expect.stringContaining('/run/awf-cloud-hypervisor/'),
    { recursive: true, force: true },
  );
  // beforeCleanup must run strictly before the run-directory removal
  // call (deps.rm), i.e. after process termination is confirmed but
  // before diagnostic files are deleted.
  const runRmIndex = (deps.rm as jest.Mock).mock.calls.findIndex(
    ([target]) => String(target).startsWith('/run/awf-cloud-hypervisor/'),
  );
  const rmCallOrder = (deps.rm as jest.Mock).mock.invocationCallOrder[runRmIndex];
  expect(beforeCleanup.mock.invocationCallOrder[0]).toBeLessThan(rmCallOrder);
});

Block B (src/cloud-hypervisor/manager-cleanup.test.ts:209-230)

it('propagates a beforeCleanup hook failure alongside other stop() errors', async () => {
  const child = processMock();
  const deps = dependencies({
    launch: jest.fn().mockReturnValue(child),
  });
  const manager = new CloudHypervisorManager(
    config(),
    '/tmp/awf',
    deps,
    'keep',
    networkConfig(),
    guestConfig(),
  );
  await manager.start();

  await expect(
    manager.stop({
      beforeCleanup: async () => {
        throw new Error('diagnostics write failed');
      },
    }),
  ).rejects.toThrow(/diagnostics write failed/);
});
Suggested Refactoring
  • Extract a createStartedManager() test helper that returns { manager, deps, child }.
  • Keep the assertion-specific code in each test and reuse the shared lifecycle bootstrap.
  • If the helper stays local to the file, it will reduce noise without changing test intent.
Affected Files
  • src/cloud-hypervisor/manager-cleanup.test.ts — lines 167-206
  • src/cloud-hypervisor/manager-cleanup.test.ts — lines 209-230
Effort Estimate

Low


Detected by Duplicate Code Detector workflow. Run date: 2026-09-30

Generated by Duplicate Code Detector · copilot · gpt50mini · 8.6 AIC · ⊞ 21.2K · ◷

  • expires on Oct 30, 2026, 9:57 PM UTC
Lingua principale
TypeScript
Stelle
147
Fork
71
Merge medio
5h 38m
PR unite (30g)
285

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di github/gh-aw-firewall

Tutte le issue di github/gh-aw-firewall

Issue simili

Altre issue su TypeScript

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.