2b1e5f69e2
* update go version to 1.18 for api and sdk, go mod tidy * removes ioutil usage everywhere which was deprecated in go1.16 in favour of io and os packages. Also introduces a lint rule which forbids use of ioutil going forward. Co-authored-by: R.B. Boyer <4903+rboyer@users.noreply.github.com>
31 lines
737 B
Go
31 lines
737 B
Go
package file
|
|
|
|
import (
|
|
"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) {
|
|
td, err := os.MkdirTemp("", "lib-file")
|
|
require.NoError(t, 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(t, WriteAtomic(path, expected))
|
|
|
|
// Read and verify
|
|
actual, err := os.ReadFile(path)
|
|
require.NoError(t, err)
|
|
require.Equal(t, expected, actual)
|
|
}
|