github.com/abayer/test-infra@v0.0.5/prow/plugins/buildifier/buildifier_test.go (about)

     1  /*
     2  Copyright 2017 The Kubernetes Authors.
     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      http://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 buildifier
    18  
    19  import (
    20  	"reflect"
    21  	"testing"
    22  
    23  	"github.com/sirupsen/logrus"
    24  
    25  	"k8s.io/test-infra/prow/git/localgit"
    26  	"k8s.io/test-infra/prow/github"
    27  )
    28  
    29  var initialFiles = map[string][]byte{
    30  	"BUILD":     []byte(`package(default_visibility = ["//visibility:public"])`),
    31  	"WORKSPACE": []byte(`workspace(name = "io_foo_bar")`),
    32  }
    33  
    34  var pullFiles = map[string][]byte{
    35  	"BUILD": []byte(`package(default_visibility = ["//visibility:public"])
    36  
    37  
    38  docker_build(
    39      name = "blah",
    40  )
    41  `),
    42  	"WORKSPACE": []byte(`workspace(name = "io_foo_bar")
    43  
    44  foo_repositories()
    45  `),
    46  	"blah.bzl": []byte(`def foo():
    47    print("bar")
    48  `),
    49  }
    50  
    51  type ghc struct {
    52  	genfile     []byte
    53  	pr          github.PullRequest
    54  	changes     []github.PullRequestChange
    55  	oldComments []github.ReviewComment
    56  	comment     github.DraftReview
    57  }
    58  
    59  func (g *ghc) GetPullRequestChanges(org, repo string, number int) ([]github.PullRequestChange, error) {
    60  	return g.changes, nil
    61  }
    62  
    63  func (g *ghc) CreateReview(org, repo string, number int, r github.DraftReview) error {
    64  	g.comment = r
    65  	return nil
    66  }
    67  
    68  func (g *ghc) ListPullRequestComments(org, repo string, number int) ([]github.ReviewComment, error) {
    69  	return g.oldComments, nil
    70  }
    71  
    72  func (g *ghc) GetFile(org, repo, filepath, commit string) ([]byte, error) {
    73  	return g.genfile, nil
    74  }
    75  
    76  func (g *ghc) GetPullRequest(org, repo string, number int) (*github.PullRequest, error) {
    77  	return &g.pr, nil
    78  }
    79  
    80  var e = &github.GenericCommentEvent{
    81  	Action:     github.GenericCommentActionCreated,
    82  	IssueState: "open",
    83  	Body:       "/buildify",
    84  	User:       github.User{Login: "mattmoor"},
    85  	Number:     42,
    86  	IsPR:       true,
    87  	Repo: github.Repo{
    88  		Owner:    github.User{Login: "foo"},
    89  		Name:     "bar",
    90  		FullName: "foo/bar",
    91  	},
    92  }
    93  
    94  func TestBuildify(t *testing.T) {
    95  	lg, c, err := localgit.New()
    96  	if err != nil {
    97  		t.Fatalf("Making localgit: %v", err)
    98  	}
    99  	defer func() {
   100  		if err := lg.Clean(); err != nil {
   101  			t.Errorf("Cleaning up localgit: %v", err)
   102  		}
   103  		if err := c.Clean(); err != nil {
   104  			t.Errorf("Cleaning up client: %v", err)
   105  		}
   106  	}()
   107  	if err := lg.MakeFakeRepo("foo", "bar"); err != nil {
   108  		t.Fatalf("Making fake repo: %v", err)
   109  	}
   110  	if err := lg.AddCommit("foo", "bar", initialFiles); err != nil {
   111  		t.Fatalf("Adding initial commit: %v", err)
   112  	}
   113  	if err := lg.CheckoutNewBranch("foo", "bar", "pull/42/head"); err != nil {
   114  		t.Fatalf("Checking out pull branch: %v", err)
   115  	}
   116  	if err := lg.AddCommit("foo", "bar", pullFiles); err != nil {
   117  		t.Fatalf("Adding PR commit: %v", err)
   118  	}
   119  
   120  	gh := &ghc{
   121  		changes: []github.PullRequestChange{
   122  			{
   123  				Filename: "BUILD",
   124  			},
   125  			{
   126  				Filename: "WORKSPACE",
   127  			},
   128  			{
   129  				Filename: "blah.bzl",
   130  			},
   131  		},
   132  	}
   133  	if err := handle(gh, c, logrus.NewEntry(logrus.New()), e); err != nil {
   134  		t.Fatalf("Got error from handle: %v", err)
   135  	}
   136  	if len(gh.comment.Comments) != 1 {
   137  		t.Fatalf("Expected one comment, got %d: %v.", len(gh.comment.Comments), gh.comment.Comments)
   138  	}
   139  	for _, c := range gh.comment.Comments {
   140  		pos := c.Position
   141  		gh.oldComments = append(gh.oldComments, github.ReviewComment{
   142  			Path:     c.Path,
   143  			Position: &pos,
   144  			Body:     c.Body,
   145  		})
   146  	}
   147  }
   148  
   149  func TestModifiedBazelFiles(t *testing.T) {
   150  	lg, c, err := localgit.New()
   151  	if err != nil {
   152  		t.Fatalf("Making localgit: %v", err)
   153  	}
   154  	defer func() {
   155  		if err := lg.Clean(); err != nil {
   156  			t.Errorf("Cleaning up localgit: %v", err)
   157  		}
   158  		if err := c.Clean(); err != nil {
   159  			t.Errorf("Cleaning up client: %v", err)
   160  		}
   161  	}()
   162  	if err := lg.MakeFakeRepo("foo", "bar"); err != nil {
   163  		t.Fatalf("Making fake repo: %v", err)
   164  	}
   165  	if err := lg.AddCommit("foo", "bar", initialFiles); err != nil {
   166  		t.Fatalf("Adding initial commit: %v", err)
   167  	}
   168  	if err := lg.CheckoutNewBranch("foo", "bar", "pull/42/head"); err != nil {
   169  		t.Fatalf("Checking out pull branch: %v", err)
   170  	}
   171  	if err := lg.AddCommit("foo", "bar", pullFiles); err != nil {
   172  		t.Fatalf("Adding PR commit: %v", err)
   173  	}
   174  
   175  	var testcases = []struct {
   176  		name                  string
   177  		gh                    *ghc
   178  		expectedModifiedFiles map[string]string
   179  	}{
   180  		{
   181  			name: "modified files include BUILD",
   182  			gh: &ghc{
   183  				changes: []github.PullRequestChange{
   184  					{
   185  						Filename: "BUILD",
   186  					},
   187  					{
   188  						Filename: "foo.go",
   189  					},
   190  				},
   191  			},
   192  			expectedModifiedFiles: map[string]string{
   193  				"BUILD": "",
   194  			},
   195  		},
   196  		{
   197  			name: "modified files include BUILD.bazel",
   198  			gh: &ghc{
   199  				changes: []github.PullRequestChange{
   200  					{
   201  						Filename: "BUILD.bazel",
   202  					},
   203  					{
   204  						Filename: "foo.go",
   205  					},
   206  				},
   207  			},
   208  			expectedModifiedFiles: map[string]string{
   209  				"BUILD.bazel": "",
   210  			},
   211  		},
   212  		{
   213  			name: "modified files include WORKSPACE",
   214  			gh: &ghc{
   215  				changes: []github.PullRequestChange{
   216  					{
   217  						Filename: "WORKSPACE",
   218  					},
   219  					{
   220  						Filename: "foo.md",
   221  					},
   222  				},
   223  			},
   224  			expectedModifiedFiles: map[string]string{
   225  				"WORKSPACE": "",
   226  			},
   227  		},
   228  		{
   229  			name: "modified files include .bzl file",
   230  			gh: &ghc{
   231  				changes: []github.PullRequestChange{
   232  					{
   233  						Filename: "qux.bzl",
   234  					},
   235  					{
   236  						Filename: "blah.md",
   237  					},
   238  				},
   239  			},
   240  			expectedModifiedFiles: map[string]string{
   241  				"qux.bzl": "",
   242  			},
   243  		},
   244  		{
   245  			name: "modified files include removed BUILD file",
   246  			gh: &ghc{
   247  				changes: []github.PullRequestChange{
   248  					{
   249  						Filename: "qux.go",
   250  					},
   251  					{
   252  						Filename: "BUILD",
   253  						Status:   github.PullRequestFileRemoved,
   254  					},
   255  				},
   256  			},
   257  			expectedModifiedFiles: map[string]string{},
   258  		},
   259  		{
   260  			name: "modified files include renamed file",
   261  			gh: &ghc{
   262  				changes: []github.PullRequestChange{
   263  					{
   264  						Filename: "qux.go",
   265  					},
   266  					{
   267  						Filename: "BUILD",
   268  						Status:   github.PullRequestFileRenamed,
   269  					},
   270  				},
   271  			},
   272  			expectedModifiedFiles: map[string]string{},
   273  		},
   274  		{
   275  			name: "added and modified files",
   276  			gh: &ghc{
   277  				changes: []github.PullRequestChange{
   278  					{
   279  						Filename: "foo/BUILD.bazel",
   280  						Status:   github.PullRequestFileAdded,
   281  					},
   282  					{
   283  						Filename: "bar/blah.bzl",
   284  					},
   285  				},
   286  			},
   287  			expectedModifiedFiles: map[string]string{
   288  				"foo/BUILD.bazel": "",
   289  				"bar/blah.bzl":    "",
   290  			},
   291  		},
   292  	}
   293  	for _, tc := range testcases {
   294  		actualModifiedFiles, _ := modifiedBazelFiles(tc.gh, "foo", "bar", 9527, "0ebb33b")
   295  		if !reflect.DeepEqual(tc.expectedModifiedFiles, actualModifiedFiles) {
   296  			t.Errorf("Expected: %#v, Got %#v in case %s.", tc.expectedModifiedFiles, actualModifiedFiles, tc.name)
   297  		}
   298  	}
   299  }