github.com/buildpack/pack@v0.5.0/internal/paths/paths_test.go (about)

     1  package paths
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"path/filepath"
     7  	"runtime"
     8  	"testing"
     9  
    10  	"github.com/sclevine/spec"
    11  	"github.com/sclevine/spec/report"
    12  
    13  	h "github.com/buildpack/pack/testhelpers"
    14  )
    15  
    16  func TestPaths(t *testing.T) {
    17  	spec.Run(t, "Paths", testPaths, spec.Report(report.Terminal{}))
    18  }
    19  
    20  func testPaths(t *testing.T, when spec.G, it spec.S) {
    21  	when("#FilePathToURI", func() {
    22  		when("is windows", func() {
    23  			it.Before(func() {
    24  				h.SkipIf(t, runtime.GOOS != "windows", "Skipped on non-windows")
    25  			})
    26  
    27  			when("path is absolute", func() {
    28  				it("returns uri", func() {
    29  					uri, err := FilePathToURI(`C:\some\file.txt`)
    30  					h.AssertNil(t, err)
    31  					h.AssertEq(t, uri, `file:///C:/some/file.txt`)
    32  				})
    33  			})
    34  
    35  			when("path is relative", func() {
    36  				var (
    37  					err    error
    38  					ogDir  string
    39  					tmpDir string
    40  				)
    41  				it.Before(func() {
    42  					ogDir, err = os.Getwd()
    43  					h.AssertNil(t, err)
    44  
    45  					tmpDir = os.TempDir()
    46  
    47  					err = os.Chdir(tmpDir)
    48  					h.AssertNil(t, err)
    49  				})
    50  
    51  				it.After(func() {
    52  					err := os.Chdir(ogDir)
    53  					h.AssertNil(t, err)
    54  				})
    55  
    56  				it("returns uri", func() {
    57  					cwd, err := os.Getwd()
    58  					h.AssertNil(t, err)
    59  
    60  					uri, err := FilePathToURI(`some\file.tgz`)
    61  					h.AssertNil(t, err)
    62  
    63  					h.AssertEq(t, uri, fmt.Sprintf(`file:///%s/some/file.tgz`, filepath.ToSlash(cwd)))
    64  				})
    65  			})
    66  		})
    67  
    68  		when("is *nix", func() {
    69  			it.Before(func() {
    70  				h.SkipIf(t, runtime.GOOS == "windows", "Skipped on windows")
    71  			})
    72  
    73  			when("path is absolute", func() {
    74  				it("returns uri", func() {
    75  					uri, err := FilePathToURI("/tmp/file.tgz")
    76  					h.AssertNil(t, err)
    77  					h.AssertEq(t, uri, "file:///tmp/file.tgz")
    78  				})
    79  			})
    80  
    81  			when("path is relative", func() {
    82  				it("returns uri", func() {
    83  					h.SkipIf(t, runtime.GOOS == "windows", "Skipped on windows")
    84  
    85  					cwd, err := os.Getwd()
    86  					h.AssertNil(t, err)
    87  
    88  					uri, err := FilePathToURI("some/file.tgz")
    89  					h.AssertNil(t, err)
    90  
    91  					h.AssertEq(t, uri, fmt.Sprintf("file://%s/some/file.tgz", cwd))
    92  				})
    93  			})
    94  		})
    95  	})
    96  
    97  	when("#URIToFilePath", func() {
    98  		when("is windows", func() {
    99  			when("uri is drive", func() {
   100  				it("returns path", func() {
   101  					h.SkipIf(t, runtime.GOOS != "windows", "Skipped on non-windows")
   102  
   103  					path, err := URIToFilePath(`file:///c:/laptop/file.tgz`)
   104  					h.AssertNil(t, err)
   105  
   106  					h.AssertEq(t, path, `c:\laptop\file.tgz`)
   107  				})
   108  			})
   109  
   110  			when("uri is network share", func() {
   111  				it("returns path", func() {
   112  					h.SkipIf(t, runtime.GOOS != "windows", "Skipped on non-windows")
   113  
   114  					path, err := URIToFilePath(`file://laptop/file.tgz`)
   115  					h.AssertNil(t, err)
   116  
   117  					h.AssertEq(t, path, `\\laptop\file.tgz`)
   118  				})
   119  			})
   120  		})
   121  
   122  		when("is *nix", func() {
   123  			when("uri is valid", func() {
   124  				it("returns path", func() {
   125  					h.SkipIf(t, runtime.GOOS == "windows", "Skipped on windows")
   126  
   127  					path, err := URIToFilePath(`file:///tmp/file.tgz`)
   128  					h.AssertNil(t, err)
   129  
   130  					h.AssertEq(t, path, `/tmp/file.tgz`)
   131  				})
   132  			})
   133  		})
   134  	})
   135  }