blob: b5b10df044fd402d36da8d725ff7af00ecdee471 [file] [log] [blame]
Russ Cox5aa761e2024-07-10 14:43:34 -04001// Copyright 2024 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
Tatiana Bradley20862ae2024-10-11 11:11:32 -04005package crawl
Russ Cox5aa761e2024-07-10 14:43:34 -04006
7import (
Russ Cox5aa761e2024-07-10 14:43:34 -04008 "os"
9 "testing"
10
Russ Cox5aa761e2024-07-10 14:43:34 -040011 "golang.org/x/oscar/internal/docs"
12 "golang.org/x/oscar/internal/storage"
13 "golang.org/x/oscar/internal/testutil"
14)
15
Tatiana Bradley20862ae2024-10-11 11:11:32 -040016func TestCrawlDocsSync(t *testing.T) {
Russ Cox5aa761e2024-07-10 14:43:34 -040017 check := testutil.Checker(t)
18 lg := testutil.Slogger(t)
19 db := storage.MemDB()
20
21 data, err := os.ReadFile("testdata/toolchain.html")
22 check(err)
Tatiana Bradley4fdbb612024-09-23 14:32:50 -040023 dc := docs.New(lg, db)
Tatiana Bradley20862ae2024-10-11 11:11:32 -040024 cr := New(lg, db, nil)
25 cr.Set(&Page{
Russ Cox5aa761e2024-07-10 14:43:34 -040026 URL: "https://go.dev/doc/toolchain",
27 HTML: data,
28 })
Tatiana Bradley20862ae2024-10-11 11:11:32 -040029 cr.Set(&Page{
Russ Cox5aa761e2024-07-10 14:43:34 -040030 URL: "https://go.dev/doc/empty",
31 HTML: nil,
32 })
33
Tatiana Bradley20862ae2024-10-11 11:11:32 -040034 docs.Sync(dc, cr)
Russ Cox5aa761e2024-07-10 14:43:34 -040035
36 var want = []string{
37 "https://go.dev/doc/toolchain#GOTOOLCHAIN",
38 "https://go.dev/doc/toolchain#config",
39 "https://go.dev/doc/toolchain#download",
40 "https://go.dev/doc/toolchain#get",
41 "https://go.dev/doc/toolchain#intro",
42 "https://go.dev/doc/toolchain#name",
43 "https://go.dev/doc/toolchain#select",
44 "https://go.dev/doc/toolchain#switch",
45 "https://go.dev/doc/toolchain#version",
46 "https://go.dev/doc/toolchain#work",
47 }
48 for d := range dc.Docs("") {
49 if len(want) == 0 {
50 t.Fatalf("unexpected extra doc: %s", d.ID)
51 }
52 if d.ID != want[0] {
53 t.Fatalf("doc mismatch: have %s, want %s", d.ID, want[0])
54 }
55 want = want[1:]
56 if d.ID == download {
57 if d.Title != downloadTitle {
58 t.Errorf("download Title = %q, want %q", d.Title, downloadTitle)
59 }
60 if d.Text != downloadText {
61 t.Errorf("download Text = %q, want %q", d.Text, downloadText)
62 }
63 }
64 }
65 if len(want) > 0 {
66 t.Fatalf("missing docs: %v", want)
67 }
68
69 dc.Add(download, "OLD TITLE", "OLD TEXT")
Tatiana Bradley20862ae2024-10-11 11:11:32 -040070 docs.Sync(dc, cr)
Russ Cox5aa761e2024-07-10 14:43:34 -040071 d, _ := dc.Get(download)
72 if d.Title != "OLD TITLE" || d.Text != "OLD TEXT" {
73 t.Errorf("Sync rewrote #download: Title=%q Text=%q, want OLD TITLE, OLD TEXT", d.Title, d.Text)
74 }
75
Tatiana Bradley20862ae2024-10-11 11:11:32 -040076 docs.Restart(cr)
77 docs.Sync(dc, cr)
Russ Cox5aa761e2024-07-10 14:43:34 -040078 d, _ = dc.Get(download)
79 if d.Title == "OLD TITLE" || d.Text == "OLD TEXT" {
80 t.Errorf("Restart+Sync did not rewrite #download: Title=%q Text=%q", d.Title, d.Text)
81 }
82}
83
84var (
85 download = "https://go.dev/doc/toolchain#download"
86 downloadTitle = "Go Toolchains > Downloading toolchains"
87 downloadText = "When using GOTOOLCHAIN=auto or GOTOOLCHAIN=<name>+auto, the Go command\ndownloads newer toolchains as needed.\nThese toolchains are packaged as special modules\nwith module path golang.org/toolchain\nand version v0.0.1-goVERSION.GOOS-GOARCH.\nToolchains are downloaded like any other module,\nmeaning that toolchain downloads can be proxied by setting GOPROXY\nand have their checksums checked by the Go checksum database.\nBecause the specific toolchain used depends on the system’s own\ndefault toolchain as well as the local operating system and architecture (GOOS and GOARCH),\nit is not practical to write toolchain module checksums to go.sum.\nInstead, toolchain downloads fail for lack of verification if GOSUMDB=off.\nGOPRIVATE and GONOSUMDB patterns do not apply to the toolchain downloads."
88)