| 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 main |
| 15 |
|
| 16 |
import ( |
| 17 |
"bytes" |
| 18 |
"encoding/base64" |
| 19 |
"encoding/json" |
| 20 |
"fmt" |
| 21 |
"io" |
| 22 |
"io/fs" |
| 23 |
"log" |
| 24 |
"net/http" |
| 25 |
"os" |
| 26 |
"path/filepath" |
| 27 |
"regexp" |
| 28 |
"runtime" |
| 29 |
"strconv" |
| 30 |
"strings" |
| 31 |
"sync" |
| 32 |
"testing" |
| 33 |
"time" |
| 34 |
|
| 35 |
"github.com/bep/helpers/envhelpers" |
| 36 |
"github.com/gohugoio/hugo/commands" |
| 37 |
"github.com/gohugoio/hugo/common/hugio" |
| 38 |
"github.com/gohugoio/hugo/htesting" |
| 39 |
"github.com/gohugoio/hugo/hugofs" |
| 40 |
"github.com/rogpeppe/go-internal/testscript" |
| 41 |
) |
| 42 |
|
| 43 |
func TestCommands(t *testing.T) { |
| 44 |
p := commonTestScriptsParam |
| 45 |
p.Dir = "testscripts/commands" |
| 46 |
// p.UpdateScripts = true |
| 47 |
testscript.Run(t, p) |
| 48 |
} |
| 49 |
|
| 50 |
func TestServer(t *testing.T) { |
| 51 |
// See issue #14439 |
| 52 |
htesting.SkipSlowTestUnlessCI(t) |
| 53 |
p := commonTestScriptsParam |
| 54 |
p.Dir = "testscripts/server" |
| 55 |
testscript.Run(t, p) |
| 56 |
} |
| 57 |
|
| 58 |
// Tests in development can be put in "testscripts/unfinished". |
| 59 |
// Also see the watch_testscripts.sh script. |
| 60 |
func TestUnfinished(t *testing.T) { |
| 61 |
if os.Getenv("CI") != "" { |
| 62 |
t.Skip("skip unfinished tests on CI") |
| 63 |
} |
| 64 |
|
| 65 |
p := commonTestScriptsParam |
| 66 |
p.Dir = "testscripts/unfinished" |
| 67 |
//p.UpdateScripts = true |
| 68 |
// p.TestWork = true |
| 69 |
|
| 70 |
testscript.Run(t, p) |
| 71 |
} |
| 72 |
|
| 73 |
func TestMain(m *testing.M) { |
| 74 |
testscript.Main(m, map[string]func(){ |
| 75 |
// The main program. |
| 76 |
"hugo": func() { |
| 77 |
err := commands.Execute(os.Args[1:]) |
| 78 |
if err != nil { |
| 79 |
fmt.Fprintln(os.Stderr, err) |
| 80 |
os.Exit(1) |
| 81 |
} |
| 82 |
}, |
| 83 |
}) |
| 84 |
} |
| 85 |
|
| 86 |
var isCaseInsensitiveFs = sync.OnceValues(func() (bool, error) { |
| 87 |
return htesting.IsCaseInsensitiveFs(os.TempDir()) |
| 88 |
}) |
| 89 |
|
| 90 |
var commonTestScriptsParam = testscript.Params{ |
| 91 |
Setup: func(env *testscript.Env) error { |
| 92 |
return testSetupFunc()(env) |
| 93 |
}, |
| 94 |
Condition: func(cond string) (bool, error) { |
| 95 |
switch cond { |
| 96 |
case "caseinsensitivefs": |
| 97 |
return isCaseInsensitiveFs() |
| 98 |
default: |
| 99 |
return false, fmt.Errorf("unknown condition %q", cond) |
| 100 |
} |
| 101 |
}, |
| 102 |
Cmds: map[string]func(ts *testscript.TestScript, neg bool, args []string){ |
| 103 |
// log prints to stderr. |
| 104 |
"log": func(ts *testscript.TestScript, neg bool, args []string) { |
| 105 |
log.Println(args) |
| 106 |
}, |
| 107 |
// cat prints a file to stdout. |
| 108 |
"cat": func(ts *testscript.TestScript, neg bool, args []string) { |
| 109 |
filename := ts.MkAbs(args[0]) |
| 110 |
b, err := os.ReadFile(filename) |
| 111 |
if err != nil { |
| 112 |
ts.Fatalf("%v", err) |
| 113 |
} |
| 114 |
fmt.Print(string(b)) |
| 115 |
}, |
| 116 |
// sleep sleeps for a second. |
| 117 |
"sleep": func(ts *testscript.TestScript, neg bool, args []string) { |
| 118 |
i := 1 |
| 119 |
if len(args) > 0 { |
| 120 |
var err error |
| 121 |
i, err = strconv.Atoi(args[0]) |
| 122 |
if err != nil { |
| 123 |
i = 1 |
| 124 |
} |
| 125 |
} |
| 126 |
time.Sleep(time.Duration(i) * time.Second) |
| 127 |
}, |
| 128 |
// tree lists a directory recursively to stdout as a simple tree. |
| 129 |
"tree": func(ts *testscript.TestScript, neg bool, args []string) { |
| 130 |
dirname := ts.MkAbs(args[0]) |
| 131 |
|
| 132 |
err := filepath.WalkDir(dirname, func(path string, d fs.DirEntry, err error) error { |
| 133 |
if err != nil { |
| 134 |
return err |
| 135 |
} |
| 136 |
rel, err := filepath.Rel(dirname, path) |
| 137 |
if err != nil { |
| 138 |
return err |
| 139 |
} |
| 140 |
if rel == "." { |
| 141 |
fmt.Fprintln(ts.Stdout(), ".") |
| 142 |
return nil |
| 143 |
} |
| 144 |
depth := strings.Count(rel, string(os.PathSeparator)) |
| 145 |
prefix := strings.Repeat(" ", depth) + "└─" |
| 146 |
if d.IsDir() { |
| 147 |
fmt.Fprintf(ts.Stdout(), "%s%s/\n", prefix, d.Name()) |
| 148 |
} else { |
| 149 |
fmt.Fprintf(ts.Stdout(), "%s%s\n", prefix, d.Name()) |
| 150 |
} |
| 151 |
return nil |
| 152 |
}) |
| 153 |
if err != nil { |
| 154 |
ts.Fatalf("%v", err) |
| 155 |
} |
| 156 |
}, |
| 157 |
// cpr copies a directory recursively to another location. |
| 158 |
"cpr": func(ts *testscript.TestScript, neg bool, args []string) { |
| 159 |
if err := hugio.CopyDir(hugofs.Os, ts.MkAbs(args[0]), ts.MkAbs(args[1]), nil); err != nil { |
| 160 |
ts.Fatalf("%v", err) |
| 161 |
} |
| 162 |
}, |
| 163 |
// ls lists a directory to stdout. |
| 164 |
"ls": func(ts *testscript.TestScript, neg bool, args []string) { |
| 165 |
dirname := ts.MkAbs(args[0]) |
| 166 |
|
| 167 |
dir, err := os.Open(dirname) |
| 168 |
if err != nil { |
| 169 |
ts.Fatalf("%v", err) |
| 170 |
} |
| 171 |
fis, err := dir.Readdir(-1) |
| 172 |
if err != nil { |
| 173 |
ts.Fatalf("%v", err) |
| 174 |
} |
| 175 |
if len(fis) == 0 { |
| 176 |
// To simplify empty dir checks. |
| 177 |
fmt.Fprintln(ts.Stdout(), "Empty dir") |
| 178 |
return |
| 179 |
} |
| 180 |
for _, fi := range fis { |
| 181 |
fmt.Fprintf(ts.Stdout(), "%s %04o %s %s\n", fi.Mode(), fi.Mode().Perm(), fi.ModTime().Format(time.RFC3339), fi.Name()) |
| 182 |
} |
| 183 |
}, |
| 184 |
// lsr lists a directory recursively to stdout. |
| 185 |
"lsr": func(ts *testscript.TestScript, neg bool, args []string) { |
| 186 |
dirname := ts.MkAbs(args[0]) |
| 187 |
|
| 188 |
err := filepath.WalkDir(dirname, func(path string, d fs.DirEntry, err error) error { |
| 189 |
if err != nil { |
| 190 |
return err |
| 191 |
} |
| 192 |
if d.IsDir() { |
| 193 |
return nil |
| 194 |
} |
| 195 |
|
| 196 |
fi, err := d.Info() |
| 197 |
if err != nil { |
| 198 |
return err |
| 199 |
} |
| 200 |
|
| 201 |
rel, err := filepath.Rel(dirname, path) |
| 202 |
if err != nil { |
| 203 |
return err |
| 204 |
} |
| 205 |
|
| 206 |
fmt.Fprintf(ts.Stdout(), "%s %04o %s\n", fi.Mode(), fi.Mode().Perm(), filepath.ToSlash(rel)) |
| 207 |
return nil |
| 208 |
}) |
| 209 |
if err != nil { |
| 210 |
ts.Fatalf("%v", err) |
| 211 |
} |
| 212 |
}, |
| 213 |
// append appends to a file with a leading newline. |
| 214 |
"append": func(ts *testscript.TestScript, neg bool, args []string) { |
| 215 |
if len(args) < 2 { |
| 216 |
ts.Fatalf("usage: append FILE TEXT") |
| 217 |
} |
| 218 |
|
| 219 |
filename := ts.MkAbs(args[0]) |
| 220 |
words := args[1:] |
| 221 |
for i, word := range words { |
| 222 |
words[i] = strings.Trim(word, "\"") |
| 223 |
} |
| 224 |
text := strings.Join(words, " ") |
| 225 |
|
| 226 |
_, err := os.Stat(filename) |
| 227 |
if err != nil { |
| 228 |
if os.IsNotExist(err) { |
| 229 |
ts.Fatalf("file does not exist: %s", filename) |
| 230 |
} |
| 231 |
ts.Fatalf("failed to stat file: %v", err) |
| 232 |
} |
| 233 |
|
| 234 |
f, err := os.OpenFile(filename, os.O_APPEND|os.O_WRONLY, 0o644) |
| 235 |
if err != nil { |
| 236 |
ts.Fatalf("failed to open file: %v", err) |
| 237 |
} |
| 238 |
defer f.Close() |
| 239 |
|
| 240 |
_, err = f.WriteString("\n" + text) |
| 241 |
if err != nil { |
| 242 |
ts.Fatalf("failed to write to file: %v", err) |
| 243 |
} |
| 244 |
}, |
| 245 |
// replace replaces a string in a file. |
| 246 |
"replace": func(ts *testscript.TestScript, neg bool, args []string) { |
| 247 |
if len(args) < 3 { |
| 248 |
ts.Fatalf("usage: replace FILE OLD NEW") |
| 249 |
} |
| 250 |
filename := ts.MkAbs(args[0]) |
| 251 |
oldContent, err := os.ReadFile(filename) |
| 252 |
if err != nil { |
| 253 |
ts.Fatalf("failed to read file %v", err) |
| 254 |
} |
| 255 |
old, new := args[1], args[2] |
| 256 |
|
| 257 |
newContent := bytes.Replace(oldContent, []byte(old), []byte(new), -1) |
| 258 |
err = os.WriteFile(filename, newContent, 0o644) |
| 259 |
if err != nil { |
| 260 |
ts.Fatalf("failed to write file: %v", err) |
| 261 |
} |
| 262 |
}, |
| 263 |
// ln creates a symlink, but throws an error on Windows. |
| 264 |
"ln": func(ts *testscript.TestScript, neg bool, args []string) { |
| 265 |
if runtime.GOOS == "windows" { |
| 266 |
ts.Fatalf("ln is not supported on Windows") |
| 267 |
} |
| 268 |
if len(args) != 2 { |
| 269 |
ts.Fatalf("usage: ln TARGET LINKNAME") |
| 270 |
} |
| 271 |
target := ts.MkAbs(args[0]) |
| 272 |
linkname := ts.MkAbs(args[1]) |
| 273 |
err := os.Symlink(target, linkname) |
| 274 |
if err != nil { |
| 275 |
ts.Fatalf("failed to create symlink: %v", err) |
| 276 |
} |
| 277 |
}, |
| 278 |
// base64decode decodes a base64-encoded file into a binary file. |
| 279 |
"base64decode": func(ts *testscript.TestScript, neg bool, args []string) { |
| 280 |
if len(args) != 2 { |
| 281 |
ts.Fatalf("usage: base64decode src_base64_file dest_binary_file") |
| 282 |
} |
| 283 |
|
| 284 |
// Resolve paths relative to the test sandbox's current directory |
| 285 |
src := ts.MkAbs(args[0]) |
| 286 |
dest := ts.MkAbs(args[1]) |
| 287 |
|
| 288 |
// Read the base64 text |
| 289 |
base64Data, err := os.ReadFile(src) |
| 290 |
if err != nil { |
| 291 |
ts.Fatalf("failed to read base64 file: %v", err) |
| 292 |
} |
| 293 |
|
| 294 |
// Decode base64 back to raw bytes. |
| 295 |
binaryBytes, err := base64.StdEncoding.DecodeString(strings.TrimSpace(string(base64Data))) |
| 296 |
if err != nil { |
| 297 |
ts.Fatalf("failed to decode base64 data: %v", err) |
| 298 |
} |
| 299 |
|
| 300 |
// Write the binary file. |
| 301 |
err = os.WriteFile(dest, binaryBytes, 0o644) |
| 302 |
if err != nil { |
| 303 |
ts.Fatalf("failed to write binary file: %v", err) |
| 304 |
} |
| 305 |
}, |
| 306 |
|
| 307 |
// httpget checks that a HTTP resource's body matches (if it compiles as a regexp) or contains all of the strings given as arguments. |
| 308 |
"httpget": func(ts *testscript.TestScript, neg bool, args []string) { |
| 309 |
if len(args) < 2 { |
| 310 |
ts.Fatalf("usage: httpget URL STRING...") |
| 311 |
} |
| 312 |
|
| 313 |
tryget := func() error { |
| 314 |
resp, err := http.Get(args[0]) |
| 315 |
if err != nil { |
| 316 |
return fmt.Errorf("failed to get URL %q: %v", args[0], err) |
| 317 |
} |
| 318 |
|
| 319 |
defer resp.Body.Close() |
| 320 |
body, err := io.ReadAll(resp.Body) |
| 321 |
if err != nil { |
| 322 |
return fmt.Errorf("failed to read response body: %v", err) |
| 323 |
} |
| 324 |
for _, s := range args[1:] { |
| 325 |
re, err := regexp.Compile(s) |
| 326 |
if err == nil { |
| 327 |
ok := re.Match(body) |
| 328 |
if ok != !neg { |
| 329 |
return fmt.Errorf("response body %q for URL %q does not match %q", body, args[0], s) |
| 330 |
} |
| 331 |
} else { |
| 332 |
ok := bytes.Contains(body, []byte(s)) |
| 333 |
if ok != !neg { |
| 334 |
return fmt.Errorf("response body %q for URL %q does not contain %q", body, args[0], s) |
| 335 |
} |
| 336 |
} |
| 337 |
} |
| 338 |
return nil |
| 339 |
} |
| 340 |
|
| 341 |
// The timing on server rebuilds can be a little tricky to get right, |
| 342 |
// so we try again a few times until the server is ready. |
| 343 |
// There may be smarter ways to do this, but this works. |
| 344 |
start := time.Now() |
| 345 |
for { |
| 346 |
time.Sleep(200 * time.Millisecond) |
| 347 |
err := tryget() |
| 348 |
if err == nil { |
| 349 |
return |
| 350 |
} |
| 351 |
if time.Since(start) > 6*time.Second { |
| 352 |
ts.Fatalf("timeout waiting for %q: %v", args[0], err) |
| 353 |
} |
| 354 |
} |
| 355 |
}, |
| 356 |
// checkfile checks that a file exists and is not empty. |
| 357 |
"checkfile": func(ts *testscript.TestScript, neg bool, args []string) { |
| 358 |
var readonly, exec bool |
| 359 |
loop: |
| 360 |
for len(args) > 0 { |
| 361 |
switch args[0] { |
| 362 |
case "-readonly": |
| 363 |
readonly = true |
| 364 |
args = args[1:] |
| 365 |
case "-exec": |
| 366 |
exec = true |
| 367 |
args = args[1:] |
| 368 |
default: |
| 369 |
break loop |
| 370 |
} |
| 371 |
} |
| 372 |
if len(args) == 0 { |
| 373 |
ts.Fatalf("usage: checkfile [-readonly] [-exec] file...") |
| 374 |
} |
| 375 |
|
| 376 |
for _, filename := range args { |
| 377 |
filename = ts.MkAbs(filename) |
| 378 |
fi, err := os.Stat(filename) |
| 379 |
ok := err == nil != neg |
| 380 |
if !ok { |
| 381 |
ts.Fatalf("stat %s: %v", filename, err) |
| 382 |
} |
| 383 |
if ok && neg { |
| 384 |
// OK. |
| 385 |
continue |
| 386 |
} |
| 387 |
if fi.Size() == 0 { |
| 388 |
ts.Fatalf("%s is empty", filename) |
| 389 |
} |
| 390 |
if readonly && fi.Mode()&0o222 != 0 { |
| 391 |
ts.Fatalf("%s is writable", filename) |
| 392 |
} |
| 393 |
if exec && runtime.GOOS != "windows" && fi.Mode()&0o111 == 0 { |
| 394 |
ts.Fatalf("%s is not executable", filename) |
| 395 |
} |
| 396 |
} |
| 397 |
}, |
| 398 |
|
| 399 |
// checkfilecount checks that the number of files in a directory is equal to the given count. |
| 400 |
"checkfilecount": func(ts *testscript.TestScript, neg bool, args []string) { |
| 401 |
if len(args) != 2 { |
| 402 |
ts.Fatalf("usage: checkfilecount count dir") |
| 403 |
} |
| 404 |
count, err := strconv.Atoi(args[0]) |
| 405 |
if err != nil { |
| 406 |
ts.Fatalf("invalid count: %v", err) |
| 407 |
} |
| 408 |
if count < 0 { |
| 409 |
ts.Fatalf("count must be non-negative") |
| 410 |
} |
| 411 |
dir := args[1] |
| 412 |
dir = ts.MkAbs(dir) |
| 413 |
|
| 414 |
found := 0 |
| 415 |
|
| 416 |
filepath.WalkDir(dir, func(path string, d fs.DirEntry, err error) error { |
| 417 |
if err != nil { |
| 418 |
return err |
| 419 |
} |
| 420 |
if d.IsDir() { |
| 421 |
return nil |
| 422 |
} |
| 423 |
found++ |
| 424 |
return nil |
| 425 |
}) |
| 426 |
|
| 427 |
ok := found == count != neg |
| 428 |
if !ok { |
| 429 |
ts.Fatalf("found %d files, want %d", found, count) |
| 430 |
} |
| 431 |
}, |
| 432 |
// waitServer waits for the .ready file to be created by the server. |
| 433 |
"waitServer": func(ts *testscript.TestScript, neg bool, args []string) { |
| 434 |
type testInfo struct { |
| 435 |
BaseURLs []string |
| 436 |
} |
| 437 |
|
| 438 |
// The server will write a .ready file when ready. |
| 439 |
// We wait for that. |
| 440 |
readyFilename := ts.MkAbs(".ready") |
| 441 |
limit := time.Now().Add(10 * time.Second) |
| 442 |
for { |
| 443 |
_, err := os.Stat(readyFilename) |
| 444 |
if err != nil { |
| 445 |
time.Sleep(500 * time.Millisecond) |
| 446 |
if time.Now().After(limit) { |
| 447 |
ts.Fatalf("timeout waiting for .ready file") |
| 448 |
} |
| 449 |
continue |
| 450 |
} |
| 451 |
var info testInfo |
| 452 |
// Read the .ready file's JSON into info. |
| 453 |
f, err := os.Open(readyFilename) |
| 454 |
if err != nil { |
| 455 |
ts.Fatalf("failed to open .ready file: %v", err) |
| 456 |
} |
| 457 |
err = json.NewDecoder(f).Decode(&info) |
| 458 |
if err != nil { |
| 459 |
ts.Fatalf("error decoding json: %v", err) |
| 460 |
} |
| 461 |
f.Close() |
| 462 |
|
| 463 |
for i, s := range info.BaseURLs { |
| 464 |
ts.Setenv(fmt.Sprintf("HUGOTEST_BASEURL_%d", i), s) |
| 465 |
} |
| 466 |
|
| 467 |
return |
| 468 |
} |
| 469 |
}, |
| 470 |
"stopServer": func(ts *testscript.TestScript, neg bool, args []string) { |
| 471 |
baseURL := ts.Getenv("HUGOTEST_BASEURL_0") |
| 472 |
if baseURL == "" { |
| 473 |
ts.Fatalf("HUGOTEST_BASEURL_0 not set") |
| 474 |
} |
| 475 |
if !strings.HasSuffix(baseURL, "/") { |
| 476 |
baseURL += "/" |
| 477 |
} |
| 478 |
resp, err := http.Head(baseURL + "__stop") |
| 479 |
if err != nil { |
| 480 |
ts.Fatalf("failed to shutdown server: %v", err) |
| 481 |
} |
| 482 |
resp.Body.Close() |
| 483 |
// Allow some time for the server to shut down. |
| 484 |
time.Sleep(2 * time.Second) |
| 485 |
}, |
| 486 |
}, |
| 487 |
} |
| 488 |
|
| 489 |
func testSetupFunc() func(env *testscript.Env) error { |
| 490 |
sourceDir, _ := os.Getwd() |
| 491 |
return func(env *testscript.Env) error { |
| 492 |
var keyVals []string |
| 493 |
keyVals = append(keyVals, "HUGO_TESTRUN", "true") |
| 494 |
keyVals = append(keyVals, "HUGO_CACHEDIR", filepath.Join(env.WorkDir, "hugocache")) |
| 495 |
xdghome := filepath.Join(env.WorkDir, "xdgcachehome") |
| 496 |
keyVals = append(keyVals, "XDG_CACHE_HOME", xdghome) |
| 497 |
home := filepath.Join(env.WorkDir, "home") |
| 498 |
keyVals = append(keyVals, "HOME", home) |
| 499 |
|
| 500 |
if runtime.GOOS == "darwin" { |
| 501 |
if err := os.MkdirAll(filepath.Join(home, "Library", "Caches"), 0o777); err != nil { |
| 502 |
return err |
| 503 |
} |
| 504 |
} |
| 505 |
|
| 506 |
if runtime.GOOS == "linux" { |
| 507 |
if err := os.MkdirAll(xdghome, 0o777); err != nil { |
| 508 |
return err |
| 509 |
} |
| 510 |
} |
| 511 |
|
| 512 |
keyVals = append(keyVals, "SOURCE", sourceDir) |
| 513 |
|
| 514 |
goVersion := runtime.Version() |
| 515 |
|
| 516 |
goVersion = strings.TrimPrefix(goVersion, "go") |
| 517 |
if strings.HasPrefix(goVersion, "1.20") { |
| 518 |
// Strip patch version. |
| 519 |
goVersion = goVersion[:strings.LastIndex(goVersion, ".")] |
| 520 |
} |
| 521 |
|
| 522 |
goModVersion := goVersion |
| 523 |
// From Go 1.26.0 on, the version used in go.mod on go mod init is the current version minus one. |
| 524 |
// This was reverted in Go 1.26.1. |
| 525 |
if goVersion == "1.26.0" { |
| 526 |
goModVersion = "1.25.0" |
| 527 |
} |
| 528 |
|
| 529 |
keyVals = append(keyVals, "GOVERSION", goVersion) |
| 530 |
keyVals = append(keyVals, "GOMODVERSION", goModVersion) |
| 531 |
envhelpers.SetEnvVars(&env.Vars, keyVals...) |
| 532 |
|
| 533 |
return nil |
| 534 |
} |
| 535 |
} |