Add regex command implementation
This commit is contained in:
8
tests/integration/helpers_test.go
Normal file
8
tests/integration/helpers_test.go
Normal file
@@ -0,0 +1,8 @@
|
||||
package integration
|
||||
|
||||
import "os"
|
||||
|
||||
func fileExistsTestHelper(path string) bool {
|
||||
_, err := os.Stat(path)
|
||||
return err == nil
|
||||
}
|
||||
26
tests/integration/regex_conflict_test.go
Normal file
26
tests/integration/regex_conflict_test.go
Normal file
@@ -0,0 +1,26 @@
|
||||
package integration
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"testing"
|
||||
|
||||
renamercmd "github.com/rogeecn/renamer/cmd"
|
||||
)
|
||||
|
||||
func TestRegexApplyBlocksConflicts(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
tmp := t.TempDir()
|
||||
copyRegexFixtureIntegration(t, "case-fold", tmp)
|
||||
|
||||
cmd := renamercmd.NewRootCommand()
|
||||
var out bytes.Buffer
|
||||
cmd.SetOut(&out)
|
||||
cmd.SetErr(&out)
|
||||
cmd.SetArgs([]string{"regex", "^(.*)$", "conflict", "--yes", "--path", tmp})
|
||||
|
||||
err := cmd.Execute()
|
||||
if err == nil {
|
||||
t.Fatalf("expected error when conflicts are present")
|
||||
}
|
||||
}
|
||||
74
tests/integration/regex_flow_test.go
Normal file
74
tests/integration/regex_flow_test.go
Normal file
@@ -0,0 +1,74 @@
|
||||
package integration
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io/fs"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
renamercmd "github.com/rogeecn/renamer/cmd"
|
||||
)
|
||||
|
||||
func TestRegexPreviewCommand(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
tmp := t.TempDir()
|
||||
copyRegexFixtureIntegration(t, "baseline", tmp)
|
||||
|
||||
var out bytes.Buffer
|
||||
cmd := renamercmd.NewRootCommand()
|
||||
cmd.SetOut(&out)
|
||||
cmd.SetErr(&out)
|
||||
cmd.SetArgs([]string{"regex", "^(\\w+)-(\\d+)", "@2_@1", "--dry-run", "--path", tmp})
|
||||
|
||||
if err := cmd.Execute(); err != nil {
|
||||
t.Fatalf("regex preview command failed: %v\noutput: %s", err, out.String())
|
||||
}
|
||||
|
||||
expected := []string{
|
||||
"alpha-123.log -> 123_alpha.log",
|
||||
"beta-456.log -> 456_beta.log",
|
||||
"gamma-789.log -> 789_gamma.log",
|
||||
"Preview complete: 3 matched, 3 changed, 0 skipped.",
|
||||
"Preview complete. Re-run with --yes to apply.",
|
||||
}
|
||||
|
||||
for _, token := range expected {
|
||||
if !bytes.Contains(out.Bytes(), []byte(token)) {
|
||||
t.Fatalf("expected output to contain %q, got: %s", token, out.String())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func copyRegexFixtureIntegration(t *testing.T, name, dest string) {
|
||||
t.Helper()
|
||||
src := filepath.Join("..", "fixtures", "regex", name)
|
||||
if err := filepath.WalkDir(src, func(path string, d fs.DirEntry, err error) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if d.IsDir() {
|
||||
return nil
|
||||
}
|
||||
|
||||
rel, err := filepath.Rel(src, path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
targetPath := filepath.Join(dest, rel)
|
||||
if err := os.MkdirAll(filepath.Dir(targetPath), 0o755); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
content, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return os.WriteFile(targetPath, content, 0o644)
|
||||
}); err != nil {
|
||||
t.Fatalf("copy regex fixture: %v", err)
|
||||
}
|
||||
}
|
||||
42
tests/integration/regex_undo_test.go
Normal file
42
tests/integration/regex_undo_test.go
Normal file
@@ -0,0 +1,42 @@
|
||||
package integration
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
renamercmd "github.com/rogeecn/renamer/cmd"
|
||||
)
|
||||
|
||||
func TestRegexUndoRestoresAutomationRun(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
tmp := t.TempDir()
|
||||
copyRegexFixtureIntegration(t, "mixed", tmp)
|
||||
|
||||
apply := renamercmd.NewRootCommand()
|
||||
var applyOut bytes.Buffer
|
||||
apply.SetOut(&applyOut)
|
||||
apply.SetErr(&applyOut)
|
||||
apply.SetArgs([]string{"regex", "^build_(\\d+)_(.*)$", "release-@1-@2", "--yes", "--path", tmp})
|
||||
if err := apply.Execute(); err != nil {
|
||||
t.Fatalf("regex apply failed: %v\noutput: %s", err, applyOut.String())
|
||||
}
|
||||
|
||||
if !fileExistsTestHelper(filepath.Join(tmp, "release-101-release.tar.gz")) || !fileExistsTestHelper(filepath.Join(tmp, "release-102-hotfix.tar.gz")) {
|
||||
t.Fatalf("expected renamed files 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 !fileExistsTestHelper(filepath.Join(tmp, "build_101_release.tar.gz")) || !fileExistsTestHelper(filepath.Join(tmp, "build_102_hotfix.tar.gz")) {
|
||||
t.Fatalf("expected originals restored after undo")
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,6 @@ package integration
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
@@ -33,7 +32,7 @@ func TestRemoveCommandAutomationUndo(t *testing.T) {
|
||||
t.Fatalf("apply failed: %v\noutput: %s", err, applyOut.String())
|
||||
}
|
||||
|
||||
if !fileExists(filepath.Join(tmp, "alpha.txt")) || !fileExists(filepath.Join(tmp, "nested", "beta.txt")) {
|
||||
if !fileExistsTestHelper(filepath.Join(tmp, "alpha.txt")) || !fileExistsTestHelper(filepath.Join(tmp, "nested", "beta.txt")) {
|
||||
t.Fatalf("expected files renamed after apply")
|
||||
}
|
||||
|
||||
@@ -46,12 +45,7 @@ func TestRemoveCommandAutomationUndo(t *testing.T) {
|
||||
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")) {
|
||||
if !fileExistsTestHelper(filepath.Join(tmp, "alpha copy.txt")) || !fileExistsTestHelper(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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user