grafana/pkg/infra/fs/exists_test.go
Eng Zer Jun b56848f006
test: use T.TempDir to create temporary test directory (#44947)
The directory created by `T.TempDir` is automatically removed when the
test and all its subtests complete.

Reference: https://pkg.go.dev/testing#T.TempDir
Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
2022-03-22 15:43:29 +01:00

41 lines
671 B
Go

package fs
import (
"io/ioutil"
"os"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestExists_NonExistent(t *testing.T) {
exists, err := Exists("non-existent")
require.NoError(t, err)
require.False(t, exists)
}
func TestExists_Existent(t *testing.T) {
f, err := ioutil.TempFile("", "")
require.NoError(t, err)
t.Cleanup(func() {
err := os.Remove(f.Name())
assert.NoError(t, err)
})
exists, err := Exists(f.Name())
require.NoError(t, err)
require.True(t, exists)
}
func TestExists_Dir(t *testing.T) {
f := t.TempDir()
exists, err := Exists(f)
require.NoError(t, err)
require.True(t, exists)
}