Skip to content

Commit

Permalink
Fix #131: Don't fail with runtime panic on wrong path trimming. Warn …
Browse files Browse the repository at this point in the history
…instead.
  • Loading branch information
samuell committed May 25, 2021
1 parent beae23e commit 653669f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
11 changes: 9 additions & 2 deletions common.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,15 @@ func applyPathModifiers(path string, modifiers []string) string {
if trimEndPtn.MatchString(modifier) {
mbits := trimEndPtn.FindStringSubmatch(modifier)
end := mbits[1]
if end == replacement[len(replacement)-len(end):] {
replacement = replacement[:len(replacement)-len(end)]
startPos := len(replacement) - len(end)
if startPos > 0 {
if end == replacement[len(replacement)-len(end):] {
replacement = replacement[:len(replacement)-len(end)]
} else {
Warning.Printf("Trying to remove piece (%s) that was not found in original path (%s)\n", end, replacement)
}
} else {
Warning.Printf("Trying to remove piece (%s) that is larger than original path (%s)\n", end, replacement)
}
}

Expand Down
21 changes: 16 additions & 5 deletions task_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ import (
"testing"
)

func TestFormatCommand(t *testing.T) {

portInfos := map[string]*PortInfo{
func getTestPortInfos() map[string]*PortInfo {
return map[string]*PortInfo{
"foo": &PortInfo{
portType: "i",
extension: "",
Expand Down Expand Up @@ -39,14 +38,26 @@ func TestFormatCommand(t *testing.T) {
joinSep: "",
},
}
inIPs := map[string]*FileIP{
}

func getTestInIPs() map[string]*FileIP {
return map[string]*FileIP{
"foo": NewFileIP("data/foofile.txt"),
"bar": NewFileIP("barfile.txt"),
}
outIPs := map[string]*FileIP{
}

func getTestOutIPs() map[string]*FileIP {
return map[string]*FileIP{
"baz": NewFileIP("data/outfile.txt"),
"baa": NewFileIP("../../ref/ref.txt"),
}
}

func TestFormatCommand(t *testing.T) {
portInfos := getTestPortInfos()
inIPs := getTestInIPs()
outIPs := getTestOutIPs()

for _, tt := range []struct {
cmdPat string
Expand Down

0 comments on commit 653669f

Please sign in to comment.