File size: 2,010 Bytes
619f93d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package utils

import (
	"reflect"
	"testing"
)

func TestEncodePath(t *testing.T) {
	t.Log(EncodePath("http://localhost:5244/d/123#.png"))
}

func TestFixAndCleanPath(t *testing.T) {
	datas := map[string]string{
		"":                          "/",
		".././":                     "/",
		"../../.../":                "/...",
		"x//\\y/":                   "/x/y",
		".././.x/.y/.//..x../..y..": "/.x/.y/..x../..y..",
	}
	for key, value := range datas {
		if FixAndCleanPath(key) != value {
			t.Logf("raw %s fix fail", key)
		}
	}
}

func TestGetPathHierarchy(t *testing.T) {
	testCases := map[string][]string{
		"":                                    {"/"},
		"/":                                   {"/"},
		"/home":                               {"/", "/home"},
		"/home/user":                          {"/", "/home", "/home/user"},
		"/home/user/documents":                {"/", "/home", "/home/user", "/home/user/documents"},
		"/home/user/documents/files/test.txt": {"/", "/home", "/home/user", "/home/user/documents", "/home/user/documents/files", "/home/user/documents/files/test.txt"},
		"home":                                {"/", "/home"},
		"home/user":                           {"/", "/home", "/home/user"},
		"./home/":                             {"/", "/home"},
		"..//home//user/../././":              {"/", "/home"},
		"/home///user///documents///":         {"/", "/home", "/home/user", "/home/user/documents"},
		"/home/user with spaces/doc":          {"/", "/home", "/home/user with spaces", "/home/user with spaces/doc"},
		"/home/user@domain.com/files":         {"/", "/home", "/home/user@domain.com", "/home/user@domain.com/files"},
		"/home/.hidden/.config":               {"/", "/home", "/home/.hidden", "/home/.hidden/.config"},
	}

	for input, expected := range testCases {
		t.Run(input, func(t *testing.T) {
			result := GetPathHierarchy(input)
			if !reflect.DeepEqual(result, expected) {
				t.Errorf("GetPathHierarchy(%q) = %v, want %v", input, result, expected)
			}
		})
	}
}