Debian Codegraph @

modules/client_test.go

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
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 "bytes"
18 "fmt"
19 "os"
20 "path/filepath"
21 "sync/atomic"
22 "testing"
23
24 "github.com/gohugoio/hugo/common/hexec"
25 "github.com/gohugoio/hugo/common/loggers"
26 "github.com/gohugoio/hugo/config/security"
27 hglob "github.com/gohugoio/hugo/hugofs/hglob"
28
29 "github.com/gohugoio/hugo/htesting"
30
31 "github.com/gohugoio/hugo/hugofs"
32
33 qt "github.com/frankban/quicktest"
34 )
35
36 func TestClient(t *testing.T) {
37 htesting.SkipSlowTestUnlessCI(t)
38 modName := "hugo-modules-basic-test"
39 modPath := "github.com/gohugoio/tests/" + modName
40 defaultImport := "modh2_2"
41 expect := `github.com/gohugoio/tests/hugo-modules-basic-test github.com/gohugoio/hugoTestModules1_darwin/[email protected]
42 github.com/gohugoio/hugoTestModules1_darwin/[email protected] github.com/gohugoio/hugoTestModules1_darwin/[email protected]
43 github.com/gohugoio/hugoTestModules1_darwin/[email protected] github.com/gohugoio/hugoTestModules1_darwin/[email protected]
44 `
45
46 c := qt.New(t)
47 var clientID uint64 // we increment this to get each test in its own directory.
48
49 newClient := func(c *qt.C, withConfig func(cfg *ClientConfig), imp string) (*Client, func()) {
50 atomic.AddUint64(&clientID, uint64(1))
51 workingDir, clean, err := htesting.CreateTempDir(hugofs.Os, fmt.Sprintf("%s-%d", modName, clientID))
52 c.Assert(err, qt.IsNil)
53 themesDir := filepath.Join(workingDir, "themes")
54 err = os.Mkdir(themesDir, 0o777)
55 c.Assert(err, qt.IsNil)
56 publishDir := filepath.Join(workingDir, "public")
57 err = os.Mkdir(publishDir, 0o777)
58 c.Assert(err, qt.IsNil)
59
60 ccfg := ClientConfig{
61 Fs: hugofs.Os,
62 CacheDir: filepath.Join(workingDir, "modcache"),
63 WorkingDir: workingDir,
64 ThemesDir: themesDir,
65 PublishDir: publishDir,
66 Exec: hexec.New(security.DefaultConfig, "", loggers.NewDefault()),
67 }
68
69 withConfig(&ccfg)
70 ccfg.ModuleConfig.Imports = []Import{{Path: "github.com/gohugoio/hugoTestModules1_darwin/" + imp}}
71 client := NewClient(ccfg)
72
73 return client, clean
74 }
75
76 c.Run("All", func(c *qt.C) {
77 client, clean := newClient(c, func(cfg *ClientConfig) {
78 cfg.ModuleConfig = DefaultModuleConfig
79 }, defaultImport)
80 defer clean()
81
82 // Test Init
83 c.Assert(client.Init(modPath), qt.IsNil)
84
85 // Test Collect
86 mc, err := client.Collect()
87 c.Assert(err, qt.IsNil)
88 c.Assert(len(mc.AllModules), qt.Equals, 4)
89 for _, m := range mc.AllModules {
90 c.Assert(m, qt.Not(qt.IsNil))
91 }
92
93 // Test Graph
94 var graphb bytes.Buffer
95 c.Assert(client.Graph(&graphb), qt.IsNil)
96
97 c.Assert(graphb.String(), qt.Equals, expect)
98
99 // Test Vendor
100 c.Assert(client.Vendor(), qt.IsNil)
101 graphb.Reset()
102 c.Assert(client.Graph(&graphb), qt.IsNil)
103
104 expectVendored := `project github.com/gohugoio/hugoTestModules1_darwin/[email protected]+vendor
105 project github.com/gohugoio/hugoTestModules1_darwin/[email protected]+vendor
106 project github.com/gohugoio/hugoTestModules1_darwin/[email protected]+vendor
107 `
108
109 c.Assert(graphb.String(), qt.Equals, expectVendored)
110
111 // Test Tidy
112 c.Assert(client.Tidy(), qt.IsNil)
113 })
114
115 c.Run("IgnoreVendor", func(c *qt.C) {
116 client, clean := newClient(
117 c, func(cfg *ClientConfig) {
118 cfg.ModuleConfig = DefaultModuleConfig
119 cfg.IgnoreVendor = globAll
120 }, defaultImport)
121 defer clean()
122
123 c.Assert(client.Init(modPath), qt.IsNil)
124 _, err := client.Collect()
125 c.Assert(err, qt.IsNil)
126 c.Assert(client.Vendor(), qt.IsNil)
127
128 var graphb bytes.Buffer
129 c.Assert(client.Graph(&graphb), qt.IsNil)
130 c.Assert(graphb.String(), qt.Equals, expect)
131 })
132
133 c.Run("NoVendor", func(c *qt.C) {
134 mcfg := DefaultModuleConfig
135 mcfg.NoVendor = "**"
136 client, clean := newClient(
137 c, func(cfg *ClientConfig) {
138 cfg.ModuleConfig = mcfg
139 }, defaultImport)
140 defer clean()
141
142 c.Assert(client.Init(modPath), qt.IsNil)
143 _, err := client.Collect()
144 c.Assert(err, qt.IsNil)
145 c.Assert(client.Vendor(), qt.IsNil)
146
147 var graphb bytes.Buffer
148 c.Assert(client.Graph(&graphb), qt.IsNil)
149 c.Assert(graphb.String(), qt.Equals, expect)
150 })
151
152 c.Run("VendorClosest", func(c *qt.C) {
153 mcfg := DefaultModuleConfig
154 mcfg.VendorClosest = true
155
156 client, clean := newClient(
157 c, func(cfg *ClientConfig) {
158 cfg.ModuleConfig = mcfg
159 s := "github.com/gohugoio/hugoTestModules1_darwin/modh1_1v"
160 g, _ := hglob.GetGlob(s)
161 cfg.IgnoreVendor = g
162 }, "modh1v")
163 defer clean()
164
165 c.Assert(client.Init(modPath), qt.IsNil)
166 _, err := client.Collect()
167 c.Assert(err, qt.IsNil)
168 c.Assert(client.Vendor(), qt.IsNil)
169
170 var graphb bytes.Buffer
171 c.Assert(client.Graph(&graphb), qt.IsNil)
172
173 c.Assert(graphb.String(), qt.Contains, "github.com/gohugoio/hugoTestModules1_darwin/[email protected] github.com/gohugoio/hugoTestModules1_darwin/[email protected]+vendor")
174 })
175
176 // https://github.com/gohugoio/hugo/issues/7908
177 c.Run("createThemeDirname", func(c *qt.C) {
178 mcfg := DefaultModuleConfig
179 client, clean := newClient(
180 c, func(cfg *ClientConfig) {
181 cfg.ModuleConfig = mcfg
182 }, defaultImport)
183 defer clean()
184
185 dirname, err := client.createThemeDirname("foo", false)
186 c.Assert(err, qt.IsNil)
187 c.Assert(dirname, qt.Equals, filepath.Join(client.ccfg.ThemesDir, "foo"))
188
189 dirname, err = client.createThemeDirname("../../foo", true)
190 c.Assert(err, qt.IsNil)
191 c.Assert(dirname, qt.Equals, filepath.Join(client.ccfg.ThemesDir, "../../foo"))
192
193 _, err = client.createThemeDirname("../../foo", false)
194 c.Assert(err, qt.Not(qt.IsNil))
195
196 absDir := filepath.Join(client.ccfg.WorkingDir, "..", "..")
197 dirname, err = client.createThemeDirname(absDir, true)
198 c.Assert(err, qt.IsNil)
199 c.Assert(dirname, qt.Equals, absDir)
200 dirname, err = client.createThemeDirname(absDir, false)
201 fmt.Println(dirname)
202 c.Assert(err, qt.Not(qt.IsNil))
203 })
204 }
205
206 var globAll, _ = hglob.GetGlob("**")
207
208 func TestGetModlineSplitter(t *testing.T) {
209 c := qt.New(t)
210
211 gomodSplitter := getModlineSplitter(true)
212
213 c.Assert(gomodSplitter("require ("), qt.IsNil)
214 c.Assert(gomodSplitter("\tgithub.com/BurntSushi/toml v0.3.1"), qt.DeepEquals, []string{"github.com/BurntSushi/toml", "v0.3.1"})
215 c.Assert(gomodSplitter("\tgithub.com/cpuguy83/go-md2man v1.0.8 // indirect"), qt.DeepEquals, []string{"github.com/cpuguy83/go-md2man", "v1.0.8"})
216
217 gosumSplitter := getModlineSplitter(false)
218 c.Assert(gosumSplitter("github.com/BurntSushi/toml v0.3.1"), qt.DeepEquals, []string{"github.com/BurntSushi/toml", "v0.3.1"})
219 }
220
221 // Issue 14783
222 func TestGetModlineSplitterIgnoresToolBlockIssue14783(t *testing.T) {
223 c := qt.New(t)
224
225 gomodSplitter := getModlineSplitter(true)
226
227 c.Assert(gomodSplitter("tool ("), qt.IsNil)
228 c.Assert(gomodSplitter("\tgithub.com/gohugoio/hugo"), qt.IsNil)
229 c.Assert(gomodSplitter(")"), qt.IsNil)
230 c.Assert(gomodSplitter("require ("), qt.IsNil)
231 c.Assert(gomodSplitter("\tgithub.com/foo/bar v1.2.3"), qt.DeepEquals, []string{"github.com/foo/bar", "v1.2.3"})
232 }
233
234 func TestClientConfigToEnv(t *testing.T) {
235 c := qt.New(t)
236
237 ccfg := ClientConfig{
238 WorkingDir: "/mywork",
239 CacheDir: "/mycache",
240 }
241
242 env := ccfg.toEnv()
243
244 c.Assert(env, qt.DeepEquals, []string{"PWD=/mywork", "GO111MODULE=on", "GOFLAGS=-modcacherw", "GOPATH=/mycache", "GOWORK=", filepath.FromSlash("GOCACHE=/mycache/pkg/mod")})
245
246 ccfg = ClientConfig{
247 WorkingDir: "/mywork",
248 CacheDir: "/mycache",
249 ModuleConfig: Config{
250 Proxy: "https://proxy.example.org",
251 Private: "myprivate",
252 NoProxy: "mynoproxy",
253 Workspace: "myworkspace",
254 Auth: "myauth",
255 },
256 }
257
258 env = ccfg.toEnv()
259
260 c.Assert(env, qt.DeepEquals, []string{
261 "PWD=/mywork",
262 "GO111MODULE=on",
263 "GOFLAGS=-modcacherw",
264 "GOPATH=/mycache",
265 "GOWORK=myworkspace",
266 filepath.FromSlash("GOCACHE=/mycache/pkg/mod"),
267 "GOPROXY=https://proxy.example.org",
268 "GOPRIVATE=myprivate",
269 "GONOPROXY=mynoproxy",
270 "GOAUTH=myauth",
271 })
272 }