Debian Codegraph @

resources/resource/resources_test.go

raw
1 // Copyright 2024 The Hugo Authors. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13
14 package resource
15
16 import (
17 "testing"
18
19 qt "github.com/frankban/quicktest"
20 )
21
22 func TestResourcesMount(t *testing.T) {
23 c := qt.New(t)
24 c.Assert(true, qt.IsTrue)
25
26 var m ResourceGetter
27 var r Resources
28
29 check := func(in, expect string) {
30 c.Helper()
31 r := m.Get(in)
32 c.Assert(r, qt.Not(qt.IsNil))
33 c.Assert(r.Name(), qt.Equals, expect)
34 }
35
36 checkNil := func(in string) {
37 c.Helper()
38 r := m.Get(in)
39 c.Assert(r, qt.IsNil)
40 }
41
42 // Misc tests.
43 r = Resources{
44 testResource{name: "/foo/theme.css"},
45 }
46
47 m = r.Mount("/foo", ".")
48 check("./theme.css", "/foo/theme.css")
49
50 // Relative target.
51 r = Resources{
52 testResource{name: "/a/b/c/d.txt"},
53 testResource{name: "/a/b/c/e/f.txt"},
54 testResource{name: "/a/b/d.txt"},
55 testResource{name: "/a/b/e.txt"},
56 }
57
58 m = r.Mount("/a/b/c", "z")
59 check("z/d.txt", "/a/b/c/d.txt")
60 check("z/e/f.txt", "/a/b/c/e/f.txt")
61
62 m = r.Mount("/a/b", "")
63 check("d.txt", "/a/b/d.txt")
64 m = r.Mount("/a/b", ".")
65 check("d.txt", "/a/b/d.txt")
66 m = r.Mount("/a/b", "./")
67 check("d.txt", "/a/b/d.txt")
68 check("./d.txt", "/a/b/d.txt")
69
70 m = r.Mount("/a/b", ".")
71 check("./d.txt", "/a/b/d.txt")
72
73 // Absolute target.
74 m = r.Mount("/a/b/c", "/z")
75 check("/z/d.txt", "/a/b/c/d.txt")
76 check("/z/e/f.txt", "/a/b/c/e/f.txt")
77 checkNil("/z/f.txt")
78
79 m = r.Mount("/a/b", "/z")
80 check("/z/c/d.txt", "/a/b/c/d.txt")
81 check("/z/c/e/f.txt", "/a/b/c/e/f.txt")
82 check("/z/d.txt", "/a/b/d.txt")
83 checkNil("/z/f.txt")
84
85 m = r.Mount("", "")
86 check("/a/b/c/d.txt", "/a/b/c/d.txt")
87 check("/a/b/c/e/f.txt", "/a/b/c/e/f.txt")
88 check("/a/b/d.txt", "/a/b/d.txt")
89 checkNil("/a/b/f.txt")
90
91 m = r.Mount("/a/b", "/a/b")
92 check("/a/b/c/d.txt", "/a/b/c/d.txt")
93 check("/a/b/c/e/f.txt", "/a/b/c/e/f.txt")
94 check("/a/b/d.txt", "/a/b/d.txt")
95 checkNil("/a/b/f.txt")
96
97 // Resources with relative paths.
98 r = Resources{
99 testResource{name: "a/b/c/d.txt"},
100 testResource{name: "a/b/c/e/f.txt"},
101 testResource{name: "a/b/d.txt"},
102 testResource{name: "a/b/e.txt"},
103 testResource{name: "n.txt"},
104 }
105
106 m = r.Mount("a/b", "z")
107 check("z/d.txt", "a/b/d.txt")
108 checkNil("/z/d.txt")
109 }
110
111 type testResource struct {
112 Resource
113 name string
114 }
115
116 func (r testResource) Name() string {
117 return r.name
118 }
119
120 func (r testResource) NameNormalized() string {
121 return r.name
122 }