github.com/golang/dep@v0.5.4/gps/solution_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 gps
     6  
     7  import (
     8  	"io/ioutil"
     9  	"log"
    10  	"os"
    11  	"path"
    12  	"path/filepath"
    13  	"runtime"
    14  	"testing"
    15  
    16  	"github.com/golang/dep/internal/test"
    17  )
    18  
    19  var basicResult solution
    20  
    21  func pi(n string) ProjectIdentifier {
    22  	return ProjectIdentifier{
    23  		ProjectRoot: ProjectRoot(n),
    24  	}
    25  }
    26  
    27  func init() {
    28  	basicResult = solution{
    29  		att: 1,
    30  		p: []LockedProject{
    31  			pa2lp(atom{
    32  				id: pi("github.com/sdboyer/testrepo"),
    33  				v:  NewBranch("master").Pair(Revision("4d59fb584b15a94d7401e356d2875c472d76ef45")),
    34  			}, nil),
    35  			pa2lp(atom{
    36  				id: pi("github.com/Masterminds/VCSTestRepo"),
    37  				v:  NewVersion("1.0.0").Pair(Revision("30605f6ac35fcb075ad0bfa9296f90a7d891523e")),
    38  			}, nil),
    39  		},
    40  	}
    41  	basicResult.analyzerInfo = (naiveAnalyzer{}).Info()
    42  }
    43  
    44  func testWriteDepTree(t *testing.T) {
    45  	t.Parallel()
    46  
    47  	// This test is a bit slow, skip it on -short
    48  	if testing.Short() {
    49  		t.Skip("Skipping dep tree writing test in short mode")
    50  	}
    51  	requiresBins(t, "git", "hg", "bzr")
    52  
    53  	tmp, err := ioutil.TempDir("", "writetree")
    54  	if err != nil {
    55  		t.Fatalf("Failed to create temp dir: %s", err)
    56  	}
    57  	defer os.RemoveAll(tmp)
    58  
    59  	// bzr appears to not be consistent across...versions? platforms? (who
    60  	// knows) with respect to its internal object identifiers. This has tanked
    61  	// our Windows tests, because it's sniffing for this one revision, but the
    62  	// rev is reported differently on some Windows versions, with some versions
    63  	// of bzr. It's especially vexing because these tests worked fine for years,
    64  	// then abruptly broke for no obvious reason.
    65  	var bzrv Version = NewVersion("1.0.0")
    66  	if runtime.GOOS != "windows" {
    67  		bzrv = bzrv.(semVersion).Pair(Revision("matt@mattfarina.com-20150731135137-pbphasfppmygpl68"))
    68  	}
    69  
    70  	r := solution{
    71  		att: 1,
    72  		p: []LockedProject{
    73  			pa2lp(atom{
    74  				id: pi("github.com/sdboyer/testrepo"),
    75  				v:  NewBranch("master").Pair(Revision("4d59fb584b15a94d7401e356d2875c472d76ef45")),
    76  			}, nil),
    77  			pa2lp(atom{
    78  				id: pi("launchpad.net/govcstestbzrrepo"),
    79  				v:  bzrv,
    80  			}, nil),
    81  			pa2lp(atom{
    82  				id: pi("bitbucket.org/sdboyer/withbm"),
    83  				v:  NewVersion("v1.0.0").Pair(Revision("aa110802a0c64195d0a6c375c9f66668827c90b4")),
    84  			}, nil),
    85  		},
    86  	}
    87  
    88  	sm, clean := mkNaiveSM(t)
    89  	defer clean()
    90  
    91  	// Trigger simultaneous fetch of all three to speed up test execution time
    92  	for _, p := range r.p {
    93  		go func(pi ProjectIdentifier) {
    94  			sm.SyncSourceFor(pi)
    95  		}(p.Ident())
    96  	}
    97  
    98  	// nil lock/result should err immediately
    99  	err = WriteDepTree(tmp, nil, sm, defaultCascadingPruneOptions(), nil)
   100  	if err == nil {
   101  		t.Errorf("Should error if nil lock is passed to WriteDepTree")
   102  	}
   103  
   104  	err = WriteDepTree(tmp, r, sm, defaultCascadingPruneOptions(), nil)
   105  	if err != nil {
   106  		t.Errorf("Unexpected error while creating vendor tree: %s", err)
   107  	}
   108  
   109  	if _, err = os.Stat(filepath.Join(tmp, "github.com", "sdboyer", "testrepo")); err != nil {
   110  		t.Errorf("Directory for github.com/sdboyer/testrepo does not exist")
   111  	}
   112  	if _, err = os.Stat(filepath.Join(tmp, "launchpad.net", "govcstestbzrrepo")); err != nil {
   113  		t.Errorf("Directory for launchpad.net/govcstestbzrrepo does not exist")
   114  	}
   115  	if _, err = os.Stat(filepath.Join(tmp, "bitbucket.org", "sdboyer", "withbm")); err != nil {
   116  		t.Errorf("Directory for bitbucket.org/sdboyer/withbm does not exist")
   117  	}
   118  }
   119  
   120  func BenchmarkCreateVendorTree(b *testing.B) {
   121  	// We're fs-bound here, so restrict to single parallelism
   122  	b.SetParallelism(1)
   123  
   124  	r := basicResult
   125  	tmp := path.Join(os.TempDir(), "vsolvtest")
   126  
   127  	clean := true
   128  	sm, err := NewSourceManager(SourceManagerConfig{
   129  		Cachedir: path.Join(tmp, "cache"),
   130  		Logger:   log.New(test.Writer{TB: b}, "", 0),
   131  	})
   132  	if err != nil {
   133  		b.Fatalf("failed to create SourceManager: %q", err)
   134  	}
   135  
   136  	// Prefetch the projects before timer starts
   137  	for _, lp := range r.p {
   138  		err := sm.SyncSourceFor(lp.Ident())
   139  		if err != nil {
   140  			b.Errorf("failed getting project info during prefetch: %s", err)
   141  			clean = false
   142  		}
   143  	}
   144  
   145  	if clean {
   146  		b.ResetTimer()
   147  		b.StopTimer()
   148  		exp := path.Join(tmp, "export")
   149  		for i := 0; i < b.N; i++ {
   150  			// Order the loop this way to make it easy to disable final cleanup, to
   151  			// ease manual inspection
   152  			os.RemoveAll(exp)
   153  			b.StartTimer()
   154  			err = WriteDepTree(exp, r, sm, defaultCascadingPruneOptions(), nil)
   155  			b.StopTimer()
   156  			if err != nil {
   157  				b.Errorf("unexpected error after %v iterations: %s", i, err)
   158  				break
   159  			}
   160  		}
   161  	}
   162  
   163  	sm.Release()
   164  	os.RemoveAll(tmp) // comment this to leave temp dir behind for inspection
   165  }