Skip to content

Commit 3fc4495

Browse files
authored
Merge pull request #31 from Automattic/fix/search-replace-with-no-trailing-newline
Don't ignore last line with no trailing newline
2 parents 4ffa519 + 1f537e3 commit 3fc4495

File tree

2 files changed

+94
-6
lines changed

2 files changed

+94
-6
lines changed

main_test.go

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package main
2+
3+
import (
4+
"bytes"
5+
"os/exec"
6+
"path/filepath"
7+
"runtime"
8+
"strings"
9+
"testing"
10+
)
11+
12+
var (
13+
_, b, _, _ = runtime.Caller(0)
14+
basePath = filepath.Dir(b)
15+
)
16+
17+
func doMainTest(t *testing.T, input string, expected string, mainArgs []string) {
18+
execArgs := append([]string{"run", basePath}, mainArgs...)
19+
cmd := exec.Command("go", execArgs...)
20+
21+
cmd.Stdin = strings.NewReader(input)
22+
var out bytes.Buffer
23+
cmd.Stdout = &out
24+
25+
if err := cmd.Run(); err != nil {
26+
t.Errorf("%v", err)
27+
}
28+
actual := out.String()
29+
30+
if actual != expected {
31+
t.Errorf("%v does not match expected: %v", actual, expected)
32+
}
33+
}
34+
35+
func TestSimpleReplaceWithNewlineAtEOF(t *testing.T) {
36+
mainArgs := []string{
37+
"http://uss-enterprise.com",
38+
"https://ncc-1701-d.space",
39+
}
40+
41+
input := "Space, the final frontier!\nCheck out: http://uss-enterprise.com/decks/10/sections/forward\n"
42+
expected := "Space, the final frontier!\nCheck out: https://ncc-1701-d.space/decks/10/sections/forward\n"
43+
doMainTest(t, input, expected, mainArgs)
44+
}
45+
46+
func TestSimpleReplaceWithoutNewlineAtEOF(t *testing.T) {
47+
mainArgs := []string{
48+
"http://uss-enterprise.com",
49+
"https://ncc-1701-d.space",
50+
}
51+
input := "I tend bar, and I listen.\nhttp://uss-enterprise.com/personnel/guinan"
52+
expected := "I tend bar, and I listen.\nhttps://ncc-1701-d.space/personnel/guinan"
53+
doMainTest(t, input, expected, mainArgs)
54+
}
55+
56+
func TestMultipleReplaceWithNewlineAtEOF(t *testing.T) {
57+
mainArgs := []string{
58+
"http://uss-enterprise.com",
59+
"https://ncc-1701-d.space",
60+
61+
"sections",
62+
"areas",
63+
64+
"https",
65+
"warp",
66+
}
67+
input := "Space, the final frontier!\nCheck out: http://uss-enterprise.com/decks/10/sections/forward\n"
68+
expected := "Space, the final frontier!\nCheck out: warp://ncc-1701-d.space/decks/10/areas/forward\n"
69+
doMainTest(t, input, expected, mainArgs)
70+
}
71+
72+
func TestMultipleReplaceWithoutNewlineAtEOF(t *testing.T) {
73+
mainArgs := []string{
74+
"http://uss-enterprise.com",
75+
"https://ncc-1701-d.space",
76+
77+
"sections",
78+
"areas",
79+
80+
"https",
81+
"warp",
82+
}
83+
input := "Space, the final frontier!\nCheck out: http://uss-enterprise.com/decks/10/sections/forward"
84+
expected := "Space, the final frontier!\nCheck out: warp://ncc-1701-d.space/decks/10/areas/forward"
85+
doMainTest(t, input, expected, mainArgs)
86+
}

search-replace.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -96,13 +96,15 @@ func main() {
9696
for {
9797
line, err := r.ReadBytes('\n')
9898

99-
if err == io.EOF {
100-
break
101-
}
102-
10399
if err != nil {
104-
fmt.Fprintln(os.Stderr, err.Error())
105-
break
100+
if err == io.EOF {
101+
if 0 == len(line) {
102+
break
103+
}
104+
} else {
105+
fmt.Fprintln(os.Stderr, err.Error())
106+
break
107+
}
106108
}
107109

108110
wg.Add(1)

0 commit comments

Comments
 (0)