github.com/gitbundle/modules@v0.0.0-20231025071548-85b91c5c3b01/util/path_test.go (about)

     1  // Copyright 2023 The GitBundle Inc. All rights reserved.
     2  // Copyright 2017 The Gitea Authors. All rights reserved.
     3  // Use of this source code is governed by a MIT-style
     4  // license that can be found in the LICENSE file.
     5  
     6  package util
     7  
     8  import (
     9  	"net/url"
    10  	"runtime"
    11  	"testing"
    12  
    13  	"github.com/stretchr/testify/assert"
    14  )
    15  
    16  func TestFileURLToPath(t *testing.T) {
    17  	cases := []struct {
    18  		url      string
    19  		expected string
    20  		haserror bool
    21  		windows  bool
    22  	}{
    23  		// case 0
    24  		{
    25  			url:      "",
    26  			haserror: true,
    27  		},
    28  		// case 1
    29  		{
    30  			url:      "http://test.io",
    31  			haserror: true,
    32  		},
    33  		// case 2
    34  		{
    35  			url:      "file:///path",
    36  			expected: "/path",
    37  		},
    38  		// case 3
    39  		{
    40  			url:      "file:///C:/path",
    41  			expected: "C:/path",
    42  			windows:  true,
    43  		},
    44  	}
    45  
    46  	for n, c := range cases {
    47  		if c.windows && runtime.GOOS != "windows" {
    48  			continue
    49  		}
    50  		u, _ := url.Parse(c.url)
    51  		p, err := FileURLToPath(u)
    52  		if c.haserror {
    53  			assert.Error(t, err, "case %d: should return error", n)
    54  		} else {
    55  			assert.NoError(t, err, "case %d: should not return error", n)
    56  			assert.Equal(t, c.expected, p, "case %d: should be equal", n)
    57  		}
    58  	}
    59  }