Debian Codegraph @

modules/collect_test.go

raw
1 // Copyright 2019 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 modules
15
16 import (
17 "path/filepath"
18 "testing"
19
20 qt "github.com/frankban/quicktest"
21 "github.com/spf13/afero"
22 )
23
24 func TestPathKey(t *testing.T) {
25 c := qt.New(t)
26
27 for _, test := range []struct {
28 in string
29 expect string
30 }{
31 {"github.com/foo", "github.com/foo"},
32 {"github.com/foo/v2", "github.com/foo"},
33 {"github.com/foo/v12", "github.com/foo"},
34 {"github.com/foo/v3d", "github.com/foo/v3d"},
35 {"MyTheme", "mytheme"},
36 } {
37 c.Assert(pathBase(test.in), qt.Equals, test.expect)
38 }
39 }
40
41 func TestResolveWorkspacePattern(t *testing.T) {
42 c := qt.New(t)
43
44 fs := afero.NewMemMapFs()
45 root := filepath.FromSlash("/project/mod")
46
47 // Create workspace package.json files.
48 for _, dir := range []string{
49 "packages/aws1",
50 "packages/aws2",
51 "packages/hugoautogen",
52 "other/foo",
53 } {
54 p := filepath.Join(root, dir, "package.json")
55 c.Assert(afero.WriteFile(fs, p, []byte(`{}`), 0o644), qt.IsNil)
56 }
57
58 // Literal path, no glob.
59 c.Assert(ResolveWorkspacePattern(fs, root, "packages/aws1"), qt.DeepEquals, []string{"packages/aws1"})
60
61 got := ResolveWorkspacePattern(fs, root, "packages/*")
62 c.Assert(got, qt.DeepEquals, []string{
63 filepath.FromSlash("packages/aws1"),
64 filepath.FromSlash("packages/aws2"),
65 filepath.FromSlash("packages/hugoautogen"),
66 })
67
68 // We currently support only one level of globbing, so this should give the same result as above.
69 got = ResolveWorkspacePattern(fs, root, "packages/**")
70 c.Assert(got, qt.DeepEquals, []string{
71 filepath.FromSlash("packages/aws1"),
72 filepath.FromSlash("packages/aws2"),
73 filepath.FromSlash("packages/hugoautogen"),
74 })
75
76 got = ResolveWorkspacePattern(fs, root, "packages/{aws1,aws2}")
77 c.Assert(got, qt.DeepEquals, []string{
78 filepath.FromSlash("packages/aws1"),
79 filepath.FromSlash("packages/aws2"),
80 })
81
82 // ** matches recursively.
83 nestedDir := filepath.Join(root, "packages", "aws1", "sub", "package.json")
84 c.Assert(afero.WriteFile(fs, nestedDir, []byte(`{}`), 0o644), qt.IsNil)
85 got = ResolveWorkspacePattern(fs, root, "packages/**")
86 c.Assert(got, qt.DeepEquals, []string{
87 filepath.FromSlash("packages/aws1"),
88 filepath.FromSlash("packages/aws1/sub"),
89 filepath.FromSlash("packages/aws2"),
90 filepath.FromSlash("packages/hugoautogen"),
91 })
92
93 // No matches.
94 got = ResolveWorkspacePattern(fs, root, "nope/*")
95 c.Assert(got, qt.HasLen, 0)
96 }
97
98 func TestFilterUnwantedMounts(t *testing.T) {
99 mounts := []Mount{
100 {Source: "a", Target: "b", Lang: "en"},
101 {Source: "a", Target: "b", Lang: "en"},
102 {Source: "b", Target: "c", Lang: "en"},
103 }
104
105 filtered := filterDuplicateMounts(mounts)
106
107 c := qt.New(t)
108 c.Assert(len(filtered), qt.Equals, 2)
109 c.Assert(filtered, qt.DeepEquals, []Mount{{Source: "a", Target: "b", Lang: "en"}, {Source: "b", Target: "c", Lang: "en"}})
110 }