go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/vpython/spec/match_test.go (about)

     1  // Copyright 2017 The LUCI Authors.
     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 spec
    16  
    17  import (
    18  	"fmt"
    19  	"strings"
    20  	"testing"
    21  
    22  	"go.chromium.org/luci/vpython/api/vpython"
    23  
    24  	. "github.com/smartystreets/goconvey/convey"
    25  )
    26  
    27  func mkTag(python, abi, platform string) *vpython.PEP425Tag {
    28  	return &vpython.PEP425Tag{
    29  		Python:   python,
    30  		Abi:      abi,
    31  		Platform: platform,
    32  	}
    33  }
    34  
    35  func tagString(tags []*vpython.PEP425Tag) string {
    36  	parts := make([]string, len(tags))
    37  	for i, tag := range tags {
    38  		parts[i] = tag.TagString()
    39  	}
    40  	return strings.Join(parts, ", ")
    41  }
    42  
    43  func TestPEP425Matches(t *testing.T) {
    44  	t.Parallel()
    45  
    46  	testCases := []struct {
    47  		tags       []*vpython.PEP425Tag
    48  		matches    []*vpython.PEP425Tag
    49  		notMatches []*vpython.PEP425Tag
    50  	}{
    51  		{
    52  			tags: nil,
    53  			notMatches: []*vpython.PEP425Tag{
    54  				mkTag("", "", ""),
    55  				mkTag("cp27", "cp27mu", "manylinux1_x86_64"),
    56  			},
    57  		},
    58  		{
    59  			tags: []*vpython.PEP425Tag{
    60  				mkTag("cp27", "cp27mu", "manylinux1_x86_64"),
    61  				mkTag("py2", "cp27m", "macosx_10_9_universal"),
    62  			},
    63  			matches: []*vpython.PEP425Tag{
    64  				mkTag("cp27", "", ""),
    65  				mkTag("", "cp27mu", ""),
    66  				mkTag("", "", "manylinux1_x86_64"),
    67  				mkTag("py2", "", ""),
    68  				mkTag("", "cp27m", ""),
    69  				mkTag("", "", "macosx_10_9_universal"),
    70  				mkTag("", "cp27mu", "manylinux1_x86_64"),
    71  			},
    72  			notMatches: []*vpython.PEP425Tag{
    73  				mkTag("", "", ""),
    74  				mkTag("cp27", "cp27mu", "win_amd64"),
    75  				mkTag("cp27", "cp27mu", "macosx_10_9_universal"),
    76  			},
    77  		},
    78  		{
    79  			tags: []*vpython.PEP425Tag{
    80  				mkTag("cp27", "cp27mu", ""),
    81  			},
    82  			matches: []*vpython.PEP425Tag{
    83  				mkTag("cp27", "cp27mu", ""),
    84  			},
    85  			notMatches: []*vpython.PEP425Tag{
    86  				mkTag("", "", ""),
    87  				mkTag("cp27", "cp27mu", "otherArch"),
    88  			},
    89  		},
    90  	}
    91  
    92  	Convey(`Test cases for PEP425 tag matching`, t, func() {
    93  		for _, tc := range testCases {
    94  			Convey(fmt.Sprintf(`With system tags: %s`, tagString(tc.tags)), func() {
    95  				for _, m := range tc.matches {
    96  					Convey(fmt.Sprintf(`Tag matches: %s`, m.TagString()), func() {
    97  						So(PEP425Matches(m, tc.tags), ShouldBeTrue)
    98  					})
    99  				}
   100  
   101  				for _, m := range tc.notMatches {
   102  					Convey(fmt.Sprintf(`Tag doesn't match: %s`, m.TagString()), func() {
   103  						So(PEP425Matches(m, tc.tags), ShouldBeFalse)
   104  					})
   105  				}
   106  			})
   107  		}
   108  	})
   109  }
   110  
   111  func TestPackageMatches(t *testing.T) {
   112  	t.Parallel()
   113  
   114  	testCases := []struct {
   115  		tags         []*vpython.PEP425Tag
   116  		matchPkgs    []*vpython.Spec_Package
   117  		notMatchPkgs []*vpython.Spec_Package
   118  	}{
   119  		{
   120  			tags: nil,
   121  			matchPkgs: []*vpython.Spec_Package{
   122  				{Name: "NoTags"},
   123  			},
   124  			notMatchPkgs: []*vpython.Spec_Package{
   125  				{
   126  					Name:     "EmptyMatch",
   127  					MatchTag: []*vpython.PEP425Tag{mkTag("", "", "")},
   128  				},
   129  				{
   130  					Name:     "MissingMatch",
   131  					MatchTag: []*vpython.PEP425Tag{mkTag("cp27", "cp27mu", "manylinux1_x86_64")},
   132  				},
   133  			},
   134  		},
   135  		{
   136  			tags: []*vpython.PEP425Tag{
   137  				mkTag("cp27", "cp27mu", "manylinux1_x86_64"),
   138  				mkTag("py2", "cp27m", "macosx_10_9_universal"),
   139  			},
   140  			matchPkgs: []*vpython.Spec_Package{
   141  				{Name: "NoTags"},
   142  				{
   143  					Name:     "OneMatchingTag",
   144  					MatchTag: []*vpython.PEP425Tag{mkTag("cp27", "", "")},
   145  				},
   146  				{
   147  					Name: "MultipleMatchingTag",
   148  					MatchTag: []*vpython.PEP425Tag{
   149  						mkTag("cp27", "", ""),
   150  						mkTag("", "cp27m", ""),
   151  					},
   152  				},
   153  			},
   154  			notMatchPkgs: []*vpython.Spec_Package{
   155  				{
   156  					Name:     "EmptyMatch",
   157  					MatchTag: []*vpython.PEP425Tag{mkTag("", "", "")},
   158  				},
   159  				{
   160  					Name:     "MissingMatch",
   161  					MatchTag: []*vpython.PEP425Tag{mkTag("none", "none", "none")},
   162  				},
   163  				{
   164  					Name:        "NotMatchTag",
   165  					NotMatchTag: []*vpython.PEP425Tag{mkTag("", "cp27mu", "")},
   166  				},
   167  				{
   168  					Name:        "NotMatchTagWithMatchTag",
   169  					MatchTag:    []*vpython.PEP425Tag{mkTag("py2", "", "")},
   170  					NotMatchTag: []*vpython.PEP425Tag{mkTag("", "cp27mu", "")},
   171  				},
   172  			},
   173  		},
   174  	}
   175  
   176  	Convey(`Test cases for package tag matching`, t, func() {
   177  		for _, tc := range testCases {
   178  			Convey(fmt.Sprintf(`With system tags: %s`, tagString(tc.tags)), func() {
   179  				for _, m := range tc.matchPkgs {
   180  					Convey(fmt.Sprintf(`Package %q matches: %s`, m.Name, tagString(m.MatchTag)), func() {
   181  						So(PackageMatches(m, tc.tags), ShouldBeTrue)
   182  					})
   183  				}
   184  
   185  				for _, m := range tc.notMatchPkgs {
   186  					Convey(fmt.Sprintf(`Package %q doesn't match: %s`, m.Name, tagString(m.MatchTag)), func() {
   187  						So(PackageMatches(m, tc.tags), ShouldBeFalse)
   188  					})
   189  				}
   190  			})
   191  		}
   192  	})
   193  }