Add insert command

This commit is contained in:
Rogee
2025-10-30 15:15:16 +08:00
parent 6a353b5086
commit a0d7084c28
35 changed files with 2044 additions and 37 deletions

View 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)
}
}

View 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)
}
}

View 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)
}
}