golang.org/x/tools@v0.21.0/go/analysis/passes/directive/directive_test.go (about)

     1  // Copyright 2023 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 directive_test
     6  
     7  import (
     8  	"runtime"
     9  	"strings"
    10  	"testing"
    11  
    12  	"golang.org/x/tools/go/analysis"
    13  	"golang.org/x/tools/go/analysis/analysistest"
    14  	"golang.org/x/tools/go/analysis/passes/directive"
    15  )
    16  
    17  func Test(t *testing.T) {
    18  	if strings.HasPrefix(runtime.Version(), "go1.") && runtime.Version() < "go1.16" {
    19  		t.Skipf("skipping on %v", runtime.Version())
    20  	}
    21  	analyzer := *directive.Analyzer
    22  	analyzer.Run = func(pass *analysis.Pass) (interface{}, error) {
    23  		defer func() {
    24  			// The directive pass is unusual in that it checks the IgnoredFiles.
    25  			// After analysis, add IgnoredFiles to OtherFiles so that
    26  			// the test harness checks for expected diagnostics in those.
    27  			// (The test harness shouldn't do this by default because most
    28  			// passes can't do anything with the IgnoredFiles without type
    29  			// information, which is unavailable because they are ignored.)
    30  			var files []string
    31  			files = append(files, pass.OtherFiles...)
    32  			files = append(files, pass.IgnoredFiles...)
    33  			pass.OtherFiles = files
    34  		}()
    35  
    36  		return directive.Analyzer.Run(pass)
    37  	}
    38  	analysistest.Run(t, analysistest.TestData(), &analyzer, "a")
    39  }