github.com/afking/bazel-gazelle@v0.0.0-20180301150245-c02bc0f529e8/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 bf "github.com/bazelbuild/buildtools/build" 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 got := strings.TrimSpace(string(bf.Format(&bf.File{Stmt: rules}))) 80 want := strings.TrimSpace(` 81 go_repository( 82 name = "com_github_armon_go_radix", 83 commit = "4239b77079c7b5d1243b7b4736304ce8ddb6f0f2", 84 importpath = "github.com/armon/go-radix", 85 ) 86 87 go_repository( 88 name = "com_github_masterminds_semver", 89 commit = "a93e51b5a57ef416dac8bb02d11407b6f55d8929", 90 importpath = "github.com/Masterminds/semver", 91 remote = "https://github.com/carolynvs/semver.git", 92 ) 93 94 go_repository( 95 name = "com_github_masterminds_vcs", 96 commit = "3084677c2c188840777bff30054f2b553729d329", 97 importpath = "github.com/Masterminds/vcs", 98 ) 99 100 go_repository( 101 name = "org_golang_x_net", 102 commit = "66aacef3dd8a676686c7ae3716979581e8b03c47", 103 importpath = "golang.org/x/net", 104 ) 105 `) 106 if got != want { 107 t.Errorf("got %s ; want %s", got, want) 108 } 109 }