github.com/bazelbuild/bazel-gazelle@v0.36.1-0.20240520142334-61b277ba6fed/cmd/gazelle/diff_test.go (about)

     1  /* Copyright 2017 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 main
    17  
    18  import (
    19  	"fmt"
    20  	"path/filepath"
    21  	"testing"
    22  
    23  	"github.com/bazelbuild/bazel-gazelle/testtools"
    24  )
    25  
    26  func TestDiffExisting(t *testing.T) {
    27  	files := []testtools.FileSpec{
    28  		{Path: "WORKSPACE"},
    29  		{
    30  			Path: "BUILD.bazel",
    31  			Content: `
    32  # gazelle:prefix example.com/hello
    33  `,
    34  		},
    35  		{
    36  			Path:    "hello.go",
    37  			Content: `package hello`,
    38  		},
    39  	}
    40  	dir, cleanup := testtools.CreateFiles(t, files)
    41  	defer cleanup()
    42  
    43  	wantError := "encountered changes while running diff"
    44  	if err := runGazelle(dir, []string{"-mode=diff", "-patch=p"}); err.Error() != wantError {
    45  		t.Fatalf("got %q; want %q", err, wantError)
    46  	}
    47  
    48  	want := append(files, testtools.FileSpec{
    49  		Path: "p",
    50  		Content: `
    51  --- BUILD.bazel	1970-01-01 00:00:00.000000001 +0000
    52  +++ BUILD.bazel	1970-01-01 00:00:00.000000001 +0000
    53  @@ -1,3 +1,11 @@
    54  +load("@io_bazel_rules_go//go:def.bzl", "go_library")
    55   
    56   # gazelle:prefix example.com/hello
    57   
    58  +go_library(
    59  +    name = "hello",
    60  +    srcs = ["hello.go"],
    61  +    importpath = "example.com/hello",
    62  +    visibility = ["//visibility:public"],
    63  +)
    64  +
    65  `,
    66  	})
    67  	testtools.CheckFiles(t, dir, want)
    68  }
    69  
    70  func TestDiffNew(t *testing.T) {
    71  	files := []testtools.FileSpec{
    72  		{Path: "WORKSPACE"},
    73  		{
    74  			Path:    "hello.go",
    75  			Content: `package hello`,
    76  		},
    77  	}
    78  	dir, cleanup := testtools.CreateFiles(t, files)
    79  	defer cleanup()
    80  
    81  	wantError := "encountered changes while running diff"
    82  	if err := runGazelle(dir, []string{"-go_prefix=example.com/hello", "-mode=diff", "-patch=p"}); err.Error() != wantError {
    83  		t.Fatalf("got %q; want %q", err, wantError)
    84  	}
    85  
    86  	want := append(files, testtools.FileSpec{
    87  		Path: "p",
    88  		Content: `
    89  --- /dev/null	1970-01-01 00:00:00.000000001 +0000
    90  +++ BUILD.bazel	1970-01-01 00:00:00.000000001 +0000
    91  @@ -0,0 +1,9 @@
    92  +load("@io_bazel_rules_go//go:def.bzl", "go_library")
    93  +
    94  +go_library(
    95  +    name = "hello",
    96  +    srcs = ["hello.go"],
    97  +    importpath = "example.com/hello",
    98  +    visibility = ["//visibility:public"],
    99  +)
   100  +
   101  `,
   102  	})
   103  	testtools.CheckFiles(t, dir, want)
   104  }
   105  
   106  func TestDiffMissingAndNoChange(t *testing.T) {
   107  	files := []testtools.FileSpec{
   108  		{Path: "WORKSPACE"},
   109  	}
   110  	dir, cleanup := testtools.CreateFiles(t, files)
   111  	defer cleanup()
   112  
   113  	if err := runGazelle(dir, []string{"-go_prefix=example.com/hello", "-mode=diff", "-patch=p"}); err != nil {
   114  		t.Error("Expected no diff, but got a diff.")
   115  	}
   116  	testtools.CheckFiles(t, dir, []testtools.FileSpec{{Path: "p"}})
   117  }
   118  
   119  func TestDiffEmptyAndNoChange(t *testing.T) {
   120  	files := []testtools.FileSpec{
   121  		{Path: "WORKSPACE"},
   122  		{Path: "BUILD.bazel"},
   123  	}
   124  	dir, cleanup := testtools.CreateFiles(t, files)
   125  	defer cleanup()
   126  
   127  	if err := runGazelle(dir, []string{"-go_prefix=example.com/hello", "-mode=diff", "-patch=p"}); err != nil {
   128  		t.Error("Expected no diff, but got a diff.")
   129  	}
   130  	testtools.CheckFiles(t, dir, []testtools.FileSpec{{Path: "p"}})
   131  }
   132  
   133  func TestDiffReadWriteDir(t *testing.T) {
   134  	files := []testtools.FileSpec{
   135  		{
   136  			Path:    "repo/hello.go",
   137  			Content: "package hello",
   138  		}, {
   139  			Path:    "read/BUILD.bazel",
   140  			Content: "# gazelle:prefix example.com/hello",
   141  		},
   142  	}
   143  	dir, cleanup := testtools.CreateFiles(t, files)
   144  	defer cleanup()
   145  
   146  	args := []string{
   147  		"-repo_root=repo",
   148  		"-mode=diff",
   149  		"-patch=p",
   150  		"-experimental_read_build_files_dir=read",
   151  		"-experimental_write_build_files_dir=write",
   152  		"repo",
   153  	}
   154  
   155  	wantError := "encountered changes while running diff"
   156  	if err := runGazelle(dir, args); err.Error() != wantError {
   157  		t.Fatalf("got %q; want %q", err, wantError)
   158  	}
   159  
   160  	wantPatch := fmt.Sprintf(`
   161  --- %s	1970-01-01 00:00:00.000000001 +0000
   162  +++ %s	1970-01-01 00:00:00.000000001 +0000
   163  @@ -1 +1,11 @@
   164  +load("@io_bazel_rules_go//go:def.bzl", "go_library")
   165  +
   166   # gazelle:prefix example.com/hello
   167  +
   168  +go_library(
   169  +    name = "hello",
   170  +    srcs = ["hello.go"],
   171  +    importpath = "example.com/hello",
   172  +    visibility = ["//visibility:public"],
   173  +)
   174  +
   175  `,
   176  		filepath.Join(dir, "read", "BUILD.bazel"),
   177  		filepath.Join(dir, "write", "BUILD.bazel"))
   178  	want := append(files, testtools.FileSpec{Path: "p", Content: wantPatch})
   179  	testtools.CheckFiles(t, dir, want)
   180  }