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

Commit c92b728

Browse files
committed
Create BrokenImportFeedback and use in FinalizeRootManifest
- Creates a new feedback type to alert users of broken imports - Unit tests for above - Utillize new functionality in FinalizeRootManifestAndLock - Teach brokenImport String()
1 parent a926781 commit c92b728

File tree

3 files changed

+101
-0
lines changed

3 files changed

+101
-0
lines changed

cmd/dep/root_analyzer.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,9 @@ func (a *rootAnalyzer) DeriveManifestAndLock(dir string, pr gps.ProjectRoot) (gp
167167
func (a *rootAnalyzer) FinalizeRootManifestAndLock(m *dep.Manifest, l *dep.Lock, ol dep.Lock) {
168168
// Iterate through the new projects in solved lock and add them to manifest
169169
// if they are direct deps and log feedback for all the new projects.
170+
diff := gps.DiffLocks(&ol, l)
171+
bi := fb.NewBrokenImportFeedback(diff)
172+
bi.LogFeedback(a.ctx.Err)
170173
for _, y := range l.Projects() {
171174
var f *fb.ConstraintFeedback
172175
pr := y.Ident().ProjectRoot

internal/feedback/feedback.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,53 @@ func (cf ConstraintFeedback) LogFeedback(logger *log.Logger) {
8282
}
8383
}
8484

85+
type brokenImport struct {
86+
projectPath string
87+
version *gps.StringDiff
88+
revision *gps.StringDiff
89+
}
90+
91+
func (bi brokenImport) String() string {
92+
var pr string
93+
var cr string
94+
if bi.revision != nil {
95+
pr = fmt.Sprintf("(%s)", trimSHA(bi.revision.Previous))
96+
cr = fmt.Sprintf("(%s)", trimSHA(bi.revision.Current))
97+
}
98+
99+
return fmt.Sprintf("%v %s for %s. Locking in %v %s",
100+
bi.version.Previous,
101+
pr,
102+
bi.projectPath,
103+
bi.version.Current,
104+
cr,
105+
)
106+
}
107+
108+
// ConstraintFeedback holds project constraint feedback data
109+
type BrokenImportFeedback struct {
110+
brokenImports []brokenImport
111+
}
112+
113+
func NewBrokenImportFeedback(ld *gps.LockDiff) *BrokenImportFeedback {
114+
bi := &BrokenImportFeedback{}
115+
for _, lpd := range ld.Modify {
116+
bi.brokenImports = append(bi.brokenImports, brokenImport{
117+
projectPath: string(lpd.Name),
118+
version: lpd.Version,
119+
revision: lpd.Revision,
120+
})
121+
}
122+
123+
return bi
124+
}
125+
126+
func (b BrokenImportFeedback) LogFeedback(logger *log.Logger) {
127+
for _, bi := range b.brokenImports {
128+
logger.Printf("Warning: Unable to preserve imported lock %v\n", bi)
129+
}
130+
}
131+
85132
// GetUsingFeedback returns a dependency "using" feedback message. For example:
86133
//
87134
// Using ^1.0.0 as constraint for direct dep github.com/foo/bar
@@ -115,3 +162,14 @@ func GetLockingFeedback(version, revision, depType, projectPath string) string {
115162
}
116163
return fmt.Sprintf("Locking in %s (%s) for %s %s", version, revision, depType, projectPath)
117164
}
165+
166+
func trimSHA(revision string) string {
167+
if len(revision) == 40 {
168+
if _, err := hex.DecodeString(revision); err == nil {
169+
// Valid SHA1 digest
170+
revision = revision[0:7]
171+
}
172+
}
173+
174+
return revision
175+
}

internal/feedback/feedback_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"strings"
1111
"testing"
1212

13+
"github.com/golang/dep"
1314
"github.com/golang/dep/gps"
1415
)
1516

@@ -92,3 +93,42 @@ func TestFeedback_LockedProject(t *testing.T) {
9293
}
9394
}
9495
}
96+
97+
func TestFeedback_BrokenImport(t *testing.T) {
98+
ov := gps.NewVersion("v1.1.4").Pair("bc29b4f")
99+
lv := gps.NewVersion("v1.2.0").Pair("ia3da28")
100+
pi := gps.ProjectIdentifier{ProjectRoot: gps.ProjectRoot("github.com/foo/bar")}
101+
102+
ol := dep.Lock{
103+
P: []gps.LockedProject{gps.NewLockedProject(pi, ov, nil)},
104+
}
105+
l := dep.Lock{
106+
P: []gps.LockedProject{gps.NewLockedProject(pi, lv, nil)},
107+
}
108+
109+
diff := gps.DiffLocks(&ol, &l)
110+
111+
cases := []struct {
112+
feedback *BrokenImportFeedback
113+
want string
114+
name string
115+
}{
116+
{
117+
feedback: NewBrokenImportFeedback(diff),
118+
want: "Warning: Unable to preserve imported lock v1.1.4 (bc29b4f) for github.com/foo/bar. Locking in v1.2.0 (ia3da28)",
119+
name: "Basic broken import",
120+
},
121+
}
122+
123+
for _, c := range cases {
124+
t.Run(c.name, func(t *testing.T) {
125+
buf := &bytes.Buffer{}
126+
log := log2.New(buf, "", 0)
127+
c.feedback.LogFeedback(log)
128+
got := strings.TrimSpace(buf.String())
129+
if c.want != got {
130+
t.Errorf("Feedbacks are not expected: \n\t(GOT) '%s'\n\t(WNT) '%s'", got, c.want)
131+
}
132+
})
133+
}
134+
}

0 commit comments

Comments
 (0)