builder/common: make rel path absolute in DownloadableURL [GH-215]

This commit is contained in:
Mitchell Hashimoto 2013-07-29 00:06:21 -07:00
parent 8db5390553
commit c06c1fee45
2 changed files with 25 additions and 5 deletions

View File

@ -2,6 +2,7 @@ package common
import (
"fmt"
"path/filepath"
"github.com/mitchellh/mapstructure"
"github.com/mitchellh/packer/packer"
"net/url"
@ -75,6 +76,18 @@ func DownloadableURL(original string) (string, error) {
if _, err := os.Stat(url.Path); err != nil {
return "", err
}
url.Path, err = filepath.Abs(url.Path)
if err != nil {
return "", err
}
url.Path, err = filepath.EvalSymlinks(url.Path)
if err != nil {
return "", err
}
url.Path = filepath.Clean(url.Path)
}
// Make sure it is lowercased

View File

@ -98,6 +98,13 @@ func TestDownloadableURL_FilePaths(t *testing.T) {
defer os.Remove(tf.Name())
tf.Close()
tfPath, err := filepath.EvalSymlinks(tf.Name())
if err != nil {
t.Fatalf("tempfile err: %s", err)
}
tfPath = filepath.Clean(tfPath)
// Relative filepath. We run this test in a func so that
// the defers run right away.
func() {
@ -106,19 +113,19 @@ func TestDownloadableURL_FilePaths(t *testing.T) {
t.Fatalf("getwd err: %s", err)
}
err = os.Chdir(filepath.Dir(tf.Name()))
err = os.Chdir(filepath.Dir(tfPath))
if err != nil {
t.Fatalf("chdir err: %s", err)
}
defer os.Chdir(wd)
filename := filepath.Base(tf.Name())
filename := filepath.Base(tfPath)
u, err := DownloadableURL(filename)
if err != nil {
t.Fatalf("err: %s", err)
}
if u != fmt.Sprintf("file:///%s", filename) {
if u != fmt.Sprintf("file://%s", tfPath) {
t.Fatalf("unexpected: %s", u)
}
}()
@ -132,12 +139,12 @@ func TestDownloadableURL_FilePaths(t *testing.T) {
}
// Good file
u, err := DownloadableURL(prefix + tf.Name())
u, err := DownloadableURL(prefix + tfPath)
if err != nil {
t.Fatalf("err: %s", err)
}
if u != fmt.Sprintf("file://%s", tf.Name()) {
if u != fmt.Sprintf("file://%s", tfPath) {
t.Fatalf("unexpected: %s", u)
}
}