2016-10-18 07:07:38 +03:00
|
|
|
// Copyright 2016 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2017-01-27 23:39:07 +03:00
|
|
|
package dep
|
2016-10-18 07:07:38 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2017-02-01 23:14:41 +03:00
|
|
|
"github.com/golang/dep/test"
|
2016-10-18 07:07:38 +03:00
|
|
|
"github.com/sdboyer/gps"
|
|
|
|
)
|
|
|
|
|
2016-11-30 20:48:06 +03:00
|
|
|
func TestReadManifest(t *testing.T) {
|
2017-02-01 23:14:41 +03:00
|
|
|
h := test.NewHelper(t)
|
2017-02-02 05:18:35 +03:00
|
|
|
defer h.Cleanup()
|
|
|
|
|
2017-02-27 21:47:49 +03:00
|
|
|
mf := h.GetTestFile("manifest/golden.json")
|
|
|
|
defer mf.Close()
|
|
|
|
got, err := readManifest(mf)
|
2016-10-18 07:07:38 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Should have read Manifest correctly, but got err %q", err)
|
|
|
|
}
|
|
|
|
|
2016-11-30 20:48:06 +03:00
|
|
|
c, _ := gps.NewSemverConstraint(">=0.12.0, <1.0.0")
|
2017-02-10 21:08:47 +03:00
|
|
|
want := Manifest{
|
2016-10-18 07:07:38 +03:00
|
|
|
Dependencies: map[gps.ProjectRoot]gps.ProjectProperties{
|
|
|
|
gps.ProjectRoot("github.com/sdboyer/gps"): {
|
|
|
|
Constraint: c,
|
|
|
|
},
|
|
|
|
gps.ProjectRoot("github.com/babble/brook"): {
|
|
|
|
Constraint: gps.Revision("d05d5aca9f895d19e9265839bffeadd74a2d2ecb"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Ovr: map[gps.ProjectRoot]gps.ProjectProperties{
|
|
|
|
gps.ProjectRoot("github.com/sdboyer/gps"): {
|
2017-01-13 22:17:20 +03:00
|
|
|
Source: "https://github.com/sdboyer/gps",
|
|
|
|
Constraint: gps.NewBranch("master"),
|
2016-10-18 07:07:38 +03:00
|
|
|
},
|
|
|
|
},
|
|
|
|
Ignores: []string{"github.com/foo/bar"},
|
|
|
|
}
|
|
|
|
|
2017-02-10 21:08:47 +03:00
|
|
|
if !reflect.DeepEqual(got.Dependencies, want.Dependencies) {
|
2016-10-18 07:07:38 +03:00
|
|
|
t.Error("Valid manifest's dependencies did not parse as expected")
|
|
|
|
}
|
2017-02-10 21:08:47 +03:00
|
|
|
if !reflect.DeepEqual(got.Ovr, want.Ovr) {
|
2016-10-18 07:07:38 +03:00
|
|
|
t.Error("Valid manifest's overrides did not parse as expected")
|
|
|
|
}
|
2017-02-10 21:08:47 +03:00
|
|
|
if !reflect.DeepEqual(got.Ignores, want.Ignores) {
|
2016-10-18 07:07:38 +03:00
|
|
|
t.Error("Valid manifest's ignores did not parse as expected")
|
|
|
|
}
|
|
|
|
}
|
2016-11-30 20:48:06 +03:00
|
|
|
|
|
|
|
func TestWriteManifest(t *testing.T) {
|
2017-02-01 23:14:41 +03:00
|
|
|
h := test.NewHelper(t)
|
2017-02-02 05:18:35 +03:00
|
|
|
defer h.Cleanup()
|
|
|
|
|
2017-03-20 21:13:48 +03:00
|
|
|
golden := "manifest/golden.toml"
|
2017-02-10 21:08:47 +03:00
|
|
|
want := h.GetTestFileString(golden)
|
2016-11-30 20:48:06 +03:00
|
|
|
c, _ := gps.NewSemverConstraint("^v0.12.0")
|
2017-01-27 23:39:07 +03:00
|
|
|
m := &Manifest{
|
2016-11-30 20:48:06 +03:00
|
|
|
Dependencies: map[gps.ProjectRoot]gps.ProjectProperties{
|
|
|
|
gps.ProjectRoot("github.com/sdboyer/gps"): {
|
|
|
|
Constraint: c,
|
|
|
|
},
|
|
|
|
gps.ProjectRoot("github.com/babble/brook"): {
|
|
|
|
Constraint: gps.Revision("d05d5aca9f895d19e9265839bffeadd74a2d2ecb"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Ovr: map[gps.ProjectRoot]gps.ProjectProperties{
|
|
|
|
gps.ProjectRoot("github.com/sdboyer/gps"): {
|
2017-01-13 22:17:20 +03:00
|
|
|
Source: "https://github.com/sdboyer/gps",
|
|
|
|
Constraint: gps.NewBranch("master"),
|
2016-11-30 20:48:06 +03:00
|
|
|
},
|
|
|
|
},
|
|
|
|
Ignores: []string{"github.com/foo/bar"},
|
|
|
|
}
|
|
|
|
|
2017-03-20 21:14:02 +03:00
|
|
|
got, err := m.MarshalTOML()
|
2016-11-30 20:48:06 +03:00
|
|
|
if err != nil {
|
2017-03-20 21:13:48 +03:00
|
|
|
t.Fatalf("Error while marshaling valid manifest to TOML: %q", err)
|
2016-11-30 20:48:06 +03:00
|
|
|
}
|
|
|
|
|
2017-02-10 21:08:47 +03:00
|
|
|
if string(got) != want {
|
2017-02-09 05:37:31 +03:00
|
|
|
if *test.UpdateGolden {
|
2017-02-10 21:08:47 +03:00
|
|
|
if err = h.WriteTestFile(golden, string(got)); err != nil {
|
2017-02-09 05:37:31 +03:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
} else {
|
2017-03-20 21:13:48 +03:00
|
|
|
t.Errorf("Valid manifest did not marshal to TOML as expected:\n\t(GOT): %s\n\t(WNT): %s", string(got), want)
|
2017-02-10 21:08:47 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestReadManifestErrors(t *testing.T) {
|
|
|
|
h := test.NewHelper(t)
|
|
|
|
defer h.Cleanup()
|
|
|
|
var err error
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
file string
|
|
|
|
}{
|
|
|
|
{"multiple constraints", "manifest/error.json"},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tst := range tests {
|
2017-02-27 21:47:49 +03:00
|
|
|
mf := h.GetTestFile(tst.file)
|
|
|
|
defer mf.Close()
|
|
|
|
_, err = readManifest(mf)
|
2017-02-10 21:08:47 +03:00
|
|
|
if err == nil {
|
|
|
|
t.Errorf("Reading manifest with %s should have caused error, but did not", tst.name)
|
|
|
|
} else if !strings.Contains(err.Error(), tst.name) {
|
|
|
|
t.Errorf("Unexpected error %q; expected %s error", err, tst.name)
|
2017-02-09 05:37:31 +03:00
|
|
|
}
|
2016-11-30 20:48:06 +03:00
|
|
|
}
|
|
|
|
}
|