2019-11-06 14:12:33 +00:00
|
|
|
package io
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
2021-11-17 11:14:22 +00:00
|
|
|
"path/filepath"
|
2019-11-06 14:12:33 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestMakeDirForFile_HappyPath(t *testing.T) {
|
2021-08-25 19:17:37 +00:00
|
|
|
tempDir := t.TempDir()
|
2021-11-17 11:14:22 +00:00
|
|
|
filePath := filepath.Join(tempDir, "testDir", "testFile.test")
|
2021-08-25 19:17:37 +00:00
|
|
|
err := MakeDirForFile(filePath, "test")
|
2019-11-06 14:12:33 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-11-17 13:42:09 +00:00
|
|
|
f, errChDir := os.Create(filePath)
|
2019-11-06 14:12:33 +00:00
|
|
|
require.NoError(t, errChDir)
|
2021-11-17 13:42:09 +00:00
|
|
|
require.NoError(t, f.Close())
|
2019-11-06 14:12:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestMakeDirForFile_Negative(t *testing.T) {
|
2021-08-25 19:17:37 +00:00
|
|
|
tempDir := t.TempDir()
|
2021-11-17 11:14:22 +00:00
|
|
|
filePath := filepath.Join(tempDir, "testFile.test")
|
2021-11-17 13:42:09 +00:00
|
|
|
f, err := os.Create(filePath)
|
2019-11-06 14:12:33 +00:00
|
|
|
require.NoError(t, err)
|
2021-11-17 13:42:09 +00:00
|
|
|
require.NoError(t, f.Close())
|
2019-11-06 14:12:33 +00:00
|
|
|
|
2021-11-17 11:14:22 +00:00
|
|
|
filePath = filepath.Join(filePath, "error")
|
2019-11-06 14:12:33 +00:00
|
|
|
err = MakeDirForFile(filePath, "test")
|
|
|
|
require.Errorf(t, err, "could not create dir for test: mkdir %s : not a directory", filePath)
|
|
|
|
}
|