Skip to content

Commit 23d29bf

Browse files
committed
Reset section state when parsing new file in diff
1 parent 2f73bd8 commit 23d29bf

File tree

2 files changed

+90
-0
lines changed

2 files changed

+90
-0
lines changed

src/check-dependency-bumps.test.ts

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2261,6 +2261,94 @@ diff --git a/packages/controller-utils/package.json b/packages/controller-utils/
22612261
expect(result).toStrictEqual({});
22622262
});
22632263

2264+
it('resets section state when switching to a different file', async () => {
2265+
const getStdoutSpy = jest.spyOn(miscUtilsModule, 'getStdoutFromCommand');
2266+
2267+
// Diff with two different files - section state should reset between them
2268+
const diffWithMultipleFiles = `
2269+
diff --git a/packages/package-a/package.json b/packages/package-a/package.json
2270+
index 1234567..890abcd 100644
2271+
--- a/packages/package-a/package.json
2272+
+++ b/packages/package-a/package.json
2273+
@@ -10,7 +10,7 @@
2274+
"dependencies": {
2275+
- "@metamask/transaction-controller": "^61.0.0"
2276+
+ "@metamask/transaction-controller": "^62.0.0"
2277+
}
2278+
}
2279+
diff --git a/packages/package-b/package.json b/packages/package-b/package.json
2280+
index abc1234..def5678 100644
2281+
--- a/packages/package-b/package.json
2282+
+++ b/packages/package-b/package.json
2283+
@@ -10,7 +10,7 @@
2284+
"peerDependencies": {
2285+
- "@metamask/network-controller": "^5.0.0"
2286+
+ "@metamask/network-controller": "^6.0.0"
2287+
}
2288+
}
2289+
`;
2290+
2291+
when(getStdoutSpy)
2292+
.calledWith(
2293+
'git',
2294+
['diff', '-U9999', 'abc123', 'HEAD', '--', '**/package.json'],
2295+
{ cwd: '/path/to/project' },
2296+
)
2297+
.mockResolvedValue(diffWithMultipleFiles);
2298+
2299+
when(jest.spyOn(packageManifestModule, 'readPackageManifest'))
2300+
.calledWith('/path/to/project/package.json')
2301+
.mockResolvedValue({
2302+
unvalidated: {
2303+
repository: 'https://github.com/example-org/example-repo',
2304+
},
2305+
validated: buildMockManifest(),
2306+
});
2307+
2308+
when(jest.spyOn(packageManifestModule, 'readPackageManifest'))
2309+
.calledWith('/path/to/project/packages/package-a/package.json')
2310+
.mockResolvedValue({
2311+
unvalidated: {},
2312+
validated: buildMockManifest({
2313+
name: '@metamask/package-a',
2314+
}),
2315+
});
2316+
2317+
when(jest.spyOn(packageManifestModule, 'readPackageManifest'))
2318+
.calledWith('/path/to/project/packages/package-b/package.json')
2319+
.mockResolvedValue({
2320+
unvalidated: {},
2321+
validated: buildMockManifest({
2322+
name: '@metamask/package-b',
2323+
}),
2324+
});
2325+
2326+
jest
2327+
.spyOn(projectModule, 'getValidRepositoryUrl')
2328+
.mockResolvedValue('https://github.com/example-org/example-repo');
2329+
2330+
jest
2331+
.spyOn(changelogValidatorModule, 'validateChangelogs')
2332+
.mockResolvedValue([]);
2333+
2334+
const result = await checkDependencyBumps({
2335+
fromRef: 'abc123',
2336+
projectRoot: '/path/to/project',
2337+
stdout,
2338+
stderr,
2339+
});
2340+
2341+
// Should detect changes in both files with correct section types
2342+
expect(result['package-a']).toBeDefined();
2343+
expect(result['package-a'].dependencyChanges).toHaveLength(1);
2344+
expect(result['package-a'].dependencyChanges[0].type).toBe('dependencies');
2345+
expect(result['package-b']).toBeDefined();
2346+
expect(result['package-b'].dependencyChanges).toHaveLength(1);
2347+
expect(result['package-b'].dependencyChanges[0].type).toBe(
2348+
'peerDependencies',
2349+
);
2350+
});
2351+
22642352
it('detects package version changes for release detection', async () => {
22652353
const getStdoutSpy = jest.spyOn(miscUtilsModule, 'getStdoutFromCommand');
22662354

src/check-dependency-bumps.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ async function parseDiff(
8282
const match = line.match(/b\/(.+)/u);
8383

8484
if (match) {
85+
// Reset section state when starting a new file (diff --git always starts a new file)
86+
currentSection = null;
8587
currentFile = match[1];
8688
}
8789
}

0 commit comments

Comments
 (0)