From e5510873bb8a10ba3742c6248fd1438c41105cb6 Mon Sep 17 00:00:00 2001 From: Taliesin Sisson Date: Fri, 1 Apr 2016 10:55:02 +0100 Subject: [PATCH] Added file with correct line endings When dealing with windows the file url format is file:///c:/ On windows a lot of git clients will convert LF to CRLF. This would be a problem where file contents are compared exactly --- common/config_test.go | 2 +- common/download_test.go | 10 +++++++++- common/iso_config.go | 9 ++++++++- common/iso_config_test.go | 9 +++++++-- 4 files changed, 25 insertions(+), 5 deletions(-) diff --git a/common/config_test.go b/common/config_test.go index 92a7316a3..8bd684739 100644 --- a/common/config_test.go +++ b/common/config_test.go @@ -120,7 +120,7 @@ func TestDownloadableURL_FilePaths(t *testing.T) { }() // Test some cases with and without a schema prefix - for _, prefix := range []string{"", "file://"} { + for _, prefix := range []string{"", filePrefix} { // Nonexistent file _, err = DownloadableURL(prefix + "i/dont/exist") if err != nil { diff --git a/common/download_test.go b/common/download_test.go index 51f6f270c..497a05649 100644 --- a/common/download_test.go +++ b/common/download_test.go @@ -8,6 +8,7 @@ import ( "net/http" "net/http/httptest" "os" + "runtime" "testing" ) @@ -56,6 +57,7 @@ func TestDownloadClient_basic(t *testing.T) { Url: ts.URL + "/basic.txt", TargetPath: tf.Name(), }) + path, err := client.Get() if err != nil { t.Fatalf("err: %s", err) @@ -352,7 +354,13 @@ func TestDownloadFileUrl(t *testing.T) { // source_path is a file path and source is a network path sourcePath := fmt.Sprintf("%s/test-fixtures/fileurl/%s", cwd, "cake") - source := fmt.Sprintf("file://" + sourcePath) + + filePrefix := "file://" + if runtime.GOOS == "windows" { + filePrefix += "/" + } + + source := fmt.Sprintf(filePrefix + sourcePath) t.Logf("Trying to download %s", source) config := &DownloadConfig{ diff --git a/common/iso_config.go b/common/iso_config.go index 655e92d98..49ee681ab 100644 --- a/common/iso_config.go +++ b/common/iso_config.go @@ -8,6 +8,7 @@ import ( "net/url" "os" "path/filepath" + "runtime" "strings" "github.com/mitchellh/packer/template/interpolate" @@ -77,7 +78,13 @@ func (c *ISOConfig) Prepare(ctx *interpolate.Context) (warnings []string, errs [ return warnings, errs } case "file": - file, err := os.Open(u.Path) + path := u.Path + + if runtime.GOOS == "windows" && len(path) > 2 && path[0] == '/' && path[2] == ':' { + path = strings.TrimLeft(path, "/") + } + + file, err := os.Open(path) if err != nil { errs = append(errs, err) return warnings, errs diff --git a/common/iso_config_test.go b/common/iso_config_test.go index 845e4e9dc..b64a00bcf 100644 --- a/common/iso_config_test.go +++ b/common/iso_config_test.go @@ -6,6 +6,7 @@ import ( "net/http" "net/http/httptest" "reflect" + "runtime" "testing" ) @@ -80,7 +81,11 @@ func TestISOConfigPrepare_ISOChecksumURL(t *testing.T) { i.ISOChecksum = "" cs_file, _ := ioutil.TempFile("", "packer-test-") ioutil.WriteFile(cs_file.Name(), []byte(cs_bsd_style), 0666) - i.ISOChecksumURL = fmt.Sprintf("file://%s", cs_file.Name()) + filePrefix := "file://" + if runtime.GOOS == "windows" { + filePrefix += "/" + } + i.ISOChecksumURL = fmt.Sprintf("%s%s", filePrefix, cs_file.Name()) warns, err = i.Prepare(nil) if len(warns) > 0 { t.Fatalf("bad: %#v", warns) @@ -98,7 +103,7 @@ func TestISOConfigPrepare_ISOChecksumURL(t *testing.T) { i.ISOChecksum = "" cs_file, _ = ioutil.TempFile("", "packer-test-") ioutil.WriteFile(cs_file.Name(), []byte(cs_gnu_style), 0666) - i.ISOChecksumURL = fmt.Sprintf("file://%s", cs_file.Name()) + i.ISOChecksumURL = fmt.Sprintf("%s%s", filePrefix, cs_file.Name()) warns, err = i.Prepare(nil) if len(warns) > 0 { t.Fatalf("bad: %#v", warns)