github.com/golang/dep@v0.5.4/txn_writer_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 dep
     6  
     7  import (
     8  	"io/ioutil"
     9  	"os"
    10  	"path/filepath"
    11  	"strings"
    12  	"testing"
    13  
    14  	"github.com/golang/dep/gps"
    15  	"github.com/golang/dep/internal/test"
    16  	"github.com/pkg/errors"
    17  )
    18  
    19  const safeWriterProject = "safewritertest"
    20  const safeWriterGoldenManifest = "txn_writer/expected_manifest.toml"
    21  const safeWriterGoldenLock = "txn_writer/expected_lock.toml"
    22  
    23  func defaultCascadingPruneOptions() gps.CascadingPruneOptions {
    24  	return gps.CascadingPruneOptions{
    25  		DefaultOptions:    gps.PruneNestedVendorDirs,
    26  		PerProjectOptions: map[gps.ProjectRoot]gps.PruneOptionSet{},
    27  	}
    28  }
    29  
    30  func TestSafeWriter_BadInput_MissingRoot(t *testing.T) {
    31  	h := test.NewHelper(t)
    32  	defer h.Cleanup()
    33  	pc := NewTestProjectContext(h, safeWriterProject)
    34  	defer pc.Release()
    35  
    36  	sw, _ := NewSafeWriter(nil, nil, nil, VendorOnChanged, defaultCascadingPruneOptions(), nil)
    37  	err := sw.Write("", pc.SourceManager, true, nil)
    38  
    39  	if err == nil {
    40  		t.Fatal("should have errored without a root path, but did not")
    41  	} else if !strings.Contains(err.Error(), "root path") {
    42  		t.Fatalf("expected root path error, got %s", err.Error())
    43  	}
    44  }
    45  
    46  func TestSafeWriter_BadInput_MissingSourceManager(t *testing.T) {
    47  	h := test.NewHelper(t)
    48  	defer h.Cleanup()
    49  	pc := NewTestProjectContext(h, safeWriterProject)
    50  	defer pc.Release()
    51  	pc.CopyFile(LockName, safeWriterGoldenLock)
    52  	pc.Load()
    53  
    54  	sw, _ := NewSafeWriter(nil, nil, pc.Project.Lock, VendorAlways, defaultCascadingPruneOptions(), nil)
    55  	err := sw.Write(pc.Project.AbsRoot, nil, true, nil)
    56  
    57  	if err == nil {
    58  		t.Fatal("should have errored without a source manager when forceVendor is true, but did not")
    59  	} else if !strings.Contains(err.Error(), "SourceManager") {
    60  		t.Fatalf("expected SourceManager error, got %s", err.Error())
    61  	}
    62  }
    63  
    64  func TestSafeWriter_BadInput_ForceVendorMissingLock(t *testing.T) {
    65  	h := test.NewHelper(t)
    66  	defer h.Cleanup()
    67  	pc := NewTestProjectContext(h, safeWriterProject)
    68  	defer pc.Release()
    69  
    70  	_, err := NewSafeWriter(nil, nil, nil, VendorAlways, defaultCascadingPruneOptions(), nil)
    71  	if err == nil {
    72  		t.Fatal("should have errored without a lock when forceVendor is true, but did not")
    73  	} else if !strings.Contains(err.Error(), "newLock") {
    74  		t.Fatalf("expected newLock error, got %s", err.Error())
    75  	}
    76  }
    77  
    78  func TestSafeWriter_BadInput_OldLockOnly(t *testing.T) {
    79  	h := test.NewHelper(t)
    80  	defer h.Cleanup()
    81  	pc := NewTestProjectContext(h, safeWriterProject)
    82  	defer pc.Release()
    83  	pc.CopyFile(LockName, safeWriterGoldenLock)
    84  	pc.Load()
    85  
    86  	_, err := NewSafeWriter(nil, pc.Project.Lock, nil, VendorAlways, defaultCascadingPruneOptions(), nil)
    87  	if err == nil {
    88  		t.Fatal("should have errored with only an old lock, but did not")
    89  	} else if !strings.Contains(err.Error(), "oldLock") {
    90  		t.Fatalf("expected oldLock error, got %s", err.Error())
    91  	}
    92  }
    93  
    94  func TestSafeWriter_BadInput_NonexistentRoot(t *testing.T) {
    95  	h := test.NewHelper(t)
    96  	defer h.Cleanup()
    97  	pc := NewTestProjectContext(h, safeWriterProject)
    98  	defer pc.Release()
    99  
   100  	sw, _ := NewSafeWriter(nil, nil, nil, VendorOnChanged, defaultCascadingPruneOptions(), nil)
   101  
   102  	missingroot := filepath.Join(pc.Project.AbsRoot, "nonexistent")
   103  	err := sw.Write(missingroot, pc.SourceManager, true, nil)
   104  
   105  	if err == nil {
   106  		t.Fatal("should have errored with nonexistent dir for root path, but did not")
   107  	} else if !strings.Contains(err.Error(), "does not exist") {
   108  		t.Fatalf("expected does not exist error, got %s", err.Error())
   109  	}
   110  }
   111  
   112  func TestSafeWriter_BadInput_RootIsFile(t *testing.T) {
   113  	h := test.NewHelper(t)
   114  	defer h.Cleanup()
   115  	pc := NewTestProjectContext(h, safeWriterProject)
   116  	defer pc.Release()
   117  
   118  	sw, _ := NewSafeWriter(nil, nil, nil, VendorOnChanged, defaultCascadingPruneOptions(), nil)
   119  
   120  	fileroot := pc.CopyFile("fileroot", "txn_writer/badinput_fileroot")
   121  	err := sw.Write(fileroot, pc.SourceManager, true, nil)
   122  
   123  	if err == nil {
   124  		t.Fatal("should have errored when root path is a file, but did not")
   125  	} else if !strings.Contains(err.Error(), "does not exist") {
   126  		t.Fatalf("expected does not exist error, got %s", err.Error())
   127  	}
   128  }
   129  
   130  func TestSafeWriter_Manifest(t *testing.T) {
   131  	test.NeedsExternalNetwork(t)
   132  	test.NeedsGit(t)
   133  
   134  	h := test.NewHelper(t)
   135  	defer h.Cleanup()
   136  
   137  	pc := NewTestProjectContext(h, safeWriterProject)
   138  	defer pc.Release()
   139  	pc.CopyFile(ManifestName, safeWriterGoldenManifest)
   140  	pc.Load()
   141  
   142  	sw, _ := NewSafeWriter(pc.Project.Manifest, nil, nil, VendorOnChanged, defaultCascadingPruneOptions(), nil)
   143  
   144  	// Verify prepared actions
   145  	if !sw.HasManifest() {
   146  		t.Fatal("Expected the payload to contain the manifest")
   147  	}
   148  	if sw.HasLock() {
   149  		t.Fatal("Did not expect the payload to contain the lock")
   150  	}
   151  	if sw.writeVendor {
   152  		t.Fatal("Did not expect the payload to contain the vendor directory")
   153  	}
   154  
   155  	// Write changes
   156  	err := sw.Write(pc.Project.AbsRoot, pc.SourceManager, true, nil)
   157  	h.Must(errors.Wrap(err, "SafeWriter.Write failed"))
   158  
   159  	// Verify file system changes
   160  	if err := pc.ManifestShouldMatchGolden(safeWriterGoldenManifest); err != nil {
   161  		t.Fatal(err)
   162  	}
   163  	if err := pc.LockShouldNotExist(); err != nil {
   164  		t.Fatal(err)
   165  	}
   166  	if err := pc.VendorShouldNotExist(); err != nil {
   167  		t.Fatal(err)
   168  	}
   169  }
   170  
   171  func TestSafeWriter_ManifestAndUnmodifiedLock(t *testing.T) {
   172  	test.NeedsExternalNetwork(t)
   173  	test.NeedsGit(t)
   174  
   175  	h := test.NewHelper(t)
   176  	defer h.Cleanup()
   177  
   178  	pc := NewTestProjectContext(h, safeWriterProject)
   179  	defer pc.Release()
   180  	pc.CopyFile(ManifestName, safeWriterGoldenManifest)
   181  	pc.CopyFile(LockName, safeWriterGoldenLock)
   182  	pc.Load()
   183  
   184  	sw, _ := NewSafeWriter(pc.Project.Manifest, pc.Project.Lock, pc.Project.Lock, VendorOnChanged, defaultCascadingPruneOptions(), nil)
   185  
   186  	// Verify prepared actions
   187  	if !sw.HasManifest() {
   188  		t.Fatal("Expected the payload to contain the manifest")
   189  	}
   190  	if !sw.HasLock() {
   191  		t.Fatal("Expected the payload to contain the lock.")
   192  	}
   193  	if sw.writeLock {
   194  		t.Fatal("Did not expect that the writer should plan to write the lock")
   195  	}
   196  	if sw.writeVendor {
   197  		t.Fatal("Did not expect the payload to contain the vendor directory")
   198  	}
   199  
   200  	// Write changes
   201  	err := sw.Write(pc.Project.AbsRoot, pc.SourceManager, true, nil)
   202  	h.Must(errors.Wrap(err, "SafeWriter.Write failed"))
   203  
   204  	// Verify file system changes
   205  	if err := pc.ManifestShouldMatchGolden(safeWriterGoldenManifest); err != nil {
   206  		t.Fatal(err)
   207  	}
   208  	if err := pc.LockShouldMatchGolden(safeWriterGoldenLock); err != nil {
   209  		t.Fatal(err)
   210  	}
   211  	if err := pc.VendorShouldNotExist(); err != nil {
   212  		t.Fatal(err)
   213  	}
   214  }
   215  
   216  func TestSafeWriter_ManifestAndUnmodifiedLockWithForceVendor(t *testing.T) {
   217  	test.NeedsExternalNetwork(t)
   218  	test.NeedsGit(t)
   219  
   220  	h := test.NewHelper(t)
   221  	defer h.Cleanup()
   222  
   223  	pc := NewTestProjectContext(h, safeWriterProject)
   224  	defer pc.Release()
   225  	pc.CopyFile(ManifestName, safeWriterGoldenManifest)
   226  	pc.CopyFile(LockName, safeWriterGoldenLock)
   227  	pc.Load()
   228  
   229  	sw, _ := NewSafeWriter(pc.Project.Manifest, pc.Project.Lock, pc.Project.Lock, VendorAlways, defaultCascadingPruneOptions(), nil)
   230  
   231  	// Verify prepared actions
   232  	if !sw.HasManifest() {
   233  		t.Fatal("Expected the payload to contain the manifest")
   234  	}
   235  	if !sw.HasLock() {
   236  		t.Fatal("Expected the payload to contain the lock")
   237  	}
   238  	if sw.writeLock {
   239  		t.Fatal("Did not expect that the writer should plan to write the lock")
   240  	}
   241  	if !sw.writeVendor {
   242  		t.Fatal("Expected the payload to contain the vendor directory")
   243  	}
   244  
   245  	// Write changes
   246  	err := sw.Write(pc.Project.AbsRoot, pc.SourceManager, true, nil)
   247  	h.Must(errors.Wrap(err, "SafeWriter.Write failed"))
   248  
   249  	// Verify file system changes
   250  	if err := pc.ManifestShouldMatchGolden(safeWriterGoldenManifest); err != nil {
   251  		t.Fatal(err)
   252  	}
   253  	if err := pc.LockShouldMatchGolden(safeWriterGoldenLock); err != nil {
   254  		t.Fatal(err)
   255  	}
   256  	if err := pc.VendorShouldExist(); err != nil {
   257  		t.Fatal(err)
   258  	}
   259  	if err := pc.VendorFileShouldExist("github.com/sdboyer/dep-test"); err != nil {
   260  		t.Fatal(err)
   261  	}
   262  }
   263  
   264  func TestSafeWriter_ForceVendorWhenVendorAlreadyExists(t *testing.T) {
   265  	test.NeedsExternalNetwork(t)
   266  	test.NeedsGit(t)
   267  
   268  	h := test.NewHelper(t)
   269  	defer h.Cleanup()
   270  
   271  	pc := NewTestProjectContext(h, safeWriterProject)
   272  	defer pc.Release()
   273  	pc.CopyFile(LockName, safeWriterGoldenLock)
   274  	pc.Load()
   275  
   276  	sw, _ := NewSafeWriter(nil, pc.Project.Lock, pc.Project.Lock, VendorAlways, defaultCascadingPruneOptions(), nil)
   277  	err := sw.Write(pc.Project.AbsRoot, pc.SourceManager, true, nil)
   278  	h.Must(errors.Wrap(err, "SafeWriter.Write failed"))
   279  
   280  	// Verify prepared actions
   281  	sw, _ = NewSafeWriter(nil, nil, pc.Project.Lock, VendorAlways, defaultCascadingPruneOptions(), nil)
   282  	if sw.HasManifest() {
   283  		t.Fatal("Did not expect the payload to contain the manifest")
   284  	}
   285  	if !sw.HasLock() {
   286  		t.Fatal("Expected the payload to contain the lock")
   287  	}
   288  	if !sw.writeLock {
   289  		t.Fatal("Expected that the writer should plan to write the lock")
   290  	}
   291  	if !sw.writeVendor {
   292  		t.Fatal("Expected the payload to contain the vendor directory ")
   293  	}
   294  
   295  	err = sw.Write(pc.Project.AbsRoot, pc.SourceManager, true, nil)
   296  	h.Must(errors.Wrap(err, "SafeWriter.Write failed"))
   297  
   298  	// Verify file system changes
   299  	if err := pc.ManifestShouldNotExist(); err != nil {
   300  		t.Fatal(err)
   301  	}
   302  	if err := pc.LockShouldMatchGolden(safeWriterGoldenLock); err != nil {
   303  		t.Fatal(err)
   304  	}
   305  	if err := pc.VendorShouldExist(); err != nil {
   306  		t.Fatal(err)
   307  	}
   308  	if err := pc.VendorFileShouldExist("github.com/sdboyer/dep-test"); err != nil {
   309  		t.Fatal(err)
   310  	}
   311  }
   312  
   313  func TestSafeWriter_NewLock(t *testing.T) {
   314  	test.NeedsExternalNetwork(t)
   315  	test.NeedsGit(t)
   316  
   317  	h := test.NewHelper(t)
   318  	defer h.Cleanup()
   319  
   320  	pc := NewTestProjectContext(h, safeWriterProject)
   321  	defer pc.Release()
   322  	pc.Load()
   323  
   324  	lf := h.GetTestFile(safeWriterGoldenLock)
   325  	defer lf.Close()
   326  	newLock, err := readLock(lf)
   327  	h.Must(err)
   328  	sw, _ := NewSafeWriter(nil, nil, newLock, VendorOnChanged, defaultCascadingPruneOptions(), nil)
   329  
   330  	// Verify prepared actions
   331  	if sw.HasManifest() {
   332  		t.Fatal("Did not expect the payload to contain the manifest")
   333  	}
   334  	if !sw.HasLock() {
   335  		t.Fatal("Expected the payload to contain the lock")
   336  	}
   337  	if !sw.writeLock {
   338  		t.Fatal("Expected that the writer should plan to write the lock")
   339  	}
   340  	if !sw.writeVendor {
   341  		t.Fatal("Expected the payload to contain the vendor directory")
   342  	}
   343  
   344  	// Write changes
   345  	err = sw.Write(pc.Project.AbsRoot, pc.SourceManager, true, nil)
   346  	h.Must(errors.Wrap(err, "SafeWriter.Write failed"))
   347  
   348  	// Verify file system changes
   349  	if err := pc.ManifestShouldNotExist(); err != nil {
   350  		t.Fatal(err)
   351  	}
   352  	if err := pc.LockShouldMatchGolden(safeWriterGoldenLock); err != nil {
   353  		t.Fatal(err)
   354  	}
   355  	if err := pc.VendorShouldExist(); err != nil {
   356  		t.Fatal(err)
   357  	}
   358  }
   359  
   360  func TestSafeWriter_NewLockSkipVendor(t *testing.T) {
   361  	test.NeedsExternalNetwork(t)
   362  	test.NeedsGit(t)
   363  
   364  	h := test.NewHelper(t)
   365  	defer h.Cleanup()
   366  
   367  	pc := NewTestProjectContext(h, safeWriterProject)
   368  	defer pc.Release()
   369  	pc.Load()
   370  
   371  	lf := h.GetTestFile(safeWriterGoldenLock)
   372  	defer lf.Close()
   373  	newLock, err := readLock(lf)
   374  	h.Must(err)
   375  	sw, _ := NewSafeWriter(nil, nil, newLock, VendorNever, defaultCascadingPruneOptions(), nil)
   376  
   377  	// Verify prepared actions
   378  	if sw.HasManifest() {
   379  		t.Fatal("Did not expect the payload to contain the manifest")
   380  	}
   381  	if !sw.HasLock() {
   382  		t.Fatal("Expected the payload to contain the lock")
   383  	}
   384  	if !sw.writeLock {
   385  		t.Fatal("Expected that the writer should plan to write the lock")
   386  	}
   387  	if sw.writeVendor {
   388  		t.Fatal("Did not expect the payload to contain the vendor directory")
   389  	}
   390  
   391  	// Write changes
   392  	err = sw.Write(pc.Project.AbsRoot, pc.SourceManager, true, nil)
   393  	h.Must(errors.Wrap(err, "SafeWriter.Write failed"))
   394  
   395  	// Verify file system changes
   396  	if err := pc.ManifestShouldNotExist(); err != nil {
   397  		t.Fatal(err)
   398  	}
   399  	if err := pc.LockShouldMatchGolden(safeWriterGoldenLock); err != nil {
   400  		t.Fatal(err)
   401  	}
   402  	if err := pc.VendorShouldNotExist(); err != nil {
   403  		t.Fatal(err)
   404  	}
   405  }
   406  
   407  func TestHasDotGit(t *testing.T) {
   408  	// Create a tempdir with .git file
   409  	td, err := ioutil.TempDir(os.TempDir(), "dotGitFile")
   410  	if err != nil {
   411  		t.Fatal(err)
   412  	}
   413  	defer os.RemoveAll(td)
   414  
   415  	os.OpenFile(td+string(filepath.Separator)+".git", os.O_CREATE, 0777)
   416  	if !hasDotGit(td) {
   417  		t.Fatal("Expected hasDotGit to find .git")
   418  	}
   419  }
   420  
   421  func TestSafeWriter_VendorDotGitPreservedWithForceVendor(t *testing.T) {
   422  	h := test.NewHelper(t)
   423  	defer h.Cleanup()
   424  
   425  	pc := NewTestProjectContext(h, safeWriterProject)
   426  	defer pc.Release()
   427  
   428  	gitDirPath := filepath.Join(pc.Project.AbsRoot, "vendor", ".git")
   429  	os.MkdirAll(gitDirPath, 0777)
   430  	dummyFile := filepath.Join("vendor", ".git", "badinput_fileroot")
   431  	pc.CopyFile(dummyFile, "txn_writer/badinput_fileroot")
   432  	pc.CopyFile(ManifestName, safeWriterGoldenManifest)
   433  	pc.CopyFile(LockName, safeWriterGoldenLock)
   434  	pc.Load()
   435  
   436  	sw, _ := NewSafeWriter(pc.Project.Manifest, pc.Project.Lock, pc.Project.Lock, VendorAlways, defaultCascadingPruneOptions(), nil)
   437  
   438  	// Verify prepared actions
   439  	if !sw.HasManifest() {
   440  		t.Fatal("Expected the payload to contain the manifest")
   441  	}
   442  	if !sw.HasLock() {
   443  		t.Fatal("Expected the payload to contain the lock")
   444  	}
   445  	if sw.writeLock {
   446  		t.Fatal("Did not expect that the writer should plan to write the lock")
   447  	}
   448  	if !sw.writeVendor {
   449  		t.Fatal("Expected the payload to contain the vendor directory")
   450  	}
   451  
   452  	err := sw.Write(pc.Project.AbsRoot, pc.SourceManager, true, nil)
   453  	h.Must(errors.Wrap(err, "SafeWriter.Write failed"))
   454  
   455  	// Verify file system changes
   456  	if err := pc.ManifestShouldMatchGolden(safeWriterGoldenManifest); err != nil {
   457  		t.Fatal(err)
   458  	}
   459  	if err := pc.LockShouldMatchGolden(safeWriterGoldenLock); err != nil {
   460  		t.Fatal(err)
   461  	}
   462  	if err := pc.VendorShouldExist(); err != nil {
   463  		t.Fatal(err)
   464  	}
   465  	if err := pc.VendorFileShouldExist("github.com/sdboyer/dep-test"); err != nil {
   466  		t.Fatal(err)
   467  	}
   468  	if err := pc.VendorFileShouldExist(".git/badinput_fileroot"); err != nil {
   469  		t.Fatal(err)
   470  	}
   471  }