workspace-tools
Version:
A collection of tools that are useful in a git-controlled monorepo that is managed by one of these software:
126 lines (125 loc) • 6.27 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const path_1 = __importDefault(require("path"));
const fs_1 = __importDefault(require("fs"));
const setupFixture_1 = require("../helpers/setupFixture");
const git_1 = require("../git");
const getChangedPackages_1 = require("../workspaces/getChangedPackages");
describe("getChangedPackages()", () => {
afterAll(() => {
setupFixture_1.cleanupFixtures();
});
it("can detect changes inside an untracked file", () => {
// arrange
const root = setupFixture_1.setupFixture("monorepo");
const newFile = path_1.default.join(root, "packages/package-a/footest.txt");
fs_1.default.writeFileSync(newFile, "hello foo test");
// act
const changedPkgs = getChangedPackages_1.getChangedPackages(root, "main");
// assert
expect(changedPkgs).toContain("package-a");
});
it("can detect changes inside an untracked file in a nested monorepo", () => {
// arrange
const root = path_1.default.join(setupFixture_1.setupFixture("monorepo-nested"), "monorepo");
const newFile = path_1.default.join(root, "packages/package-a/footest.txt");
fs_1.default.writeFileSync(newFile, "hello foo test");
// act
const changedPkgs = getChangedPackages_1.getChangedPackages(root, "main");
// assert
expect(changedPkgs).toEqual(["package-a"]);
});
it("can detect changes inside an unstaged file", () => {
// arrange
const root = setupFixture_1.setupFixture("monorepo");
const newFile = path_1.default.join(root, "packages/package-a/src/index.ts");
fs_1.default.writeFileSync(newFile, "hello foo test");
// act
const changedPkgs = getChangedPackages_1.getChangedPackages(root, "main");
// assert
expect(changedPkgs).toContain("package-a");
});
it("can detect changes inside an unstaged file in a nested monorepo", () => {
// arrange
const root = path_1.default.join(setupFixture_1.setupFixture("monorepo-nested"), "monorepo");
const newFile = path_1.default.join(root, "packages/package-a/src/index.ts");
fs_1.default.writeFileSync(newFile, "hello foo test");
// act
const changedPkgs = getChangedPackages_1.getChangedPackages(root, "main");
// assert
expect(changedPkgs).toEqual(["package-a"]);
});
it("can detect changes inside a staged file", () => {
// arrange
const root = setupFixture_1.setupFixture("monorepo");
const newFile = path_1.default.join(root, "packages/package-a/footest.txt");
fs_1.default.writeFileSync(newFile, "hello foo test");
git_1.git(["add", newFile], { cwd: root });
// act
const changedPkgs = getChangedPackages_1.getChangedPackages(root, "main");
// assert
expect(changedPkgs).toContain("package-a");
});
it("can detect changes inside a staged file in a nested monorepo", () => {
// arrange
const root = path_1.default.join(setupFixture_1.setupFixture("monorepo-nested"), "monorepo");
const newFile = path_1.default.join(root, "packages/package-a/footest.txt");
fs_1.default.writeFileSync(newFile, "hello foo test");
git_1.git(["add", newFile], { cwd: root });
// act
const changedPkgs = getChangedPackages_1.getChangedPackages(root, "main");
// assert
expect(changedPkgs).toEqual(["package-a"]);
});
it("can detect changes inside a file that has been committed in a different branch", () => {
// arrange
const root = setupFixture_1.setupFixture("monorepo");
const newFile = path_1.default.join(root, "packages/package-a/footest.txt");
fs_1.default.writeFileSync(newFile, "hello foo test");
git_1.git(["checkout", "-b", "newbranch"], { cwd: root });
git_1.stageAndCommit(["add", newFile], "test commit", root);
// act
const changedPkgs = getChangedPackages_1.getChangedPackages(root, "main");
// assert
expect(changedPkgs).toContain("package-a");
});
it("can detect changes inside a file that has been committed in a different branch in a nested monorepo", () => {
// arrange
const root = path_1.default.join(setupFixture_1.setupFixture("monorepo-nested"), "monorepo");
const newFile = path_1.default.join(root, "packages/package-a/footest.txt");
fs_1.default.writeFileSync(newFile, "hello foo test");
git_1.git(["checkout", "-b", "newbranch"], { cwd: root });
git_1.stageAndCommit(["add", newFile], "test commit", root);
// act
const changedPkgs = getChangedPackages_1.getChangedPackages(root, "main");
// assert
expect(changedPkgs).toEqual(["package-a"]);
});
it("can detect changes inside a file that has been committed in a different branch using default remote", () => {
// arrange
const root = setupFixture_1.setupFixture("monorepo");
setupFixture_1.setupLocalRemote(root, "origin", "basic");
const newFile = path_1.default.join(root, "packages/package-a/footest.txt");
fs_1.default.writeFileSync(newFile, "hello foo test");
git_1.git(["checkout", "-b", "newbranch"], { cwd: root });
git_1.stageAndCommit(["add", newFile], "test commit", root);
// act
const changedPkgs = getChangedPackages_1.getChangedPackages(root, undefined);
// assert
expect(changedPkgs).toContain("package-a");
});
it("can ignore glob patterns in detecting changes", () => {
// arrange
const root = setupFixture_1.setupFixture("monorepo");
const newFile = path_1.default.join(root, "packages/package-a/footest.txt");
fs_1.default.writeFileSync(newFile, "hello foo test");
git_1.git(["add", newFile], { cwd: root });
// act
const changedPkgs = getChangedPackages_1.getChangedPackages(root, "main", ["packages/package-a/*"]);
// assert
expect(changedPkgs).toEqual([]);
});
});