1
0
mirror of https://github.com/danog/gojekyll.git synced 2024-11-26 19:44:46 +01:00
gojekyll/utils/ioutil_test.go
2023-03-03 21:58:41 +01:00

70 lines
1.4 KiB
Go

package utils
import (
"io"
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/require"
)
var testDataDir = filepath.Join("testdata")
func testFile(name string) string {
return filepath.Join(testDataDir, name)
}
func TestCopyFileContents(t *testing.T) {
f, err := os.CreateTemp("", "ioutil-test")
if err != nil {
t.Fatal(err)
}
err = f.Close()
require.NoError(t, err)
err = CopyFileContents(f.Name(), testFile("test.txt"), 0x644)
require.NoError(t, err)
b, err := os.ReadFile(f.Name())
if err != nil {
t.Fatal(err)
}
require.Equal(t, "content\n", string(b))
err = CopyFileContents(f.Name(), testFile("missing.txt"), 0x644)
require.Error(t, err)
}
func TestReadFileMagic(t *testing.T) {
b, err := ReadFileMagic(testFile("test.txt"))
require.NoError(t, err)
require.Equal(t, "cont", string(b))
b, err = ReadFileMagic(testFile("empty.txt"))
require.NoError(t, err)
require.Equal(t, []byte{0, 0, 0, 0}, b)
_, err = ReadFileMagic(testFile("missing.txt"))
require.Error(t, err)
}
func TestVisitCreatedFile(t *testing.T) {
f, err := os.CreateTemp("", "ioutil-test")
if err != nil {
t.Fatal(err)
}
err = f.Close()
require.NoError(t, err)
err = VisitCreatedFile(f.Name(), func(w io.Writer) error {
_, e := io.WriteString(w, "test")
return e
})
require.NoError(t, err)
b, err := os.ReadFile(f.Name())
if err != nil {
t.Fatal(err)
}
require.Equal(t, "test", string(b))
}