Skip to content

Commit

Permalink
fix(manifest): split commits on exact package path prefix (#842)
Browse files Browse the repository at this point in the history
prior to this change commits touching both the following files would be
attributed to the "foo" package:
- foo/bar.ts
- some/sub/dir/foo/another.ts

also moved the '.' package path skipping into the constructor
  • Loading branch information
joeldodge79 committed Mar 30, 2021
1 parent 1014bb0 commit 2728bfe
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 6 deletions.
11 changes: 6 additions & 5 deletions src/commit-split.ts
Expand Up @@ -49,6 +49,11 @@ export class CommitSplit {
if (opts.packagePaths) {
const paths: string[] = [];
for (let newPath of opts.packagePaths) {
// The special "." path, representing the root of the module, should be
// ignored by commit-split as it is assigned all commits in manifest.ts
if (newPath === '.') {
continue;
}
// normalize so that all paths have leading and trailing slashes for
// non-overlap validation.
// NOTE: GitHub API always returns paths using the `/` separator,
Expand Down Expand Up @@ -91,11 +96,7 @@ export class CommitSplit {
let pkgName;
if (this.packagePaths) {
// only track paths under this.packagePaths
pkgName = this.packagePaths.find(p => {
// The special "." path, representing the root of the module
// should be ignored by commit-split:
return file.indexOf(p) >= 0 && p !== '.';
});
pkgName = this.packagePaths.find(p => file.indexOf(p) === 0);
} else {
// track paths by top level folder
pkgName = splitPath[0];
Expand Down
27 changes: 26 additions & 1 deletion test/commit-split.ts
Expand Up @@ -196,7 +196,7 @@ describe('CommitSplit', () => {
'foo/bar-baz',
],
});
expect(cs.packagePaths).to.be.eql([
expect(cs.packagePaths).to.eql([
'two-three',
'one-two',
'three',
Expand All @@ -207,6 +207,31 @@ describe('CommitSplit', () => {
]);
});

it('ignore the "." package', () => {
const foo = buildMockCommit('fix(foo): fix foo', ['foo/foo.ts']);
const topLevel = buildMockCommit('fix: fix top level', ['bar.ts']);
const topAndFoo = buildMockCommit('fix: foo and top level', [
'foo/foo.ts',
'bar.ts',
]);
const commits = [foo, topLevel, topAndFoo];
const cs = new CommitSplit({packagePaths: ['foo', '.']});
expect(cs.packagePaths).to.eql(['foo']);
const actualSplitCommits = cs.split(commits);
expect(actualSplitCommits).to.eql({foo: [foo, topAndFoo]});
});

it('package path exact start match', () => {
const foo = buildMockCommit('fix(foo): fix foo', ['foo/foo.ts']);
const notFoo = buildMockCommit('fix(not-foo): fix not foo', [
'not/foo/bar.ts',
]);
const commits = [foo, notFoo];
const cs = new CommitSplit({packagePaths: ['foo']});
const actualSplitCommits = cs.split(commits);
expect(actualSplitCommits).to.eql({foo: [foo]});
});

// Test invalid CommitSplitOptions.packagePaths combinations.
// Intentionally inconsistent trailing slashes to test path normalization.
const invalidPaths = [
Expand Down

0 comments on commit 2728bfe

Please sign in to comment.