summaryrefslogtreecommitdiffstats
path: root/dependencies/pkg/mod/golang.org/x/exp@v0.0.0-20220613132600-b0d781184e0d/cmd/modgraphviz/main_test.go
blob: bec88cfd79d5c83f289217a07baa1a203ba1ed64 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// Copyright 2019 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package main

import (
	"bytes"
	"fmt"
	"reflect"
	"testing"
)

func TestRun(t *testing.T) {
	out := &bytes.Buffer{}
	in := bytes.NewBuffer([]byte(`
test.com/A@v1.0.0 test.com/B@v1.2.3
test.com/B@v1.0.0 test.com/C@v4.5.6
`))
	if err := modgraphviz(in, out); err != nil {
		t.Fatal(err)
	}

	gotGraph := string(out.Bytes())
	wantGraph := `digraph gomodgraph {
	node [ shape=rectangle fontsize=12 ]
	"test.com/A@v1.0.0" -> "test.com/B@v1.2.3"
	"test.com/B@v1.0.0" -> "test.com/C@v4.5.6"
	"test.com/A@v1.0.0" [style = filled, fillcolor = green]
	"test.com/B@v1.2.3" [style = filled, fillcolor = green]
	"test.com/C@v4.5.6" [style = filled, fillcolor = green]
	"test.com/B@v1.0.0" [style = filled, fillcolor = gray]
}
`
	if gotGraph != wantGraph {
		t.Fatalf("\ngot: %s\nwant: %s", gotGraph, wantGraph)
	}
}

func TestMVSPicking(t *testing.T) {
	for _, tc := range []struct {
		name         string
		in           []string
		wantPicked   []string
		wantUnpicked []string
	}{
		{
			name:         "single node",
			in:           []string{"foo@v0.0.1"},
			wantPicked:   []string{"foo@v0.0.1"},
			wantUnpicked: nil,
		},
		{
			name:         "duplicate same node",
			in:           []string{"foo@v0.0.1", "foo@v0.0.1"},
			wantPicked:   []string{"foo@v0.0.1"},
			wantUnpicked: nil,
		},
		{
			name:         "multiple semver - same major",
			in:           []string{"foo@v1.0.0", "foo@v1.3.7", "foo@v1.2.0", "foo@v1.0.1"},
			wantPicked:   []string{"foo@v1.3.7"},
			wantUnpicked: []string{"foo@v1.0.0", "foo@v1.2.0", "foo@v1.0.1"},
		},
		{
			name:         "multiple semver - multiple major",
			in:           []string{"foo@v1.0.0", "foo@v1.3.7", "foo/v2@v2.2.0", "foo/v2@v2.0.1", "foo@v1.1.1"},
			wantPicked:   []string{"foo/v2@v2.2.0", "foo@v1.3.7"},
			wantUnpicked: []string{"foo@v1.0.0", "foo/v2@v2.0.1", "foo@v1.1.1"},
		},
		{
			name:         "semver and pseudo version",
			in:           []string{"foo@v1.0.0", "foo@v1.3.7", "foo/v2@v2.2.0", "foo/v2@v2.0.1", "foo@v1.1.1", "foo@v0.0.0-20190311183353-d8887717615a"},
			wantPicked:   []string{"foo/v2@v2.2.0", "foo@v1.3.7"},
			wantUnpicked: []string{"foo@v1.0.0", "foo/v2@v2.0.1", "foo@v1.1.1", "foo@v0.0.0-20190311183353-d8887717615a"},
		},
		{
			name: "multiple pseudo version",
			in: []string{
				"foo@v0.0.0-20190311183353-d8887717615a",
				"foo@v0.0.0-20190227222117-0694c2d4d067",
				"foo@v0.0.0-20190312151545-0bb0c0a6e846",
			},
			wantPicked: []string{"foo@v0.0.0-20190312151545-0bb0c0a6e846"},
			wantUnpicked: []string{
				"foo@v0.0.0-20190227222117-0694c2d4d067",
				"foo@v0.0.0-20190311183353-d8887717615a",
			},
		},
		{
			name:         "semver and suffix",
			in:           []string{"foo@v1.0.0", "foo@v1.3.8-rc1", "foo@v1.3.7"},
			wantPicked:   []string{"foo@v1.3.8-rc1"},
			wantUnpicked: []string{"foo@v1.0.0", "foo@v1.3.7"},
		},
	} {
		t.Run(tc.name, func(t *testing.T) {
			buf := bytes.Buffer{}
			for _, node := range tc.in {
				fmt.Fprintf(&buf, "A %s\n", node)
			}

			g, err := convert(&buf)
			if err != nil {
				t.Fatal(err)
			}

			if !reflect.DeepEqual(g.mvsPicked, tc.wantPicked) {
				t.Fatalf("picked: got %v, want %v", g.mvsPicked, tc.wantPicked)
			}
			if !reflect.DeepEqual(g.mvsUnpicked, tc.wantUnpicked) {
				t.Fatalf("unpicked: got %v, want %v", g.mvsUnpicked, tc.wantUnpicked)
			}
		})
	}
}