1// Copyright 2020 Google LLC 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// 7// http://www.apache.org/licenses/LICENSE-2.0 8// 9// Unless required by applicable law or agreed to in writing, software 10// distributed under the License is distributed on an "AS IS" BASIS, 11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12// See the License for the specific language governing permissions and 13// limitations under the License. 14 15// +build go1.15 16 17package main 18 19import ( 20 "flag" 21 "io/ioutil" 22 "os" 23 "path/filepath" 24 "testing" 25 26 _ "cloud.google.com/go/storage" // Implicitly required by test. 27) 28 29var updateGoldens bool 30 31func TestMain(m *testing.M) { 32 flag.BoolVar(&updateGoldens, "update-goldens", false, "Update the golden files") 33 flag.Parse() 34 os.Exit(m.Run()) 35} 36 37func TestParse(t *testing.T) { 38 testPath := "cloud.google.com/go/storage" 39 pages, toc, module, err := parse(testPath) 40 if err != nil { 41 t.Fatalf("Parse: %v", err) 42 } 43 if got, want := len(toc), 1; got != want { 44 t.Fatalf("Parse got len(toc) = %d, want %d", got, want) 45 } 46 if got, want := len(pages), 1; got != want { 47 t.Errorf("Parse got len(pages) = %d, want %d", got, want) 48 } 49 if got := module.Path; got != testPath { 50 t.Fatalf("Parse got module = %q, want %q", got, testPath) 51 } 52 53 page := pages[testPath] 54 55 // Check invariants for every item. 56 for _, item := range page.Items { 57 if got := item.UID; got == "" { 58 t.Errorf("Parse found missing UID: %v", item) 59 } 60 61 if got, want := item.Langs, []string{"go"}; len(got) != 1 || got[0] != want[0] { 62 t.Errorf("Parse %v got langs = %v, want %v", item.UID, got, want) 63 } 64 } 65 66 // Check there is at least one type, const, variable, and function. 67 // Note: no method because they aren't printed for Namespaces yet. 68 wants := []string{"type", "const", "variable", "function"} 69 for _, want := range wants { 70 found := false 71 for _, c := range page.Items { 72 if c.Type == want { 73 found = true 74 break 75 } 76 } 77 if !found { 78 t.Errorf("Parse got no %q, want at least one", want) 79 } 80 } 81} 82 83func TestGoldens(t *testing.T) { 84 gotDir := "testdata/out" 85 goldenDir := "testdata/golden" 86 87 testPath := "cloud.google.com/go/storage" 88 pages, toc, module, err := parse(testPath) 89 if err != nil { 90 t.Fatalf("parse: %v", err) 91 } 92 93 ignoreFiles := map[string]bool{"docs.metadata": true} 94 95 if updateGoldens { 96 os.RemoveAll(goldenDir) 97 98 if err := write(goldenDir, pages, toc, module); err != nil { 99 t.Fatalf("write: %v", err) 100 } 101 102 for ignore := range ignoreFiles { 103 if err := os.Remove(filepath.Join(goldenDir, ignore)); err != nil { 104 t.Fatalf("Remove: %v", err) 105 } 106 } 107 108 t.Logf("Successfully updated goldens in %s", goldenDir) 109 110 return 111 } 112 113 if err := write(gotDir, pages, toc, module); err != nil { 114 t.Fatalf("write: %v", err) 115 } 116 117 gotFiles, err := ioutil.ReadDir(gotDir) 118 if err != nil { 119 t.Fatalf("ReadDir: %v", err) 120 } 121 122 goldens, err := ioutil.ReadDir(goldenDir) 123 if err != nil { 124 t.Fatalf("ReadDir: %v", err) 125 } 126 127 if got, want := len(gotFiles)-len(ignoreFiles), len(goldens); got != want { 128 t.Fatalf("parse & write got %d files in %s, want %d ignoring %v", got, gotDir, want, ignoreFiles) 129 } 130 131 for _, golden := range goldens { 132 gotPath := filepath.Join(gotDir, golden.Name()) 133 goldenPath := filepath.Join(goldenDir, golden.Name()) 134 135 gotContent, err := ioutil.ReadFile(gotPath) 136 if err != nil { 137 t.Fatalf("ReadFile: %v", err) 138 } 139 140 goldenContent, err := ioutil.ReadFile(goldenPath) 141 if err != nil { 142 t.Fatalf("ReadFile: %v", err) 143 } 144 145 if string(gotContent) != string(goldenContent) { 146 t.Errorf("got %s is different from expected %s", gotPath, goldenPath) 147 } 148 } 149} 150