fix abs path with symlinks

Signed-off-by: Nestor <nesterran@gmail.com>
This commit is contained in:
Nestor
2019-02-22 15:08:26 +01:00
parent 70719a8f65
commit 1eab47b63f
3 changed files with 31 additions and 3 deletions

View File

@@ -26,7 +26,7 @@ import (
// that was confirmed to point to an existing directory.
type ConfirmedDir string
// Return a temporary dir, else error.
// NewTmpConfirmedDir returns a temporary dir, else error.
// The directory is cleaned, no symlinks, etc. so its
// returned as a ConfirmedDir.
func NewTmpConfirmedDir() (ConfirmedDir, error) {
@@ -34,7 +34,15 @@ func NewTmpConfirmedDir() (ConfirmedDir, error) {
if err != nil {
return "", err
}
return ConfirmedDir(n), nil
// In MacOs `ioutil.TempDir` creates a directory
// with root in the `/var` folder, which is in turn a symlinked path
// to `/private/var`.
// Function `filepath.EvalSymlinks`is used to
// resolve the real absolute path.
deLinked, err := filepath.EvalSymlinks(n)
return ConfirmedDir(deLinked), err
}
// HasPrefix returns true if the directory argument

View File

@@ -17,6 +17,7 @@ limitations under the License.
package fs
import (
"path/filepath"
"testing"
)
@@ -101,3 +102,18 @@ func TestHasPrefix_SlashFooBar(t *testing.T) {
t.Fatalf("/foo/bar should have prefix /")
}
}
func TestNewTempConfirmDir(t *testing.T) {
tmp, err := NewTmpConfirmedDir()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
delinked, err := filepath.EvalSymlinks(string(tmp))
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if string(tmp) != delinked {
t.Fatalf("unexpected path containing symlinks")
}
}

View File

@@ -27,7 +27,11 @@ import (
func makeTestDir(t *testing.T) (FileSystem, string) {
x := MakeRealFS()
testDir, err := ioutil.TempDir("", "kustomize_testing_dir")
td, err := ioutil.TempDir("", "kustomize_testing_dir")
if err != nil {
t.Fatalf("unexpected error %s", err)
}
testDir, err := filepath.EvalSymlinks(td)
if err != nil {
t.Fatalf("unexpected error %s", err)
}