// Copyright (c) 2023-2024 Canonical Ltd
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

package atomicfile

import (
	"os"
	"path/filepath"
	"testing"

	"github.com/stretchr/testify/require"
)

func TestWriteFile(t *testing.T) {
	path := filepath.Join(t.TempDir(), t.Name())

	err := WriteFile(path, []byte("hello"), 0o640)
	require.NoError(t, err)

	expected := []byte("hello world")

	err = WriteFile(path, expected, 0o640)
	require.NoError(t, err)

	result, err := os.ReadFile(path)
	require.NoError(t, err)

	require.Equal(t, expected, result)

	fi, err := os.Stat(path)
	require.NoError(t, err)

	require.Equal(t, os.FileMode(0o640), fi.Mode())
}
