Files
shakespeare/src/lib/commands/git/rm.test.ts
T
Alex Gleason b3fddf6cb3 Audit and overhaul virtual shell's git command
Fix critical bugs, add extensive missing flags, and add 8 new subcommands
to bring the browser-side git command closer to parity with real git.

Critical bug fixes:
- git commit: file-count classification used impossible stageStatus=1
  comparisons, always reporting 0 files changed
- git status --porcelain: untracked files could clobber staged indicators
- git add: directory walker skipped all dotfiles (including .gitignore
  itself), ignoring real git's .gitignore-based filtering
- git pull: untracked files incorrectly blocked pulls
- git reset: target regex rejected valid refs like branch names
- git diff: path filter used substring matching; --cached read working
  directory instead of index; <commit> and <commit>..<commit> forms were
  stubbed/unimplemented; used fake JS hashes instead of git.hashBlob

Implemented git reset --soft (was returning "not implemented").

New flags added across existing subcommands:
- commit: --signoff, --no-verify, -F/--file, --author, -C/--reuse-message,
  multiple -m support
- add: -u/--update, -f/--force, -n/--dry-run, absolute paths inside repo
- log: --stat, -p/--patch, --name-only, --name-status, --author, --grep,
  --since, --until, --no-merges, --reverse, --follow, custom
  --pretty=format:, proper timezone in output
- push: -u/--set-upstream (now wired to config), --tags, --delete,
  --dry-run, refspec (local:remote)
- fetch: --all, --tags, --prune now actually passed through
- branch: -a/--all, -r/--remotes, -m/-M (rename), --show-current, -v
- checkout: -B, -f/--force, commit-hash support (detached HEAD)
- config: --unset, --get-all, --add, --local, proper --list that reads
  the whole config file
- tag: real annotated tags via git.annotatedTag, -m, -f, -n, glob patterns
- clone: -b/--branch, -o/--origin, --bare
- init: -b/--initial-branch
- show: --stat, --name-only, --name-status
- diff: --stat, --name-only, --name-status
- remote: set-url, get-url, rename, show subcommands

New subcommands:
- git rm: remove files from index (and optionally worktree)
- git mv: rename/move files with index updates
- git restore: modern replacement for checkout of files
- git switch: modern replacement for branch checkout
- git merge: wrapper over git.merge (including --abort, --ff-only)
- git revert: create an inverse commit
- git rev-parse: resolve refs, --short, --abbrev-ref, --show-toplevel,
  --git-dir, HEAD~N / HEAD^N
- git ls-files: list tracked/modified/deleted/untracked files

All existing tests updated where behavior intentionally changed; new
subcommands have dedicated test suites. Final suite: 1392 passing.
2026-04-27 00:42:10 -05:00

98 lines
3.5 KiB
TypeScript

import { describe, it, expect, beforeEach, vi } from 'vitest';
import { GitRmCommand } from './rm';
import type { Git } from '../../git';
import type { JSRuntimeFS } from '../../JSRuntime';
const createMockFS = () => {
const files = new Map<string, string>();
const dirs = new Set(['/test-repo', '/test-repo/subdir']);
files.set('/test-repo/file1.txt', 'contents');
files.set('/test-repo/subdir/nested.txt', 'nested');
return {
stat: vi.fn(async (path: string) => {
if (path.endsWith('/.git') || dirs.has(path)) {
return { isDirectory: () => true, isFile: () => false };
}
if (files.has(path)) {
return { isDirectory: () => false, isFile: () => true };
}
throw new Error('ENOENT');
}),
readdir: vi.fn(async (path: string) => {
if (path === '/test-repo/subdir') {
return [{ name: 'nested.txt', isDirectory: () => false, isFile: () => true }];
}
return [];
}),
unlink: vi.fn(async (path: string) => {
files.delete(path);
}),
_files: files,
} as unknown as JSRuntimeFS & { _files: Map<string, string> };
};
describe('GitRmCommand', () => {
let rmCommand: GitRmCommand;
let mockGit: Git;
let mockFS: JSRuntimeFS & { _files: Map<string, string> };
beforeEach(() => {
mockFS = createMockFS() as JSRuntimeFS & { _files: Map<string, string> };
mockGit = {
remove: vi.fn(async () => {}),
} as unknown as Git;
rmCommand = new GitRmCommand({ git: mockGit, fs: mockFS });
});
it('should fail without pathspec', async () => {
const result = await rmCommand.execute([], '/test-repo');
expect(result.exitCode).toBe(1);
expect(result.stderr).toContain('No pathspec');
});
it('should fail when not in a git repo', async () => {
const mockFSNoGit = {
stat: async () => { throw new Error('ENOENT'); },
} as unknown as JSRuntimeFS;
const cmd = new GitRmCommand({ git: mockGit, fs: mockFSNoGit });
const result = await cmd.execute(['foo.txt'], '/not-a-repo');
expect(result.exitCode).toBe(1);
expect(result.stderr).toContain('not a git repository');
});
it('should remove a file from working tree and index', async () => {
const result = await rmCommand.execute(['file1.txt'], '/test-repo');
expect(result.exitCode).toBe(0);
expect(result.stdout).toContain("rm 'file1.txt'");
expect(mockGit.remove).toHaveBeenCalledWith({ dir: '/test-repo', filepath: 'file1.txt' });
expect(mockFS.unlink).toHaveBeenCalled();
});
it('should only remove from index with --cached', async () => {
const result = await rmCommand.execute(['--cached', 'file1.txt'], '/test-repo');
expect(result.exitCode).toBe(0);
expect(mockGit.remove).toHaveBeenCalled();
expect(mockFS.unlink).not.toHaveBeenCalled();
});
it('should not remove files with --dry-run', async () => {
const result = await rmCommand.execute(['-n', 'file1.txt'], '/test-repo');
expect(result.exitCode).toBe(0);
expect(result.stdout).toContain("rm 'file1.txt'");
expect(mockGit.remove).not.toHaveBeenCalled();
});
it('should require -r for directories', async () => {
const result = await rmCommand.execute(['subdir'], '/test-repo');
expect(result.exitCode).toBe(1);
expect(result.stderr).toContain('-r');
});
it('should remove directory recursively with -r', async () => {
const result = await rmCommand.execute(['-r', 'subdir'], '/test-repo');
expect(result.exitCode).toBe(0);
expect(result.stdout).toContain('subdir/nested.txt');
});
});