feat: implement remove command with sequential removals
This commit is contained in:
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)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user