github.com/afking/bazel-gazelle@v0.0.0-20180301150245-c02bc0f529e8/cmd/gazelle/fix_test.go (about) 1 /* Copyright 2016 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 "flag" 20 "io/ioutil" 21 "os" 22 "path/filepath" 23 "testing" 24 25 "github.com/bazelbuild/bazel-gazelle/internal/config" 26 bf "github.com/bazelbuild/buildtools/build" 27 ) 28 29 func TestMain(m *testing.M) { 30 tmpdir := os.Getenv("TEST_TMPDIR") 31 flag.Set("repo_root", tmpdir) 32 os.Exit(m.Run()) 33 } 34 35 func defaultArgs(dir string) []string { 36 return []string{ 37 "-repo_root", dir, 38 "-go_prefix", "example.com/repo", 39 dir, 40 } 41 } 42 43 func TestFixFile(t *testing.T) { 44 tmpdir := os.Getenv("TEST_TMPDIR") 45 dir, err := ioutil.TempDir(tmpdir, "") 46 if err != nil { 47 t.Fatalf("ioutil.TempDir(%q, %q) failed with %v; want success", tmpdir, "", err) 48 } 49 defer os.RemoveAll(dir) 50 51 stubFile := &bf.File{ 52 Path: filepath.Join(dir, "BUILD.bazel"), 53 Stmt: []bf.Expr{ 54 &bf.CallExpr{ 55 X: &bf.LiteralExpr{Token: "foo_rule"}, 56 List: []bf.Expr{ 57 &bf.BinaryExpr{ 58 X: &bf.LiteralExpr{Token: "name"}, 59 Op: "=", 60 Y: &bf.StringExpr{Value: "bar"}, 61 }, 62 }, 63 }, 64 }, 65 } 66 c := &config.Config{} 67 68 if err := fixFile(c, stubFile, stubFile.Path); err != nil { 69 t.Errorf("fixFile(%#v) failed with %v; want success", stubFile, err) 70 return 71 } 72 73 buf, err := ioutil.ReadFile(stubFile.Path) 74 if err != nil { 75 t.Errorf("ioutil.ReadFile(%q) failed with %v; want success", stubFile.Path, err) 76 return 77 } 78 if got, want := string(buf), bf.FormatString(stubFile); got != want { 79 t.Errorf("buf = %q; want %q", got, want) 80 } 81 } 82 83 func TestCreateFile(t *testing.T) { 84 // Create a directory with a simple .go file. 85 tmpdir := os.Getenv("TEST_TMPDIR") 86 dir, err := ioutil.TempDir(tmpdir, "") 87 if err != nil { 88 t.Fatalf("ioutil.TempDir(%q, %q) failed with %v; want success", tmpdir, "", err) 89 } 90 defer os.RemoveAll(dir) 91 92 goFile := filepath.Join(dir, "main.go") 93 if err = ioutil.WriteFile(goFile, []byte("package main"), 0600); err != nil { 94 t.Fatalf("error writing file %q: %v", goFile, err) 95 } 96 97 // Check that Gazelle creates a new file named "BUILD.bazel". 98 run(defaultArgs(dir)) 99 100 buildFile := filepath.Join(dir, "BUILD.bazel") 101 if _, err = os.Stat(buildFile); err != nil { 102 t.Errorf("could not stat BUILD.bazel: %v", err) 103 } 104 } 105 106 func TestUpdateFile(t *testing.T) { 107 // Create a directory with a simple .go file and an empty BUILD file. 108 tmpdir := os.Getenv("TEST_TMPDIR") 109 dir, err := ioutil.TempDir(tmpdir, "") 110 if err != nil { 111 t.Fatalf("ioutil.TempDir(%q, %q) failed with %v; want success", tmpdir, "", err) 112 } 113 defer os.RemoveAll(dir) 114 115 goFile := filepath.Join(dir, "main.go") 116 if err = ioutil.WriteFile(goFile, []byte("package main"), 0600); err != nil { 117 t.Fatalf("error writing file %q: %v", goFile, err) 118 } 119 120 buildFile := filepath.Join(dir, "BUILD") 121 if err = ioutil.WriteFile(buildFile, nil, 0600); err != nil { 122 t.Fatalf("error writing file %q: %v", buildFile, err) 123 } 124 125 // Check that Gazelle updates the BUILD file in place. 126 run(defaultArgs(dir)) 127 if st, err := os.Stat(buildFile); err != nil { 128 t.Errorf("could not stat BUILD: %v", err) 129 } else if st.Size() == 0 { 130 t.Errorf("BUILD was not updated") 131 } 132 133 if _, err = os.Stat(filepath.Join(dir, "BUILD.bazel")); err == nil { 134 t.Errorf("BUILD.bazel should not exist") 135 } 136 }