From 9deb7944f98092c48551e9ca53d19e4f07c816a6 Mon Sep 17 00:00:00 2001 From: Sam Lanning Date: Sat, 10 May 2025 00:13:57 +0100 Subject: [PATCH 1/2] Clean up arguments for multiple functions - Use `cwd` consistently across functions - Remove unnecessary extra directory args for `commitChangesFromRepo` - Introduce `recursivelyFindRoot` argument --- .changeset/blue-carpets-jam.md | 21 ++ README.md | 35 +- package.json | 1 - pnpm-lock.yaml | 32 -- src/fs.ts | 4 +- src/git.ts | 26 +- src/interface.ts | 31 +- src/test/integration/fs.test.ts | 2 +- src/test/integration/git.test.ts | 568 +++++++++++-------------------- 9 files changed, 259 insertions(+), 461 deletions(-) create mode 100644 .changeset/blue-carpets-jam.md diff --git a/.changeset/blue-carpets-jam.md b/.changeset/blue-carpets-jam.md new file mode 100644 index 0000000..b9fea7a --- /dev/null +++ b/.changeset/blue-carpets-jam.md @@ -0,0 +1,21 @@ +--- +"@changesets/ghcommit": major +--- + +Refactor & clean up options for multiple functions + +- For `commitFilesFromDirectory`: + - Rename `workingDirectory` to `cwd` for consistency across repos, + and utils like `exec` + - Make `cwd` a required argument +- For `commitChangesFromRepo`: + - Merge `repoDirectory` and `addFromDirectory` into a single required argument + `cwd`. This folder will now both be used to filter which files are added, + and to find the root of the repository. + - Introduce `recursivelyFindRoot` option (default: `true`), + to optionally search for the root of the repository, + by checking for existence of `.git` directory in parent directories, + starting from `cwd`. + +This effectively removes all usage of process.cwd() within the package, +instead requiring all usage to be very explicit with specifying paths. diff --git a/README.md b/README.md index 1be7cfe..51dc287 100644 --- a/README.md +++ b/README.md @@ -82,6 +82,13 @@ In addition to `CommitFilesBasedArgs`, this function has the following arguments ```ts { + /** + * The directory used to find the repository root, + * and search for changed files to commit. + * + * Any files that have been changed outside of this directory will be ignored. + */ + cwd: string; /** * The base commit to build your changes on-top of * @@ -91,21 +98,13 @@ In addition to `CommitFilesBasedArgs`, this function has the following arguments commit: string; }; /** - * The root of the repository. - * - * When unspecified, the root of the repository will be found by recursively - * searching for the `.git` directory from the current working directory. - */ - repoDirectory?: string; - /** - * The starting directory to recurse from when detecting changed files. - * - * Useful for monorepos where you want to add files from a specific directory only. + * Don't require {@link cwd} to be the root of the repository, + * and use it as a starting point to recursively search for the `.git` + * directory in parent directories. * - * Defaults to resolved value of {@link repoDirectory}, - * which will add all changed files in the repository. + * @default true */ - addFromDirectory?: string; + recursivelyFindRoot?: boolean; /** * An optional function that can be used to filter which files are included * in the commit. True should be returned for files that should be included. @@ -131,6 +130,7 @@ await commitChangesFromRepo({ ...context.repo, branch: "new-branch-to-create", message: "[chore] do something", + cwd: process.cwd(), }); // Commit & push the files from a specific directory @@ -141,7 +141,7 @@ await commitChangesFromRepo({ repository: "my-repo", branch: "another-new-branch-to-create", message: "[chore] do something else\n\nsome more details", - repoDirectory: "/tmp/some-repo", + cwd: "/tmp/some-repo", }); // Commit & push the files from the current directory, @@ -154,6 +154,7 @@ await commitChangesFromRepo({ headline: "[chore] do something else", body: "some more details", }, + cwd: process.cwd(), base: { // This will be the original sha from the workflow run, // even if we've made commits locally @@ -178,7 +179,7 @@ In addition to `CommitFilesBasedArgs`, this function has the following arguments * The directory to consider the root of the repository when calculating * file paths */ - workingDirectory?: string; + cwd: string; /** * The file paths, relative to {@link workingDirectory}, * to add or delete from the branch on GitHub. @@ -210,7 +211,7 @@ await commitFilesFromDirectory({ base: { branch: "main", }, - workingDirectory: "foo/bar", + cwd: "foo/bar", fileChanges: { additions: ["package-lock.json", "package.json"], }, @@ -226,7 +227,7 @@ await commitFilesFromDirectory({ base: { tag: "v1.0.0", }, - workingDirectory: "some-dir", + cwd: "some-dir", fileChanges: { additions: ["index.html"], }, diff --git a/package.json b/package.json index 8403b3e..9d252a3 100644 --- a/package.json +++ b/package.json @@ -74,7 +74,6 @@ "eslint-plugin-jest": "^28.6.0", "eslint-plugin-only-warn": "^1.1.0", "jest": "^29.7.0", - "mock-cwd": "^1.0.0", "pino": "^9.3.2", "pino-pretty": "^11.2.2", "prettier": "^3.3.3", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 32ba5db..647a8bc 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -75,9 +75,6 @@ importers: jest: specifier: ^29.7.0 version: 29.7.0(@types/node@20.14.10)(ts-node@10.9.2(@types/node@20.14.10)(typescript@5.5.3)) - mock-cwd: - specifier: ^1.0.0 - version: 1.0.0 pino: specifier: ^9.3.2 version: 9.3.2 @@ -2841,9 +2838,6 @@ packages: resolution: {integrity: sha512-qOOzS1cBTWYF4BH8fVePDBOO9iptMnGUEZwNc/cMWnTV2nVLZ7VoNWEPHkYczZA0pdoA7dl6e7FL659nX9S2aw==} engines: {node: '>=16 || 14 >=14.17'} - mock-cwd@1.0.0: - resolution: {integrity: sha512-g+hi96nCWcS4HC5lA6VtL9SHmPywPkXSX6S4DTctmGoi6H8uYVa7lK/h22s8rMQie8b+b1+ywnPC61iXF6mriQ==} - mri@1.2.0: resolution: {integrity: sha512-tzzskb3bG8LvYGFF/mDTpq3jpI6Q9wc3LEmBaghu+DdCssd1FakN7Bc0hVNmEyGq1bq3RgfkCb3cmQLpNPOroA==} engines: {node: '>=4'} @@ -3136,13 +3130,6 @@ packages: quick-format-unescaped@4.0.4: resolution: {integrity: sha512-tYC1Q1hgyRuHgloV/YXs2w15unPVh8qfu/qCTfhTYamaw7fyhumKa2yGpdSo87vY32rIclj+4fWYQXUMs9EHvg==} - randombytes@2.1.0: - resolution: {integrity: sha512-vYl3iOX+4CKUWuxGi9Ukhie6fsqXqS9FE2Zaic4tNFD2N2QQaXOMFbuKK4QmDHC0JO6B1Zp41J0LpT0oR68amQ==} - - randomstring@1.3.1: - resolution: {integrity: sha512-lgXZa80MUkjWdE7g2+PZ1xDLzc7/RokXVEQOv5NN2UOTChW1I8A9gha5a9xYBOqgaSoI6uJikDmCU8PyRdArRQ==} - hasBin: true - react-is@18.3.1: resolution: {integrity: sha512-/LLMVyas0ljjAtoYiPqYiL8VWXzUUdThrmU5+n20DZv+a+ClRoevUzw5JxU+Ieh5/c87ytoTBV9G1FiKfNJdmg==} @@ -3428,10 +3415,6 @@ packages: swap-case@2.0.2: resolution: {integrity: sha512-kc6S2YS/2yXbtkSMunBtKdah4VFETZ8Oh6ONSmSd9bRxhqTrtARUCBUiWXH3xVPpvR7tz2CSnkuXVE42EcGnMw==} - temp-dir@2.0.0: - resolution: {integrity: sha512-aoBAniQmmwtcKp/7BzsH8Cxzv8OL736p7v1ihGb5e9DJ9kTwGWHrQrVB5+lfVDzfGrdRzXch+ig7LHaY1JTOrg==} - engines: {node: '>=8'} - term-size@2.2.1: resolution: {integrity: sha512-wK0Ri4fOGjv/XPy8SBHZChl8CM7uMc5VML7SqiQ0zG7+J5Vr+RMQDoHa2CNT6KHUnTGIXH34UDMkPzAUyapBZg==} engines: {node: '>=8'} @@ -7312,11 +7295,6 @@ snapshots: minipass@7.1.2: {} - mock-cwd@1.0.0: - dependencies: - randomstring: 1.3.1 - temp-dir: 2.0.0 - mri@1.2.0: {} ms@2.1.2: {} @@ -7594,14 +7572,6 @@ snapshots: quick-format-unescaped@4.0.4: {} - randombytes@2.1.0: - dependencies: - safe-buffer: 5.2.1 - - randomstring@1.3.1: - dependencies: - randombytes: 2.1.0 - react-is@18.3.1: {} read-yaml-file@1.1.0: @@ -7891,8 +7861,6 @@ snapshots: dependencies: tslib: 2.6.3 - temp-dir@2.0.0: {} - term-size@2.2.1: {} test-exclude@6.0.0: diff --git a/src/fs.ts b/src/fs.ts index a622a35..d2d633e 100644 --- a/src/fs.ts +++ b/src/fs.ts @@ -8,7 +8,7 @@ import { } from "./interface.js"; export const commitFilesFromDirectory = async ({ - workingDirectory = process.cwd(), + cwd, fileChanges, ...otherArgs }: CommitFilesFromDirectoryArgs): Promise => { @@ -16,7 +16,7 @@ export const commitFilesFromDirectory = async ({ (fileChanges.additions || []).map(async (p) => { return { path: p, - contents: await fs.readFile(path.join(workingDirectory, p)), + contents: await fs.readFile(path.join(cwd, p)), }; }), ); diff --git a/src/git.ts b/src/git.ts index e81daa8..a6e2a80 100644 --- a/src/git.ts +++ b/src/git.ts @@ -6,7 +6,7 @@ import { CommitFilesFromBuffersArgs, CommitFilesResult, } from "./interface"; -import { isAbsolute, relative } from "path"; +import { relative, resolve } from "path"; /** * @see https://isomorphic-git.org/docs/en/walk#walkerentry-mode @@ -20,18 +20,20 @@ const FILE_MODES = { export const commitChangesFromRepo = async ({ base, - repoDirectory, - addFromDirectory, + cwd: workingDirectory, + recursivelyFindRoot = true, filterFiles, log, ...otherArgs }: CommitChangesFromRepoArgs): Promise => { const ref = base?.commit ?? "HEAD"; - const resolvedRepoDirectory = - repoDirectory ?? (await git.findRoot({ fs, filepath: process.cwd() })); + const cwd = resolve(workingDirectory); + const repoRoot = recursivelyFindRoot + ? await git.findRoot({ fs, filepath: cwd }) + : cwd; const gitLog = await git.log({ fs, - dir: resolvedRepoDirectory, + dir: repoRoot, ref, depth: 1, }); @@ -42,18 +44,12 @@ export const commitChangesFromRepo = async ({ throw new Error(`Could not determine oid for ${ref}`); } - if (addFromDirectory && !isAbsolute(addFromDirectory)) { - throw new Error( - `addFromDirectory must be an absolute path, got ${addFromDirectory}`, - ); - } - /** * The directory to add files from. This is relative to the repository * root, and is used to filter files. */ const relativeStartDirectory = - addFromDirectory && relative(resolvedRepoDirectory, addFromDirectory) + "/"; + cwd === repoRoot ? null : relative(repoRoot, cwd) + "/"; // Determine changed files const trees = [git.TREE({ ref: oid }), git.WORKDIR()]; @@ -65,14 +61,14 @@ export const commitChangesFromRepo = async ({ }; await git.walk({ fs, - dir: resolvedRepoDirectory, + dir: repoRoot, trees, map: async (filepath, [commit, workdir]) => { // Don't include ignored files if ( await git.isIgnored({ fs, - dir: resolvedRepoDirectory, + dir: repoRoot, filepath, }) ) { diff --git a/src/interface.ts b/src/interface.ts index 282e219..62e1077 100644 --- a/src/interface.ts +++ b/src/interface.ts @@ -72,13 +72,13 @@ export interface CommitFilesFromDirectoryArgs * The directory to consider the root of the repository when calculating * file paths */ - workingDirectory?: string; + cwd: string; /** - * The file paths, relative to {@link workingDirectory}, + * The file paths, relative to {@link cwd}, * to add or delete from the branch on GitHub. */ fileChanges: { - /** File paths, relative to {@link workingDirectory}, to remove from the repo. */ + /** File paths, relative to {@link cwd}, to remove from the repo. */ additions?: string[]; /** File paths, relative to the repository root, to remove from the repo. */ deletions?: string[]; @@ -86,6 +86,13 @@ export interface CommitFilesFromDirectoryArgs } export interface CommitChangesFromRepoArgs extends CommitFilesBasedArgs { + /** + * The directory used to find the repository root, + * and search for changed files to commit. + * + * Any files that have been changed outside of this directory will be ignored. + */ + cwd: string; /** * The base commit to build your changes on-top of. * @@ -105,21 +112,13 @@ export interface CommitChangesFromRepoArgs extends CommitFilesBasedArgs { commit: string; }; /** - * The root of the repository. - * - * When unspecified, the root of the repository will be found by recursively - * searching for the `.git` directory from the current working directory. - */ - repoDirectory?: string; - /** - * The starting directory to recurse from when detecting changed files. - * - * Useful for monorepos where you want to add files from a specific directory only. + * Don't require {@link cwd} to be the root of the repository, + * and use it as a starting point to recursively search for the `.git` + * directory in parent directories. * - * Defaults to resolved value of {@link repoDirectory}, - * which will add all changed files in the repository. + * @default true */ - addFromDirectory?: string; + recursivelyFindRoot?: boolean; /** * An optional function that can be used to filter which files are included * in the commit. True should be returned for files that should be included. diff --git a/src/test/integration/fs.test.ts b/src/test/integration/fs.test.ts index a006287..dbfbca3 100644 --- a/src/test/integration/fs.test.ts +++ b/src/test/integration/fs.test.ts @@ -37,7 +37,7 @@ describe("fs", () => { headline: "Test commit", body: "This is a test commit", }, - workingDirectory: tmpDir, + cwd: tmpDir, fileChanges: { additions: ["foo.txt"], }, diff --git a/src/test/integration/git.test.ts b/src/test/integration/git.test.ts index 7e0619d..2e46c60 100644 --- a/src/test/integration/git.test.ts +++ b/src/test/integration/git.test.ts @@ -13,7 +13,6 @@ import { commitChangesFromRepo } from "../../git"; import { getRefTreeQuery } from "../../github/graphql/queries"; import { deleteBranches, waitForGitHubToBeReady } from "./util"; import git from "isomorphic-git"; -import { mockCwd } from "mock-cwd"; const octokit = getOctokit(ENV.GITHUB_TOKEN); @@ -102,7 +101,7 @@ const makeFileChanges = async ( path.join(repoDirectory, "new-file.txt"), "This is a new file", ); - // Add a new file nested in a directory + // Add new files nested in a directory await fs.promises.mkdir(path.join(repoDirectory, "nested"), { recursive: true, }); @@ -110,6 +109,10 @@ const makeFileChanges = async ( path.join(repoDirectory, "nested", "nested-file.txt"), "This is a nested file", ); + await fs.promises.writeFile( + path.join(repoDirectory, "nested", "nested-file-2.md"), + "This is a nested file", + ); // Add files that should be ignored await fs.promises.writeFile( path.join(repoDirectory, ".env"), @@ -184,6 +187,11 @@ const makeFileChangeAssertions = async (branch: string) => { path: "nested/nested-file.txt", oid: "60eb5af9a0c03dc16dc6d0bd9a370c1aa4e095a3", }); + await expectBranchHasFile({ + branch, + path: "nested/nested-file-2.md", + oid: "60eb5af9a0c03dc16dc6d0bd9a370c1aa4e095a3", + }); // Expect ignored files to not exist await expectBranchHasFile({ branch, path: ".env", oid: null }); await expectBranchHasFile({ @@ -193,7 +201,10 @@ const makeFileChangeAssertions = async (branch: string) => { }); }; -const makeSubdirectoryFileChangeAssertions = async (branch: string) => { +const makeSubdirectoryFileChangeAssertions = async ( + branch: string, + includeNonTxt: boolean, +) => { // Expect new file outside of subdir to not exist await expectBranchHasFile({ branch, @@ -206,6 +217,11 @@ const makeSubdirectoryFileChangeAssertions = async (branch: string) => { path: "nested/nested-file.txt", oid: "60eb5af9a0c03dc16dc6d0bd9a370c1aa4e095a3", }); + await expectBranchHasFile({ + branch, + path: "nested/nested-file-2.md", + oid: includeNonTxt ? "60eb5af9a0c03dc16dc6d0bd9a370c1aa4e095a3" : null, + }); // Expect ignored files to not exist await expectBranchHasFile({ branch, path: ".env", oid: null }); await expectBranchHasFile({ @@ -285,7 +301,7 @@ describe("git", () => { headline: "Test commit", body: "This is a test commit", }, - repoDirectory, + cwd: repoDirectory, log, }); @@ -346,7 +362,7 @@ describe("git", () => { headline: "Test commit", body: "This is a test commit", }, - repoDirectory, + cwd: repoDirectory, log, }), ).rejects.toThrow( @@ -391,7 +407,7 @@ describe("git", () => { headline: "Test commit", body: "This is a test commit", }, - repoDirectory, + cwd: repoDirectory, log, }), ).rejects.toThrow( @@ -437,7 +453,7 @@ describe("git", () => { headline: "Test commit", body: "This is a test commit", }, - repoDirectory, + cwd: repoDirectory, log, }), ).rejects.toThrow( @@ -491,7 +507,7 @@ describe("git", () => { headline: "Test commit", body: "This is a test commit", }, - repoDirectory, + cwd: repoDirectory, log, base: { commit: oid, @@ -505,405 +521,203 @@ describe("git", () => { await expectParentHasOid({ branch, oid }); }); - describe("when running directly in repository directory", () => { - describe("repoDirectory: unspecified", () => { - it(`should correctly commit all changes`, async () => { - const branch = `${TEST_BRANCH_PREFIX}-root-repodirectory-unspecified`; - branches.push(branch); - - await fs.promises.mkdir(testDir, { recursive: true }); - const repoDirectory = path.join( - testDir, - `repo-root-repodirectory-unspecified`, - ); - - // Clone the git repo locally using the git cli and child-process - await new Promise((resolve, reject) => { - const p = execFile( - "git", - ["clone", process.cwd(), `repo-root-repodirectory-unspecified`], - { cwd: testDir }, - (error) => { - if (error) { - reject(error); - } else { - resolve(); - } - }, - ); - p.stdout?.pipe(process.stdout); - p.stderr?.pipe(process.stderr); - }); - - await makeFileChanges(repoDirectory, "standard"); - - // Push the changes - await mockCwd(repoDirectory, () => - commitChangesFromRepo({ - octokit, - ...REPO, - branch, - message: { - headline: "Test commit", - body: "This is a test commit", - }, - log, - }), - ); - - await waitForGitHubToBeReady(); - - await makeFileChangeAssertions(branch); - - // Expect the OID to be the HEAD commit - const oid = - ( - await git.log({ - fs, - dir: repoDirectory, - ref: "HEAD", - depth: 1, - }) - )[0]?.oid ?? "NO_OID"; - - await expectParentHasOid({ branch, oid }); - }); - - it(`addFromDirectory should correctly filter files`, async () => { - const branch = `${TEST_BRANCH_PREFIX}-root-repodirectory-unspecified-add`; - branches.push(branch); - - await fs.promises.mkdir(testDir, { recursive: true }); - const repoDirectory = path.join( - testDir, - `repo-root-repodirectory-unspecified-add`, - ); - - // Clone the git repo locally using the git cli and child-process - await new Promise((resolve, reject) => { - const p = execFile( - "git", - [ - "clone", - process.cwd(), - `repo-root-repodirectory-unspecified-add`, - ], - { cwd: testDir }, - (error) => { - if (error) { - reject(error); - } else { - resolve(); - } - }, - ); - p.stdout?.pipe(process.stdout); - p.stderr?.pipe(process.stderr); - }); - - await makeFileChanges(repoDirectory, "standard"); - - // Push the changes - await mockCwd(repoDirectory, () => - commitChangesFromRepo({ - octokit, - ...REPO, - branch, - message: { - headline: "Test commit", - body: "This is a test commit", - }, - addFromDirectory: path.join(repoDirectory, "nested"), - log, - }), - ); - - await waitForGitHubToBeReady(); - - await makeSubdirectoryFileChangeAssertions(branch); - - // Expect the OID to be the HEAD commit - const oid = - ( - await git.log({ - fs, - dir: repoDirectory, - ref: "HEAD", - depth: 1, - }) - )[0]?.oid ?? "NO_OID"; - - await expectParentHasOid({ branch, oid }); - }); + it(`filterFiles should correctly filter files`, async () => { + const branch = `${TEST_BRANCH_PREFIX}-root-repodirectory-unspecified-filter`; + branches.push(branch); - it(`filterFiles should correctly filter files`, async () => { - const branch = `${TEST_BRANCH_PREFIX}-root-repodirectory-unspecified-filter`; - branches.push(branch); + await fs.promises.mkdir(testDir, { recursive: true }); + const repoDirectory = path.join( + testDir, + `repo-root-repodirectory-unspecified-filter`, + ); - await fs.promises.mkdir(testDir, { recursive: true }); - const repoDirectory = path.join( - testDir, + // Clone the git repo locally using the git cli and child-process + await new Promise((resolve, reject) => { + const p = execFile( + "git", + [ + "clone", + process.cwd(), `repo-root-repodirectory-unspecified-filter`, - ); + ], + { cwd: testDir }, + (error) => { + if (error) { + reject(error); + } else { + resolve(); + } + }, + ); + p.stdout?.pipe(process.stdout); + p.stderr?.pipe(process.stderr); + }); - // Clone the git repo locally using the git cli and child-process - await new Promise((resolve, reject) => { - const p = execFile( - "git", - [ - "clone", - process.cwd(), - `repo-root-repodirectory-unspecified-filter`, - ], - { cwd: testDir }, - (error) => { - if (error) { - reject(error); - } else { - resolve(); - } - }, - ); - p.stdout?.pipe(process.stdout); - p.stderr?.pipe(process.stderr); - }); - - await makeFileChanges(repoDirectory, "standard"); - - // Push the changes - await mockCwd(repoDirectory, () => - commitChangesFromRepo({ - octokit, - ...REPO, - branch, - message: { - headline: "Test commit", - body: "This is a test commit", - }, - // Only include top-level files - filterFiles: (file) => !file.includes("/"), - log, - }), - ); + await makeFileChanges(repoDirectory, "standard"); + + // Push the changes + await commitChangesFromRepo({ + octokit, + ...REPO, + branch, + message: { + headline: "Test commit", + body: "This is a test commit", + }, + cwd: repoDirectory, + // Only include top-level files + filterFiles: (file) => !file.includes("/"), + log, + }); - await waitForGitHubToBeReady(); + await waitForGitHubToBeReady(); - await makeFilteredFileChangeAssertions(branch); + await makeFilteredFileChangeAssertions(branch); - // Expect the OID to be the HEAD commit - const oid = - ( - await git.log({ - fs, - dir: repoDirectory, - ref: "HEAD", - depth: 1, - }) - )[0]?.oid ?? "NO_OID"; + // Expect the OID to be the HEAD commit + const oid = + ( + await git.log({ + fs, + dir: repoDirectory, + ref: "HEAD", + depth: 1, + }) + )[0]?.oid ?? "NO_OID"; - await expectParentHasOid({ branch, oid }); - }); - }); + await expectParentHasOid({ branch, oid }); }); describe("when running in repository sub-directory", () => { - describe("repoDirectory: unspecified", () => { - it(`should correctly commit all changes`, async () => { - const branch = `${TEST_BRANCH_PREFIX}-subdir-repodirectory-unspecified`; - branches.push(branch); - - await fs.promises.mkdir(testDir, { recursive: true }); - const repoDirectory = path.join( - testDir, - `repo-subdir-repodirectory-unspecified`, - ); - - // Clone the git repo locally using the git cli and child-process - await new Promise((resolve, reject) => { - const p = execFile( - "git", - ["clone", process.cwd(), `repo-subdir-repodirectory-unspecified`], - { cwd: testDir }, - (error) => { - if (error) { - reject(error); - } else { - resolve(); - } - }, - ); - p.stdout?.pipe(process.stdout); - p.stderr?.pipe(process.stderr); - }); - - await makeFileChanges(repoDirectory, "standard"); - - // Push the changes - await mockCwd(path.join(repoDirectory, "nested"), () => - commitChangesFromRepo({ - octokit, - ...REPO, - branch, - message: { - headline: "Test commit", - body: "This is a test commit", - }, - log, - }), - ); + it(`should correctly filter files to sub-directory`, async () => { + const branch = `${TEST_BRANCH_PREFIX}-subdir-repodirectory-unspecified-add`; + branches.push(branch); - await waitForGitHubToBeReady(); + await fs.promises.mkdir(testDir, { recursive: true }); + const repoDirectory = path.join( + testDir, + `repo-subdir-repodirectory-unspecified-add`, + ); - await makeFileChangeAssertions(branch); + // Clone the git repo locally using the git cli and child-process + await new Promise((resolve, reject) => { + const p = execFile( + "git", + [ + "clone", + process.cwd(), + `repo-subdir-repodirectory-unspecified-add`, + ], + { cwd: testDir }, + (error) => { + if (error) { + reject(error); + } else { + resolve(); + } + }, + ); + p.stdout?.pipe(process.stdout); + p.stderr?.pipe(process.stderr); + }); - // Expect the OID to be the HEAD commit - const oid = - ( - await git.log({ - fs, - dir: repoDirectory, - ref: "HEAD", - depth: 1, - }) - )[0]?.oid ?? "NO_OID"; + await makeFileChanges(repoDirectory, "standard"); - await expectParentHasOid({ branch, oid }); + // Push the changes + await commitChangesFromRepo({ + octokit, + ...REPO, + branch, + message: { + headline: "Test commit", + body: "This is a test commit", + }, + cwd: path.join(repoDirectory, "nested"), + log, }); - it(`addFromDirectory should correctly filter files`, async () => { - const branch = `${TEST_BRANCH_PREFIX}-subdir-repodirectory-unspecified-add`; - branches.push(branch); + await waitForGitHubToBeReady(); - await fs.promises.mkdir(testDir, { recursive: true }); - const repoDirectory = path.join( - testDir, - `repo-subdir-repodirectory-unspecified-add`, - ); + await makeSubdirectoryFileChangeAssertions(branch, true); - // Clone the git repo locally using the git cli and child-process - await new Promise((resolve, reject) => { - const p = execFile( - "git", - [ - "clone", - process.cwd(), - `repo-subdir-repodirectory-unspecified-add`, - ], - { cwd: testDir }, - (error) => { - if (error) { - reject(error); - } else { - resolve(); - } - }, - ); - p.stdout?.pipe(process.stdout); - p.stderr?.pipe(process.stderr); - }); - - await makeFileChanges(repoDirectory, "standard"); - - // Push the changes - await mockCwd(path.join(repoDirectory, "nested"), () => - commitChangesFromRepo({ - octokit, - ...REPO, - branch, - message: { - headline: "Test commit", - body: "This is a test commit", - }, - addFromDirectory: path.join(repoDirectory, "nested"), - log, - }), - ); + // Expect the OID to be the HEAD commit + const oid = + ( + await git.log({ + fs, + dir: repoDirectory, + ref: "HEAD", + depth: 1, + }) + )[0]?.oid ?? "NO_OID"; - await waitForGitHubToBeReady(); + await expectParentHasOid({ branch, oid }); + }); - await makeSubdirectoryFileChangeAssertions(branch); + it(`filterFiles should correctly filter files`, async () => { + const branch = `${TEST_BRANCH_PREFIX}-subdir-repodirectory-unspecified-filter`; + branches.push(branch); - // Expect the OID to be the HEAD commit - const oid = - ( - await git.log({ - fs, - dir: repoDirectory, - ref: "HEAD", - depth: 1, - }) - )[0]?.oid ?? "NO_OID"; + await fs.promises.mkdir(testDir, { recursive: true }); + const repoDirectory = path.join( + testDir, + `repo-subdir-repodirectory-unspecified-filter`, + ); - await expectParentHasOid({ branch, oid }); + // Clone the git repo locally using the git cli and child-process + await new Promise((resolve, reject) => { + const p = execFile( + "git", + [ + "clone", + process.cwd(), + `repo-subdir-repodirectory-unspecified-filter`, + ], + { cwd: testDir }, + (error) => { + if (error) { + reject(error); + } else { + resolve(); + } + }, + ); + p.stdout?.pipe(process.stdout); + p.stderr?.pipe(process.stderr); }); - it(`filterFiles should correctly filter files`, async () => { - const branch = `${TEST_BRANCH_PREFIX}-subdir-repodirectory-unspecified-filter`; - branches.push(branch); + await makeFileChanges(repoDirectory, "standard"); - await fs.promises.mkdir(testDir, { recursive: true }); - const repoDirectory = path.join( - testDir, - `repo-subdir-repodirectory-unspecified-filter`, - ); - - // Clone the git repo locally using the git cli and child-process - await new Promise((resolve, reject) => { - const p = execFile( - "git", - [ - "clone", - process.cwd(), - `repo-subdir-repodirectory-unspecified-filter`, - ], - { cwd: testDir }, - (error) => { - if (error) { - reject(error); - } else { - resolve(); - } - }, - ); - p.stdout?.pipe(process.stdout); - p.stderr?.pipe(process.stderr); - }); - - await makeFileChanges(repoDirectory, "standard"); - - // Push the changes - await mockCwd(path.join(repoDirectory, "nested"), () => - commitChangesFromRepo({ - octokit, - ...REPO, - branch, - message: { - headline: "Test commit", - body: "This is a test commit", - }, - // Only include top-level files - filterFiles: (file) => !file.includes("/"), - log, - }), - ); + // Push the changes + await commitChangesFromRepo({ + octokit, + ...REPO, + branch, + message: { + headline: "Test commit", + body: "This is a test commit", + }, + cwd: path.join(repoDirectory, "nested"), + filterFiles: (file) => file.includes(".txt"), + log, + }); - await waitForGitHubToBeReady(); + await waitForGitHubToBeReady(); - await makeFilteredFileChangeAssertions(branch); + await makeSubdirectoryFileChangeAssertions(branch, false); - // Expect the OID to be the HEAD commit - const oid = - ( - await git.log({ - fs, - dir: repoDirectory, - ref: "HEAD", - depth: 1, - }) - )[0]?.oid ?? "NO_OID"; + // Expect the OID to be the HEAD commit + const oid = + ( + await git.log({ + fs, + dir: repoDirectory, + ref: "HEAD", + depth: 1, + }) + )[0]?.oid ?? "NO_OID"; - await expectParentHasOid({ branch, oid }); - }); + await expectParentHasOid({ branch, oid }); }); }); }); From 2171089537223041ed41b6df6d681ef91cf63137 Mon Sep 17 00:00:00 2001 From: Sam Lanning Date: Sat, 10 May 2025 00:33:17 +0100 Subject: [PATCH 2/2] Fix node tests --- src/test/integration/node.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/integration/node.test.ts b/src/test/integration/node.test.ts index 1ecb1e6..304bfa3 100644 --- a/src/test/integration/node.test.ts +++ b/src/test/integration/node.test.ts @@ -219,7 +219,7 @@ describe("node", () => { ...REPO, branch, base: { - tag: "v0.1.0", + tag: "v1.4.0", }, ...BASIC_FILE_CONTENTS, });