...

Source file src/github.com/bazelbuild/buildtools/warn/warn_cosmetic_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 TestPackageOnTop(t *testing.T) {
    22  	checkFindingsAndFix(t,
    23  		"package-on-top",
    24  		`
    25  my_macro(name = "foo")
    26  package()`,
    27  		`
    28  package()
    29  my_macro(name = "foo")`,
    30  		[]string{":2: Package declaration should be at the top of the file, after the load() statements, but before any call to a rule or a macro. package_group() and licenses() may be called before package()."},
    31  		scopeDefault|scopeBzl|scopeBuild)
    32  
    33  	checkFindingsAndFix(t,
    34  		"package-on-top",
    35  		`
    36  # Some comments
    37  
    38  """This is a docstring"""
    39  
    40  load(":foo.bzl", "foo")
    41  load(":bar.bzl", baz = "bar")
    42  
    43  package()
    44  
    45  foo(baz)`,
    46  		`
    47  # Some comments
    48  
    49  """This is a docstring"""
    50  
    51  load(":foo.bzl", "foo")
    52  load(":bar.bzl", baz = "bar")
    53  
    54  package()
    55  
    56  foo(baz)`,
    57  		[]string{},
    58  		scopeDefault|scopeBzl|scopeBuild)
    59  
    60  	checkFindingsAndFix(t,
    61  		"package-on-top",
    62  		`
    63  # Some comments
    64  
    65  """This is a docstring"""
    66  
    67  load(":foo.bzl", "foo")
    68  load(":bar.bzl", baz = "bar")
    69  
    70  package_group(name = "my_group")
    71  licenses(["my_license"])
    72  foo(baz)
    73  package()`,
    74  		`
    75  # Some comments
    76  
    77  """This is a docstring"""
    78  
    79  load(":foo.bzl", "foo")
    80  load(":bar.bzl", baz = "bar")
    81  
    82  package_group(name = "my_group")
    83  licenses(["my_license"])
    84  
    85  package()
    86  foo(baz)`,
    87  		[]string{":11: Package declaration should be at the top of the file, after the load() statements, but before any call to a rule or a macro. package_group() and licenses() may be called before package()."},
    88  		scopeDefault|scopeBzl|scopeBuild)
    89  
    90  	checkFindingsAndFix(t,
    91  		"package-on-top",
    92  		`
    93  """This is a docstring"""
    94  
    95  load(":foo.bzl", "foo")
    96  load(":bar.bzl", baz = "bar")
    97  
    98  VISIBILITY = baz
    99  
   100  foo()
   101  
   102  package(default_visibility = VISIBILITY)`,
   103  		`
   104  """This is a docstring"""
   105  
   106  load(":foo.bzl", "foo")
   107  load(":bar.bzl", baz = "bar")
   108  
   109  VISIBILITY = baz
   110  
   111  foo()
   112  
   113  package(default_visibility = VISIBILITY)`,
   114  		[]string{},
   115  		scopeDefault|scopeBzl|scopeBuild)
   116  
   117  	checkFindingsAndFix(t,
   118  		"package-on-top",
   119  		`
   120  """This is a docstring"""
   121  
   122  load(":foo.bzl", "foo")
   123  load(":bar.bzl", baz = "bar")
   124  
   125  irrelevant = baz
   126  
   127  foo()
   128  
   129  package()`,
   130  		`
   131  """This is a docstring"""
   132  
   133  load(":foo.bzl", "foo")
   134  load(":bar.bzl", baz = "bar")
   135  
   136  irrelevant = baz
   137  
   138  foo()
   139  
   140  package()`,
   141  		[]string{},
   142  		scopeDefault|scopeBzl|scopeBuild)
   143  }
   144  
   145  func TestUnsortedDictItems(t *testing.T) {
   146  	checkFindingsAndFix(t, "unsorted-dict-items", `
   147  d = {
   148  	"b": "b value",
   149  	"a": "a value",
   150  }`, `
   151  d = {
   152  	"a": "a value",
   153  	"b": "b value",
   154  }`,
   155  		[]string{":3: Dictionary items are out of their lexicographical order."},
   156  		scopeEverywhere)
   157  
   158  	checkFindings(t, "unsorted-dict-items", `
   159  d = {
   160  	"a": "a value",
   161  	"a": "a value",
   162  }`,
   163  		[]string{},
   164  		scopeEverywhere)
   165  
   166  	checkFindingsAndFix(t, "unsorted-dict-items", `
   167  d = {
   168  	2: "two",
   169  	"b": "b value",
   170  	1: "one",
   171  	"a": "a value",
   172  	3: "three",
   173  }`, `
   174  d = {
   175  	2: "two",
   176  	"a": "a value",
   177  	1: "one",
   178  	"b": "b value",
   179  	3: "three",
   180  }`,
   181  		[]string{":5: Dictionary items are out of their lexicographical order."},
   182  		scopeEverywhere)
   183  
   184  	checkFindings(t, "unsorted-dict-items", `
   185  d = {}`,
   186  		[]string{},
   187  		scopeEverywhere)
   188  
   189  	checkFindingsAndFix(t, "unsorted-dict-items", `
   190  d = {
   191  	# b comment
   192  	"b": "b value",
   193  	"a": "a value",
   194  }`, `
   195  d = {
   196  	"a": "a value",
   197  	# b comment
   198  	"b": "b value",
   199  }`,
   200  		[]string{":4: Dictionary items are out of their lexicographical order."},
   201  		scopeEverywhere)
   202  
   203  	checkFindings(t, "unsorted-dict-items", `
   204  # @unsorted-dict-items
   205  d = {
   206  	"b": "b value",
   207  	"a": "a value",
   208  }`,
   209  		[]string{},
   210  		scopeEverywhere)
   211  
   212  	checkFindingsAndFix(t, "unsorted-dict-items", `
   213  d = {
   214  	"key" : {
   215  		"b": "b value",
   216  		"a": "a value",
   217  	}
   218  }`, `
   219  d = {
   220  	"key" : {
   221  		"a": "a value",
   222  		"b": "b value",
   223  	}
   224  }`,
   225  		[]string{"4: Dictionary items are out of their lexicographical order."},
   226  		scopeEverywhere)
   227  
   228  	checkFindingsAndFix(t, "unsorted-dict-items", `
   229  d = {
   230  	"srcs": ["foo.go"],
   231  	"deps": [],
   232  }`, `
   233  d = {
   234  	"deps": [],
   235  	"srcs": ["foo.go"],
   236  }`,
   237  		[]string{"3: Dictionary items are out of their lexicographical order."},
   238  		scopeEverywhere)
   239  
   240  	checkFindingsAndFix(t, "unsorted-dict-items", `
   241  d = select({
   242  	"//conditions:zzz": ["myrule_b.sh"],
   243  	"//conditions:default": ["myrule_default.sh"],
   244  })`, `
   245  d = select({
   246  	"//conditions:zzz": ["myrule_b.sh"],
   247  	"//conditions:default": ["myrule_default.sh"],
   248  })`,
   249  		[]string{},
   250  		scopeEverywhere)
   251  
   252  	checkFindingsAndFix(t, "unsorted-dict-items", `
   253  foo_binary = rule(
   254  	implementation = _foo_binary_impl,
   255  	attrs = {
   256  		"_foocc": attr.label(
   257  			default = Label("//depsets:foocc"),
   258  		),
   259  		"srcs": attr.label_list(allow_files = True),
   260  		"deps": attr.label_list(),
   261  	},
   262  	outputs = {"out": "%{name}.out"},
   263  )`, `
   264  foo_binary = rule(
   265  	implementation = _foo_binary_impl,
   266  	attrs = {
   267  		"deps": attr.label_list(),
   268  		"srcs": attr.label_list(allow_files = True),
   269  		"_foocc": attr.label(
   270  			default = Label("//depsets:foocc"),
   271  		),
   272  	},
   273  	outputs = {"out": "%{name}.out"},
   274  )`,
   275  		[]string{
   276  			"7: Dictionary items are out of their lexicographical order.",
   277  			"8: Dictionary items are out of their lexicographical order.",
   278  		},
   279  		scopeEverywhere)
   280  
   281  	checkFindings(t, "unsorted-dict-items", `
   282  # @unsorted-dict-items
   283  d = {
   284  	"key" : {
   285  		"b": "b value",
   286  		"a": "a value",
   287  	}
   288  }`,
   289  		[]string{},
   290  		scopeEverywhere)
   291  
   292  	checkFindings(t, "unsorted-dict-items", `
   293  d.update(
   294  	# @unsorted-dict-items
   295  	{
   296  		"b": "value2",
   297  		"a": "value1",
   298  	},
   299  )`,
   300  		[]string{},
   301  		scopeEverywhere)
   302  
   303  	checkFindings(t, "unsorted-dict-items", `
   304  d.update(
   305  	{
   306  		"b": "value2",
   307  		"a": "value1",
   308  	}, # @unsorted-dict-items
   309  )`,
   310  		[]string{},
   311  		scopeEverywhere)
   312  }
   313  
   314  func TestSkylark(t *testing.T) {
   315  	checkFindingsAndFix(t, "skylark-comment", `
   316  # Skyline
   317  foo()
   318  # SkyLark
   319  
   320  # Implemented in skylark
   321  # Skylark
   322  bar() # SKYLARK
   323  
   324  # see https://docs.bazel.build/versions/master/skylark/lib/Label.html
   325  Label()
   326  `, `
   327  # Skyline
   328  foo()
   329  # Starlark
   330  
   331  # Implemented in starlark
   332  # Starlark
   333  bar() # STARLARK
   334  
   335  # see https://docs.bazel.build/versions/master/skylark/lib/Label.html
   336  Label()
   337  `,
   338  		[]string{
   339  			`:3: "Skylark" is an outdated name of the language, please use "starlark" instead.`,
   340  			`:7: "Skylark" is an outdated name of the language, please use "starlark" instead.`,
   341  		},
   342  		scopeEverywhere)
   343  
   344  	checkFindingsAndFix(t, "skylark-comment", `
   345  """
   346  Some docstring with skylark
   347  """ # buildifier: disable=skylark-docstring
   348  
   349  def f():
   350    """Some docstring with skylark"""
   351    # buildozer: disable=skylark-docstring
   352  `, `
   353  """
   354  Some docstring with skylark
   355  """ # buildifier: disable=skylark-docstring
   356  
   357  def f():
   358    """Some docstring with skylark"""
   359    # buildozer: disable=skylark-docstring
   360  `,
   361  		[]string{},
   362  		scopeEverywhere)
   363  
   364  	checkFindingsAndFix(t, "skylark-docstring", `
   365  # Some file
   366  
   367  """
   368  This is a docstring describing a skylark file
   369  """
   370  
   371  def f():
   372    """SKYLARK"""
   373  
   374  def l():
   375    """
   376    Returns https://docs.bazel.build/versions/master/skylark/lib/Label.html
   377    """
   378    return Label("skylark")
   379  `, `
   380  # Some file
   381  
   382  """
   383  This is a docstring describing a starlark file
   384  """ 
   385  
   386  def f():
   387    """STARLARK"""
   388  
   389  def l():
   390    """
   391    Returns https://docs.bazel.build/versions/master/skylark/lib/Label.html
   392    """
   393    return Label("skylark")
   394  `,
   395  		[]string{
   396  			`:3: "Skylark" is an outdated name of the language, please use "starlark" instead.`,
   397  			`:8: "Skylark" is an outdated name of the language, please use "starlark" instead.`,
   398  		},
   399  		scopeEverywhere)
   400  }
   401  

View as plain text