feat: implement remove command with sequential removals
This commit is contained in:
104
tests/contract/remove_command_ledger_test.go
Normal file
104
tests/contract/remove_command_ledger_test.go
Normal file
@@ -0,0 +1,104 @@
|
||||
package contract
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
renamercmd "github.com/rogeecn/renamer/cmd"
|
||||
"github.com/rogeecn/renamer/internal/history"
|
||||
)
|
||||
|
||||
func TestRemoveCommandLedgerMetadata(t *testing.T) {
|
||||
tmp := t.TempDir()
|
||||
|
||||
createRemoveFile(t, filepath.Join(tmp, "report copy draft.txt"))
|
||||
createRemoveFile(t, filepath.Join(tmp, "notes draft.txt"))
|
||||
|
||||
root := renamercmd.NewRootCommand()
|
||||
var out bytes.Buffer
|
||||
root.SetOut(&out)
|
||||
root.SetErr(&out)
|
||||
root.SetArgs([]string{"remove", " copy", " draft", "--path", tmp, "--yes"})
|
||||
|
||||
if err := root.Execute(); err != nil {
|
||||
t.Fatalf("remove command error: %v\noutput: %s", err, out.String())
|
||||
}
|
||||
|
||||
ledgerPath := filepath.Join(tmp, ".renamer")
|
||||
data, err := os.ReadFile(ledgerPath)
|
||||
if err != nil {
|
||||
t.Fatalf("read ledger: %v", err)
|
||||
}
|
||||
|
||||
lines := bytes.Split(bytes.TrimSpace(data), []byte("\n"))
|
||||
if len(lines) == 0 {
|
||||
t.Fatalf("expected ledger entry written")
|
||||
}
|
||||
|
||||
var entry history.Entry
|
||||
if err := json.Unmarshal(lines[len(lines)-1], &entry); err != nil {
|
||||
t.Fatalf("decode ledger entry: %v", err)
|
||||
}
|
||||
|
||||
if entry.Command != "remove" {
|
||||
t.Fatalf("expected remove command recorded, got %q", entry.Command)
|
||||
}
|
||||
|
||||
tokensVal, ok := entry.Metadata["tokens"].([]any)
|
||||
if !ok {
|
||||
t.Fatalf("expected tokens metadata, got %#v", entry.Metadata)
|
||||
}
|
||||
tokens := make([]string, len(tokensVal))
|
||||
for i, v := range tokensVal {
|
||||
s, ok := v.(string)
|
||||
if !ok {
|
||||
t.Fatalf("token entry not string: %#v", v)
|
||||
}
|
||||
tokens[i] = s
|
||||
}
|
||||
if len(tokens) != 2 || tokens[0] != " copy" || tokens[1] != " draft" {
|
||||
t.Fatalf("unexpected tokens metadata: %#v", tokens)
|
||||
}
|
||||
|
||||
matchesVal, ok := entry.Metadata["matches"].(map[string]any)
|
||||
if !ok {
|
||||
t.Fatalf("expected matches metadata, got %#v", entry.Metadata)
|
||||
}
|
||||
if len(matchesVal) != 2 {
|
||||
t.Fatalf("unexpected matches metadata: %#v", matchesVal)
|
||||
}
|
||||
if toFloat(matchesVal[" copy"]) != 1 || toFloat(matchesVal[" draft"]) != 2 {
|
||||
t.Fatalf("unexpected match counts: %#v", matchesVal)
|
||||
}
|
||||
|
||||
// Ensure undo restores originals for automation workflows.
|
||||
undo := renamercmd.NewRootCommand()
|
||||
undo.SetOut(&bytes.Buffer{})
|
||||
undo.SetErr(&bytes.Buffer{})
|
||||
undo.SetArgs([]string{"undo", "--path", tmp})
|
||||
if err := undo.Execute(); err != nil {
|
||||
t.Fatalf("undo command error: %v", err)
|
||||
}
|
||||
|
||||
if _, err := os.Stat(filepath.Join(tmp, "report copy draft.txt")); err != nil {
|
||||
t.Fatalf("expected original restored after undo: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func toFloat(value any) float64 {
|
||||
switch v := value.(type) {
|
||||
case float64:
|
||||
return v
|
||||
case float32:
|
||||
return float64(v)
|
||||
case int:
|
||||
return float64(v)
|
||||
case int64:
|
||||
return float64(v)
|
||||
default:
|
||||
return -1
|
||||
}
|
||||
}
|
||||
46
tests/contract/remove_command_preview_test.go
Normal file
46
tests/contract/remove_command_preview_test.go
Normal file
@@ -0,0 +1,46 @@
|
||||
package contract
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
renamercmd "github.com/rogeecn/renamer/cmd"
|
||||
)
|
||||
|
||||
func TestRemoveCommandDryRunPreview(t *testing.T) {
|
||||
tmp := t.TempDir()
|
||||
createRemoveFile(t, filepath.Join(tmp, "report copy draft.txt"))
|
||||
createRemoveFile(t, filepath.Join(tmp, "notes draft.txt"))
|
||||
|
||||
root := renamercmd.NewRootCommand()
|
||||
var out bytes.Buffer
|
||||
root.SetOut(&out)
|
||||
root.SetErr(&out)
|
||||
root.SetArgs([]string{"remove", " copy", " draft", "--path", tmp, "--dry-run"})
|
||||
|
||||
if err := root.Execute(); err != nil {
|
||||
t.Fatalf("remove command returned error: %v (output: %s)", err, out.String())
|
||||
}
|
||||
|
||||
output := out.String()
|
||||
if !strings.Contains(output, "report copy draft.txt -> report.txt") {
|
||||
t.Fatalf("expected preview mapping in output, got: %s", output)
|
||||
}
|
||||
|
||||
if !strings.Contains(output, "Preview complete") {
|
||||
t.Fatalf("expected dry-run completion message, got: %s", output)
|
||||
}
|
||||
}
|
||||
|
||||
func createRemoveFile(t *testing.T, path string) {
|
||||
t.Helper()
|
||||
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
|
||||
t.Fatalf("mkdir %s: %v", path, err)
|
||||
}
|
||||
if err := os.WriteFile(path, []byte("x"), 0o644); err != nil {
|
||||
t.Fatalf("write file %s: %v", path, err)
|
||||
}
|
||||
}
|
||||
86
tests/integration/remove_flow_test.go
Normal file
86
tests/integration/remove_flow_test.go
Normal file
@@ -0,0 +1,86 @@
|
||||
package integration
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/rogeecn/renamer/internal/history"
|
||||
"github.com/rogeecn/renamer/internal/remove"
|
||||
)
|
||||
|
||||
func TestRemoveApplyAndUndo(t *testing.T) {
|
||||
tmp := t.TempDir()
|
||||
|
||||
createFile(t, filepath.Join(tmp, "report copy draft.txt"))
|
||||
createFile(t, filepath.Join(tmp, "nested", "notes draft.txt"))
|
||||
|
||||
parsed, err := remove.ParseArgs([]string{" copy", " draft"})
|
||||
if err != nil {
|
||||
t.Fatalf("ParseArgs error: %v", err)
|
||||
}
|
||||
|
||||
req := &remove.Request{
|
||||
WorkingDir: tmp,
|
||||
Tokens: parsed.Tokens,
|
||||
Recursive: true,
|
||||
}
|
||||
if err := req.Validate(); err != nil {
|
||||
t.Fatalf("request validation error: %v", err)
|
||||
}
|
||||
|
||||
summary, planned, err := remove.Preview(context.Background(), req, parsed, nil)
|
||||
if err != nil {
|
||||
t.Fatalf("Preview error: %v", err)
|
||||
}
|
||||
|
||||
if summary.ChangedCount != 2 {
|
||||
t.Fatalf("expected 2 changes, got %d", summary.ChangedCount)
|
||||
}
|
||||
|
||||
entry, err := remove.Apply(context.Background(), req, planned, summary, parsed.Tokens)
|
||||
if err != nil {
|
||||
t.Fatalf("Apply error: %v", err)
|
||||
}
|
||||
|
||||
if len(entry.Operations) != 2 {
|
||||
t.Fatalf("expected 2 operations recorded, got %d", len(entry.Operations))
|
||||
}
|
||||
if entry.Metadata == nil {
|
||||
t.Fatalf("expected metadata to be recorded")
|
||||
}
|
||||
tokens, ok := entry.Metadata["tokens"].([]string)
|
||||
if !ok {
|
||||
t.Fatalf("expected ordered tokens metadata, got %#v", entry.Metadata)
|
||||
}
|
||||
if len(tokens) != 2 || tokens[0] != " copy" || tokens[1] != " draft" {
|
||||
t.Fatalf("unexpected tokens metadata: %#v", tokens)
|
||||
}
|
||||
matches, ok := entry.Metadata["matches"].(map[string]int)
|
||||
if !ok {
|
||||
t.Fatalf("expected matches metadata, got %#v", entry.Metadata)
|
||||
}
|
||||
if matches[" copy"] != 1 || matches[" draft"] != 2 {
|
||||
t.Fatalf("unexpected match counts: %#v", matches)
|
||||
}
|
||||
|
||||
if _, err := os.Stat(filepath.Join(tmp, "report.txt")); err != nil {
|
||||
t.Fatalf("expected renamed file exists: %v", err)
|
||||
}
|
||||
|
||||
if _, err := os.Stat(filepath.Join(tmp, "nested", "notes.txt")); err != nil {
|
||||
t.Fatalf("expected nested rename exists: %v", err)
|
||||
}
|
||||
|
||||
if _, err := history.Undo(tmp); err != nil {
|
||||
t.Fatalf("undo error: %v", err)
|
||||
}
|
||||
|
||||
if _, err := os.Stat(filepath.Join(tmp, "report copy draft.txt")); err != nil {
|
||||
t.Fatalf("expected original restored: %v", err)
|
||||
}
|
||||
if _, err := os.Stat(filepath.Join(tmp, "nested", "notes draft.txt")); err != nil {
|
||||
t.Fatalf("expected nested original restored: %v", err)
|
||||
}
|
||||
}
|
||||
57
tests/integration/remove_undo_test.go
Normal file
57
tests/integration/remove_undo_test.go
Normal file
@@ -0,0 +1,57 @@
|
||||
package integration
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
renamercmd "github.com/rogeecn/renamer/cmd"
|
||||
)
|
||||
|
||||
func TestRemoveCommandAutomationUndo(t *testing.T) {
|
||||
tmp := t.TempDir()
|
||||
|
||||
createFile(t, filepath.Join(tmp, "alpha copy.txt"))
|
||||
createFile(t, filepath.Join(tmp, "nested", "beta draft.txt"))
|
||||
|
||||
preview := renamercmd.NewRootCommand()
|
||||
var previewOut bytes.Buffer
|
||||
preview.SetOut(&previewOut)
|
||||
preview.SetErr(&previewOut)
|
||||
preview.SetArgs([]string{"remove", " copy", " draft", "--path", tmp, "--recursive", "--dry-run"})
|
||||
if err := preview.Execute(); err != nil {
|
||||
t.Fatalf("preview failed: %v\noutput: %s", err, previewOut.String())
|
||||
}
|
||||
|
||||
apply := renamercmd.NewRootCommand()
|
||||
var applyOut bytes.Buffer
|
||||
apply.SetOut(&applyOut)
|
||||
apply.SetErr(&applyOut)
|
||||
apply.SetArgs([]string{"remove", " copy", " draft", "--path", tmp, "--recursive", "--yes"})
|
||||
if err := apply.Execute(); err != nil {
|
||||
t.Fatalf("apply failed: %v\noutput: %s", err, applyOut.String())
|
||||
}
|
||||
|
||||
if !fileExists(filepath.Join(tmp, "alpha.txt")) || !fileExists(filepath.Join(tmp, "nested", "beta.txt")) {
|
||||
t.Fatalf("expected files renamed after apply")
|
||||
}
|
||||
|
||||
undo := renamercmd.NewRootCommand()
|
||||
var undoOut bytes.Buffer
|
||||
undo.SetOut(&undoOut)
|
||||
undo.SetErr(&undoOut)
|
||||
undo.SetArgs([]string{"undo", "--path", tmp})
|
||||
if err := undo.Execute(); err != nil {
|
||||
t.Fatalf("undo failed: %v\noutput: %s", err, undoOut.String())
|
||||
}
|
||||
|
||||
if !fileExists(filepath.Join(tmp, "alpha copy.txt")) || !fileExists(filepath.Join(tmp, "nested", "beta draft.txt")) {
|
||||
t.Fatalf("expected originals restored after undo")
|
||||
}
|
||||
}
|
||||
|
||||
func fileExists(path string) bool {
|
||||
_, err := os.Stat(path)
|
||||
return err == nil
|
||||
}
|
||||
62
tests/integration/remove_validation_test.go
Normal file
62
tests/integration/remove_validation_test.go
Normal file
@@ -0,0 +1,62 @@
|
||||
package integration
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
renamercmd "github.com/rogeecn/renamer/cmd"
|
||||
)
|
||||
|
||||
func TestRemoveCommandEmptyBasenameWarning(t *testing.T) {
|
||||
tmp := t.TempDir()
|
||||
|
||||
createValidationFile(t, filepath.Join(tmp, "draft"))
|
||||
createValidationFile(t, filepath.Join(tmp, "draft copy.txt"))
|
||||
|
||||
root := renamercmd.NewRootCommand()
|
||||
var out bytes.Buffer
|
||||
root.SetOut(&out)
|
||||
root.SetErr(&out)
|
||||
root.SetArgs([]string{"remove", "draft", "--path", tmp, "--dry-run"})
|
||||
|
||||
if err := root.Execute(); err != nil {
|
||||
t.Fatalf("remove dry-run failed: %v\noutput: %s", err, out.String())
|
||||
}
|
||||
|
||||
if !strings.Contains(out.String(), "Warning: draft would become empty; skipping") {
|
||||
t.Fatalf("expected empty basename warning, got: %s", out.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestRemoveCommandDuplicateWarning(t *testing.T) {
|
||||
tmp := t.TempDir()
|
||||
|
||||
createValidationFile(t, filepath.Join(tmp, "foo draft draft.txt"))
|
||||
|
||||
root := renamercmd.NewRootCommand()
|
||||
var out bytes.Buffer
|
||||
root.SetOut(&out)
|
||||
root.SetErr(&out)
|
||||
root.SetArgs([]string{"remove", " draft", " draft", "--path", tmp, "--dry-run"})
|
||||
|
||||
if err := root.Execute(); err != nil {
|
||||
t.Fatalf("remove dry-run failed: %v\noutput: %s", err, out.String())
|
||||
}
|
||||
|
||||
if !strings.Contains(out.String(), "Warning: token \" draft\" provided multiple times") {
|
||||
t.Fatalf("expected duplicate warning, got: %s", out.String())
|
||||
}
|
||||
}
|
||||
|
||||
func createValidationFile(t *testing.T, path string) {
|
||||
t.Helper()
|
||||
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
|
||||
t.Fatalf("mkdir %s: %v", path, err)
|
||||
}
|
||||
if err := os.WriteFile(path, []byte("x"), 0o644); err != nil {
|
||||
t.Fatalf("write file %s: %v", path, err)
|
||||
}
|
||||
}
|
||||
74
tests/unit/remove_engine_test.go
Normal file
74
tests/unit/remove_engine_test.go
Normal file
@@ -0,0 +1,74 @@
|
||||
package replace_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/rogeecn/renamer/internal/remove"
|
||||
)
|
||||
|
||||
func TestApplyTokensSequentialRemoval(t *testing.T) {
|
||||
candidate := remove.Candidate{
|
||||
BaseName: "report copy draft.txt",
|
||||
RelativePath: "report copy draft.txt",
|
||||
}
|
||||
|
||||
result := remove.ApplyTokens(candidate, []string{" copy", " draft"})
|
||||
|
||||
if !result.Changed {
|
||||
t.Fatalf("expected result to be marked as changed")
|
||||
}
|
||||
|
||||
if result.ProposedName != "report.txt" {
|
||||
t.Fatalf("expected proposed name to be report.txt, got %q", result.ProposedName)
|
||||
}
|
||||
|
||||
if result.Matches[" copy"] != 1 {
|
||||
t.Fatalf("expected match count for ' copy' to be 1, got %d", result.Matches[" copy"])
|
||||
}
|
||||
|
||||
if result.Matches[" draft"] != 1 {
|
||||
t.Fatalf("expected match count for ' draft' to be 1, got %d", result.Matches[" draft"])
|
||||
}
|
||||
}
|
||||
|
||||
func TestApplyTokensNoChange(t *testing.T) {
|
||||
candidate := remove.Candidate{
|
||||
BaseName: "notes.txt",
|
||||
RelativePath: "notes.txt",
|
||||
}
|
||||
|
||||
result := remove.ApplyTokens(candidate, []string{" copy"})
|
||||
|
||||
if result.Changed {
|
||||
t.Fatalf("expected no change for candidate without matches")
|
||||
}
|
||||
|
||||
if len(result.Matches) != 0 {
|
||||
t.Fatalf("expected no matches to be recorded, got %#v", result.Matches)
|
||||
}
|
||||
|
||||
if result.ProposedName != candidate.BaseName {
|
||||
t.Fatalf("expected proposed name to remain %q, got %q", candidate.BaseName, result.ProposedName)
|
||||
}
|
||||
}
|
||||
|
||||
func TestApplyTokensEmptyName(t *testing.T) {
|
||||
candidate := remove.Candidate{
|
||||
BaseName: "draft",
|
||||
RelativePath: "draft",
|
||||
}
|
||||
|
||||
result := remove.ApplyTokens(candidate, []string{"draft"})
|
||||
|
||||
if !result.Changed {
|
||||
t.Fatalf("expected change when removing the full name")
|
||||
}
|
||||
|
||||
if result.ProposedName != "" {
|
||||
t.Fatalf("expected proposed name to be empty, got %q", result.ProposedName)
|
||||
}
|
||||
|
||||
if result.Matches["draft"] != 1 {
|
||||
t.Fatalf("expected matches to record removal, got %#v", result.Matches)
|
||||
}
|
||||
}
|
||||
46
tests/unit/remove_parser_test.go
Normal file
46
tests/unit/remove_parser_test.go
Normal file
@@ -0,0 +1,46 @@
|
||||
package replace_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/rogeecn/renamer/internal/remove"
|
||||
)
|
||||
|
||||
func TestParseArgsDeduplicatesPreservingOrder(t *testing.T) {
|
||||
args := []string{" draft", " draft", " copy", " draft"}
|
||||
|
||||
result, err := remove.ParseArgs(args)
|
||||
if err != nil {
|
||||
t.Fatalf("ParseArgs returned error: %v", err)
|
||||
}
|
||||
|
||||
expected := []string{" draft", " copy"}
|
||||
if len(result.Tokens) != len(expected) {
|
||||
t.Fatalf("expected %d tokens, got %d", len(expected), len(result.Tokens))
|
||||
}
|
||||
for i, token := range expected {
|
||||
if result.Tokens[i] != token {
|
||||
t.Fatalf("token[%d] mismatch: expected %q, got %q", i, token, result.Tokens[i])
|
||||
}
|
||||
}
|
||||
|
||||
if len(result.Duplicates) != 2 {
|
||||
t.Fatalf("expected 2 duplicates recorded, got %d", len(result.Duplicates))
|
||||
}
|
||||
if result.Duplicates[0] != " draft" || result.Duplicates[1] != " draft" {
|
||||
t.Fatalf("unexpected duplicates: %#v", result.Duplicates)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseArgsSkipsWhitespaceOnlyTokens(t *testing.T) {
|
||||
args := []string{" ", "\t", "foo"}
|
||||
|
||||
result, err := remove.ParseArgs(args)
|
||||
if err != nil {
|
||||
t.Fatalf("ParseArgs returned error: %v", err)
|
||||
}
|
||||
|
||||
if len(result.Tokens) != 1 || result.Tokens[0] != "foo" {
|
||||
t.Fatalf("expected single token 'foo', got %#v", result.Tokens)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user