|
| 1 | +// Copyright 2019 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 modfile |
| 6 | + |
| 7 | +import ( |
| 8 | + "bytes" |
| 9 | + "fmt" |
| 10 | + "testing" |
| 11 | +) |
| 12 | + |
| 13 | +var addDropReplaceTests = []struct { |
| 14 | + in string |
| 15 | + oldpath string |
| 16 | + oldvers string |
| 17 | + newpath string |
| 18 | + newvers string |
| 19 | + dropOld bool |
| 20 | + out string |
| 21 | +}{ |
| 22 | + { |
| 23 | + ` |
| 24 | + module m |
| 25 | + require x.y/z v1.2.3 |
| 26 | + `, |
| 27 | + "x.y/z", "v1.2.3", |
| 28 | + "my.x.y/z", "v1.2.3", |
| 29 | + false, |
| 30 | + ` |
| 31 | + module m |
| 32 | + require x.y/z v1.2.3 |
| 33 | + replace x.y/z v1.2.3 => my.x.y/z v1.2.3 |
| 34 | + `, |
| 35 | + }, |
| 36 | + |
| 37 | + { |
| 38 | + ` |
| 39 | + module m |
| 40 | + require x.y/z v1.2.3 |
| 41 | + replace x.y/z => my.x.y/z v0.0.0-20190214113530-db6c41c15648 |
| 42 | + `, |
| 43 | + "x.y/z", "", |
| 44 | + "my.x.y/z", "v1.2.3", |
| 45 | + true, |
| 46 | + ` |
| 47 | + module m |
| 48 | + require x.y/z v1.2.3 |
| 49 | + replace x.y/z => my.x.y/z v1.2.3 |
| 50 | + `, |
| 51 | + }, |
| 52 | + { |
| 53 | + ` |
| 54 | + module m |
| 55 | + require x.y/z v1.2.3 |
| 56 | + replace x.y/z => my.x.y/z v0.0.0-20190214113530-db6c41c15648 |
| 57 | + `, |
| 58 | + "x.y/z", "", |
| 59 | + "", "", // empty newpath and newvers - drop only, no add |
| 60 | + true, |
| 61 | + ` |
| 62 | + module m |
| 63 | + require x.y/z v1.2.3 |
| 64 | + `, |
| 65 | + }, |
| 66 | +} |
| 67 | + |
| 68 | +func TestAddDropReplace(t *testing.T) { |
| 69 | + for i, tt := range addDropReplaceTests { |
| 70 | + t.Run(fmt.Sprintf("#%d", i), func(t *testing.T) { |
| 71 | + f, err := Parse("in", []byte(tt.in), nil) |
| 72 | + if err != nil { |
| 73 | + t.Fatal(err) |
| 74 | + } |
| 75 | + g, err := Parse("out", []byte(tt.out), nil) |
| 76 | + if err != nil { |
| 77 | + t.Fatal(err) |
| 78 | + } |
| 79 | + golden, err := g.Format() |
| 80 | + if err != nil { |
| 81 | + t.Fatal(err) |
| 82 | + } |
| 83 | + if tt.dropOld { |
| 84 | + if err := f.DropReplace(tt.oldpath, tt.oldvers); err != nil { |
| 85 | + t.Fatal(err) |
| 86 | + } |
| 87 | + } |
| 88 | + if tt.newpath != "" || tt.newvers != "" { |
| 89 | + if err := f.AddReplace(tt.oldpath, tt.oldvers, tt.newpath, tt.newvers); err != nil { |
| 90 | + t.Fatal(err) |
| 91 | + } |
| 92 | + } |
| 93 | + f.Cleanup() |
| 94 | + out, err := f.Format() |
| 95 | + if err != nil { |
| 96 | + t.Fatal(err) |
| 97 | + } |
| 98 | + if !bytes.Equal(out, golden) { |
| 99 | + t.Errorf("have:\n%s\nwant:\n%s", out, golden) |
| 100 | + } |
| 101 | + }) |
| 102 | + } |
| 103 | +} |
0 commit comments