github.com/wolfd/bazel-gazelle@v0.14.0/internal/repos/import_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 repos
    17  
    18  import (
    19  	"io/ioutil"
    20  	"os"
    21  	"path/filepath"
    22  	"strings"
    23  	"testing"
    24  
    25  	"github.com/bazelbuild/bazel-gazelle/internal/rule"
    26  )
    27  
    28  func TestImportDep(t *testing.T) {
    29  	dir, err := ioutil.TempDir(os.Getenv("TEST_TEMPDIR"), "TestImportDep")
    30  	if err != nil {
    31  		t.Fatal(err)
    32  	}
    33  	defer os.RemoveAll(dir)
    34  	lockFilename := filepath.Join(dir, "Gopkg.lock")
    35  	lockContent := []byte(`
    36  # This is an abbreviated version of dep's Gopkg.lock
    37  # Retrieved 2017-12-20
    38  
    39  [[projects]]
    40    branch = "parse-constraints-with-dash-in-pre"
    41    name = "github.com/Masterminds/semver"
    42    packages = ["."]
    43    revision = "a93e51b5a57ef416dac8bb02d11407b6f55d8929"
    44    source = "https://github.com/carolynvs/semver.git"
    45  
    46  [[projects]]
    47    name = "github.com/Masterminds/vcs"
    48    packages = ["."]
    49    revision = "3084677c2c188840777bff30054f2b553729d329"
    50    version = "v1.11.1"
    51  
    52  [[projects]]
    53    branch = "master"
    54    name = "github.com/armon/go-radix"
    55    packages = ["."]
    56    revision = "4239b77079c7b5d1243b7b4736304ce8ddb6f0f2"
    57  
    58  [[projects]]
    59    branch = "master"
    60    name = "golang.org/x/net"
    61    packages = ["context"]
    62    revision = "66aacef3dd8a676686c7ae3716979581e8b03c47"
    63  
    64  [solve-meta]
    65    analyzer-name = "dep"
    66    analyzer-version = 1
    67    inputs-digest = "05c1cd69be2c917c0cc4b32942830c2acfa044d8200fdc94716aae48a8083702"
    68    solver-name = "gps-cdcl"
    69    solver-version = 1
    70  `)
    71  	if err := ioutil.WriteFile(lockFilename, lockContent, 0666); err != nil {
    72  		t.Fatal(err)
    73  	}
    74  
    75  	rules, err := ImportRepoRules(lockFilename)
    76  	if err != nil {
    77  		t.Fatal(err)
    78  	}
    79  	f := rule.EmptyFile("test", "")
    80  	for _, r := range rules {
    81  		r.Insert(f)
    82  	}
    83  	got := strings.TrimSpace(string(f.Format()))
    84  	want := strings.TrimSpace(`
    85  go_repository(
    86      name = "com_github_armon_go_radix",
    87      commit = "4239b77079c7b5d1243b7b4736304ce8ddb6f0f2",
    88      importpath = "github.com/armon/go-radix",
    89  )
    90  
    91  go_repository(
    92      name = "com_github_masterminds_semver",
    93      commit = "a93e51b5a57ef416dac8bb02d11407b6f55d8929",
    94      importpath = "github.com/Masterminds/semver",
    95      remote = "https://github.com/carolynvs/semver.git",
    96  )
    97  
    98  go_repository(
    99      name = "com_github_masterminds_vcs",
   100      commit = "3084677c2c188840777bff30054f2b553729d329",
   101      importpath = "github.com/Masterminds/vcs",
   102  )
   103  
   104  go_repository(
   105      name = "org_golang_x_net",
   106      commit = "66aacef3dd8a676686c7ae3716979581e8b03c47",
   107      importpath = "golang.org/x/net",
   108  )
   109  `)
   110  	if got != want {
   111  		t.Errorf("got %s ; want %s", got, want)
   112  	}
   113  }