Skip to content

Commit

Permalink
Switch to using text/template instead of strings.Replace
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Mikusa authored and ForestEckhardt committed Apr 21, 2021
1 parent 01d612c commit e919823
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 23 deletions.
16 changes: 10 additions & 6 deletions mtimes/mtimes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"
"text/template"
"time"

"github.com/dmikusa/rust-cargo-cnb/mtimes"
Expand All @@ -21,8 +21,9 @@ import (

func testMTimes(t *testing.T, context spec.G, it spec.S) {
var (
Expect = NewWithT(t).Expect
workDir string
Expect = NewWithT(t).Expect
workDir string
mtimesTemplate *template.Template
)

it.Before(func() {
Expand All @@ -31,6 +32,9 @@ func testMTimes(t *testing.T, context spec.G, it spec.S) {
workDir, err = ioutil.TempDir("", "mtimes-test")
Expect(err).NotTo(HaveOccurred())

mtimesTemplate, err = template.New("mtimes.json").ParseFiles("testdata/mtimes.json")
Expect(err).ToNot(HaveOccurred())

// set up expected files
Expect(touch(filepath.Join(workDir, "testdata/folder1/file1a.txt"))).ToNot(HaveOccurred())
Expect(touch(filepath.Join(workDir, "testdata/folder1/file1b.txt"))).ToNot(HaveOccurred())
Expand Down Expand Up @@ -107,10 +111,10 @@ func testMTimes(t *testing.T, context spec.G, it spec.S) {
Expect(filepath.Join(workDir, "testdata", "folder1")).To(HaveMTime(originTime))

// copy test mtimes.json file
data, err := ioutil.ReadFile("testdata/mtimes.json")
var b bytes.Buffer
err := mtimesTemplate.Execute(&b, map[string]string{"WorkDir": workDir})
Expect(err).ToNot(HaveOccurred())
data = []byte(strings.ReplaceAll(string(data), "##workdir##", workDir))
err = ioutil.WriteFile(filepath.Join(workDir, "testdata/mtimes.json"), data, 0644)
err = ioutil.WriteFile(filepath.Join(workDir, "testdata/mtimes.json"), b.Bytes(), 0644)
Expect(err).ToNot(HaveOccurred())

preserver := mtimes.NewPreserver(scribe.NewEmitter(&logs))
Expand Down
34 changes: 17 additions & 17 deletions mtimes/testdata/mtimes.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
{"Path":"##workdir##/testdata","MTime":"2021-04-13T21:32:42.266625461Z"}
{"Path":"##workdir##/testdata/folder1","MTime":"2021-04-13T21:32:16.56220856Z"}
{"Path":"##workdir##/testdata/folder1/file1a.txt","MTime":"2021-04-13T21:32:11.619000841Z"}
{"Path":"##workdir##/testdata/folder1/file1b.txt","MTime":"2021-04-13T21:32:16.562185855Z"}
{"Path":"##workdir##/testdata/folder1/folder2","MTime":"2021-04-13T21:33:12.836115365Z"}
{"Path":"##workdir##/testdata/folder1/folder2/file2a.txt","MTime":"2021-04-13T21:32:24.132Z"}
{"Path":"##workdir##/testdata/folder1/folder2/file2b.txt","MTime":"2021-04-13T21:33:12.836100946Z"}
{"Path":"##workdir##/testdata/folder1/folder2/folder3","MTime":"2021-04-13T21:33:24.454010729Z"}
{"Path":"##workdir##/testdata/folder1/folder2/folder3/file3a.txt","MTime":"2021-04-13T21:33:21.115193516Z"}
{"Path":"##workdir##/testdata/folder1/folder2/folder3/file3b.txt","MTime":"2021-04-17T23:20:26.337053Z"}
{"Path":"##workdir##/testdata/foldera","MTime":"2021-04-13T21:31:49.712696151Z"}
{"Path":"##workdir##/testdata/foldera/filea1.txt","MTime":"2021-04-13T21:31:44.719991295Z"}
{"Path":"##workdir##/testdata/foldera/filea2.txt","MTime":"2021-04-13T21:31:49.712681414Z"}
{"Path":"##workdir##/testdata/foldera/folderb","MTime":"2021-04-13T21:31:36.645595542Z"}
{"Path":"##workdir##/testdata/foldera/folderb/fileb1.txt","MTime":"2021-04-13T21:31:36.645581261Z"}
{"Path":"##workdir##/testdata/foldera/folderb/folderc","MTime":"2021-04-13T21:31:27.300292894Z"}
{"Path":"##workdir##/testdata/foldera/folderb/folderc/filec1.txt","MTime":"2021-04-13T21:31:27.30028183Z"}
{"Path":"{{.WorkDir}}/testdata","MTime":"2021-04-13T21:32:42.266625461Z"}
{"Path":"{{.WorkDir}}/testdata/folder1","MTime":"2021-04-13T21:32:16.56220856Z"}
{"Path":"{{.WorkDir}}/testdata/folder1/file1a.txt","MTime":"2021-04-13T21:32:11.619000841Z"}
{"Path":"{{.WorkDir}}/testdata/folder1/file1b.txt","MTime":"2021-04-13T21:32:16.562185855Z"}
{"Path":"{{.WorkDir}}/testdata/folder1/folder2","MTime":"2021-04-13T21:33:12.836115365Z"}
{"Path":"{{.WorkDir}}/testdata/folder1/folder2/file2a.txt","MTime":"2021-04-13T21:32:24.132Z"}
{"Path":"{{.WorkDir}}/testdata/folder1/folder2/file2b.txt","MTime":"2021-04-13T21:33:12.836100946Z"}
{"Path":"{{.WorkDir}}/testdata/folder1/folder2/folder3","MTime":"2021-04-13T21:33:24.454010729Z"}
{"Path":"{{.WorkDir}}/testdata/folder1/folder2/folder3/file3a.txt","MTime":"2021-04-13T21:33:21.115193516Z"}
{"Path":"{{.WorkDir}}/testdata/folder1/folder2/folder3/file3b.txt","MTime":"2021-04-17T23:20:26.337053Z"}
{"Path":"{{.WorkDir}}/testdata/foldera","MTime":"2021-04-13T21:31:49.712696151Z"}
{"Path":"{{.WorkDir}}/testdata/foldera/filea1.txt","MTime":"2021-04-13T21:31:44.719991295Z"}
{"Path":"{{.WorkDir}}/testdata/foldera/filea2.txt","MTime":"2021-04-13T21:31:49.712681414Z"}
{"Path":"{{.WorkDir}}/testdata/foldera/folderb","MTime":"2021-04-13T21:31:36.645595542Z"}
{"Path":"{{.WorkDir}}/testdata/foldera/folderb/fileb1.txt","MTime":"2021-04-13T21:31:36.645581261Z"}
{"Path":"{{.WorkDir}}/testdata/foldera/folderb/folderc","MTime":"2021-04-13T21:31:27.300292894Z"}
{"Path":"{{.WorkDir}}/testdata/foldera/folderb/folderc/filec1.txt","MTime":"2021-04-13T21:31:27.30028183Z"}

0 comments on commit e919823

Please sign in to comment.