golang.org/x/build@v0.0.0-20240506185731-218518f32b70/cloudfns/sendwikidiff/sendwikidiff_test.go (about) 1 // Copyright 2019 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 5 package sendwikidiff 6 7 import ( 8 "context" 9 "os" 10 "os/exec" 11 "testing" 12 ) 13 14 func TestWikiPubSub(t *testing.T) { 15 if testing.Short() { 16 return 17 } 18 if _, err := exec.LookPath("git"); err != nil { 19 t.Skipf("git binary not available") 20 } 21 oldSendgridKey := sendgridAPIKey 22 sendgridAPIKey = "super secret key" 23 oldSendEmail := sendEmail 24 sendEmail = func(page, diff string) error { 25 return nil 26 } 27 defer func() { 28 sendgridAPIKey = oldSendgridKey 29 sendEmail = oldSendEmail 30 if err := os.RemoveAll(tempRepoDir); err != nil { 31 t.Errorf("Could not remove temp repo dir %q: %v", tempRepoDir, err) 32 } 33 }() 34 m := pubsubMessage{Data: []byte(`{ 35 "pages": [{ 36 "page_name": "CodeReviewComments", 37 "sha": "962830bfa92499e00a31572b0eeff9efdd68d374" 38 }] 39 } 40 `)} 41 if err := HandleWikiChangePubSub(context.Background(), m); err != nil { 42 t.Errorf("Unexpected error from HandleWikiChangePubSub: %v", err) 43 } 44 } 45 46 func TestEmailDiff(t *testing.T) { 47 want := `<p><a href="https://go.dev/wiki/CodeReviewComments">View page</a></p> 48 <pre style="font-family: monospace,monospace; white-space: pre-wrap;">Diff 49 50 Code</pre> 51 ` 52 got, err := emailBody("CodeReviewComments", "Diff\n\nCode") 53 if err != nil { 54 t.Fatalf("emailBody: got unexpected error: %v", err) 55 } 56 if got != want { 57 t.Errorf("Unexpected email body: got %q; want %q", got, want) 58 } 59 }