Black Lives Matter. Support the Equal Justice Initiative.

Source file src/cmd/go/internal/imports/scan_test.go

Documentation: cmd/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  	"bytes"
     9  	"internal/testenv"
    10  	"os"
    11  	"path"
    12  	"path/filepath"
    13  	"runtime"
    14  	"strings"
    15  	"testing"
    16  )
    17  
    18  func TestScan(t *testing.T) {
    19  	testenv.MustHaveGoBuild(t)
    20  
    21  	imports, testImports, err := ScanDir(filepath.Join(runtime.GOROOT(), "src/encoding/json"), Tags())
    22  	if err != nil {
    23  		t.Fatal(err)
    24  	}
    25  	foundBase64 := false
    26  	for _, p := range imports {
    27  		if p == "encoding/base64" {
    28  			foundBase64 = true
    29  		}
    30  		if p == "encoding/binary" {
    31  			// A dependency but not an import
    32  			t.Errorf("json reported as importing encoding/binary but does not")
    33  		}
    34  		if p == "net/http" {
    35  			// A test import but not an import
    36  			t.Errorf("json reported as importing encoding/binary but does not")
    37  		}
    38  	}
    39  	if !foundBase64 {
    40  		t.Errorf("json missing import encoding/base64 (%q)", imports)
    41  	}
    42  
    43  	foundHTTP := false
    44  	for _, p := range testImports {
    45  		if p == "net/http" {
    46  			foundHTTP = true
    47  		}
    48  		if p == "unicode/utf16" {
    49  			// A package import but not a test import
    50  			t.Errorf("json reported as test-importing unicode/utf16  but does not")
    51  		}
    52  	}
    53  	if !foundHTTP {
    54  		t.Errorf("json missing test import net/http (%q)", testImports)
    55  	}
    56  }
    57  func TestScanDir(t *testing.T) {
    58  	testenv.MustHaveGoBuild(t)
    59  
    60  	dirs, err := os.ReadDir("testdata")
    61  	if err != nil {
    62  		t.Fatal(err)
    63  	}
    64  	for _, dir := range dirs {
    65  		if !dir.IsDir() || strings.HasPrefix(dir.Name(), ".") {
    66  			continue
    67  		}
    68  		t.Run(dir.Name(), func(t *testing.T) {
    69  			tagsData, err := os.ReadFile(filepath.Join("testdata", dir.Name(), "tags.txt"))
    70  			if err != nil {
    71  				t.Fatalf("error reading tags: %v", err)
    72  			}
    73  			tags := make(map[string]bool)
    74  			for _, t := range strings.Fields(string(tagsData)) {
    75  				tags[t] = true
    76  			}
    77  
    78  			wantData, err := os.ReadFile(filepath.Join("testdata", dir.Name(), "want.txt"))
    79  			if err != nil {
    80  				t.Fatalf("error reading want: %v", err)
    81  			}
    82  			want := string(bytes.TrimSpace(wantData))
    83  
    84  			imports, _, err := ScanDir(path.Join("testdata", dir.Name()), tags)
    85  			if err != nil {
    86  				t.Fatal(err)
    87  			}
    88  			got := strings.Join(imports, "\n")
    89  			if got != want {
    90  				t.Errorf("ScanDir: got imports:\n%s\n\nwant:\n%s", got, want)
    91  			}
    92  		})
    93  	}
    94  }
    95  

View as plain text