template / windows / files / file_test.go
Branch windows · default branch · all branches
file_test.go
c3fc7a22 2022-10-30 Pavel Safronov | 1 | package main |
| 2 | ||
| 3 | import ( | |
| 4 | "os" | |
| 5 | "testing" | |
| 6 | ||
| 7 | "github.com/stretchr/testify/assert" | |
| 8 | ) | |
| 9 | ||
| 10 | func TestCreateFile(t *testing.T) { | |
| 11 | // settings | |
| 12 | path := "~/test/out/test.txt" | |
| 13 | prePatternData := testPrePatternData() | |
| 14 | expectedText := "test-project-2022-04-20-16-20-69-value1" | |
| 15 | ||
| 16 | // monkey patch | |
| 17 | applyTestMonkeyPatch(t, monkeyPatch{ | |
| 18 | OSWriteFile: func(filename string, data []byte, perm os.FileMode) error { | |
| 19 | assert.Equal(t, path, filename) | |
| 20 | assert.Equal(t, perm, os.ModePerm) | |
| 21 | assert.Equal(t, string(data), expectedText) | |
| 22 | return nil | |
| 23 | }, | |
| 24 | }) | |
| 25 | ||
| 26 | // test error path | |
| 27 | err := createFile(prePatternData, File{ | |
| 28 | Template: "{{.ProjectName", | |
| 29 | }, path) | |
| 30 | assert.NotNil(t, err) | |
| 31 | ||
| 32 | // test happy path | |
| 33 | err = createFile(prePatternData, File{ | |
| 34 | Template: "{{.ProjectName}}-{{.Year}}-{{.Month}}-{{.Day}}-{{.Hour}}-{{.Minute}}-{{.Second}}-{{.Extras.key1}}", | |
| 35 | }, path) | |
| 36 | assert.Nil(t, err) | |
| 37 | ||
| 38 | // test empty template | |
| 39 | expectedText = "" | |
| 40 | err = createFile(prePatternData, File{ | |
| 41 | Template: "", | |
| 42 | }, path) | |
| 43 | assert.Nil(t, err) | |
| 44 | } |