golang.org/x/tools@v0.21.0/godoc/dirtrees_test.go (about)

     1  // Copyright 2018 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 godoc
     6  
     7  import (
     8  	"go/build"
     9  	"path/filepath"
    10  	"runtime"
    11  	"sort"
    12  	"testing"
    13  
    14  	"golang.org/x/tools/godoc/vfs"
    15  	"golang.org/x/tools/godoc/vfs/gatefs"
    16  )
    17  
    18  func TestNewDirTree(t *testing.T) {
    19  	fsGate := make(chan bool, 20)
    20  	rootfs := gatefs.New(vfs.OS(runtime.GOROOT()), fsGate)
    21  	fs := vfs.NameSpace{}
    22  	fs.Bind("/", rootfs, "/", vfs.BindReplace)
    23  
    24  	c := NewCorpus(fs)
    25  	// 3 levels deep is enough for testing
    26  	dir := c.newDirectory("/", 3)
    27  
    28  	processDir(t, dir)
    29  }
    30  
    31  func processDir(t *testing.T, dir *Directory) {
    32  	var list []string
    33  	for _, d := range dir.Dirs {
    34  		list = append(list, d.Name)
    35  		// recursively process the lower level
    36  		processDir(t, d)
    37  	}
    38  
    39  	if sort.StringsAreSorted(list) == false {
    40  		t.Errorf("list: %v is not sorted\n", list)
    41  	}
    42  }
    43  
    44  func BenchmarkNewDirectory(b *testing.B) {
    45  	if testing.Short() {
    46  		b.Skip("not running tests requiring large file scan in short mode")
    47  	}
    48  
    49  	fsGate := make(chan bool, 20)
    50  
    51  	goroot := runtime.GOROOT()
    52  	rootfs := gatefs.New(vfs.OS(goroot), fsGate)
    53  	fs := vfs.NameSpace{}
    54  	fs.Bind("/", rootfs, "/", vfs.BindReplace)
    55  	for _, p := range filepath.SplitList(build.Default.GOPATH) {
    56  		fs.Bind("/src/golang.org", gatefs.New(vfs.OS(p), fsGate), "/src/golang.org", vfs.BindAfter)
    57  	}
    58  	b.ResetTimer()
    59  	b.ReportAllocs()
    60  	for tries := 0; tries < b.N; tries++ {
    61  		corpus := NewCorpus(fs)
    62  		corpus.newDirectory("/", -1)
    63  	}
    64  }