github.com/graybobo/golang.org-package-offline-cache@v0.0.0-20200626051047-6608995c132f/x/tools/refactor/importgraph/graph_test.go (about)

     1  // Copyright 2015 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  // Incomplete std lib sources on Android.
     6  
     7  // +build !android
     8  
     9  package importgraph_test
    10  
    11  import (
    12  	"fmt"
    13  	"go/build"
    14  	"sort"
    15  	"strings"
    16  	"testing"
    17  
    18  	"golang.org/x/tools/go/buildutil"
    19  	"golang.org/x/tools/refactor/importgraph"
    20  
    21  	_ "crypto/hmac" // just for test, below
    22  )
    23  
    24  const this = "golang.org/x/tools/refactor/importgraph"
    25  
    26  func TestBuild(t *testing.T) {
    27  	forward, reverse, errors := importgraph.Build(&build.Default)
    28  
    29  	// Test direct edges.
    30  	// We throw in crypto/hmac to prove that external test files
    31  	// (such as this one) are inspected.
    32  	for _, p := range []string{"go/build", "testing", "crypto/hmac"} {
    33  		if !forward[this][p] {
    34  			t.Errorf("forward[importgraph][%s] not found", p)
    35  		}
    36  		if !reverse[p][this] {
    37  			t.Errorf("reverse[%s][importgraph] not found", p)
    38  		}
    39  	}
    40  
    41  	// Test non-existent direct edges
    42  	for _, p := range []string{"errors", "reflect"} {
    43  		if forward[this][p] {
    44  			t.Errorf("unexpected: forward[importgraph][%s] found", p)
    45  		}
    46  		if reverse[p][this] {
    47  			t.Errorf("unexpected: reverse[%s][importgraph] found", p)
    48  		}
    49  	}
    50  
    51  	// Test vendor packages appear under their absolute names.
    52  	if buildutil.AllowVendor != 0 { // hack: Go 1.6+ only
    53  		if !forward["net/http"]["vendor/golang.org/x/net/http2/hpack"] {
    54  			t.Errorf("forward[net/http] does not include vendor/golang.org/x/net/http2/hpack: %v",
    55  				strings.Replace(fmt.Sprint(forward["net/http"]), ":true", "", -1))
    56  		}
    57  	}
    58  
    59  	// Test Search is reflexive.
    60  	if !forward.Search(this)[this] {
    61  		t.Errorf("irreflexive: forward.Search(importgraph)[importgraph] not found")
    62  	}
    63  	if !reverse.Search(this)[this] {
    64  		t.Errorf("irrefexive: reverse.Search(importgraph)[importgraph] not found")
    65  	}
    66  
    67  	// Test Search is transitive.  (There is no direct edge to these packages.)
    68  	for _, p := range []string{"errors", "reflect", "unsafe"} {
    69  		if !forward.Search(this)[p] {
    70  			t.Errorf("intransitive: forward.Search(importgraph)[%s] not found", p)
    71  		}
    72  		if !reverse.Search(p)[this] {
    73  			t.Errorf("intransitive: reverse.Search(%s)[importgraph] not found", p)
    74  		}
    75  	}
    76  
    77  	// Test strongly-connected components.  Because A's external
    78  	// test package can depend on B, and vice versa, most of the
    79  	// standard libraries are mutually dependent when their external
    80  	// tests are considered.
    81  	//
    82  	// For any nodes x, y in the same SCC, y appears in the results
    83  	// of both forward and reverse searches starting from x
    84  	if !forward.Search("fmt")["io"] ||
    85  		!forward.Search("io")["fmt"] ||
    86  		!reverse.Search("fmt")["io"] ||
    87  		!reverse.Search("io")["fmt"] {
    88  		t.Errorf("fmt and io are not mutually reachable despite being in the same SCC")
    89  	}
    90  
    91  	// debugging
    92  	if false {
    93  		for path, err := range errors {
    94  			t.Logf("%s: %s", path, err)
    95  		}
    96  		printSorted := func(direction string, g importgraph.Graph, start string) {
    97  			t.Log(direction)
    98  			var pkgs []string
    99  			for pkg := range g.Search(start) {
   100  				pkgs = append(pkgs, pkg)
   101  			}
   102  			sort.Strings(pkgs)
   103  			for _, pkg := range pkgs {
   104  				t.Logf("\t%s", pkg)
   105  			}
   106  		}
   107  		printSorted("forward", forward, this)
   108  		printSorted("reverse", reverse, this)
   109  	}
   110  }