@@ -20,6 +20,7 @@ import (
20
20
"os"
21
21
"os/exec"
22
22
"path/filepath"
23
+ "regexp"
23
24
"strings"
24
25
25
26
. "github.com/onsi/ginkgo" //nolint:golint
@@ -101,6 +102,22 @@ func ReplaceInFile(path, old, new string) {
101
102
b , err := ioutil .ReadFile (path )
102
103
ExpectWithOffset (1 , err ).NotTo (HaveOccurred ())
103
104
s := strings .Replace (string (b ), old , new , - 1 )
105
+ ExpectWithOffset (1 , s ).NotTo (Equal (string (b )), "No replacement occurred" )
106
+ err = ioutil .WriteFile (path , []byte (s ), info .Mode ())
107
+ ExpectWithOffset (1 , err ).NotTo (HaveOccurred ())
108
+ }
109
+
110
+ // ReplaceRegexInFile finds all strings that match `match` and replaces them
111
+ // with `replace` in the file at path.
112
+ func ReplaceRegexInFile (path , match , replace string ) {
113
+ matcher , err := regexp .Compile (match )
114
+ ExpectWithOffset (1 , err ).NotTo (HaveOccurred ())
115
+ info , err := os .Stat (path )
116
+ ExpectWithOffset (1 , err ).NotTo (HaveOccurred ())
117
+ b , err := ioutil .ReadFile (path )
118
+ ExpectWithOffset (1 , err ).NotTo (HaveOccurred ())
119
+ s := matcher .ReplaceAllString (string (b ), replace )
120
+ ExpectWithOffset (1 , s ).NotTo (Equal (string (b )), "No replacement occurred" )
104
121
err = ioutil .WriteFile (path , []byte (s ), info .Mode ())
105
122
ExpectWithOffset (1 , err ).NotTo (HaveOccurred ())
106
123
}
0 commit comments