...

Source file src/github.com/rogpeppe/go-internal/imports/scan_test.go

Documentation: github.com/rogpeppe/go-internal/imports

     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 imports
     6  
     7  import (
     8  	"path/filepath"
     9  	"reflect"
    10  	"runtime"
    11  	"testing"
    12  
    13  	"github.com/rogpeppe/go-internal/testenv"
    14  )
    15  
    16  func TestScan(t *testing.T) {
    17  	testenv.MustHaveGoBuild(t)
    18  
    19  	imports, testImports, err := ScanDir(filepath.Join(runtime.GOROOT(), "src/encoding/json"), nil)
    20  	if err != nil {
    21  		t.Fatal(err)
    22  	}
    23  	foundBase64 := false
    24  	for _, p := range imports {
    25  		if p == "encoding/base64" {
    26  			foundBase64 = true
    27  		}
    28  		if p == "encoding/binary" {
    29  			// A dependency but not an import
    30  			t.Errorf("json reported as importing encoding/binary but does not")
    31  		}
    32  		if p == "net/http" {
    33  			// A test import but not an import
    34  			t.Errorf("json reported as importing encoding/binary but does not")
    35  		}
    36  	}
    37  	if !foundBase64 {
    38  		t.Errorf("json missing import encoding/base64 (%q)", imports)
    39  	}
    40  
    41  	foundHTTP := false
    42  	for _, p := range testImports {
    43  		if p == "net/http" {
    44  			foundHTTP = true
    45  		}
    46  		if p == "unicode/utf16" {
    47  			// A package import but not a test import
    48  			t.Errorf("json reported as test-importing unicode/utf16  but does not")
    49  		}
    50  	}
    51  	if !foundHTTP {
    52  		t.Errorf("json missing test import net/http (%q)", testImports)
    53  	}
    54  }
    55  
    56  func TestScanStar(t *testing.T) {
    57  	testenv.MustHaveGoBuild(t)
    58  
    59  	imports, _, err := ScanDir("testdata/import1", map[string]bool{"*": true})
    60  	if err != nil {
    61  		t.Fatal(err)
    62  	}
    63  
    64  	want := []string{"import1", "import2", "import3", "import4"}
    65  	if !reflect.DeepEqual(imports, want) {
    66  		t.Errorf("ScanDir testdata/import1:\nhave %v\nwant %v", imports, want)
    67  	}
    68  }
    69  

View as plain text