github.com/golang/dep@v0.5.4/internal/feedback/feedback_test.go (about)

     1  // Copyright 2017 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package feedback
     6  
     7  import (
     8  	"bytes"
     9  	log2 "log"
    10  	"strings"
    11  	"testing"
    12  
    13  	"github.com/golang/dep/gps"
    14  	_ "github.com/golang/dep/internal/test" // DO NOT REMOVE, allows go test ./... -update to work
    15  )
    16  
    17  func TestFeedback_Constraint(t *testing.T) {
    18  	ver, _ := gps.NewSemverConstraint("^1.0.0")
    19  	rev := gps.Revision("1b8edb3")
    20  	pi := gps.ProjectIdentifier{ProjectRoot: gps.ProjectRoot("github.com/foo/bar")}
    21  
    22  	cases := []struct {
    23  		feedback *ConstraintFeedback
    24  		want     string
    25  	}{
    26  		{
    27  			feedback: NewConstraintFeedback(gps.ProjectConstraint{Constraint: ver, Ident: pi}, DepTypeDirect),
    28  			want:     "Using ^1.0.0 as constraint for direct dep github.com/foo/bar",
    29  		},
    30  		{
    31  			feedback: NewConstraintFeedback(gps.ProjectConstraint{Constraint: ver, Ident: pi}, DepTypeImported),
    32  			want:     "Using ^1.0.0 as initial constraint for imported dep github.com/foo/bar",
    33  		},
    34  		{
    35  			feedback: NewConstraintFeedback(gps.ProjectConstraint{Constraint: gps.Any(), Ident: pi}, DepTypeImported),
    36  			want:     "Using * as initial constraint for imported dep github.com/foo/bar",
    37  		},
    38  		{
    39  			feedback: NewConstraintFeedback(gps.ProjectConstraint{Constraint: rev, Ident: pi}, DepTypeDirect),
    40  			want:     "Using 1b8edb3 as hint for direct dep github.com/foo/bar",
    41  		},
    42  		{
    43  			feedback: NewConstraintFeedback(gps.ProjectConstraint{Constraint: rev, Ident: pi}, DepTypeImported),
    44  			want:     "Using 1b8edb3 as initial hint for imported dep github.com/foo/bar",
    45  		},
    46  	}
    47  
    48  	for _, c := range cases {
    49  		buf := &bytes.Buffer{}
    50  		log := log2.New(buf, "", 0)
    51  		c.feedback.LogFeedback(log)
    52  		got := strings.TrimSpace(buf.String())
    53  		if c.want != got {
    54  			t.Errorf("Feedbacks are not expected: \n\t(GOT) '%s'\n\t(WNT) '%s'", got, c.want)
    55  		}
    56  	}
    57  }
    58  
    59  func TestFeedback_LockedProject(t *testing.T) {
    60  	v := gps.NewVersion("v1.1.4").Pair("bc29b4f")
    61  	b := gps.NewBranch("master").Pair("436f39d")
    62  	pi := gps.ProjectIdentifier{ProjectRoot: gps.ProjectRoot("github.com/foo/bar")}
    63  
    64  	cases := []struct {
    65  		feedback *ConstraintFeedback
    66  		want     string
    67  	}{
    68  		{
    69  			feedback: NewLockedProjectFeedback(gps.NewLockedProject(pi, v, nil), DepTypeDirect),
    70  			want:     "Locking in v1.1.4 (bc29b4f) for direct dep github.com/foo/bar",
    71  		},
    72  		{
    73  			feedback: NewLockedProjectFeedback(gps.NewLockedProject(pi, v, nil), DepTypeImported),
    74  			want:     "Trying v1.1.4 (bc29b4f) as initial lock for imported dep github.com/foo/bar",
    75  		},
    76  		{
    77  			feedback: NewLockedProjectFeedback(gps.NewLockedProject(pi, gps.NewVersion("").Pair("bc29b4f"), nil), DepTypeImported),
    78  			want:     "Trying * (bc29b4f) as initial lock for imported dep github.com/foo/bar",
    79  		},
    80  		{
    81  			feedback: NewLockedProjectFeedback(gps.NewLockedProject(pi, b, nil), DepTypeTransitive),
    82  			want:     "Locking in master (436f39d) for transitive dep github.com/foo/bar",
    83  		},
    84  	}
    85  
    86  	for _, c := range cases {
    87  		buf := &bytes.Buffer{}
    88  		log := log2.New(buf, "", 0)
    89  		c.feedback.LogFeedback(log)
    90  		got := strings.TrimSpace(buf.String())
    91  		if c.want != got {
    92  			t.Errorf("Feedbacks are not expected: \n\t(GOT) '%s'\n\t(WNT) '%s'", got, c.want)
    93  		}
    94  	}
    95  }
    96  
    97  func TestFeedback_BrokenImport(t *testing.T) {
    98  	pi := gps.ProjectIdentifier{ProjectRoot: gps.ProjectRoot("github.com/foo/bar")}
    99  
   100  	cases := []struct {
   101  		oldVersion     gps.Version
   102  		currentVersion gps.Version
   103  		pID            gps.ProjectIdentifier
   104  		altPID         gps.ProjectIdentifier
   105  		want           string
   106  		name           string
   107  	}{
   108  		{
   109  			oldVersion:     gps.NewVersion("v1.1.4").Pair("bc29b4f"),
   110  			currentVersion: gps.NewVersion("v1.2.0").Pair("ia3da28"),
   111  			pID:            pi,
   112  			altPID:         pi,
   113  			want:           "Warning: Unable to preserve imported lock v1.1.4 (bc29b4f) for github.com/foo/bar. Locking in v1.2.0 (ia3da28)",
   114  			name:           "Basic broken import",
   115  		},
   116  		{
   117  			oldVersion:     gps.NewBranch("master").Pair("bc29b4f"),
   118  			currentVersion: gps.NewBranch("dev").Pair("ia3da28"),
   119  			pID:            pi,
   120  			altPID:         pi,
   121  			want:           "Warning: Unable to preserve imported lock master (bc29b4f) for github.com/foo/bar. Locking in dev (ia3da28)",
   122  			name:           "Branches",
   123  		},
   124  		{
   125  			oldVersion:     gps.NewBranch("master").Pair("bc29b4f"),
   126  			currentVersion: gps.NewBranch("dev").Pair("ia3da28"),
   127  			pID:            pi,
   128  			altPID:         gps.ProjectIdentifier{ProjectRoot: gps.ProjectRoot("github.com/foo/boo")},
   129  			want:           "Warning: Unable to preserve imported lock master (bc29b4f) for github.com/foo/bar. The project was removed from the lock because it is not used.",
   130  			name:           "Branches",
   131  		},
   132  		{
   133  			oldVersion:     gps.NewBranch("master").Pair("bc29b4f"),
   134  			currentVersion: gps.NewBranch("dev").Pair("ia3da28"),
   135  			pID:            gps.ProjectIdentifier{ProjectRoot: gps.ProjectRoot("github.com/foo/boo"), Source: "github.com/das/foo"},
   136  			altPID:         gps.ProjectIdentifier{ProjectRoot: gps.ProjectRoot("github.com/foo/boo"), Source: "github.com/das/bar"},
   137  			want:           "Warning: Unable to preserve imported lock master (bc29b4f) for github.com/foo/boo(github.com/das/foo). Locking in dev (ia3da28) for github.com/foo/boo(github.com/das/bar)",
   138  			name:           "With a source",
   139  		},
   140  	}
   141  
   142  	for _, c := range cases {
   143  		t.Run(c.name, func(t *testing.T) {
   144  			buf := &bytes.Buffer{}
   145  			ol := gps.SimpleLock{
   146  				gps.NewLockedProject(c.pID, c.oldVersion, nil),
   147  			}
   148  			l := gps.SimpleLock{
   149  				gps.NewLockedProject(c.altPID, c.currentVersion, nil),
   150  			}
   151  			log := log2.New(buf, "", 0)
   152  			feedback := NewBrokenImportFeedback(DiffLocks(&ol, &l))
   153  			feedback.LogFeedback(log)
   154  			got := strings.TrimSpace(buf.String())
   155  			if c.want != got {
   156  				t.Errorf("Feedbacks are not expected: \n\t(GOT) '%s'\n\t(WNT) '%s'", got, c.want)
   157  			}
   158  		})
   159  	}
   160  }