Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 88 additions & 0 deletions packages/nx/src/command-line/release/utils/git-add.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import { gitAdd } from './git';

const mockExecCommand = vi.fn();
vi.mock('./exec-command', () => ({
execCommand: (...args: unknown[]) => mockExecCommand(...args),
}));

vi.mock('../../../utils/workspace-root', () => ({
workspaceRoot: '/workspace',
}));

describe('gitAdd', () => {
beforeEach(() => {
mockExecCommand.mockReset();
});

it('should normalize backslash paths in deletedFiles to match git status output', async () => {
// git status --porcelain returns forward-slash paths
mockExecCommand.mockImplementation(
(cmd: string, args: string[], _opts?: unknown) => {
if (cmd === 'git' && args[0] === 'status') {
return Promise.resolve(
' D .nx/version-plans/version-plan-123.md\n'
);
}
if (cmd === 'git' && args[0] === 'check-ignore') {
return Promise.reject(new Error('not ignored'));
}
if (cmd === 'git' && args[0] === 'add') {
return Promise.resolve('');
}
return Promise.resolve('');
}
);

// On Windows, path.join produces backslash-separated paths
await gitAdd({
deletedFiles: ['.nx\\version-plans\\version-plan-123.md'],
cwd: '/workspace',
});

const addCall = mockExecCommand.mock.calls.find(
(c: unknown[]) =>
c[0] === 'git' &&
Array.isArray(c[1]) &&
(c[1] as string[])[0] === 'add'
);
expect(addCall).toBeDefined();
expect((addCall![1] as string[])[1]).toBe(
'.nx/version-plans/version-plan-123.md'
);
});

it('should stage deleted files with forward-slash paths unchanged', async () => {
mockExecCommand.mockImplementation(
(cmd: string, args: string[], _opts?: unknown) => {
if (cmd === 'git' && args[0] === 'status') {
return Promise.resolve(
' D .nx/version-plans/version-plan-456.md\n'
);
}
if (cmd === 'git' && args[0] === 'check-ignore') {
return Promise.reject(new Error('not ignored'));
}
if (cmd === 'git' && args[0] === 'add') {
return Promise.resolve('');
}
return Promise.resolve('');
}
);

await gitAdd({
deletedFiles: ['.nx/version-plans/version-plan-456.md'],
cwd: '/workspace',
});

const addCall = mockExecCommand.mock.calls.find(
(c: unknown[]) =>
c[0] === 'git' &&
Array.isArray(c[1]) &&
(c[1] as string[])[0] === 'add'
);
expect(addCall).toBeDefined();
expect((addCall![1] as string[])[1]).toBe(
'.nx/version-plans/version-plan-456.md'
);
});
});
9 changes: 5 additions & 4 deletions packages/nx/src/command-line/release/utils/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -348,12 +348,13 @@ export async function gitAdd({
if (deletedFiles?.length > 0) {
const changedTrackedFiles = await getChangedTrackedFiles(cwd);
for (const f of deletedFiles ?? []) {
const isFileIgnored = await isIgnored(f, cwd);
const normalized = f.replace(/\\/g, '/');
const isFileIgnored = await isIgnored(normalized, cwd);
if (isFileIgnored) {
ignoredFiles.push(f);
ignoredFiles.push(normalized);
// git add will fail if trying to add an untracked file that doesn't exist
} else if (changedTrackedFiles.has(f) || dryRun) {
filesToAdd.push(f);
} else if (changedTrackedFiles.has(normalized) || dryRun) {
filesToAdd.push(normalized);
}
}
}
Expand Down