Add insert command
This commit is contained in:
91
tests/contract/insert_command_test.go
Normal file
91
tests/contract/insert_command_test.go
Normal file
@@ -0,0 +1,91 @@
|
||||
package contract
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/rogeecn/renamer/internal/insert"
|
||||
"github.com/rogeecn/renamer/internal/listing"
|
||||
)
|
||||
|
||||
func TestInsertPreviewAndApply(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
tmp := t.TempDir()
|
||||
writeInsertFile(t, filepath.Join(tmp, "项目A报告.docx"))
|
||||
writeInsertFile(t, filepath.Join(tmp, "项目B报告.docx"))
|
||||
|
||||
scope := &listing.ListingRequest{
|
||||
WorkingDir: tmp,
|
||||
IncludeDirectories: false,
|
||||
Recursive: false,
|
||||
IncludeHidden: false,
|
||||
Extensions: nil,
|
||||
Format: listing.FormatTable,
|
||||
}
|
||||
if err := scope.Validate(); err != nil {
|
||||
t.Fatalf("validate scope: %v", err)
|
||||
}
|
||||
|
||||
req := insert.NewRequest(scope)
|
||||
req.SetExecutionMode(true, false)
|
||||
req.SetPositionAndText("^", "2025-")
|
||||
|
||||
var buf bytes.Buffer
|
||||
summary, planned, err := insert.Preview(context.Background(), req, &buf)
|
||||
if err != nil {
|
||||
t.Fatalf("Preview error: %v", err)
|
||||
}
|
||||
|
||||
if summary.TotalCandidates != 2 {
|
||||
t.Fatalf("expected 2 candidates, got %d", summary.TotalCandidates)
|
||||
}
|
||||
if summary.TotalChanged != 2 {
|
||||
t.Fatalf("expected 2 changes, got %d", summary.TotalChanged)
|
||||
}
|
||||
|
||||
output := buf.String()
|
||||
if !containsAll(output, "2025-项目A报告.docx", "2025-项目B报告.docx") {
|
||||
t.Fatalf("preview output missing expected names: %s", output)
|
||||
}
|
||||
|
||||
req.SetExecutionMode(false, true)
|
||||
entry, err := insert.Apply(context.Background(), req, planned, summary)
|
||||
if err != nil {
|
||||
t.Fatalf("Apply error: %v", err)
|
||||
}
|
||||
|
||||
if len(entry.Operations) != 2 {
|
||||
t.Fatalf("expected 2 ledger operations, got %d", len(entry.Operations))
|
||||
}
|
||||
|
||||
if _, err := os.Stat(filepath.Join(tmp, "项目A报告.docx")); !errors.Is(err, os.ErrNotExist) {
|
||||
t.Fatalf("expected original name to be renamed: %v", err)
|
||||
}
|
||||
if _, err := os.Stat(filepath.Join(tmp, "2025-项目A报告.docx")); err != nil {
|
||||
t.Fatalf("expected renamed file: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func writeInsertFile(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("test"), 0o644); err != nil {
|
||||
t.Fatalf("write file %s: %v", path, err)
|
||||
}
|
||||
}
|
||||
|
||||
func containsAll(haystack string, needles ...string) bool {
|
||||
for _, n := range needles {
|
||||
if !bytes.Contains([]byte(haystack), []byte(n)) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
106
tests/contract/insert_ledger_test.go
Normal file
106
tests/contract/insert_ledger_test.go
Normal file
@@ -0,0 +1,106 @@
|
||||
package contract
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
renamercmd "github.com/rogeecn/renamer/cmd"
|
||||
"github.com/rogeecn/renamer/internal/history"
|
||||
"github.com/rogeecn/renamer/internal/insert"
|
||||
"github.com/rogeecn/renamer/internal/listing"
|
||||
)
|
||||
|
||||
func TestInsertLedgerMetadataAndUndo(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
tmp := t.TempDir()
|
||||
writeInsertLedgerFile(t, filepath.Join(tmp, "doc.txt"))
|
||||
|
||||
scope := &listing.ListingRequest{
|
||||
WorkingDir: tmp,
|
||||
IncludeDirectories: false,
|
||||
Recursive: false,
|
||||
IncludeHidden: false,
|
||||
Format: listing.FormatTable,
|
||||
}
|
||||
if err := scope.Validate(); err != nil {
|
||||
t.Fatalf("validate scope: %v", err)
|
||||
}
|
||||
|
||||
req := insert.NewRequest(scope)
|
||||
req.SetExecutionMode(false, true)
|
||||
req.SetPositionAndText("$", "_ARCHIVE")
|
||||
|
||||
summary, planned, err := insert.Preview(context.Background(), req, nil)
|
||||
if err != nil {
|
||||
t.Fatalf("Preview error: %v", err)
|
||||
}
|
||||
|
||||
entry, err := insert.Apply(context.Background(), req, planned, summary)
|
||||
if err != nil {
|
||||
t.Fatalf("Apply error: %v", err)
|
||||
}
|
||||
|
||||
meta := entry.Metadata
|
||||
if meta == nil {
|
||||
t.Fatalf("expected metadata to be recorded")
|
||||
}
|
||||
|
||||
if got := meta["insertText"]; got != "_ARCHIVE" {
|
||||
t.Fatalf("expected insertText metadata, got %v", got)
|
||||
}
|
||||
if got := meta["positionToken"]; got != "$" {
|
||||
t.Fatalf("expected position token metadata, got %v", got)
|
||||
}
|
||||
scopeMeta, ok := meta["scope"].(map[string]any)
|
||||
if !ok {
|
||||
t.Fatalf("expected scope metadata, got %T", meta["scope"])
|
||||
}
|
||||
if includeHidden, _ := scopeMeta["includeHidden"].(bool); includeHidden {
|
||||
t.Fatalf("expected includeHidden to be false")
|
||||
}
|
||||
|
||||
undoEntry, err := history.Undo(tmp)
|
||||
if err != nil {
|
||||
t.Fatalf("undo error: %v", err)
|
||||
}
|
||||
if undoEntry.Command != "insert" {
|
||||
t.Fatalf("expected undo command to be insert, got %s", undoEntry.Command)
|
||||
}
|
||||
if _, err := os.Stat(filepath.Join(tmp, "doc.txt")); err != nil {
|
||||
t.Fatalf("expected original file restored: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestInsertZeroMatchExitsSuccessfully(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
tmp := t.TempDir()
|
||||
var out bytes.Buffer
|
||||
|
||||
cmd := renamercmd.NewRootCommand()
|
||||
cmd.SetOut(&out)
|
||||
cmd.SetErr(&out)
|
||||
cmd.SetArgs([]string{"insert", "^", "TEST", "--yes", "--path", tmp})
|
||||
|
||||
if err := cmd.Execute(); err != nil {
|
||||
t.Fatalf("expected zero-match insert to succeed, err=%v output=%s", err, out.String())
|
||||
}
|
||||
if !strings.Contains(out.String(), "No candidates found.") {
|
||||
t.Fatalf("expected zero-match notice, output=%s", out.String())
|
||||
}
|
||||
}
|
||||
|
||||
func writeInsertLedgerFile(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("ledger"), 0o644); err != nil {
|
||||
t.Fatalf("write file %s: %v", path, err)
|
||||
}
|
||||
}
|
||||
78
tests/contract/insert_validation_test.go
Normal file
78
tests/contract/insert_validation_test.go
Normal file
@@ -0,0 +1,78 @@
|
||||
package contract
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/rogeecn/renamer/internal/insert"
|
||||
"github.com/rogeecn/renamer/internal/listing"
|
||||
)
|
||||
|
||||
func TestInsertRejectsOutOfRangePositions(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
tmp := t.TempDir()
|
||||
writeInsertValidationFile(t, filepath.Join(tmp, "短.txt"))
|
||||
|
||||
scope := &listing.ListingRequest{
|
||||
WorkingDir: tmp,
|
||||
IncludeDirectories: false,
|
||||
Recursive: false,
|
||||
IncludeHidden: false,
|
||||
Format: listing.FormatTable,
|
||||
}
|
||||
if err := scope.Validate(); err != nil {
|
||||
t.Fatalf("validate scope: %v", err)
|
||||
}
|
||||
|
||||
req := insert.NewRequest(scope)
|
||||
req.SetExecutionMode(true, false)
|
||||
req.SetPositionAndText("50", "X")
|
||||
|
||||
if _, _, err := insert.Preview(context.Background(), req, nil); err == nil {
|
||||
t.Fatalf("expected error for out-of-range position")
|
||||
}
|
||||
}
|
||||
|
||||
func TestInsertBlocksExistingTargetConflicts(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
tmp := t.TempDir()
|
||||
writeInsertValidationFile(t, filepath.Join(tmp, "report.txt"))
|
||||
writeInsertValidationFile(t, filepath.Join(tmp, "report_ARCHIVE.txt"))
|
||||
|
||||
scope := &listing.ListingRequest{
|
||||
WorkingDir: tmp,
|
||||
IncludeDirectories: false,
|
||||
Recursive: false,
|
||||
IncludeHidden: false,
|
||||
Format: listing.FormatTable,
|
||||
}
|
||||
if err := scope.Validate(); err != nil {
|
||||
t.Fatalf("validate scope: %v", err)
|
||||
}
|
||||
|
||||
req := insert.NewRequest(scope)
|
||||
req.SetExecutionMode(true, false)
|
||||
req.SetPositionAndText("$", "_ARCHIVE")
|
||||
|
||||
summary, _, err := insert.Preview(context.Background(), req, nil)
|
||||
if err != nil {
|
||||
t.Fatalf("preview error: %v", err)
|
||||
}
|
||||
if !summary.HasConflicts() {
|
||||
t.Fatalf("expected conflicts to be detected")
|
||||
}
|
||||
}
|
||||
|
||||
func writeInsertValidationFile(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("validation"), 0o644); err != nil {
|
||||
t.Fatalf("write file %s: %v", path, err)
|
||||
}
|
||||
}
|
||||
69
tests/integration/insert_flow_test.go
Normal file
69
tests/integration/insert_flow_test.go
Normal file
@@ -0,0 +1,69 @@
|
||||
package integration
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
renamercmd "github.com/rogeecn/renamer/cmd"
|
||||
)
|
||||
|
||||
func TestInsertCommandFlow(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
tmp := t.TempDir()
|
||||
createInsertFile(t, filepath.Join(tmp, "holiday.jpg"))
|
||||
createInsertFile(t, filepath.Join(tmp, "trip.jpg"))
|
||||
|
||||
var previewOut bytes.Buffer
|
||||
preview := renamercmd.NewRootCommand()
|
||||
preview.SetOut(&previewOut)
|
||||
preview.SetErr(&previewOut)
|
||||
preview.SetArgs([]string{"insert", "3", "_tag", "--dry-run", "--path", tmp})
|
||||
|
||||
if err := preview.Execute(); err != nil {
|
||||
t.Fatalf("preview command failed: %v\noutput: %s", err, previewOut.String())
|
||||
}
|
||||
|
||||
if !contains(t, previewOut.String(), "hol_tagiday.jpg", "tri_tagp.jpg") {
|
||||
t.Fatalf("preview output missing expected inserts: %s", previewOut.String())
|
||||
}
|
||||
|
||||
var applyOut bytes.Buffer
|
||||
apply := renamercmd.NewRootCommand()
|
||||
apply.SetOut(&applyOut)
|
||||
apply.SetErr(&applyOut)
|
||||
apply.SetArgs([]string{"insert", "3", "_tag", "--yes", "--path", tmp})
|
||||
|
||||
if err := apply.Execute(); err != nil {
|
||||
t.Fatalf("apply command failed: %v\noutput: %s", err, applyOut.String())
|
||||
}
|
||||
|
||||
if _, err := os.Stat(filepath.Join(tmp, "hol_tagiday.jpg")); err != nil {
|
||||
t.Fatalf("expected renamed file: %v", err)
|
||||
}
|
||||
if _, err := os.Stat(filepath.Join(tmp, "tri_tagp.jpg")); err != nil {
|
||||
t.Fatalf("expected renamed file: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func contains(t *testing.T, haystack string, expected ...string) bool {
|
||||
t.Helper()
|
||||
for _, s := range expected {
|
||||
if !bytes.Contains([]byte(haystack), []byte(s)) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func createInsertFile(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)
|
||||
}
|
||||
}
|
||||
60
tests/integration/insert_undo_test.go
Normal file
60
tests/integration/insert_undo_test.go
Normal file
@@ -0,0 +1,60 @@
|
||||
package integration
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
renamercmd "github.com/rogeecn/renamer/cmd"
|
||||
)
|
||||
|
||||
func TestInsertAutomationUndo(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
tmp := t.TempDir()
|
||||
createInsertAutomationFile(t, filepath.Join(tmp, "config.yaml"))
|
||||
|
||||
var applyOut bytes.Buffer
|
||||
apply := renamercmd.NewRootCommand()
|
||||
apply.SetOut(&applyOut)
|
||||
apply.SetErr(&applyOut)
|
||||
apply.SetArgs([]string{"insert", "$", "_ARCHIVE", "--yes", "--path", tmp})
|
||||
|
||||
if err := apply.Execute(); err != nil {
|
||||
t.Fatalf("apply command failed: %v\noutput: %s", err, applyOut.String())
|
||||
}
|
||||
|
||||
if _, err := os.Stat(filepath.Join(tmp, "config_ARCHIVE.yaml")); err != nil {
|
||||
t.Fatalf("expected renamed file: %v", err)
|
||||
}
|
||||
|
||||
var undoOut bytes.Buffer
|
||||
undo := renamercmd.NewRootCommand()
|
||||
undo.SetOut(&undoOut)
|
||||
undo.SetErr(&undoOut)
|
||||
undo.SetArgs([]string{"undo", "--path", tmp})
|
||||
|
||||
if err := undo.Execute(); err != nil {
|
||||
t.Fatalf("undo command failed: %v\noutput: %s", err, undoOut.String())
|
||||
}
|
||||
|
||||
if _, err := os.Stat(filepath.Join(tmp, "config.yaml")); err != nil {
|
||||
t.Fatalf("expected original file restored: %v", err)
|
||||
}
|
||||
|
||||
if !strings.Contains(undoOut.String(), "Inserted text") {
|
||||
t.Fatalf("expected undo output to describe inserted text, got: %s", undoOut.String())
|
||||
}
|
||||
}
|
||||
|
||||
func createInsertAutomationFile(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("automation"), 0o644); err != nil {
|
||||
t.Fatalf("write file %s: %v", path, err)
|
||||
}
|
||||
}
|
||||
55
tests/integration/insert_validation_test.go
Normal file
55
tests/integration/insert_validation_test.go
Normal file
@@ -0,0 +1,55 @@
|
||||
package integration
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
renamercmd "github.com/rogeecn/renamer/cmd"
|
||||
)
|
||||
|
||||
func TestInsertValidationConflictsBlockApply(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
tmp := t.TempDir()
|
||||
createInsertValidationFile(t, filepath.Join(tmp, "baseline.txt"))
|
||||
createInsertValidationFile(t, filepath.Join(tmp, "baseline_MARKED.txt"))
|
||||
|
||||
var out bytes.Buffer
|
||||
cmd := renamercmd.NewRootCommand()
|
||||
cmd.SetOut(&out)
|
||||
cmd.SetErr(&out)
|
||||
cmd.SetArgs([]string{"insert", "$", "_MARKED", "--yes", "--path", tmp})
|
||||
|
||||
if err := cmd.Execute(); err == nil {
|
||||
t.Fatalf("expected command to fail when conflicts present")
|
||||
}
|
||||
}
|
||||
|
||||
func TestInsertValidationInvalidPosition(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
tmp := t.TempDir()
|
||||
createInsertValidationFile(t, filepath.Join(tmp, "短.txt"))
|
||||
|
||||
var out bytes.Buffer
|
||||
cmd := renamercmd.NewRootCommand()
|
||||
cmd.SetOut(&out)
|
||||
cmd.SetErr(&out)
|
||||
cmd.SetArgs([]string{"insert", "50", "X", "--dry-run", "--path", tmp})
|
||||
|
||||
if err := cmd.Execute(); err == nil {
|
||||
t.Fatalf("expected invalid position to produce error")
|
||||
}
|
||||
}
|
||||
|
||||
func createInsertValidationFile(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("validation"), 0o644); err != nil {
|
||||
t.Fatalf("write file %s: %v", path, err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user