github.com/shulhan/golangci-lint@v1.10.1/pkg/lint/load_test.go (about)

     1  package lint
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/golangci/golangci-lint/pkg/golinters"
     7  	"github.com/golangci/golangci-lint/pkg/logutils"
     8  
     9  	"github.com/golangci/golangci-lint/pkg/config"
    10  	"github.com/golangci/golangci-lint/pkg/lint/astcache"
    11  	"github.com/golangci/golangci-lint/pkg/lint/linter"
    12  	"github.com/golangci/golangci-lint/pkg/packages"
    13  	"github.com/stretchr/testify/assert"
    14  )
    15  
    16  func TestASTCacheLoading(t *testing.T) {
    17  	linters := []linter.Config{
    18  		linter.NewConfig(golinters.Errcheck{}).WithFullImport(),
    19  	}
    20  
    21  	inputPaths := []string{"./...", "./", "./load.go", "load.go"}
    22  	log := logutils.NewStderrLog("")
    23  	for _, inputPath := range inputPaths {
    24  		r, err := packages.NewResolver(nil, nil, log)
    25  		assert.NoError(t, err)
    26  
    27  		pkgProg, err := r.Resolve(inputPath)
    28  		assert.NoError(t, err)
    29  
    30  		assert.NoError(t, err)
    31  		assert.NotEmpty(t, pkgProg.Files(true))
    32  
    33  		cfg := &config.Config{
    34  			Run: config.Run{
    35  				AnalyzeTests: true,
    36  			},
    37  		}
    38  		prog, _, err := loadWholeAppIfNeeded(linters, cfg, pkgProg, logutils.NewStderrLog(""))
    39  		assert.NoError(t, err)
    40  
    41  		astCacheFromProg, err := astcache.LoadFromProgram(prog, log)
    42  		assert.NoError(t, err)
    43  
    44  		astCacheFromFiles, err := astcache.LoadFromFiles(pkgProg.Files(true), log)
    45  		assert.NoError(t, err)
    46  
    47  		filesFromProg := astCacheFromProg.GetAllValidFiles()
    48  		filesFromFiles := astCacheFromFiles.GetAllValidFiles()
    49  		if len(filesFromProg) != len(filesFromFiles) {
    50  			t.Logf("files: %s", pkgProg.Files(true))
    51  			t.Logf("from prog:")
    52  			for _, f := range filesFromProg {
    53  				t.Logf("%+v", *f)
    54  			}
    55  			t.Logf("from files:")
    56  			for _, f := range filesFromFiles {
    57  				t.Logf("%+v", *f)
    58  			}
    59  			t.Fatalf("lengths differ")
    60  		}
    61  
    62  		if len(filesFromProg) != len(pkgProg.Files(true)) {
    63  			t.Fatalf("filesFromProg differ from path.Files")
    64  		}
    65  	}
    66  }