...

Source file src/github.com/bazelbuild/buildtools/edit/fix_test.go

Documentation: github.com/bazelbuild/buildtools/edit

     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 edit
    18  
    19  import (
    20  	"fmt"
    21  	"strings"
    22  	"testing"
    23  
    24  	"github.com/bazelbuild/buildtools/build"
    25  	"github.com/google/go-cmp/cmp"
    26  )
    27  
    28  func TestMovePackageDeclarationToTheTop(t *testing.T) {
    29  	tests := []struct {
    30  		input, expected string
    31  		shouldMove      bool
    32  	}{
    33  		{`"""Docstring."""
    34  
    35  load(":path.bzl", "x")
    36  
    37  foo()
    38  
    39  package(attr = "val")`,
    40  			`"""Docstring."""
    41  
    42  load(":path.bzl", "x")
    43  
    44  package(attr = "val")
    45  
    46  foo()`,
    47  			true},
    48  		{`"""Docstring."""
    49  
    50  load(":path.bzl", "x")
    51  
    52  package(attr = "val")
    53  
    54  foo()`,
    55  			`"""Docstring."""
    56  
    57  load(":path.bzl", "x")
    58  
    59  package(attr = "val")
    60  
    61  foo()`,
    62  			false},
    63  		{`"""Docstring."""
    64  
    65  load(":path.bzl", "x")
    66  
    67  foo()`,
    68  			`"""Docstring."""
    69  
    70  load(":path.bzl", "x")
    71  
    72  foo()`,
    73  			false,
    74  		},
    75  	}
    76  
    77  	for i, tst := range tests {
    78  		t.Run(fmt.Sprintf("case %d", i), func(t *testing.T) {
    79  			bld, err := build.Parse("BUILD", []byte(tst.input))
    80  			if err != nil {
    81  				t.Fatalf("Failed to parse %s; %v", tst.input, err)
    82  			}
    83  			if result := movePackageDeclarationToTheTop(bld); result != tst.shouldMove {
    84  				t.Errorf("TestMovePackageDeclarationToTheTop: expected %v, got %v", tst.shouldMove, result)
    85  			}
    86  
    87  			got := strings.TrimSpace(string(build.Format(bld)))
    88  			want := strings.TrimSpace(tst.expected)
    89  			if diff := cmp.Diff(want, got); diff != "" {
    90  				t.Errorf("TestMovePackageDeclarationToTheTop: (-want +got): %s", diff)
    91  			}
    92  		})
    93  	}
    94  }
    95  
    96  // Test cases for the full fix process.
    97  func TestFixAll(t *testing.T) {
    98  	tests := []struct {
    99  		name  string
   100  		input string
   101  		want  string
   102  	}{
   103  		{
   104  			name: "no empty package",
   105  			input: `load(":path.bzl", "x")
   106  
   107  x()
   108  `,
   109  			want: `load(":path.bzl", "x")
   110  
   111  x()
   112  `,
   113  		},
   114  	}
   115  
   116  	for _, tc := range tests {
   117  		t.Run(tc.name, func(t *testing.T) {
   118  			bld, err := build.Parse("BUILD", []byte(tc.input))
   119  			if err != nil {
   120  				t.Fatalf("Failed to parse %s; %v", tc.input, err)
   121  			}
   122  			FixFile(bld, "//who/cares", []string{})
   123  			if diff := cmp.Diff(tc.want, string(build.Format(bld))); diff != "" {
   124  				t.Errorf("%s: (-want +got): %s", tc.name, diff)
   125  			}
   126  		})
   127  	}
   128  }
   129  

View as plain text