Merge pull request #1460 from arbourd/prune-init

Add default prune options for init cmd
This commit is contained in:
Carolyn Van Slyck 2017-12-23 22:01:11 -06:00 коммит произвёл GitHub
Родитель bf5b9fc8d8 dd15e17728
Коммит cf67c0a950
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
28 изменённых файлов: 189 добавлений и 11 удалений

Просмотреть файл

@ -152,6 +152,9 @@ func (cmd *initCommand) Run(ctx *dep.Ctx, args []string) error {
return errors.Wrap(err, "init failed: unable to prepare an initial manifest and lock for the solver")
}
// Set default prune options for go-tests and unused-packages
p.Manifest.PruneOptions.PruneOptions = gps.PruneNestedVendorDirs + gps.PruneGoTestFiles + gps.PruneUnusedPackages
if cmd.gopath {
gs := newGopathScanner(ctx, directDeps, sm)
err = gs.InitializeRootManifestAndLock(p.Manifest, p.Lock)

Просмотреть файл

@ -3,6 +3,10 @@
branch = "master"
name = "github.com/sdboyer/deptesttres"
[prune]
go-tests = true
unused-packages = true
[[constraint]]
name = "github.com/sdboyer/deptest"
version = "0.8.1"

Просмотреть файл

@ -3,6 +3,10 @@
name = "github.com/sdboyer/deptest"
version = "1.0.0"
[prune]
go-tests = true
unused-packages = true
[[constraint]]
branch = "master"
name = "github.com/sdboyer/deptesttres"

Просмотреть файл

@ -3,6 +3,10 @@
branch = "master"
name = "github.com/sdboyer/deptesttres"
[prune]
go-tests = true
unused-packages = true
[[constraint]]
name = "github.com/sdboyer/deptest"
version = "0.8.1"

Просмотреть файл

@ -3,6 +3,10 @@
branch = "master"
name = "github.com/sdboyer/deptesttres"
[prune]
go-tests = true
unused-packages = true
[[constraint]]
name = "github.com/sdboyer/deptest"
version = "1.0.0"

Просмотреть файл

@ -2,3 +2,7 @@
[[constraint]]
branch = "master"
name = "github.com/sdboyer/deptesttres"
[prune]
go-tests = true
unused-packages = true

Просмотреть файл

@ -0,0 +1,4 @@
[prune]
go-tests = true
unused-packages = true

Просмотреть файл

@ -2,3 +2,7 @@
[[constraint]]
name = "github.com/sdboyer/deptest"
version = "1.0.0"
[prune]
go-tests = true
unused-packages = true

Просмотреть файл

@ -0,0 +1,4 @@
[prune]
go-tests = true
unused-packages = true

Просмотреть файл

@ -2,3 +2,7 @@
[[constraint]]
name = "github.com/sdboyer/deptest"
version = "0.8.0"
[prune]
go-tests = true
unused-packages = true

Просмотреть файл

@ -6,3 +6,7 @@
[[constraint]]
name = "github.com/sdboyer/deptestdos"
version = "2.0.0"
[prune]
go-tests = true
unused-packages = true

Просмотреть файл

@ -2,3 +2,7 @@
[[constraint]]
branch = "master"
name = "github.com/sdboyer/deptest"
[prune]
go-tests = true
unused-packages = true

Просмотреть файл

@ -6,3 +6,7 @@
[[constraint]]
name = "github.com/sdboyer/deptestdos"
version = "2.0.0"
[prune]
go-tests = true
unused-packages = true

Просмотреть файл

@ -3,3 +3,7 @@ ignored = ["github.com/golang/notexist/samples","github.com/sdboyer/dep-test"]
[[constraint]]
name = "github.com/sdboyer/deptestdos"
version = "2.0.0"
[prune]
go-tests = true
unused-packages = true

Просмотреть файл

@ -2,3 +2,7 @@
[[constraint]]
name = "github.com/sdboyer/deptestdos"
version = "2.0.0"
[prune]
go-tests = true
unused-packages = true

Просмотреть файл

@ -2,3 +2,7 @@
[[constraint]]
name = "github.com/carolynvs/deptestglide"
version = "0.1.1"
[prune]
go-tests = true
unused-packages = true

Просмотреть файл

@ -2,3 +2,7 @@
[[constraint]]
name = "github.com/sdboyer/deptestdos"
version = "2.0.0"
[prune]
go-tests = true
unused-packages = true

Просмотреть файл

@ -2,3 +2,7 @@
[[constraint]]
name = "github.com/sdboyer/deptestdos"
version = "2.0.0"
[prune]
go-tests = true
unused-packages = true

Просмотреть файл

@ -2,3 +2,7 @@
[[constraint]]
name = "github.com/sdboyer/deptestdos"
version = "2.0.0"
[prune]
go-tests = true
unused-packages = true

Просмотреть файл

@ -3,3 +3,7 @@ ignored = ["github.com/golang/notexist/samples*","github.com/sdboyer/dep-test*"]
[[constraint]]
name = "github.com/sdboyer/deptestdos"
version = "2.0.0"
[prune]
go-tests = true
unused-packages = true

Просмотреть файл

@ -10,3 +10,7 @@
[[constraint]]
branch = "v2"
name = "gopkg.in/yaml.v2"
[prune]
go-tests = true
unused-packages = true

Просмотреть файл

@ -2,3 +2,7 @@
[[constraint]]
branch = "master"
name = "github.com/sdboyer/deptest"
[prune]
go-tests = true
unused-packages = true

Просмотреть файл

@ -2,3 +2,7 @@
[[constraint]]
name = "github.com/sdboyer/deptest"
version = "1.0.0"
[prune]
go-tests = true
unused-packages = true

Просмотреть файл

@ -2,3 +2,7 @@
[[constraint]]
name = "github.com/sdboyer/deptestdos"
version = "2.0.0"
[prune]
go-tests = true
unused-packages = true

Просмотреть файл

@ -15,7 +15,7 @@ import (
"github.com/golang/dep/gps"
"github.com/golang/dep/gps/pkgtree"
"github.com/pelletier/go-toml"
toml "github.com/pelletier/go-toml"
"github.com/pkg/errors"
)
@ -417,6 +417,30 @@ func fromRawPruneOptions(raw rawPruneOptions) gps.RootPruneOptions {
return opts
}
// toRawPruneOptions converts a gps.RootPruneOption's PruneOptions to rawPruneOptions
//
// Will panic if gps.RootPruneOption includes ProjectPruneOptions
// See https://github.com/golang/dep/pull/1460#discussion_r158128740 for more information
func toRawPruneOptions(root gps.RootPruneOptions) rawPruneOptions {
if len(root.ProjectOptions) != 0 {
panic("toRawPruneOptions cannot convert ProjectOptions to rawPruneOptions")
}
raw := rawPruneOptions{}
if (root.PruneOptions & gps.PruneUnusedPackages) != 0 {
raw.UnusedPackages = true
}
if (root.PruneOptions & gps.PruneNonGoFiles) != 0 {
raw.NonGoFiles = true
}
if (root.PruneOptions & gps.PruneGoTestFiles) != 0 {
raw.GoTests = true
}
return raw
}
// toProject interprets the string representations of project information held in
// a rawProject, converting them into a proper gps.ProjectProperties. An
// error is returned if the rawProject contains some invalid combination -
@ -462,11 +486,10 @@ func (m *Manifest) MarshalTOML() ([]byte, error) {
// toRaw converts the manifest into a representation suitable to write to the manifest file
func (m *Manifest) toRaw() rawManifest {
raw := rawManifest{
Constraints: make([]rawProject, 0, len(m.Constraints)),
Overrides: make([]rawProject, 0, len(m.Ovr)),
Ignored: m.Ignored,
Required: m.Required,
PruneOptions: rawPruneOptions{},
Constraints: make([]rawProject, 0, len(m.Constraints)),
Overrides: make([]rawProject, 0, len(m.Ovr)),
Ignored: m.Ignored,
Required: m.Required,
}
for n, prj := range m.Constraints {
@ -479,7 +502,7 @@ func (m *Manifest) toRaw() rawManifest {
}
sort.Sort(sortedRawProjects(raw.Overrides))
// TODO(ibrasho): write out prune options.
raw.PruneOptions = toRawPruneOptions(m.PruneOptions)
return raw
}

Просмотреть файл

@ -608,6 +608,56 @@ func TestValidateProjectRoots(t *testing.T) {
}
}
func TestToRawPruneOptions(t *testing.T) {
cases := []struct {
name string
pruneOptions gps.RootPruneOptions
wantOptions rawPruneOptions
}{
{
name: "all options",
pruneOptions: gps.RootPruneOptions{PruneOptions: 15},
wantOptions: rawPruneOptions{
UnusedPackages: true,
NonGoFiles: true,
GoTests: true,
},
},
{
name: "no options",
pruneOptions: gps.RootPruneOptions{PruneOptions: 1},
wantOptions: rawPruneOptions{
UnusedPackages: false,
NonGoFiles: false,
GoTests: false,
},
},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
raw := toRawPruneOptions(c.pruneOptions)
if !reflect.DeepEqual(raw, c.wantOptions) {
t.Fatalf("rawPruneOptions are not as expected:\n\t(GOT) %v\n\t(WNT) %v", raw, c.wantOptions)
}
})
}
}
func TestToRawPruneOptions_Panic(t *testing.T) {
pruneOptions := gps.RootPruneOptions{
PruneOptions: 1,
ProjectOptions: gps.PruneProjectOptions{"github.com/carolynvs/deptest": 1},
}
defer func() {
if err := recover(); err == nil {
t.Error("toRawPruneOptions did not panic with non-empty ProjectOptions")
}
}()
_ = toRawPruneOptions(pruneOptions)
}
func containsErr(s []error, e error) bool {
for _, a := range s {
if a.Error() == e.Error() {

9
testdata/txn_writer/expected_manifest.toml поставляемый
Просмотреть файл

@ -16,8 +16,13 @@
# source = "github.com/myfork/project2"
#
# [[override]]
# name = "github.com/x/y"
# version = "2.4.0"
# name = "github.com/x/y"
# version = "2.4.0"
#
# [prune]
# non-go = false
# go-tests = true
# unused-packages = true
[[constraint]]

Просмотреть файл

@ -39,8 +39,13 @@ var exampleTOML = []byte(`# Gopkg.toml example
# source = "github.com/myfork/project2"
#
# [[override]]
# name = "github.com/x/y"
# version = "2.4.0"
# name = "github.com/x/y"
# version = "2.4.0"
#
# [prune]
# non-go = false
# go-tests = true
# unused-packages = true
`)