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