github.com/bazelbuild/bazel-gazelle@v0.36.1-0.20240520142334-61b277ba6fed/testtools/config.go (about)

     1  /* Copyright 2018 The Bazel Authors. All rights reserved.
     2  
     3  Licensed under the Apache License, Version 2.0 (the "License");
     4  you may not use this file except in compliance with the License.
     5  You may obtain a copy of the License at
     6  
     7     http://www.apache.org/licenses/LICENSE-2.0
     8  
     9  Unless required by applicable law or agreed to in writing, software
    10  distributed under the License is distributed on an "AS IS" BASIS,
    11  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  See the License for the specific language governing permissions and
    13  limitations under the License.
    14  */
    15  
    16  package testtools
    17  
    18  import (
    19  	"flag"
    20  	"testing"
    21  
    22  	"github.com/bazelbuild/bazel-gazelle/config"
    23  	"github.com/bazelbuild/bazel-gazelle/language"
    24  )
    25  
    26  // NewTestConfig returns a Config used for tests in any language extension.
    27  // cexts is a list of configuration extensions to use. langs is a list of
    28  // language extensions to use (languages are also configuration extensions,
    29  // but it may be convenient to keep them separate). args is a list of
    30  // command line arguments to apply. NewTestConfig calls t.Fatal if any
    31  // error is encountered while processing flags.
    32  func NewTestConfig(t *testing.T, cexts []config.Configurer, langs []language.Language, args []string) *config.Config {
    33  	c := config.New()
    34  	fs := flag.NewFlagSet("test", flag.ContinueOnError)
    35  
    36  	for _, lang := range langs {
    37  		cexts = append(cexts, lang)
    38  	}
    39  	for _, cext := range cexts {
    40  		cext.RegisterFlags(fs, "update", c)
    41  	}
    42  
    43  	if err := fs.Parse(args); err != nil {
    44  		t.Fatal(err)
    45  	}
    46  	for _, cext := range cexts {
    47  		if err := cext.CheckFlags(fs, c); err != nil {
    48  			t.Fatal(err)
    49  		}
    50  	}
    51  
    52  	return c
    53  }