code.gitea.io/gitea@v1.22.3/modules/util/path_test.go (about)

     1  // Copyright 2021 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package util
     5  
     6  import (
     7  	"net/url"
     8  	"runtime"
     9  	"testing"
    10  
    11  	"github.com/stretchr/testify/assert"
    12  )
    13  
    14  func TestFileURLToPath(t *testing.T) {
    15  	cases := []struct {
    16  		url      string
    17  		expected string
    18  		haserror bool
    19  		windows  bool
    20  	}{
    21  		// case 0
    22  		{
    23  			url:      "",
    24  			haserror: true,
    25  		},
    26  		// case 1
    27  		{
    28  			url:      "http://test.io",
    29  			haserror: true,
    30  		},
    31  		// case 2
    32  		{
    33  			url:      "file:///path",
    34  			expected: "/path",
    35  		},
    36  		// case 3
    37  		{
    38  			url:      "file:///C:/path",
    39  			expected: "C:/path",
    40  			windows:  true,
    41  		},
    42  	}
    43  
    44  	for n, c := range cases {
    45  		if c.windows && runtime.GOOS != "windows" {
    46  			continue
    47  		}
    48  		u, _ := url.Parse(c.url)
    49  		p, err := FileURLToPath(u)
    50  		if c.haserror {
    51  			assert.Error(t, err, "case %d: should return error", n)
    52  		} else {
    53  			assert.NoError(t, err, "case %d: should not return error", n)
    54  			assert.Equal(t, c.expected, p, "case %d: should be equal", n)
    55  		}
    56  	}
    57  }
    58  
    59  func TestMisc_IsReadmeFileName(t *testing.T) {
    60  	trueTestCases := []string{
    61  		"readme",
    62  		"README",
    63  		"readME.mdown",
    64  		"README.md",
    65  		"readme.i18n.md",
    66  	}
    67  	falseTestCases := []string{
    68  		"test.md",
    69  		"wow.MARKDOWN",
    70  		"LOL.mDoWn",
    71  		"test",
    72  		"abcdefg",
    73  		"abcdefghijklmnopqrstuvwxyz",
    74  		"test.md.test",
    75  		"readmf",
    76  	}
    77  
    78  	for _, testCase := range trueTestCases {
    79  		assert.True(t, IsReadmeFileName(testCase))
    80  	}
    81  	for _, testCase := range falseTestCases {
    82  		assert.False(t, IsReadmeFileName(testCase))
    83  	}
    84  
    85  	type extensionTestcase struct {
    86  		name     string
    87  		expected bool
    88  		idx      int
    89  	}
    90  
    91  	exts := []string{".md", ".txt", ""}
    92  	testCasesExtensions := []extensionTestcase{
    93  		{
    94  			name:     "readme",
    95  			expected: true,
    96  			idx:      2,
    97  		},
    98  		{
    99  			name:     "readme.md",
   100  			expected: true,
   101  			idx:      0,
   102  		},
   103  		{
   104  			name:     "README.md",
   105  			expected: true,
   106  			idx:      0,
   107  		},
   108  		{
   109  			name:     "ReAdMe.Md",
   110  			expected: true,
   111  			idx:      0,
   112  		},
   113  		{
   114  			name:     "readme.txt",
   115  			expected: true,
   116  			idx:      1,
   117  		},
   118  		{
   119  			name:     "readme.doc",
   120  			expected: true,
   121  			idx:      3,
   122  		},
   123  		{
   124  			name: "readmee.md",
   125  		},
   126  		{
   127  			name:     "readme..",
   128  			expected: true,
   129  			idx:      3,
   130  		},
   131  	}
   132  
   133  	for _, testCase := range testCasesExtensions {
   134  		idx, ok := IsReadmeFileExtension(testCase.name, exts...)
   135  		assert.Equal(t, testCase.expected, ok)
   136  		assert.Equal(t, testCase.idx, idx)
   137  	}
   138  }
   139  
   140  func TestCleanPath(t *testing.T) {
   141  	cases := []struct {
   142  		elems    []string
   143  		expected string
   144  	}{
   145  		{[]string{}, ``},
   146  		{[]string{``}, ``},
   147  		{[]string{`..`}, `.`},
   148  		{[]string{`a`}, `a`},
   149  		{[]string{`/a/`}, `a`},
   150  		{[]string{`../a/`, `../b`, `c/..`, `d`}, `a/b/d`},
   151  		{[]string{`a\..\b`}, `a\..\b`},
   152  		{[]string{`a`, ``, `b`}, `a/b`},
   153  		{[]string{`a`, `..`, `b`}, `a/b`},
   154  		{[]string{`lfs`, `repo/..`, `user/../path`}, `lfs/path`},
   155  	}
   156  	for _, c := range cases {
   157  		assert.Equal(t, c.expected, PathJoinRel(c.elems...), "case: %v", c.elems)
   158  	}
   159  
   160  	cases = []struct {
   161  		elems    []string
   162  		expected string
   163  	}{
   164  		{[]string{}, ``},
   165  		{[]string{``}, ``},
   166  		{[]string{`..`}, `.`},
   167  		{[]string{`a`}, `a`},
   168  		{[]string{`/a/`}, `a`},
   169  		{[]string{`../a/`, `../b`, `c/..`, `d`}, `a/b/d`},
   170  		{[]string{`a\..\b`}, `b`},
   171  		{[]string{`a`, ``, `b`}, `a/b`},
   172  		{[]string{`a`, `..`, `b`}, `a/b`},
   173  		{[]string{`lfs`, `repo/..`, `user/../path`}, `lfs/path`},
   174  	}
   175  	for _, c := range cases {
   176  		assert.Equal(t, c.expected, PathJoinRelX(c.elems...), "case: %v", c.elems)
   177  	}
   178  
   179  	// for POSIX only, but the result is similar on Windows, because the first element must be an absolute path
   180  	if isOSWindows() {
   181  		cases = []struct {
   182  			elems    []string
   183  			expected string
   184  		}{
   185  			{[]string{`C:\..`}, `C:\`},
   186  			{[]string{`C:\a`}, `C:\a`},
   187  			{[]string{`C:\a/`}, `C:\a`},
   188  			{[]string{`C:\..\a\`, `../b`, `c\..`, `d`}, `C:\a\b\d`},
   189  			{[]string{`C:\a/..\b`}, `C:\b`},
   190  			{[]string{`C:\a`, ``, `b`}, `C:\a\b`},
   191  			{[]string{`C:\a`, `..`, `b`}, `C:\a\b`},
   192  			{[]string{`C:\lfs`, `repo/..`, `user/../path`}, `C:\lfs\path`},
   193  		}
   194  	} else {
   195  		cases = []struct {
   196  			elems    []string
   197  			expected string
   198  		}{
   199  			{[]string{`/..`}, `/`},
   200  			{[]string{`/a`}, `/a`},
   201  			{[]string{`/a/`}, `/a`},
   202  			{[]string{`/../a/`, `../b`, `c/..`, `d`}, `/a/b/d`},
   203  			{[]string{`/a\..\b`}, `/b`},
   204  			{[]string{`/a`, ``, `b`}, `/a/b`},
   205  			{[]string{`/a`, `..`, `b`}, `/a/b`},
   206  			{[]string{`/lfs`, `repo/..`, `user/../path`}, `/lfs/path`},
   207  		}
   208  	}
   209  	for _, c := range cases {
   210  		assert.Equal(t, c.expected, FilePathJoinAbs(c.elems[0], c.elems[1:]...), "case: %v", c.elems)
   211  	}
   212  }