2019-05-11 05:07:36 +08:00
|
|
|
package env
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
2019-05-14 11:05:11 +08:00
|
|
|
"path/filepath"
|
2019-05-11 05:07:36 +08:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
homedir "github.com/mitchellh/go-homedir"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestShellExpand(t *testing.T) {
|
|
|
|
home, err := homedir.Dir()
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NoError(t, os.Setenv("EXPAND_TEST", "potato"))
|
|
|
|
defer func() {
|
|
|
|
require.NoError(t, os.Unsetenv("EXPAND_TEST"))
|
|
|
|
}()
|
|
|
|
for _, test := range []struct {
|
|
|
|
in, want string
|
|
|
|
}{
|
|
|
|
{"", ""},
|
2019-05-14 11:05:11 +08:00
|
|
|
{"~", filepath.FromSlash(home)},
|
|
|
|
{filepath.FromSlash("~/dir/file.txt"), filepath.FromSlash(home + "/dir/file.txt")},
|
|
|
|
{filepath.FromSlash("/dir/~/file.txt"), filepath.FromSlash("/dir/~/file.txt")},
|
|
|
|
{filepath.FromSlash("~/${EXPAND_TEST}"), filepath.FromSlash(home + "/potato")},
|
2019-05-11 05:07:36 +08:00
|
|
|
} {
|
|
|
|
got := ShellExpand(test.in)
|
|
|
|
assert.Equal(t, test.want, got, test.in)
|
|
|
|
}
|
|
|
|
}
|