github.com/opsmatic/godep@v0.1.5/update_test.go (about)

     1  package main
     2  
     3  import (
     4  	"encoding/json"
     5  	"io/ioutil"
     6  	"log"
     7  	"os"
     8  	"path/filepath"
     9  	"reflect"
    10  	"testing"
    11  )
    12  
    13  func TestUpdate(t *testing.T) {
    14  	var cases = []struct {
    15  		cwd   string
    16  		args  []string
    17  		start []*node
    18  		want  []*node
    19  		wdep  Godeps
    20  		werr  bool
    21  	}{
    22  		{ // simple case, update one dependency
    23  			cwd:  "C",
    24  			args: []string{"D"},
    25  			start: []*node{
    26  				{
    27  					"D",
    28  					"",
    29  					[]*node{
    30  						{"main.go", pkg("D") + decl("D1"), nil},
    31  						{"+git", "D1", nil},
    32  						{"main.go", pkg("D") + decl("D2"), nil},
    33  						{"+git", "D2", nil},
    34  					},
    35  				},
    36  				{
    37  					"C",
    38  					"",
    39  					[]*node{
    40  						{"main.go", pkg("main", "D"), nil},
    41  						{"Godeps/Godeps.json", godeps("C", "D", "D1"), nil},
    42  						{"Godeps/_workspace/src/D/main.go", pkg("D") + decl("D1"), nil},
    43  						{"+git", "", nil},
    44  					},
    45  				},
    46  			},
    47  			want: []*node{
    48  				{"C/Godeps/_workspace/src/D/main.go", pkg("D") + decl("D2"), nil},
    49  			},
    50  			wdep: Godeps{
    51  				ImportPath: "C",
    52  				Deps: []Dependency{
    53  					{ImportPath: "D", Comment: "D2"},
    54  				},
    55  			},
    56  		},
    57  		{ // update one dependency, keep other one
    58  			cwd:  "C",
    59  			args: []string{"D"},
    60  			start: []*node{
    61  				{
    62  					"D",
    63  					"",
    64  					[]*node{
    65  						{"main.go", pkg("D") + decl("D1"), nil},
    66  						{"+git", "D1", nil},
    67  						{"main.go", pkg("D") + decl("D2"), nil},
    68  						{"+git", "D2", nil},
    69  					},
    70  				},
    71  				{
    72  					"E",
    73  					"",
    74  					[]*node{
    75  						{"main.go", pkg("E") + decl("E1"), nil},
    76  						{"+git", "E1", nil},
    77  						{"main.go", pkg("E") + decl("E2"), nil},
    78  						{"+git", "E2", nil},
    79  					},
    80  				},
    81  				{
    82  					"C",
    83  					"",
    84  					[]*node{
    85  						{"main.go", pkg("main", "D", "E"), nil},
    86  						{"Godeps/Godeps.json", godeps("C", "D", "D1", "E", "E1"), nil},
    87  						{"Godeps/_workspace/src/D/main.go", pkg("D") + decl("D1"), nil},
    88  						{"Godeps/_workspace/src/E/main.go", pkg("E") + decl("E1"), nil},
    89  						{"+git", "", nil},
    90  					},
    91  				},
    92  			},
    93  			want: []*node{
    94  				{"C/Godeps/_workspace/src/D/main.go", pkg("D") + decl("D2"), nil},
    95  				{"C/Godeps/_workspace/src/E/main.go", pkg("E") + decl("E1"), nil},
    96  			},
    97  			wdep: Godeps{
    98  				ImportPath: "C",
    99  				Deps: []Dependency{
   100  					{ImportPath: "D", Comment: "D2"},
   101  					{ImportPath: "E", Comment: "E1"},
   102  				},
   103  			},
   104  		},
   105  		{ // update all dependencies
   106  			cwd:  "C",
   107  			args: []string{"..."},
   108  			start: []*node{
   109  				{
   110  					"D",
   111  					"",
   112  					[]*node{
   113  						{"main.go", pkg("D") + decl("D1"), nil},
   114  						{"+git", "D1", nil},
   115  						{"main.go", pkg("D") + decl("D2"), nil},
   116  						{"+git", "D2", nil},
   117  					},
   118  				},
   119  				{
   120  					"E",
   121  					"",
   122  					[]*node{
   123  						{"main.go", pkg("E") + decl("E1"), nil},
   124  						{"+git", "E1", nil},
   125  						{"main.go", pkg("E") + decl("E2"), nil},
   126  						{"+git", "E2", nil},
   127  					},
   128  				},
   129  				{
   130  					"C",
   131  					"",
   132  					[]*node{
   133  						{"main.go", pkg("main", "D", "E"), nil},
   134  						{"Godeps/Godeps.json", godeps("C", "D", "D1", "E", "E1"), nil},
   135  						{"Godeps/_workspace/src/D/main.go", pkg("D") + decl("D1"), nil},
   136  						{"Godeps/_workspace/src/E/main.go", pkg("E") + decl("E1"), nil},
   137  						{"+git", "", nil},
   138  					},
   139  				},
   140  			},
   141  			want: []*node{
   142  				{"C/Godeps/_workspace/src/D/main.go", pkg("D") + decl("D2"), nil},
   143  				{"C/Godeps/_workspace/src/E/main.go", pkg("E") + decl("E2"), nil},
   144  			},
   145  			wdep: Godeps{
   146  				ImportPath: "C",
   147  				Deps: []Dependency{
   148  					{ImportPath: "D", Comment: "D2"},
   149  					{ImportPath: "E", Comment: "E2"},
   150  				},
   151  			},
   152  		},
   153  		{ // one match of two patterns
   154  			cwd:  "C",
   155  			args: []string{"D", "X"},
   156  			start: []*node{
   157  				{
   158  					"D",
   159  					"",
   160  					[]*node{
   161  						{"main.go", pkg("D") + decl("D1"), nil},
   162  						{"+git", "D1", nil},
   163  						{"main.go", pkg("D") + decl("D2"), nil},
   164  						{"+git", "D2", nil},
   165  					},
   166  				},
   167  				{
   168  					"C",
   169  					"",
   170  					[]*node{
   171  						{"main.go", pkg("main", "D"), nil},
   172  						{"Godeps/Godeps.json", godeps("C", "D", "D1"), nil},
   173  						{"Godeps/_workspace/src/D/main.go", pkg("D") + decl("D1"), nil},
   174  						{"+git", "", nil},
   175  					},
   176  				},
   177  			},
   178  			want: []*node{
   179  				{"C/Godeps/_workspace/src/D/main.go", pkg("D") + decl("D2"), nil},
   180  			},
   181  			wdep: Godeps{
   182  				ImportPath: "C",
   183  				Deps: []Dependency{
   184  					{ImportPath: "D", Comment: "D2"},
   185  				},
   186  			},
   187  		},
   188  		{ // no matches
   189  			cwd:  "C",
   190  			args: []string{"X"},
   191  			start: []*node{
   192  				{
   193  					"D",
   194  					"",
   195  					[]*node{
   196  						{"main.go", pkg("D") + decl("D1"), nil},
   197  						{"+git", "D1", nil},
   198  						{"main.go", pkg("D") + decl("D2"), nil},
   199  						{"+git", "D2", nil},
   200  					},
   201  				},
   202  				{
   203  					"C",
   204  					"",
   205  					[]*node{
   206  						{"main.go", pkg("main", "D"), nil},
   207  						{"Godeps/Godeps.json", godeps("C", "D", "D1"), nil},
   208  						{"Godeps/_workspace/src/D/main.go", pkg("D") + decl("D1"), nil},
   209  						{"+git", "", nil},
   210  					},
   211  				},
   212  			},
   213  			want: []*node{
   214  				{"C/Godeps/_workspace/src/D/main.go", pkg("D") + decl("D1"), nil},
   215  			},
   216  			wdep: Godeps{
   217  				ImportPath: "C",
   218  				Deps: []Dependency{
   219  					{ImportPath: "D", Comment: "D1"},
   220  				},
   221  			},
   222  			werr: true,
   223  		},
   224  		{ // update just one package of two in a repo skips it
   225  			cwd:  "C",
   226  			args: []string{"D/A", "E"},
   227  			start: []*node{
   228  				{
   229  					"D",
   230  					"",
   231  					[]*node{
   232  						{"A/main.go", pkg("A") + decl("D1"), nil},
   233  						{"B/main.go", pkg("B") + decl("D1"), nil},
   234  						{"+git", "D1", nil},
   235  						{"A/main.go", pkg("A") + decl("D2"), nil},
   236  						{"B/main.go", pkg("B") + decl("D2"), nil},
   237  						{"+git", "D2", nil},
   238  					},
   239  				},
   240  				{
   241  					"E",
   242  					"",
   243  					[]*node{
   244  						{"main.go", pkg("E") + decl("E1"), nil},
   245  						{"+git", "E1", nil},
   246  						{"main.go", pkg("E") + decl("E2"), nil},
   247  						{"+git", "E2", nil},
   248  					},
   249  				},
   250  				{
   251  					"C",
   252  					"",
   253  					[]*node{
   254  						{"main.go", pkg("main", "D/A", "D/B", "E"), nil},
   255  						{"Godeps/Godeps.json", godeps("C", "D/A", "D1", "D/B", "D1", "E", "E1"), nil},
   256  						{"Godeps/_workspace/src/D/A/main.go", pkg("A") + decl("D1"), nil},
   257  						{"Godeps/_workspace/src/D/B/main.go", pkg("B") + decl("D1"), nil},
   258  						{"Godeps/_workspace/src/E/main.go", pkg("E") + decl("E1"), nil},
   259  						{"+git", "", nil},
   260  					},
   261  				},
   262  			},
   263  			want: []*node{
   264  				{"C/Godeps/_workspace/src/D/A/main.go", pkg("A") + decl("D1"), nil},
   265  				{"C/Godeps/_workspace/src/D/B/main.go", pkg("B") + decl("D1"), nil},
   266  				{"C/Godeps/_workspace/src/E/main.go", pkg("E") + decl("E2"), nil},
   267  			},
   268  			wdep: Godeps{
   269  				ImportPath: "C",
   270  				Deps: []Dependency{
   271  					{ImportPath: "D/A", Comment: "D1"},
   272  					{ImportPath: "D/B", Comment: "D1"},
   273  					{ImportPath: "E", Comment: "E2"},
   274  				},
   275  			},
   276  		},
   277  		{ // update just one package of two in a repo, none left
   278  			cwd:  "C",
   279  			args: []string{"D/A"},
   280  			start: []*node{
   281  				{
   282  					"D",
   283  					"",
   284  					[]*node{
   285  						{"A/main.go", pkg("A") + decl("D1"), nil},
   286  						{"B/main.go", pkg("B") + decl("D1"), nil},
   287  						{"+git", "D1", nil},
   288  						{"A/main.go", pkg("A") + decl("D2"), nil},
   289  						{"B/main.go", pkg("B") + decl("D2"), nil},
   290  						{"+git", "D2", nil},
   291  					},
   292  				},
   293  				{
   294  					"C",
   295  					"",
   296  					[]*node{
   297  						{"main.go", pkg("main", "D/A", "D/B"), nil},
   298  						{"Godeps/Godeps.json", godeps("C", "D/A", "D1", "D/B", "D1"), nil},
   299  						{"Godeps/_workspace/src/D/A/main.go", pkg("A") + decl("D1"), nil},
   300  						{"Godeps/_workspace/src/D/B/main.go", pkg("B") + decl("D1"), nil},
   301  						{"+git", "", nil},
   302  					},
   303  				},
   304  			},
   305  			want: []*node{
   306  				{"C/Godeps/_workspace/src/D/A/main.go", pkg("A") + decl("D1"), nil},
   307  				{"C/Godeps/_workspace/src/D/B/main.go", pkg("B") + decl("D1"), nil},
   308  			},
   309  			wdep: Godeps{
   310  				ImportPath: "C",
   311  				Deps: []Dependency{
   312  					{ImportPath: "D/A", Comment: "D1"},
   313  					{ImportPath: "D/B", Comment: "D1"},
   314  				},
   315  			},
   316  			werr: true,
   317  		},
   318  	}
   319  
   320  	wd, err := os.Getwd()
   321  	if err != nil {
   322  		t.Fatal(err)
   323  	}
   324  	const gopath = "godeptest"
   325  	defer os.RemoveAll(gopath)
   326  	for _, test := range cases {
   327  		err = os.RemoveAll(gopath)
   328  		if err != nil {
   329  			t.Fatal(err)
   330  		}
   331  		src := filepath.Join(gopath, "src")
   332  		makeTree(t, &node{src, "", test.start}, "")
   333  
   334  		dir := filepath.Join(wd, src, test.cwd)
   335  		err = os.Chdir(dir)
   336  		if err != nil {
   337  			panic(err)
   338  		}
   339  		err = os.Setenv("GOPATH", filepath.Join(wd, gopath))
   340  		if err != nil {
   341  			panic(err)
   342  		}
   343  		log.SetOutput(ioutil.Discard)
   344  		err = update(test.args)
   345  		log.SetOutput(os.Stderr)
   346  		if g := err != nil; g != test.werr {
   347  			t.Errorf("update err = %v (%v) want %v", g, err, test.werr)
   348  		}
   349  		err = os.Chdir(wd)
   350  		if err != nil {
   351  			panic(err)
   352  		}
   353  
   354  		checkTree(t, &node{src, "", test.want})
   355  
   356  		f, err := os.Open(filepath.Join(dir, "Godeps/Godeps.json"))
   357  		if err != nil {
   358  			t.Error(err)
   359  		}
   360  		g := new(Godeps)
   361  		err = json.NewDecoder(f).Decode(g)
   362  		if err != nil {
   363  			t.Error(err)
   364  		}
   365  		f.Close()
   366  
   367  		if g.ImportPath != test.wdep.ImportPath {
   368  			t.Errorf("ImportPath = %s want %s", g.ImportPath, test.wdep.ImportPath)
   369  		}
   370  		for i := range g.Deps {
   371  			g.Deps[i].Rev = ""
   372  		}
   373  		if !reflect.DeepEqual(g.Deps, test.wdep.Deps) {
   374  			t.Errorf("Deps = %v want %v", g.Deps, test.wdep.Deps)
   375  		}
   376  	}
   377  }