Add extension normalization command
This commit is contained in:
78
tests/integration/extension_flow_test.go
Normal file
78
tests/integration/extension_flow_test.go
Normal file
@@ -0,0 +1,78 @@
|
||||
package integration
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
renamercmd "github.com/rogeecn/renamer/cmd"
|
||||
)
|
||||
|
||||
func TestExtensionCommandFlow(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
tmp := t.TempDir()
|
||||
createFile(t, filepath.Join(tmp, "image.jpeg"))
|
||||
createFile(t, filepath.Join(tmp, "poster.JPG"))
|
||||
createFile(t, filepath.Join(tmp, "logo.jpg"))
|
||||
|
||||
var previewOut bytes.Buffer
|
||||
preview := renamercmd.NewRootCommand()
|
||||
preview.SetOut(&previewOut)
|
||||
preview.SetErr(&previewOut)
|
||||
preview.SetArgs([]string{"extension", ".jpeg", ".JPG", ".jpg", "--dry-run", "--path", tmp})
|
||||
|
||||
if err := preview.Execute(); err != nil {
|
||||
t.Fatalf("preview command failed: %v\noutput: %s", err, previewOut.String())
|
||||
}
|
||||
|
||||
output := previewOut.String()
|
||||
if !strings.Contains(output, "image.jpeg -> image.jpg") {
|
||||
t.Fatalf("expected preview output to include image rename, got:\n%s", output)
|
||||
}
|
||||
if !strings.Contains(output, "poster.JPG -> poster.jpg") {
|
||||
t.Fatalf("expected preview output to include poster rename, got:\n%s", output)
|
||||
}
|
||||
if !strings.Contains(output, "logo.jpg (no change)") {
|
||||
t.Fatalf("expected preview output to include no-change row for logo, got:\n%s", output)
|
||||
}
|
||||
|
||||
var applyOut bytes.Buffer
|
||||
apply := renamercmd.NewRootCommand()
|
||||
apply.SetOut(&applyOut)
|
||||
apply.SetErr(&applyOut)
|
||||
apply.SetArgs([]string{"extension", ".jpeg", ".JPG", ".jpg", "--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, "image.jpg")); err != nil {
|
||||
t.Fatalf("expected image.jpg after apply: %v", err)
|
||||
}
|
||||
if _, err := os.Stat(filepath.Join(tmp, "poster.jpg")); err != nil {
|
||||
t.Fatalf("expected poster.jpg after apply: %v", err)
|
||||
}
|
||||
if _, err := os.Stat(filepath.Join(tmp, "logo.jpg")); err != nil {
|
||||
t.Fatalf("expected logo.jpg to remain: %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, "image.jpeg")); err != nil {
|
||||
t.Fatalf("expected image.jpeg after undo: %v", err)
|
||||
}
|
||||
if _, err := os.Stat(filepath.Join(tmp, "poster.JPG")); err != nil {
|
||||
t.Fatalf("expected poster.JPG after undo: %v", err)
|
||||
}
|
||||
}
|
||||
52
tests/integration/extension_undo_test.go
Normal file
52
tests/integration/extension_undo_test.go
Normal file
@@ -0,0 +1,52 @@
|
||||
package integration
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
renamercmd "github.com/rogeecn/renamer/cmd"
|
||||
)
|
||||
|
||||
func TestExtensionAutomationUndo(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
tmp := t.TempDir()
|
||||
createFile(t, filepath.Join(tmp, "config.yaml"))
|
||||
createFile(t, filepath.Join(tmp, "notes.yml"))
|
||||
|
||||
var applyOut bytes.Buffer
|
||||
apply := renamercmd.NewRootCommand()
|
||||
apply.SetOut(&applyOut)
|
||||
apply.SetErr(&applyOut)
|
||||
apply.SetArgs([]string{"extension", ".yaml", ".yml", ".yml", "--yes", "--path", tmp})
|
||||
|
||||
if err := apply.Execute(); err != nil {
|
||||
t.Fatalf("automation apply failed: %v\noutput: %s", err, applyOut.String())
|
||||
}
|
||||
|
||||
if _, err := os.Stat(filepath.Join(tmp, "config.yml")); err != nil {
|
||||
t.Fatalf("expected config.yml after apply: %v", err)
|
||||
}
|
||||
if _, err := os.Stat(filepath.Join(tmp, "config.yaml")); !os.IsNotExist(err) {
|
||||
t.Fatalf("expected config.yaml renamed, err=%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 failed: %v\noutput: %s", err, undoOut.String())
|
||||
}
|
||||
|
||||
if _, err := os.Stat(filepath.Join(tmp, "config.yaml")); err != nil {
|
||||
t.Fatalf("expected config.yaml after undo: %v", err)
|
||||
}
|
||||
if _, err := os.Stat(filepath.Join(tmp, "config.yml")); !os.IsNotExist(err) {
|
||||
t.Fatalf("expected config.yml removed after undo, err=%v", err)
|
||||
}
|
||||
}
|
||||
42
tests/integration/extension_validation_test.go
Normal file
42
tests/integration/extension_validation_test.go
Normal file
@@ -0,0 +1,42 @@
|
||||
package integration
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
renamercmd "github.com/rogeecn/renamer/cmd"
|
||||
)
|
||||
|
||||
func TestExtensionCommandBlocksConflicts(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
tmp := t.TempDir()
|
||||
createFile(t, filepath.Join(tmp, "doc.jpeg"))
|
||||
createFile(t, filepath.Join(tmp, "doc.jpg"))
|
||||
|
||||
var out bytes.Buffer
|
||||
cmd := renamercmd.NewRootCommand()
|
||||
cmd.SetOut(&out)
|
||||
cmd.SetErr(&out)
|
||||
cmd.SetArgs([]string{"extension", ".jpeg", ".jpg", ".jpg", "--yes", "--path", tmp})
|
||||
|
||||
err := cmd.Execute()
|
||||
if err == nil {
|
||||
t.Fatalf("expected conflict to produce an error")
|
||||
}
|
||||
|
||||
if !strings.Contains(out.String(), "existing") && !strings.Contains(out.String(), "conflict") {
|
||||
t.Fatalf("expected conflict messaging in output, got: %s", out.String())
|
||||
}
|
||||
|
||||
// Ensure files unchanged after failed apply.
|
||||
if _, err := os.Stat(filepath.Join(tmp, "doc.jpeg")); err != nil {
|
||||
t.Fatalf("expected doc.jpeg to remain: %v", err)
|
||||
}
|
||||
if _, err := os.Stat(filepath.Join(tmp, "doc.jpg")); err != nil {
|
||||
t.Fatalf("expected doc.jpg to remain: %v", err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user