...

Source file src/github.com/bazelbuild/buildtools/warn/warn_bazel_operation_test.go

Documentation: github.com/bazelbuild/buildtools/warn

     1  /*
     2  Copyright 2020 Google LLC
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      https://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package warn
    18  
    19  import "testing"
    20  
    21  func TestDepsetIteration(t *testing.T) {
    22  	checkFindingsAndFix(t, "depset-iteration", `
    23  d = depset([1, 2, 3]) + bar
    24  
    25  max(d + foo)
    26  min(d)
    27  all(d)
    28  any(d)
    29  sorted(d)
    30  zip(
    31      d,
    32      a,
    33      b,
    34  )
    35  zip(
    36       a,
    37       d,
    38  )
    39  list(d)
    40  tuple(d)
    41  depset(d)
    42  len(d)
    43  1 in d
    44  2 not in d
    45  
    46  [foo(x) for x in d]
    47  
    48  for x in d:
    49      pass
    50  
    51  # Non-iteration is ok
    52  
    53  foobar(d)
    54  d == b
    55  
    56  # The following iterations over a list don't trigger warnings
    57  
    58  l = list([1, 2, 3])
    59  
    60  max(l)
    61  zip(l, foo)
    62  [foo(x) for x in l]
    63  1 in l
    64  
    65  for x in l:
    66      pass
    67  `, `
    68  d = depset([1, 2, 3]) + bar
    69  
    70  max((d + foo).to_list())
    71  min(d.to_list())
    72  all(d.to_list())
    73  any(d.to_list())
    74  sorted(d.to_list())
    75  zip(
    76      d.to_list(),
    77      a,
    78      b,
    79  )
    80  zip(
    81      a,
    82      d.to_list(),
    83  )
    84  d.to_list()
    85  tuple(d.to_list())
    86  depset(d.to_list())
    87  len(d.to_list())
    88  1 in d.to_list()
    89  2 not in d.to_list()
    90  
    91  [foo(x) for x in d.to_list()]
    92  
    93  for x in d.to_list():
    94      pass
    95  
    96  # Non-iteration is ok
    97  
    98  foobar(d)
    99  d == b
   100  
   101  # The following iterations over a list don't trigger warnings
   102  
   103  l = list([1, 2, 3])
   104  
   105  max(l)
   106  zip(l, foo)
   107  [foo(x) for x in l]
   108  1 in l
   109  
   110  for x in l:
   111      pass
   112  `,
   113  		[]string{
   114  			`:3: Depset iteration is deprecated, use the "to_list()" method instead.`,
   115  			`:4: Depset iteration is deprecated, use the "to_list()" method instead.`,
   116  			`:5: Depset iteration is deprecated, use the "to_list()" method instead.`,
   117  			`:6: Depset iteration is deprecated, use the "to_list()" method instead.`,
   118  			`:7: Depset iteration is deprecated, use the "to_list()" method instead.`,
   119  			`:9: Depset iteration is deprecated, use the "to_list()" method instead.`,
   120  			`:15: Depset iteration is deprecated, use the "to_list()" method instead.`,
   121  			`:17: Depset iteration is deprecated, use the "to_list()" method instead.`,
   122  			`:18: Depset iteration is deprecated, use the "to_list()" method instead.`,
   123  			`:19: Depset iteration is deprecated, use the "to_list()" method instead.`,
   124  			`:20: Depset iteration is deprecated, use the "to_list()" method instead.`,
   125  			`:21: Depset iteration is deprecated, use the "to_list()" method instead.`,
   126  			`:22: Depset iteration is deprecated, use the "to_list()" method instead.`,
   127  			`:24: Depset iteration is deprecated, use the "to_list()" method instead.`,
   128  			`:26: Depset iteration is deprecated, use the "to_list()" method instead.`,
   129  		},
   130  		scopeEverywhere)
   131  }
   132  
   133  func TestDepsetUnion(t *testing.T) {
   134  	checkFindings(t, "depset-union", `
   135  d = depset([1, 2, 3])
   136  
   137  d + foo
   138  foo + d
   139  d + foo + bar
   140  foo + bar + d
   141  
   142  d | foo
   143  foo | d
   144  d | foo | bar
   145  foo | bar | d
   146  
   147  d += foo
   148  d |= bar
   149  foo += d
   150  bar |= d
   151  
   152  d.union(aaa)
   153  bbb.union(d)
   154  
   155  ccc.union(ddd)
   156  eee + fff | ggg
   157  `,
   158  		[]string{
   159  			`:3: Depsets should be joined using the "depset()" constructor`,
   160  			`:4: Depsets should be joined using the "depset()" constructor`,
   161  			`:5: Depsets should be joined using the "depset()" constructor`,
   162  			`:5: Depsets should be joined using the "depset()" constructor`,
   163  			`:6: Depsets should be joined using the "depset()" constructor`,
   164  			`:8: Depsets should be joined using the "depset()" constructor`,
   165  			`:9: Depsets should be joined using the "depset()" constructor`,
   166  			`:10: Depsets should be joined using the "depset()" constructor`,
   167  			`:10: Depsets should be joined using the "depset()" constructor`,
   168  			`:11: Depsets should be joined using the "depset()" constructor`,
   169  			`:13: Depsets should be joined using the "depset()" constructor`,
   170  			`:14: Depsets should be joined using the "depset()" constructor`,
   171  			`:15: Depsets should be joined using the "depset()" constructor`,
   172  			`:16: Depsets should be joined using the "depset()" constructor`,
   173  			`:18: Depsets should be joined using the "depset()" constructor`,
   174  			`:19: Depsets should be joined using the "depset()" constructor`,
   175  		},
   176  		scopeEverywhere)
   177  }
   178  
   179  func TestOverlyNestedDepset(t *testing.T) {
   180  	checkFindings(t, "overly-nested-depset", `
   181  x = depset()
   182  x = depset(1, transitive=[a, x, b])  # not inside a loop, ok
   183  
   184  for y in z:
   185    if y:
   186      x = depset([2], transitive = [a, x, b])  # problem here
   187      y = depset([3], transitive = [a, x, b])  # ok
   188      x = depset([4])  # ok
   189  `,
   190  		[]string{
   191  			`:6: Depset "x" is potentially overly nested.`,
   192  		},
   193  		scopeEverywhere)
   194  }
   195  

View as plain text