Skip to content

Commit

Permalink
Merge pull request #24 from embroider-build/fix-similar-package-names…
Browse files Browse the repository at this point in the history
…-mismatch

Fix issue with similarly named packages matching the wrong package in the generated changelog.
  • Loading branch information
mansona authored May 22, 2024
2 parents 7e13349 + 33b9046 commit 71bd227
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
25 changes: 25 additions & 0 deletions src/changelog.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,31 @@ describe("Changelog", () => {
}
});

describe("packageFromPath with similarly named packages", () => {
const MockedChangelog = require("./changelog").default;

const TESTS = [
["", ""],
["/ember-fastboot/package.json", "ember-fastboot"],
["/ember-fastboot-2-fast-2-furious/package.json", "ember-fastboot-2-fast-2-furious"],
["/ember-fastboot-tokyo-drift/package.json", "ember-fastboot-tokyo-drift"],
];

for (let [input, expected] of TESTS) {
it(`${input} -> ${expected}`, () => {
const changelog = new MockedChangelog({
rootPath: "/",
packages: [
{ name: "ember-fastboot", path: "/ember-fastboot" },
{ name: "ember-fastboot-2-fast-2-furious", path: "/ember-fastboot-2-fast-2-furious" },
{ name: "ember-fastboot-tokyo-drift", path: "/ember-fastboot-tokyo-drift" },
],
});
expect(changelog.packageFromPath(input)).toEqual(expected);
});
}
});

describe("getCommitInfos", () => {
beforeEach(() => {
require("./fetch").__resetMockResponses();
Expand Down
15 changes: 12 additions & 3 deletions src/changelog.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const pMap = require("p-map");
const { resolve } = require("path");
const { resolve, sep } = require("path");

import progressBar from "./progress-bar";
import { Configuration } from "./configuration";
Expand Down Expand Up @@ -73,7 +73,9 @@ export default class Changelog {
}

private async getListOfUniquePackages(sha: string): Promise<string[]> {
return (await Git.changedPaths(sha))
let changedPaths = await Git.changedPaths(sha);

return changedPaths
.map(path => this.packageFromPath(path))
.filter(Boolean)
.filter(onlyUnique);
Expand All @@ -84,7 +86,14 @@ export default class Changelog {
// use the discovered packages
const absolutePath = resolve(this.config.rootPath, path);

const foundPackage = this.config.packages.find(p => absolutePath.startsWith(p.path));
// Sometimes multiple packages may exist with the same prefix:
// ember-fastboot
// ember-fastboot-2-fast-2-furious
const foundPackage = this.config.packages.find(p => {
let withSlash = p.path.endsWith(sep) ? p.path : `${p.path}${sep}`;

return absolutePath.startsWith(withSlash);
});

if (foundPackage) {
return foundPackage.name;
Expand Down

0 comments on commit 71bd227

Please sign in to comment.