From 154973241f65913b843544de044df153a23eab7e Mon Sep 17 00:00:00 2001 From: Megan Marsh Date: Tue, 9 Jan 2018 16:11:29 -0800 Subject: [PATCH] add a bunch of windows filepath tests --- common/config_test.go | 61 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/common/config_test.go b/common/config_test.go index 8bd684739..1a3920733 100644 --- a/common/config_test.go +++ b/common/config_test.go @@ -71,6 +71,67 @@ func TestDownloadableURL(t *testing.T) { } } +func TestDownloadableURL_WindowsFiles(t *testing.T) { + if runtime.GOOS == "windows" { + dirCases := []struct { + InputString string + OutputURL string + ErrExpected bool + }{ // TODO: add different directories + { + "C:\\Temp\\SomeDir\\myfile.txt", + "file:///C:/Temp/SomeDir/myfile.txt", + false, + }, + { // need windows drive + "\\Temp\\SomeDir\\myfile.txt", + "", + true, + }, + { // need windows drive + "/Temp/SomeDir/myfile.txt", + "", + true, + }, + { + "file:///C:\\Temp\\SomeDir\\myfile.txt", + "file:///c:/Temp/SomeDir/myfile.txt", + false, + }, + { + "file:///c:/Temp/Somedir/myfile.txt", + "file:///c:/Temp/SomeDir/myfile.txt", + false, + }, + } + // create absolute-pathed tempfile to play with + err := os.Mkdir("C:\\Temp\\SomeDir", 0755) + if err != nil { + t.Fatalf("err creating test dir: %s", err) + } + fi, err := os.Create("C:\\Temp\\SomeDir\\myfile.txt") + if err != nil { + t.Fatalf("err creating test file: %s", err) + } + fi.Close() + defer os.Remove("C:\\Temp\\SomeDir\\myfile.txt") + defer os.Remove("C:\\Temp\\SomeDir") + + // Run through test cases to make sure they all parse correctly + for _, tc := range dirCases { + u, err := DownloadableURL(tc.InputString) + if (err != nil) != tc.ErrExpected { + t.Fatalf("Test Case failed: Expected err = %#v, err = %#v, input = %s", + tc.ErrExpected, err, tc.InputString) + } + if u != tc.OutputURL { + t.Fatalf("Test Case failed: Expected %s but received %s from input %s", + tc.OutputURL, u, tc.InputString) + } + } + } +} + func TestDownloadableURL_FilePaths(t *testing.T) { tf, err := ioutil.TempFile("", "packer") if err != nil {