github.com/adevinta/maiao@v0.0.0-20240318133227-b6f9656b5e07/pkg/system/filesystem_test_helpers.go (about)

     1  package system
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"os"
     7  	"strings"
     8  	"testing"
     9  
    10  	"github.com/spf13/afero"
    11  	"github.com/stretchr/testify/assert"
    12  	"github.com/stretchr/testify/require"
    13  )
    14  
    15  func AssertFileContents(t *testing.T, fs afero.Fs, path string, content string) bool {
    16  	t.Helper()
    17  	fd, err := fs.Open(path)
    18  	require.NoError(t, err)
    19  	bytes, err := ioutil.ReadAll(fd)
    20  	require.NoError(t, err)
    21  	return assert.Equal(t, content, string(bytes))
    22  }
    23  
    24  func AssertPathExists(t *testing.T, fs afero.Fs, path string, msgAndArgs ...interface{}) bool {
    25  	t.Helper()
    26  	_, err := fs.Stat(path)
    27  	if os.IsNotExist(err) {
    28  		return assert.Fail(t, fmt.Sprintf("unable to find file %q", path), msgAndArgs...)
    29  	}
    30  	return true
    31  }
    32  
    33  func AssertModePerm(t testing.TB, fs afero.Fs, path, mode string) bool {
    34  	t.Helper()
    35  	s, err := fs.Stat(path)
    36  	require.NoError(t, err)
    37  	return assert.Equal(t, mode, s.Mode().Perm().String())
    38  }
    39  
    40  func EnsureTestFileContent(t *testing.T, fs afero.Fs, path string, content string) {
    41  	require.NoError(t, EnsureFileContent(fs, path, strings.NewReader(content)))
    42  }