Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

regexp option #46

Merged
merged 4 commits into from
Oct 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .snapshots/TestHelp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ Usage:

Application Options:
-b, --bare Use patterns as they are
-r, --regexp Use patterns as regular expressions
-c, --cases= Comma-separated names of enabled case styles (options:
camel, upper-camel, kebab, upper-kebab, snake, upper-snake,
space, upper-space)
Expand Down
1 change: 1 addition & 0 deletions argument_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const usage = "[options] <from> <to> [<path>]"

type arguments struct {
Bare bool `short:"b" long:"bare" description:"Use patterns as they are"`
Regexp bool `short:"r" long:"regexp" description:"Use patterns as regular expressions"`
RawCaseNames string `short:"c" long:"cases" description:"Comma-separated names of enabled case styles (options: camel, upper-camel, kebab, upper-kebab, snake, upper-snake, space, upper-space)"`
RawExclude string `short:"e" long:"exclude" description:"Exclude paths matched with the given regular expression"`
IgnoreGit bool `long:"ignore-git" description:"Ignore Git repository information"`
Expand Down
11 changes: 8 additions & 3 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,15 @@ func (c *command) Run(ss []string) error {
return nil
}

r := newBareTextRenamer(args.From, args.To)
r, err := newCaseTextRenamer(args.From, args.To, args.CaseNames)
if err != nil {
return err
}

if !args.Bare {
r, err = newCaseTextRenamer(args.From, args.To, args.CaseNames)
if args.Bare {
r = newBareTextRenamer(args.From, args.To)
} else if args.Regexp {
r, err = newRegexpTextRenamer(args.From, args.To)
if err != nil {
return err
}
Expand Down
20 changes: 19 additions & 1 deletion command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func TestCommandVersion(t *testing.T) {
)
}

func TestCommandRun(t *testing.T) {
func TestCommandRenameWithoutPathOption(t *testing.T) {
fs := memfs.New()
err := util.WriteFile(fs, "foo", []byte("foo"), 0o444)
assert.Nil(t, err)
Expand Down Expand Up @@ -196,3 +196,21 @@ func TestCommandRenameInDirectoryWithFileSpecified(t *testing.T) {

assert.Equal(t, "bar", string(bs))
}

func TestCommandRenameWithRegularExpression(t *testing.T) {
fs := memfs.New()

err := util.WriteFile(fs, "foo", []byte("foo"), 0o444)
assert.Nil(t, err)

err = newTestCommand(fs, ".").Run([]string{"-r", "(f.)o", "${1}e"})
assert.Nil(t, err)

f, err := fs.Open("foe")
assert.Nil(t, err)

bs, err := ioutil.ReadAll(f)
assert.Nil(t, err)

assert.Equal(t, "foe", string(bs))
}
2 changes: 1 addition & 1 deletion configuration.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package main

const (
version = "0.11.0"
version = "0.12.0"
errorChannelCapacity = 512
maxOpenFiles = 200 // lower than 256 on macOS by default
fileTypeDetectionBufferSize = 512
Expand Down
21 changes: 21 additions & 0 deletions regexp_text_renamer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package main

import "regexp"

type regexpTextRenamer struct {
from *regexp.Regexp
to string
}

func newRegexpTextRenamer(from, to string) (textRenamer, error) {
r, err := regexp.Compile(from)
if err != nil {
return nil, err
}

return &regexpTextRenamer{r, to}, nil
}

func (r *regexpTextRenamer) Rename(s string) string {
return r.from.ReplaceAllString(s, r.to)
}
33 changes: 33 additions & 0 deletions regexp_text_renamer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package main

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestRegexpRenamerRename(t *testing.T) {
r, err := newRegexpTextRenamer("foo", "bar")
assert.Nil(t, err)

assert.Equal(t, "bar", r.Rename("foo"))
}

func TestNewRegexpTextRenamerFail(t *testing.T) {
_, err := newRegexpTextRenamer("foo(", "bar(")
assert.NotNil(t, err)
}

func TestRegexpRenamerRenameWithMetaCharacter(t *testing.T) {
r, err := newRegexpTextRenamer("f.o", "bar")
assert.Nil(t, err)

assert.Equal(t, "bar", r.Rename("foo"))
}

func TestRegexpRenamerRenameWithPlaceholder(t *testing.T) {
r, err := newRegexpTextRenamer("foo", "($0)")
assert.Nil(t, err)

assert.Equal(t, "(foo)", r.Rename("foo"))
}