mirror of
https://github.com/status-im/consul.git
synced 2025-01-22 11:40:06 +00:00
lib/file: add tests for WriteAtomic
This commit is contained in:
parent
2809203408
commit
e9b8e5d265
32
lib/file/atomic_test.go
Normal file
32
lib/file/atomic_test.go
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
package file
|
||||||
|
|
||||||
|
import (
|
||||||
|
"io/ioutil"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
)
|
||||||
|
|
||||||
|
// This doesn't really test the "atomic" part of this function. It really
|
||||||
|
// tests that it just writes the file properly. I would love to test this
|
||||||
|
// better but I'm not sure how. -mitchellh
|
||||||
|
func TestWriteAtomic(t *testing.T) {
|
||||||
|
require := require.New(t)
|
||||||
|
td, err := ioutil.TempDir("", "lib-file")
|
||||||
|
require.NoError(err)
|
||||||
|
defer os.RemoveAll(td)
|
||||||
|
|
||||||
|
// Create a subdir that doesn't exist to test that it is created
|
||||||
|
path := filepath.Join(td, "subdir", "file")
|
||||||
|
|
||||||
|
// Write
|
||||||
|
expected := []byte("hello")
|
||||||
|
require.NoError(WriteAtomic(path, expected))
|
||||||
|
|
||||||
|
// Read and verify
|
||||||
|
actual, err := ioutil.ReadFile(path)
|
||||||
|
require.NoError(err)
|
||||||
|
require.Equal(expected, actual)
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user