github.com/lmb/consul@v1.4.1/lib/file/atomic_test.go (about)

     1  package file
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  	"path/filepath"
     7  	"testing"
     8  
     9  	"github.com/stretchr/testify/require"
    10  )
    11  
    12  // This doesn't really test the "atomic" part of this function. It really
    13  // tests that it just writes the file properly. I would love to test this
    14  // better but I'm not sure how. -mitchellh
    15  func TestWriteAtomic(t *testing.T) {
    16  	require := require.New(t)
    17  	td, err := ioutil.TempDir("", "lib-file")
    18  	require.NoError(err)
    19  	defer os.RemoveAll(td)
    20  
    21  	// Create a subdir that doesn't exist to test that it is created
    22  	path := filepath.Join(td, "subdir", "file")
    23  
    24  	// Write
    25  	expected := []byte("hello")
    26  	require.NoError(WriteAtomic(path, expected))
    27  
    28  	// Read and verify
    29  	actual, err := ioutil.ReadFile(path)
    30  	require.NoError(err)
    31  	require.Equal(expected, actual)
    32  }