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

     1  package gps
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  	"path"
     7  	"path/filepath"
     8  	"testing"
     9  )
    10  
    11  var basicResult solution
    12  var kub atom
    13  
    14  func pi(n string) ProjectIdentifier {
    15  	return ProjectIdentifier{
    16  		ProjectRoot: ProjectRoot(n),
    17  	}
    18  }
    19  
    20  func init() {
    21  	basicResult = solution{
    22  		att: 1,
    23  		p: []LockedProject{
    24  			pa2lp(atom{
    25  				id: pi("github.com/sdboyer/testrepo"),
    26  				v:  NewBranch("master").Is(Revision("4d59fb584b15a94d7401e356d2875c472d76ef45")),
    27  			}, nil),
    28  			pa2lp(atom{
    29  				id: pi("github.com/Masterminds/VCSTestRepo"),
    30  				v:  NewVersion("1.0.0").Is(Revision("30605f6ac35fcb075ad0bfa9296f90a7d891523e")),
    31  			}, nil),
    32  		},
    33  	}
    34  
    35  	// just in case something needs punishing, kubernetes is happy to oblige
    36  	kub = atom{
    37  		id: pi("github.com/kubernetes/kubernetes"),
    38  		v:  NewVersion("1.0.0").Is(Revision("528f879e7d3790ea4287687ef0ab3f2a01cc2718")),
    39  	}
    40  }
    41  
    42  func testWriteDepTree(t *testing.T) {
    43  	t.Parallel()
    44  
    45  	// This test is a bit slow, skip it on -short
    46  	if testing.Short() {
    47  		t.Skip("Skipping dep tree writing test in short mode")
    48  	}
    49  	requiresBins(t, "git", "hg", "bzr")
    50  
    51  	tmp, err := ioutil.TempDir("", "writetree")
    52  	if err != nil {
    53  		t.Fatalf("Failed to create temp dir: %s", err)
    54  	}
    55  	defer os.RemoveAll(tmp)
    56  
    57  	r := solution{
    58  		att: 1,
    59  		p: []LockedProject{
    60  			pa2lp(atom{
    61  				id: pi("github.com/sdboyer/testrepo"),
    62  				v:  NewBranch("master").Is(Revision("4d59fb584b15a94d7401e356d2875c472d76ef45")),
    63  			}, nil),
    64  			pa2lp(atom{
    65  				id: pi("launchpad.net/govcstestbzrrepo"),
    66  				v:  NewVersion("1.0.0").Is(Revision("matt@mattfarina.com-20150731135137-pbphasfppmygpl68")),
    67  			}, nil),
    68  			pa2lp(atom{
    69  				id: pi("bitbucket.org/sdboyer/withbm"),
    70  				v:  NewVersion("v1.0.0").Is(Revision("aa110802a0c64195d0a6c375c9f66668827c90b4")),
    71  			}, nil),
    72  		},
    73  	}
    74  
    75  	sm, clean := mkNaiveSM(t)
    76  	defer clean()
    77  
    78  	// Trigger simultaneous fetch of all three to speed up test execution time
    79  	for _, p := range r.p {
    80  		go sm.SyncSourceFor(p.pi)
    81  	}
    82  
    83  	// nil lock/result should err immediately
    84  	err = WriteDepTree(tmp, nil, sm, true)
    85  	if err == nil {
    86  		t.Errorf("Should error if nil lock is passed to WriteDepTree")
    87  	}
    88  
    89  	err = WriteDepTree(tmp, r, sm, true)
    90  	if err != nil {
    91  		t.Errorf("Unexpected error while creating vendor tree: %s", err)
    92  	}
    93  
    94  	if _, err = os.Stat(filepath.Join(tmp, "github.com", "sdboyer", "testrepo")); err != nil {
    95  		t.Errorf("Directory for github.com/sdboyer/testrepo does not exist")
    96  	}
    97  	if _, err = os.Stat(filepath.Join(tmp, "launchpad.net", "govcstestbzrrepo")); err != nil {
    98  		t.Errorf("Directory for launchpad.net/govcstestbzrrepo does not exist")
    99  	}
   100  	if _, err = os.Stat(filepath.Join(tmp, "bitbucket.org", "sdboyer", "withbm")); err != nil {
   101  		t.Errorf("Directory for bitbucket.org/sdboyer/withbm does not exist")
   102  	}
   103  }
   104  
   105  func BenchmarkCreateVendorTree(b *testing.B) {
   106  	// We're fs-bound here, so restrict to single parallelism
   107  	b.SetParallelism(1)
   108  
   109  	r := basicResult
   110  	tmp := path.Join(os.TempDir(), "vsolvtest")
   111  
   112  	clean := true
   113  	sm, err := NewSourceManager(path.Join(tmp, "cache"))
   114  	if err != nil {
   115  		b.Errorf("NewSourceManager errored unexpectedly: %q", err)
   116  		clean = false
   117  	}
   118  
   119  	// Prefetch the projects before timer starts
   120  	for _, lp := range r.p {
   121  		err := sm.SyncSourceFor(lp.Ident())
   122  		if err != nil {
   123  			b.Errorf("failed getting project info during prefetch: %s", err)
   124  			clean = false
   125  		}
   126  	}
   127  
   128  	if clean {
   129  		b.ResetTimer()
   130  		b.StopTimer()
   131  		exp := path.Join(tmp, "export")
   132  		for i := 0; i < b.N; i++ {
   133  			// Order the loop this way to make it easy to disable final cleanup, to
   134  			// ease manual inspection
   135  			os.RemoveAll(exp)
   136  			b.StartTimer()
   137  			err = WriteDepTree(exp, r, sm, true)
   138  			b.StopTimer()
   139  			if err != nil {
   140  				b.Errorf("unexpected error after %v iterations: %s", i, err)
   141  				break
   142  			}
   143  		}
   144  	}
   145  
   146  	sm.Release()
   147  	os.RemoveAll(tmp) // comment this to leave temp dir behind for inspection
   148  }