File: lazy_file_test.go

package info (click to toggle)
golang-github-cheekybits-genny 1.0.0-8
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 336 kB
  • sloc: makefile: 17; sh: 3
file content (47 lines) | stat: -rw-r--r-- 1,026 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package out_test

import (
	"github.com/cheekybits/genny/out"
	"github.com/stretchr/testify/assert"
	"io/ioutil"
	"os"
	"testing"
)

const testFileName = "test-file.go"

func tearDown() {
	var err = os.Remove(testFileName)
	if err != nil && !os.IsNotExist(err) {
		panic("Could not delete test file")
	}
}

func assertFileContains(t *testing.T, expected string) {
	file, err := os.Open(testFileName)
	if err != nil {
		panic(err)
	}
	fileBytes, err := ioutil.ReadAll(file)
	if err != nil {
		panic(err)
	}
	assert.Equal(t, expected, string(fileBytes), "File contents not written properly")
}

func TestMultipleWrites(t *testing.T) {
	defer tearDown()
	lf := out.LazyFile{FileName: testFileName}
	defer lf.Close()
	lf.Write([]byte("Word1"))
	lf.Write([]byte("Word2"))
	assertFileContains(t, "Word1Word2")
}

func TestNoWrite(t *testing.T) {
	defer tearDown()
	lf := out.LazyFile{FileName: testFileName}
	defer lf.Close()
	_, err := os.Stat(testFileName)
	assert.True(t, os.IsNotExist(err), "Expected file not to be created")
}