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