Skip to content
This repository has been archived by the owner on Sep 9, 2020. It is now read-only.

Commit

Permalink
Merge pull request #1460 from arbourd/prune-init
Browse files Browse the repository at this point in the history
Add default prune options for init cmd
  • Loading branch information
carolynvs authored Dec 24, 2017
2 parents bf5b9fc + dd15e17 commit cf67c0a
Show file tree
Hide file tree
Showing 28 changed files with 189 additions and 11 deletions.
3 changes: 3 additions & 0 deletions cmd/dep/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,7 @@
[[constraint]]
branch = "master"
name = "github.com/sdboyer/deptesttres"

[prune]
go-tests = true
unused-packages = true
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

[prune]
go-tests = true
unused-packages = true
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,7 @@
[[constraint]]
name = "github.com/sdboyer/deptest"
version = "1.0.0"

[prune]
go-tests = true
unused-packages = true
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

[prune]
go-tests = true
unused-packages = true
4 changes: 4 additions & 0 deletions cmd/dep/testdata/harness_tests/init/case1/final/Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,7 @@
[[constraint]]
name = "github.com/sdboyer/deptest"
version = "0.8.0"

[prune]
go-tests = true
unused-packages = true
4 changes: 4 additions & 0 deletions cmd/dep/testdata/harness_tests/init/case2/final/Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,7 @@
[[constraint]]
name = "github.com/sdboyer/deptestdos"
version = "2.0.0"

[prune]
go-tests = true
unused-packages = true
4 changes: 4 additions & 0 deletions cmd/dep/testdata/harness_tests/init/case3/final/Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,7 @@
[[constraint]]
branch = "master"
name = "github.com/sdboyer/deptest"

[prune]
go-tests = true
unused-packages = true
4 changes: 4 additions & 0 deletions cmd/dep/testdata/harness_tests/init/case4/final/Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,7 @@
[[constraint]]
name = "github.com/sdboyer/deptestdos"
version = "2.0.0"

[prune]
go-tests = true
unused-packages = true
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,7 @@
[[constraint]]
name = "github.com/sdboyer/deptestdos"
version = "2.0.0"

[prune]
go-tests = true
unused-packages = true
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,7 @@
[[constraint]]
name = "github.com/carolynvs/deptestglide"
version = "0.1.1"

[prune]
go-tests = true
unused-packages = true
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,7 @@
[[constraint]]
name = "github.com/sdboyer/deptestdos"
version = "2.0.0"

[prune]
go-tests = true
unused-packages = true
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,7 @@
[[constraint]]
name = "github.com/sdboyer/deptestdos"
version = "2.0.0"

[prune]
go-tests = true
unused-packages = true
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,7 @@
[[constraint]]
name = "github.com/sdboyer/deptestdos"
version = "2.0.0"

[prune]
go-tests = true
unused-packages = true
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,7 @@
[[constraint]]
branch = "v2"
name = "gopkg.in/yaml.v2"

[prune]
go-tests = true
unused-packages = true
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,7 @@
[[constraint]]
branch = "master"
name = "github.com/sdboyer/deptest"

[prune]
go-tests = true
unused-packages = true
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,7 @@
[[constraint]]
name = "github.com/sdboyer/deptest"
version = "1.0.0"

[prune]
go-tests = true
unused-packages = true
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,7 @@
[[constraint]]
name = "github.com/sdboyer/deptestdos"
version = "2.0.0"

[prune]
go-tests = true
unused-packages = true
37 changes: 30 additions & 7 deletions manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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 -
Expand Down Expand Up @@ -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 {
Expand All @@ -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
}
Expand Down
50 changes: 50 additions & 0 deletions manifest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
9 changes: 7 additions & 2 deletions testdata/txn_writer/expected_manifest.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]]
Expand Down
9 changes: 7 additions & 2 deletions txn_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
`)

Expand Down

0 comments on commit cf67c0a

Please sign in to comment.