func TestWriteFileAtomicDuplicateFile(t *testing.T) {
	var (
		defaultSeed    uint64 = 1
		testString            = "This is a glorious test string"
		expectedString        = "Did the test file's string appear here?"
		fileToWrite = "/tmp/TestWriteFileAtomicDuplicateFile-test.txt"
	)
	atomicWriteFileRand = defaultSeed
	firstFileRand := randWriteFileSuffix()
	atomicWriteFileRand = defaultSeed
	fname := "/tmp/" + atomicWriteFilePrefix + firstFileRand
	f, err := os.OpenFile(fname, atomicWriteFileFlag, 0777)
	defer os.Remove(fname)
	defer os.Remove(fileToWrite)
	require.NoError(t, err)
	_, err = f.WriteString(testString)
	require.NoError(t, err)
	err = WriteFileAtomic(fileToWrite, []byte(expectedString), 0777)
	require.NoError(t, err)
	firstAtomicFileBytes, err := ioutil.ReadFile(fname)
	require.NoError(t, err, "Error reading first atomic file")
	require.Equal(t, []byte(testString), firstAtomicFileBytes, "First atomic file was overwritten")
	resultantFileBytes, err := ioutil.ReadFile(fileToWrite)
	require.NoError(t, err, "Error reading resultant file")
	require.Equal(t, []byte(expectedString), resultantFileBytes, "Written file had incorrect bytes")
	atomicWriteFileRand = defaultSeed
	_ = randWriteFileSuffix()
	secondFileRand := randWriteFileSuffix()
	_, err = os.Stat("/tmp/" + atomicWriteFilePrefix + secondFileRand)
	require.True(t, os.IsNotExist(err), "Intermittent atomic write file not deleted")
}
This tests atomic write file when there is a single duplicate file.
Expected behavior is for a new file to be created, and the original write file to be unaltered.
package tempfile
import (
	"bytes"
	"fmt"
	"io/ioutil"
	mrand "math/rand"
	"os"
	testing "testing"
	"github.com/stretchr/testify/require"
	tmrand "github.com/tendermint/tendermint/libs/rand"
)
func TestWriteFileAtomic(t *testing.T) {
	var (
		data             = []byte(tmrand.Str(mrand.Intn(2048)))
		old              = tmrand.Bytes(mrand.Intn(2048))
		perm os.FileMode = 0600
	)
	f, err := ioutil.TempFile("/tmp", "write-atomic-test-")
	if err != nil {
		t.Fatal(err)
	}
	defer os.Remove(f.Name())
	if err = ioutil.WriteFile(f.Name(), old, 0600); err != nil {
		t.Fatal(err)
	}
	if err = WriteFileAtomic(f.Name(), data, perm); err != nil {
		t.Fatal(err)
	}
	rData, err := ioutil.ReadFile(f.Name())
	if err != nil {
		t.Fatal(err)
	}
	if !bytes.Equal(data, rData) {
		t.Fatalf("data mismatch: %v != %v", data, rData)
	}
	stat, err := os.Stat(f.Name())
	if err != nil {
		t.Fatal(err)
	}
	if have, want := stat.Mode().Perm(), perm; have != want {
		t.Errorf("have %v, want %v", have, want)
	}
}