1package changes
2
3import (
4	"path/filepath"
5	"strings"
6	"testing"
7
8	"github.com/aws/aws-sdk-go-v2/internal/repotools/changes/golist"
9	"github.com/google/go-cmp/cmp"
10)
11
12func TestGetCurrentModule(t *testing.T) {
13	mod, err := GetCurrentModule()
14	if err != nil {
15		t.Errorf("expected nil err, got %v", err)
16	}
17
18	if mod != "internal/repotools/changes" {
19		t.Errorf("expected mod to be \"internal/repotools/changes\", got %s", mod)
20	}
21}
22
23func TestDiscoverModules(t *testing.T) {
24	const prefix = "internal/tools/changes/testdata/modules/"
25	wantMods := []string{
26		"a",
27		"b",
28		"nested/c/d",
29		"nested/c",
30	}
31	for i := range wantMods {
32		wantMods[i] = prefix + wantMods[i]
33	}
34
35	goclient := golist.Client{
36		RootPath: filepath.Join("testdata", "modules"),
37		ShortenModPath: func(mod string) string {
38			return strings.TrimPrefix(mod, "internal/tools/changes/testdata/modules/")
39		},
40	}
41
42	mods, err := discoverModules(goclient.RootPath)
43	if err != nil {
44		t.Fatal(err)
45	}
46
47	if diff := cmp.Diff(wantMods, mods); diff != "" {
48		t.Errorf("expect modules to match (-want, +got):\n%v", diff)
49	}
50}
51
52func TestDefaultVersion(t *testing.T) {
53	var cases = map[string]struct {
54		mod         string
55		wantVersion string
56		wantErr     string
57	}{
58		"v0": {
59			mod:         "example.com/module",
60			wantVersion: "v0.1.0",
61		},
62		"v2": {
63			mod:         "example.com/module/v2",
64			wantVersion: "v2.0.0",
65		},
66		"v100": {
67			mod:         "example.com/module/v100",
68			wantVersion: "v100.0.0",
69		},
70		"invalid": {
71			mod:     "example.com/module/v2.0",
72			wantErr: "couldn't split module path",
73		},
74	}
75
76	for id, tt := range cases {
77		t.Run(id, func(t *testing.T) {
78			v, err := defaultVersion(tt.mod)
79			if tt.wantErr != "" {
80				if err == nil {
81					t.Error("expected non-nil err, got nil")
82				} else if !strings.Contains(err.Error(), tt.wantErr) {
83					t.Errorf("expected err to contain %s, got %v", tt.wantErr, err)
84				}
85			}
86
87			if tt.wantVersion != v {
88				t.Errorf("expected version to be %s, got %s", tt.wantVersion, v)
89			}
90		})
91	}
92}
93
94func TestPseudoVersion(t *testing.T) {
95	v, err := pseudoVersion(&MockGit{
96		tags: []string{
97			"internal/tools/changes/v0.1.2",
98		},
99	}, "internal/tools/changes")
100	if err != nil {
101		t.Fatal(err)
102	}
103
104	wantVer := "v0.1.3-0.1234567abcde"
105
106	if v != wantVer {
107		t.Errorf("wanted version %s, got %s", wantVer, v)
108	}
109}
110
111func TestFormatPseudoVersion(t *testing.T) {
112	var cases = map[string]struct {
113		hash        string
114		tagVersion  string
115		wantVersion string
116	}{
117		"no tag": {
118			hash:        "20200709182313-123456789012",
119			tagVersion:  "",
120			wantVersion: "v0.0.0-20200709182313-123456789012",
121		},
122		"v0.0.0 tag": {
123			hash:        "20200709182313-123456789012",
124			tagVersion:  "v0.0.0",
125			wantVersion: "v0.0.1-0.20200709182313-123456789012",
126		},
127		"v1.2.3 tag": {
128			hash:        "20200709182313-123456789012",
129			tagVersion:  "v1.2.3",
130			wantVersion: "v1.2.4-0.20200709182313-123456789012",
131		},
132		"v1.2.3 pre tag": {
133			hash:        "20200709182313-123456789012",
134			tagVersion:  "v1.2.3-pre",
135			wantVersion: "v1.2.3-pre.0.20200709182313-123456789012",
136		},
137		"v2.0.0 beta tag": {
138			hash:        "20200709182313-123456789012",
139			tagVersion:  "v2.0.0-beta",
140			wantVersion: "v2.0.0-beta.0.20200709182313-123456789012",
141		},
142		"invalid": {
143			hash:        "20200709182313-123456789012",
144			tagVersion:  "v2.0.0-beta+build.tag",
145			wantVersion: "",
146		},
147	}
148
149	for id, tt := range cases {
150		t.Run(id, func(t *testing.T) {
151			pseudoV, err := formatPseudoVersion(tt.hash, tt.tagVersion)
152			if tt.wantVersion == "" {
153				if err == nil {
154					t.Error("expected non-nil err, got nil")
155				}
156			} else if pseudoV != tt.wantVersion {
157				t.Errorf("expected pseudo-version to be %s, got %s", tt.wantVersion, pseudoV)
158			}
159		})
160	}
161}
162
163type mockGolist struct {
164	dependencies map[string][]string
165	packages     map[string][]string
166}
167
168func (c *mockGolist) Dependencies(mod string) ([]string, error) {
169	return c.dependencies[mod], nil
170}
171
172func (c *mockGolist) Packages(mod string) ([]string, error) {
173	return c.packages[mod], nil
174}
175
176func (c *mockGolist) Checksum(mod, version string) (string, error) {
177	return "01234567abcdef", nil
178}
179
180func (c *mockGolist) Tidy(mod string) error {
181	return nil
182}
183