...

Source file src/github.com/GoogleCloudPlatform/k8s-config-connector/pkg/test/utils.go

Documentation: github.com/GoogleCloudPlatform/k8s-config-connector/pkg/test

     1  // Copyright 2022 Google LLC
     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  package test
    16  
    17  import (
    18  	"regexp"
    19  	"testing"
    20  )
    21  
    22  // StringMatchesRegexList is a test utility that returns true
    23  // if the string matches any regex in a list of strings.
    24  //
    25  // if a regex fails to compile, the test will fail.
    26  func StringMatchesRegexList(t *testing.T, regexesToMatch []string, targetString string) bool {
    27  	for _, regexToMatch := range regexesToMatch {
    28  		matcher, err := regexp.Compile(regexToMatch)
    29  		if err != nil {
    30  			t.Fatalf("StringMatchesRegexList: regex '%v' failed to compile", regexToMatch)
    31  		}
    32  		if matcher.MatchString(targetString) {
    33  			return true
    34  		}
    35  	}
    36  	return false
    37  }
    38  
    39  // TrimLicenseHeaderFromYaml trims the license header in the yaml string.
    40  func TrimLicenseHeaderFromYaml(yaml string) string {
    41  	r := regexp.MustCompile("(?s)# Copyright.*under the License.\n\n")
    42  	return r.ReplaceAllString(yaml, "")
    43  }
    44  
    45  // TrimLicenseHeaderFromTF trims the license header in the tf string.
    46  func TrimLicenseHeaderFromTF(yaml string) string {
    47  	r := regexp.MustCompile("(?s)/\\*\\*\n \\* Copyright.*under the License.\n \\*/\n\n")
    48  	return r.ReplaceAllString(yaml, "")
    49  }
    50  

View as plain text