github.com/bazelbuild/bazel-gazelle@v0.36.1-0.20240520142334-61b277ba6fed/rule/merge_test.go (about) 1 /* Copyright 2021 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 rule_test 17 18 import ( 19 "testing" 20 21 "github.com/bazelbuild/bazel-gazelle/rule" 22 bzl "github.com/bazelbuild/buildtools/build" 23 ) 24 25 func TestMergeRules(t *testing.T) { 26 t.Run("private attributes are merged", func(t *testing.T) { 27 src := rule.NewRule("go_library", "go_default_library") 28 privateAttrKey := "_my_private_attr" 29 privateAttrVal := "private_value" 30 src.SetPrivateAttr(privateAttrKey, privateAttrVal) 31 dst := rule.NewRule("go_library", "go_default_library") 32 rule.MergeRules(src, dst, map[string]bool{}, "") 33 if dst.PrivateAttr(privateAttrKey).(string) != privateAttrVal { 34 t.Fatalf("private attributes are merged: got %v; want %s", 35 dst.PrivateAttr(privateAttrKey), privateAttrVal) 36 } 37 }) 38 } 39 40 func TestMergeRules_WithSortedStringAttr(t *testing.T) { 41 t.Run("sorted string attributes are merged to empty rule", func(t *testing.T) { 42 src := rule.NewRule("go_library", "go_default_library") 43 sortedStringAttrKey := "deps" 44 sortedStringAttrVal := rule.SortedStrings{"@qux", "//foo:bar", "//foo:baz"} 45 src.SetAttr(sortedStringAttrKey, sortedStringAttrVal) 46 dst := rule.NewRule("go_library", "go_default_library") 47 rule.MergeRules(src, dst, map[string]bool{}, "") 48 49 valExpr, ok := dst.Attr(sortedStringAttrKey).(*bzl.ListExpr) 50 if !ok { 51 t.Fatalf("sorted string attributes invalid: got %v; want *bzl.ListExpr", 52 dst.Attr(sortedStringAttrKey)) 53 } 54 55 expected := []string{"//foo:bar", "//foo:baz", "@qux"} 56 for i, v := range valExpr.List { 57 if v.(*bzl.StringExpr).Value != expected[i] { 58 t.Fatalf("sorted string attributes are merged: got %v; want %v", 59 v.(*bzl.StringExpr).Value, expected[i]) 60 } 61 } 62 }) 63 64 t.Run("sorted string attributes are merged to non-empty rule", func(t *testing.T) { 65 src := rule.NewRule("go_library", "go_default_library") 66 sortedStringAttrKey := "deps" 67 sortedStringAttrVal := rule.SortedStrings{"@qux", "//foo:bar", "//foo:baz"} 68 src.SetAttr(sortedStringAttrKey, sortedStringAttrVal) 69 dst := rule.NewRule("go_library", "go_default_library") 70 dst.SetAttr(sortedStringAttrKey, rule.SortedStrings{"@qux", "//foo:bar", "//bacon:eggs"}) 71 rule.MergeRules(src, dst, map[string]bool{"deps": true}, "") 72 73 valExpr, ok := dst.Attr(sortedStringAttrKey).(*bzl.ListExpr) 74 if !ok { 75 t.Fatalf("sorted string attributes are merged: got %v; want *bzl.ListExpr", 76 dst.Attr(sortedStringAttrKey)) 77 } 78 79 expected := []string{"//foo:bar", "//foo:baz", "@qux"} 80 for i, v := range valExpr.List { 81 if v.(*bzl.StringExpr).Value != expected[i] { 82 t.Fatalf("sorted string attributes are merged: got %v; want %v", 83 v.(*bzl.StringExpr).Value, expected[i]) 84 } 85 } 86 }) 87 t.Run("delete existing sorted strings", func(t *testing.T) { 88 src := rule.NewRule("go_library", "go_default_library") 89 sortedStringAttrKey := "deps" 90 dst := rule.NewRule("go_library", "go_default_library") 91 sortedStringAttrVal := rule.SortedStrings{"@qux", "//foo:bar", "//foo:baz"} 92 dst.SetAttr(sortedStringAttrKey, sortedStringAttrVal) 93 rule.MergeRules(src, dst, map[string]bool{"deps": true}, "") 94 95 if dst.Attr(sortedStringAttrKey) != nil { 96 t.Fatalf("delete existing sorted strings: got %v; want nil", 97 dst.Attr(sortedStringAttrKey)) 98 } 99 }) 100 } 101 102 func TestMergeRules_WithUnsortedStringAttr(t *testing.T) { 103 t.Run("unsorted string attributes are merged to empty rule", func(t *testing.T) { 104 src := rule.NewRule("go_library", "go_default_library") 105 sortedStringAttrKey := "deps" 106 sortedStringAttrVal := rule.UnsortedStrings{"@qux", "//foo:bar", "//foo:baz"} 107 src.SetAttr(sortedStringAttrKey, sortedStringAttrVal) 108 dst := rule.NewRule("go_library", "go_default_library") 109 rule.MergeRules(src, dst, map[string]bool{}, "") 110 111 valExpr, ok := dst.Attr(sortedStringAttrKey).(*bzl.ListExpr) 112 if !ok { 113 t.Fatalf("sorted string attributes invalid: got %v; want *bzl.ListExpr", 114 dst.Attr(sortedStringAttrKey)) 115 } 116 117 expected := []string{"@qux", "//foo:bar", "//foo:baz"} 118 for i, v := range valExpr.List { 119 if v.(*bzl.StringExpr).Value != expected[i] { 120 t.Fatalf("unsorted string attributes are merged: got %v; want %v", 121 v.(*bzl.StringExpr).Value, expected[i]) 122 } 123 } 124 }) 125 126 t.Run("unsorted string attributes are merged to non-empty rule", func(t *testing.T) { 127 src := rule.NewRule("go_library", "go_default_library") 128 sortedStringAttrKey := "deps" 129 sortedStringAttrVal := rule.UnsortedStrings{"@qux", "//foo:bar", "//foo:baz"} 130 src.SetAttr(sortedStringAttrKey, sortedStringAttrVal) 131 dst := rule.NewRule("go_library", "go_default_library") 132 dst.SetAttr(sortedStringAttrKey, rule.UnsortedStrings{"@qux", "//foo:bar", "//bacon:eggs"}) 133 rule.MergeRules(src, dst, map[string]bool{"deps": true}, "") 134 135 valExpr, ok := dst.Attr(sortedStringAttrKey).(*bzl.ListExpr) 136 if !ok { 137 t.Fatalf("unsorted string attributes are merged: got %v; want *bzl.ListExpr", 138 dst.Attr(sortedStringAttrKey)) 139 } 140 141 expected := []string{"@qux", "//foo:bar", "//foo:baz"} 142 for i, v := range valExpr.List { 143 if v.(*bzl.StringExpr).Value != expected[i] { 144 t.Fatalf("unsorted string attributes are merged: got %v; want %v", 145 v.(*bzl.StringExpr).Value, expected[i]) 146 } 147 } 148 }) 149 }