|
@ -6,10 +6,12 @@ import ( |
|
|
"io/ioutil" |
|
|
"io/ioutil" |
|
|
"os" |
|
|
"os" |
|
|
"os/exec" |
|
|
"os/exec" |
|
|
|
|
|
"path/filepath" |
|
|
"syscall" |
|
|
"syscall" |
|
|
"testing" |
|
|
"testing" |
|
|
"time" |
|
|
"time" |
|
|
|
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require" |
|
|
tmos "github.com/tendermint/tendermint/libs/os" |
|
|
tmos "github.com/tendermint/tendermint/libs/os" |
|
|
) |
|
|
) |
|
|
|
|
|
|
|
@ -67,6 +69,39 @@ func TestTrapSignal(t *testing.T) { |
|
|
t.Fatal("this error should not be triggered") |
|
|
t.Fatal("this error should not be triggered") |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func TestEnsureDir(t *testing.T) { |
|
|
|
|
|
tmp, err := ioutil.TempDir("", "ensure-dir") |
|
|
|
|
|
require.NoError(t, err) |
|
|
|
|
|
defer os.RemoveAll(tmp) |
|
|
|
|
|
|
|
|
|
|
|
// Should be possible to create a new directory.
|
|
|
|
|
|
err = tmos.EnsureDir(filepath.Join(tmp, "dir"), 0755) |
|
|
|
|
|
require.NoError(t, err) |
|
|
|
|
|
require.DirExists(t, filepath.Join(tmp, "dir")) |
|
|
|
|
|
|
|
|
|
|
|
// Should succeed on existing directory.
|
|
|
|
|
|
err = tmos.EnsureDir(filepath.Join(tmp, "dir"), 0755) |
|
|
|
|
|
require.NoError(t, err) |
|
|
|
|
|
|
|
|
|
|
|
// Should fail on file.
|
|
|
|
|
|
err = ioutil.WriteFile(filepath.Join(tmp, "file"), []byte{}, 0644) |
|
|
|
|
|
require.NoError(t, err) |
|
|
|
|
|
err = tmos.EnsureDir(filepath.Join(tmp, "file"), 0755) |
|
|
|
|
|
require.Error(t, err) |
|
|
|
|
|
|
|
|
|
|
|
// Should allow symlink to dir.
|
|
|
|
|
|
err = os.Symlink(filepath.Join(tmp, "dir"), filepath.Join(tmp, "linkdir")) |
|
|
|
|
|
require.NoError(t, err) |
|
|
|
|
|
err = tmos.EnsureDir(filepath.Join(tmp, "linkdir"), 0755) |
|
|
|
|
|
require.NoError(t, err) |
|
|
|
|
|
|
|
|
|
|
|
// Should error on symlink to file.
|
|
|
|
|
|
err = os.Symlink(filepath.Join(tmp, "file"), filepath.Join(tmp, "linkfile")) |
|
|
|
|
|
require.NoError(t, err) |
|
|
|
|
|
err = tmos.EnsureDir(filepath.Join(tmp, "linkfile"), 0755) |
|
|
|
|
|
require.Error(t, err) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
type mockLogger struct{} |
|
|
type mockLogger struct{} |
|
|
|
|
|
|
|
|
func (ml mockLogger) Info(msg string, keyvals ...interface{}) {} |
|
|
func (ml mockLogger) Info(msg string, keyvals ...interface{}) {} |
|
|