@@ -20,6 +20,7 @@ import (
2020 "os"
2121 "os/exec"
2222 "path/filepath"
23+ "regexp"
2324 "strings"
2425
2526 . "github.com/onsi/ginkgo" //nolint:golint
@@ -101,6 +102,22 @@ func ReplaceInFile(path, old, new string) {
101102 b , err := ioutil .ReadFile (path )
102103 ExpectWithOffset (1 , err ).NotTo (HaveOccurred ())
103104 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" )
104121 err = ioutil .WriteFile (path , []byte (s ), info .Mode ())
105122 ExpectWithOffset (1 , err ).NotTo (HaveOccurred ())
106123}
0 commit comments