github.com/sdboyer/gps@v0.16.3/lockdiff_test.go (about)

     1  package gps
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/hex"
     6  	"testing"
     7  )
     8  
     9  func TestStringDiff_NoChange(t *testing.T) {
    10  	diff := StringDiff{Previous: "foo", Current: "foo"}
    11  	want := "foo"
    12  	got := diff.String()
    13  	if got != want {
    14  		t.Fatalf("Expected '%s', got '%s'", want, got)
    15  	}
    16  }
    17  
    18  func TestStringDiff_Add(t *testing.T) {
    19  	diff := StringDiff{Current: "foo"}
    20  	got := diff.String()
    21  	if got != "+ foo" {
    22  		t.Fatalf("Expected '+ foo', got '%s'", got)
    23  	}
    24  }
    25  
    26  func TestStringDiff_Remove(t *testing.T) {
    27  	diff := StringDiff{Previous: "foo"}
    28  	want := "- foo"
    29  	got := diff.String()
    30  	if got != want {
    31  		t.Fatalf("Expected '%s', got '%s'", want, got)
    32  	}
    33  }
    34  
    35  func TestStringDiff_Modify(t *testing.T) {
    36  	diff := StringDiff{Previous: "foo", Current: "bar"}
    37  	want := "foo -> bar"
    38  	got := diff.String()
    39  	if got != want {
    40  		t.Fatalf("Expected '%s', got '%s'", want, got)
    41  	}
    42  }
    43  
    44  func TestDiffProjects_NoChange(t *testing.T) {
    45  	p1 := NewLockedProject(mkPI("github.com/sdboyer/gps"), NewVersion("v0.10.0"), []string{"gps"})
    46  	p2 := NewLockedProject(mkPI("github.com/sdboyer/gps"), NewVersion("v0.10.0"), []string{"gps"})
    47  
    48  	diff := DiffProjects(p1, p2)
    49  	if diff != nil {
    50  		t.Fatal("Expected the diff to be nil")
    51  	}
    52  }
    53  
    54  func TestDiffProjects_Modify(t *testing.T) {
    55  	p1 := LockedProject{
    56  		pi:   ProjectIdentifier{ProjectRoot: "github.com/foo/bar"},
    57  		v:    NewBranch("master"),
    58  		r:    "abc123",
    59  		pkgs: []string{"baz", "qux"},
    60  	}
    61  
    62  	p2 := LockedProject{
    63  		pi:   ProjectIdentifier{ProjectRoot: "github.com/foo/bar", Source: "https://github.com/mcfork/gps.git"},
    64  		v:    NewVersion("v1.0.0"),
    65  		r:    "def456",
    66  		pkgs: []string{"baz", "derp"},
    67  	}
    68  
    69  	diff := DiffProjects(p1, p2)
    70  	if diff == nil {
    71  		t.Fatal("Expected the diff to be populated")
    72  	}
    73  
    74  	wantSource := "+ https://github.com/mcfork/gps.git"
    75  	gotSource := diff.Source.String()
    76  	if gotSource != wantSource {
    77  		t.Fatalf("Expected diff.Source to be '%s', got '%s'", wantSource, diff.Source)
    78  	}
    79  
    80  	wantVersion := "+ v1.0.0"
    81  	gotVersion := diff.Version.String()
    82  	if gotVersion != wantVersion {
    83  		t.Fatalf("Expected diff.Version to be '%s', got '%s'", wantVersion, gotVersion)
    84  	}
    85  
    86  	wantRevision := "abc123 -> def456"
    87  	gotRevision := diff.Revision.String()
    88  	if gotRevision != wantRevision {
    89  		t.Fatalf("Expected diff.Revision to be '%s', got '%s'", wantRevision, gotRevision)
    90  	}
    91  
    92  	wantBranch := "- master"
    93  	gotBranch := diff.Branch.String()
    94  	if gotBranch != wantBranch {
    95  		t.Fatalf("Expected diff.Branch to be '%s', got '%s'", wantBranch, gotBranch)
    96  	}
    97  
    98  	fmtPkgs := func(pkgs []StringDiff) string {
    99  		b := bytes.NewBufferString("[")
   100  		for _, pkg := range pkgs {
   101  			b.WriteString(pkg.String())
   102  			b.WriteString(",")
   103  		}
   104  		b.WriteString("]")
   105  		return b.String()
   106  	}
   107  
   108  	wantPackages := "[+ derp,- qux,]"
   109  	gotPackages := fmtPkgs(diff.Packages)
   110  	if gotPackages != wantPackages {
   111  		t.Fatalf("Expected diff.Packages to be '%s', got '%s'", wantPackages, gotPackages)
   112  	}
   113  }
   114  
   115  func TestDiffProjects_AddPackages(t *testing.T) {
   116  	p1 := LockedProject{
   117  		pi:   ProjectIdentifier{ProjectRoot: "github.com/foo/bar"},
   118  		v:    NewBranch("master"),
   119  		r:    "abc123",
   120  		pkgs: []string{"foobar"},
   121  	}
   122  
   123  	p2 := LockedProject{
   124  		pi:   ProjectIdentifier{ProjectRoot: "github.com/foo/bar", Source: "https://github.com/mcfork/gps.git"},
   125  		v:    NewVersion("v1.0.0"),
   126  		r:    "def456",
   127  		pkgs: []string{"bazqux", "foobar", "zugzug"},
   128  	}
   129  
   130  	diff := DiffProjects(p1, p2)
   131  	if diff == nil {
   132  		t.Fatal("Expected the diff to be populated")
   133  	}
   134  
   135  	if len(diff.Packages) != 2 {
   136  		t.Fatalf("Expected diff.Packages to have 2 packages, got %d", len(diff.Packages))
   137  	}
   138  
   139  	want0 := "+ bazqux"
   140  	got0 := diff.Packages[0].String()
   141  	if got0 != want0 {
   142  		t.Fatalf("Expected diff.Packages[0] to contain %s, got %s", want0, got0)
   143  	}
   144  
   145  	want1 := "+ zugzug"
   146  	got1 := diff.Packages[1].String()
   147  	if got1 != want1 {
   148  		t.Fatalf("Expected diff.Packages[1] to contain %s, got %s", want1, got1)
   149  	}
   150  }
   151  
   152  func TestDiffProjects_RemovePackages(t *testing.T) {
   153  	p1 := LockedProject{
   154  		pi:   ProjectIdentifier{ProjectRoot: "github.com/foo/bar"},
   155  		v:    NewBranch("master"),
   156  		r:    "abc123",
   157  		pkgs: []string{"athing", "foobar"},
   158  	}
   159  
   160  	p2 := LockedProject{
   161  		pi:   ProjectIdentifier{ProjectRoot: "github.com/foo/bar", Source: "https://github.com/mcfork/gps.git"},
   162  		v:    NewVersion("v1.0.0"),
   163  		r:    "def456",
   164  		pkgs: []string{"bazqux"},
   165  	}
   166  
   167  	diff := DiffProjects(p1, p2)
   168  	if diff == nil {
   169  		t.Fatal("Expected the diff to be populated")
   170  	}
   171  
   172  	if len(diff.Packages) > 3 {
   173  		t.Fatalf("Expected diff.Packages to have 3 packages, got %d", len(diff.Packages))
   174  	}
   175  
   176  	want0 := "- athing"
   177  	got0 := diff.Packages[0].String()
   178  	if got0 != want0 {
   179  		t.Fatalf("Expected diff.Packages[0] to contain %s, got %s", want0, got0)
   180  	}
   181  
   182  	// diff.Packages[1] is '+ bazqux'
   183  
   184  	want2 := "- foobar"
   185  	got2 := diff.Packages[2].String()
   186  	if got2 != want2 {
   187  		t.Fatalf("Expected diff.Packages[2] to contain %s, got %s", want2, got2)
   188  	}
   189  }
   190  
   191  func TestDiffLocks_NoChange(t *testing.T) {
   192  	l1 := safeLock{
   193  		h: []byte("abc123"),
   194  		p: []LockedProject{
   195  			{pi: ProjectIdentifier{ProjectRoot: "github.com/foo/bar"}, v: NewVersion("v1.0.0")},
   196  		},
   197  	}
   198  	l2 := safeLock{
   199  		h: []byte("abc123"),
   200  		p: []LockedProject{
   201  			{pi: ProjectIdentifier{ProjectRoot: "github.com/foo/bar"}, v: NewVersion("v1.0.0")},
   202  		},
   203  	}
   204  
   205  	diff := DiffLocks(l1, l2)
   206  	if diff != nil {
   207  		t.Fatal("Expected the diff to be nil")
   208  	}
   209  }
   210  
   211  func TestDiffLocks_AddProjects(t *testing.T) {
   212  	l1 := safeLock{
   213  		h: []byte("abc123"),
   214  		p: []LockedProject{
   215  			{pi: ProjectIdentifier{ProjectRoot: "github.com/foo/bar"}, v: NewVersion("v1.0.0")},
   216  		},
   217  	}
   218  	l2 := safeLock{
   219  		h: []byte("abc123"),
   220  		p: []LockedProject{
   221  			{
   222  				pi:   ProjectIdentifier{ProjectRoot: "github.com/baz/qux", Source: "https://github.com/mcfork/bazqux.git"},
   223  				v:    NewVersion("v0.5.0"),
   224  				r:    "def456",
   225  				pkgs: []string{"p1", "p2"},
   226  			},
   227  			{pi: ProjectIdentifier{ProjectRoot: "github.com/foo/bar"}, v: NewVersion("v1.0.0")},
   228  			{pi: ProjectIdentifier{ProjectRoot: "github.com/zug/zug"}, v: NewVersion("v1.0.0")},
   229  		},
   230  	}
   231  
   232  	diff := DiffLocks(l1, l2)
   233  	if diff == nil {
   234  		t.Fatal("Expected the diff to be populated")
   235  	}
   236  
   237  	if len(diff.Add) != 2 {
   238  		t.Fatalf("Expected diff.Add to have 2 projects, got %d", len(diff.Add))
   239  	}
   240  
   241  	want0 := "github.com/baz/qux"
   242  	got0 := string(diff.Add[0].Name)
   243  	if got0 != want0 {
   244  		t.Fatalf("Expected diff.Add[0] to contain %s, got %s", want0, got0)
   245  	}
   246  
   247  	want1 := "github.com/zug/zug"
   248  	got1 := string(diff.Add[1].Name)
   249  	if got1 != want1 {
   250  		t.Fatalf("Expected diff.Add[1] to contain %s, got %s", want1, got1)
   251  	}
   252  
   253  	add0 := diff.Add[0]
   254  	wantSource := "https://github.com/mcfork/bazqux.git"
   255  	gotSource := add0.Source.String()
   256  	if gotSource != wantSource {
   257  		t.Fatalf("Expected diff.Add[0].Source to be '%s', got '%s'", wantSource, add0.Source)
   258  	}
   259  
   260  	wantVersion := "v0.5.0"
   261  	gotVersion := add0.Version.String()
   262  	if gotVersion != wantVersion {
   263  		t.Fatalf("Expected diff.Add[0].Version to be '%s', got '%s'", wantVersion, gotVersion)
   264  	}
   265  
   266  	wantRevision := "def456"
   267  	gotRevision := add0.Revision.String()
   268  	if gotRevision != wantRevision {
   269  		t.Fatalf("Expected diff.Add[0].Revision to be '%s', got '%s'", wantRevision, gotRevision)
   270  	}
   271  
   272  	wantBranch := ""
   273  	gotBranch := add0.Branch.String()
   274  	if gotBranch != wantBranch {
   275  		t.Fatalf("Expected diff.Add[0].Branch to be '%s', got '%s'", wantBranch, gotBranch)
   276  	}
   277  
   278  	fmtPkgs := func(pkgs []StringDiff) string {
   279  		b := bytes.NewBufferString("[")
   280  		for _, pkg := range pkgs {
   281  			b.WriteString(pkg.String())
   282  			b.WriteString(",")
   283  		}
   284  		b.WriteString("]")
   285  		return b.String()
   286  	}
   287  
   288  	wantPackages := "[p1,p2,]"
   289  	gotPackages := fmtPkgs(add0.Packages)
   290  	if gotPackages != wantPackages {
   291  		t.Fatalf("Expected diff.Add[0].Packages to be '%s', got '%s'", wantPackages, gotPackages)
   292  	}
   293  }
   294  
   295  func TestDiffLocks_RemoveProjects(t *testing.T) {
   296  	l1 := safeLock{
   297  		h: []byte("abc123"),
   298  		p: []LockedProject{
   299  			{
   300  				pi:   ProjectIdentifier{ProjectRoot: "github.com/a/thing", Source: "https://github.com/mcfork/athing.git"},
   301  				v:    NewBranch("master"),
   302  				r:    "def456",
   303  				pkgs: []string{"p1", "p2"},
   304  			},
   305  			{pi: ProjectIdentifier{ProjectRoot: "github.com/foo/bar"}, v: NewVersion("v1.0.0")},
   306  		},
   307  	}
   308  	l2 := safeLock{
   309  		h: []byte("abc123"),
   310  		p: []LockedProject{
   311  			{pi: ProjectIdentifier{ProjectRoot: "github.com/baz/qux"}, v: NewVersion("v1.0.0")},
   312  		},
   313  	}
   314  
   315  	diff := DiffLocks(l1, l2)
   316  	if diff == nil {
   317  		t.Fatal("Expected the diff to be populated")
   318  	}
   319  
   320  	if len(diff.Remove) != 2 {
   321  		t.Fatalf("Expected diff.Remove to have 2 projects, got %d", len(diff.Remove))
   322  	}
   323  
   324  	want0 := "github.com/a/thing"
   325  	got0 := string(diff.Remove[0].Name)
   326  	if got0 != want0 {
   327  		t.Fatalf("Expected diff.Remove[0] to contain %s, got %s", want0, got0)
   328  	}
   329  
   330  	want1 := "github.com/foo/bar"
   331  	got1 := string(diff.Remove[1].Name)
   332  	if got1 != want1 {
   333  		t.Fatalf("Expected diff.Remove[1] to contain %s, got %s", want1, got1)
   334  	}
   335  
   336  	remove0 := diff.Remove[0]
   337  	wantSource := "https://github.com/mcfork/athing.git"
   338  	gotSource := remove0.Source.String()
   339  	if gotSource != wantSource {
   340  		t.Fatalf("Expected diff.Remove[0].Source to be '%s', got '%s'", wantSource, remove0.Source)
   341  	}
   342  
   343  	wantVersion := ""
   344  	gotVersion := remove0.Version.String()
   345  	if gotVersion != wantVersion {
   346  		t.Fatalf("Expected diff.Remove[0].Version to be '%s', got '%s'", wantVersion, gotVersion)
   347  	}
   348  
   349  	wantRevision := "def456"
   350  	gotRevision := remove0.Revision.String()
   351  	if gotRevision != wantRevision {
   352  		t.Fatalf("Expected diff.Remove[0].Revision to be '%s', got '%s'", wantRevision, gotRevision)
   353  	}
   354  
   355  	wantBranch := "master"
   356  	gotBranch := remove0.Branch.String()
   357  	if gotBranch != wantBranch {
   358  		t.Fatalf("Expected diff.Remove[0].Branch to be '%s', got '%s'", wantBranch, gotBranch)
   359  	}
   360  
   361  	fmtPkgs := func(pkgs []StringDiff) string {
   362  		b := bytes.NewBufferString("[")
   363  		for _, pkg := range pkgs {
   364  			b.WriteString(pkg.String())
   365  			b.WriteString(",")
   366  		}
   367  		b.WriteString("]")
   368  		return b.String()
   369  	}
   370  
   371  	wantPackages := "[p1,p2,]"
   372  	gotPackages := fmtPkgs(remove0.Packages)
   373  	if gotPackages != wantPackages {
   374  		t.Fatalf("Expected diff.Remove[0].Packages to be '%s', got '%s'", wantPackages, gotPackages)
   375  	}
   376  }
   377  
   378  func TestDiffLocks_ModifyProjects(t *testing.T) {
   379  	l1 := safeLock{
   380  		h: []byte("abc123"),
   381  		p: []LockedProject{
   382  			{pi: ProjectIdentifier{ProjectRoot: "github.com/foo/bar"}, v: NewVersion("v1.0.0")},
   383  			{pi: ProjectIdentifier{ProjectRoot: "github.com/foo/bu"}, v: NewVersion("v1.0.0")},
   384  			{pi: ProjectIdentifier{ProjectRoot: "github.com/zig/zag"}, v: NewVersion("v1.0.0")},
   385  		},
   386  	}
   387  	l2 := safeLock{
   388  		h: []byte("abc123"),
   389  		p: []LockedProject{
   390  			{pi: ProjectIdentifier{ProjectRoot: "github.com/baz/qux"}, v: NewVersion("v1.0.0")},
   391  			{pi: ProjectIdentifier{ProjectRoot: "github.com/foo/bar"}, v: NewVersion("v2.0.0")},
   392  			{pi: ProjectIdentifier{ProjectRoot: "github.com/zig/zag"}, v: NewVersion("v2.0.0")},
   393  			{pi: ProjectIdentifier{ProjectRoot: "github.com/zug/zug"}, v: NewVersion("v1.0.0")},
   394  		},
   395  	}
   396  
   397  	diff := DiffLocks(l1, l2)
   398  	if diff == nil {
   399  		t.Fatal("Expected the diff to be populated")
   400  	}
   401  
   402  	if len(diff.Modify) != 2 {
   403  		t.Fatalf("Expected diff.Remove to have 2 projects, got %d", len(diff.Remove))
   404  	}
   405  
   406  	want0 := "github.com/foo/bar"
   407  	got0 := string(diff.Modify[0].Name)
   408  	if got0 != want0 {
   409  		t.Fatalf("Expected diff.Modify[0] to contain %s, got %s", want0, got0)
   410  	}
   411  
   412  	want1 := "github.com/zig/zag"
   413  	got1 := string(diff.Modify[1].Name)
   414  	if got1 != want1 {
   415  		t.Fatalf("Expected diff.Modify[1] to contain %s, got %s", want1, got1)
   416  	}
   417  }
   418  
   419  func TestDiffLocks_ModifyHash(t *testing.T) {
   420  	h1, _ := hex.DecodeString("abc123")
   421  	l1 := safeLock{
   422  		h: h1,
   423  		p: []LockedProject{
   424  			{pi: ProjectIdentifier{ProjectRoot: "github.com/foo/bar"}, v: NewVersion("v1.0.0")},
   425  		},
   426  	}
   427  
   428  	h2, _ := hex.DecodeString("def456")
   429  	l2 := safeLock{
   430  		h: h2,
   431  		p: []LockedProject{
   432  			{pi: ProjectIdentifier{ProjectRoot: "github.com/foo/bar"}, v: NewVersion("v1.0.0")},
   433  		},
   434  	}
   435  
   436  	diff := DiffLocks(l1, l2)
   437  	if diff == nil {
   438  		t.Fatal("Expected the diff to be populated")
   439  	}
   440  
   441  	want := "abc123 -> def456"
   442  	got := diff.HashDiff.String()
   443  	if got != want {
   444  		t.Fatalf("Expected diff.HashDiff to be '%s', got '%s'", want, got)
   445  	}
   446  }
   447  
   448  func TestDiffLocks_EmptyInitialLock(t *testing.T) {
   449  	h2, _ := hex.DecodeString("abc123")
   450  	l2 := safeLock{
   451  		h: h2,
   452  		p: []LockedProject{
   453  			{pi: ProjectIdentifier{ProjectRoot: "github.com/foo/bar"}, v: NewVersion("v1.0.0")},
   454  		},
   455  	}
   456  
   457  	diff := DiffLocks(nil, l2)
   458  
   459  	wantHash := "+ abc123"
   460  	gotHash := diff.HashDiff.String()
   461  	if gotHash != wantHash {
   462  		t.Fatalf("Expected diff.HashDiff to be '%s', got '%s'", wantHash, gotHash)
   463  	}
   464  
   465  	if len(diff.Add) != 1 {
   466  		t.Fatalf("Expected diff.Add to contain 1 project, got %d", len(diff.Add))
   467  	}
   468  }
   469  
   470  func TestDiffLocks_EmptyFinalLock(t *testing.T) {
   471  	h1, _ := hex.DecodeString("abc123")
   472  	l1 := safeLock{
   473  		h: h1,
   474  		p: []LockedProject{
   475  			{pi: ProjectIdentifier{ProjectRoot: "github.com/foo/bar"}, v: NewVersion("v1.0.0")},
   476  		},
   477  	}
   478  
   479  	diff := DiffLocks(l1, nil)
   480  
   481  	wantHash := "- abc123"
   482  	gotHash := diff.HashDiff.String()
   483  	if gotHash != wantHash {
   484  		t.Fatalf("Expected diff.HashDiff to be '%s', got '%s'", wantHash, gotHash)
   485  	}
   486  
   487  	if len(diff.Remove) != 1 {
   488  		t.Fatalf("Expected diff.Remove to contain 1 project, got %d", len(diff.Remove))
   489  	}
   490  }
   491  
   492  func TestDiffLocks_EmptyLocks(t *testing.T) {
   493  	diff := DiffLocks(nil, nil)
   494  	if diff != nil {
   495  		t.Fatal("Expected the diff to be empty")
   496  	}
   497  }