1// Copyright 2020 Google LLC
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13
14// +build go1.12
15
16package wire
17
18import (
19	"runtime/debug"
20	"testing"
21
22	"cloud.google.com/go/internal/testutil"
23)
24
25func TestPubsubliteModuleVersion(t *testing.T) {
26	for _, tc := range []struct {
27		desc        string
28		buildInfo   *debug.BuildInfo
29		wantVersion version
30		wantOk      bool
31	}{
32		{
33			desc: "version valid",
34			buildInfo: &debug.BuildInfo{
35				Deps: []*debug.Module{
36					{Path: "cloud.google.com/go/pubsublite", Version: "v1.2.2"},
37					{Path: "cloud.google.com/go/pubsub", Version: "v1.8.3"},
38				},
39			},
40			wantVersion: version{Major: "1", Minor: "2"},
41			wantOk:      true,
42		},
43		{
44			desc: "version corner case",
45			buildInfo: &debug.BuildInfo{
46				Deps: []*debug.Module{
47					{Path: "cloud.google.com/go/pubsublite", Version: "2.3"},
48				},
49			},
50			wantVersion: version{Major: "2", Minor: "3"},
51			wantOk:      true,
52		},
53		{
54			desc: "version missing",
55			buildInfo: &debug.BuildInfo{
56				Deps: []*debug.Module{
57					{Path: "cloud.google.com/go/pubsub", Version: "v1.8.3"},
58				},
59			},
60			wantOk: false,
61		},
62		{
63			desc: "minor version invalid",
64			buildInfo: &debug.BuildInfo{
65				Deps: []*debug.Module{
66					{Path: "cloud.google.com/go/pubsublite", Version: "v1.a.2"},
67				},
68			},
69			wantOk: false,
70		},
71		{
72			desc: "major version invalid",
73			buildInfo: &debug.BuildInfo{
74				Deps: []*debug.Module{
75					{Path: "cloud.google.com/go/pubsublite", Version: "vb.1.2"},
76				},
77			},
78			wantOk: false,
79		},
80		{
81			desc: "minor version missing",
82			buildInfo: &debug.BuildInfo{
83				Deps: []*debug.Module{
84					{Path: "cloud.google.com/go/pubsublite", Version: "v4"},
85				},
86			},
87			wantOk: false,
88		},
89	} {
90		t.Run(tc.desc, func(t *testing.T) {
91			if gotVersion, gotOk := pubsubliteModuleVersion(tc.buildInfo); !testutil.Equal(gotVersion, tc.wantVersion) || gotOk != tc.wantOk {
92				t.Errorf("pubsubliteModuleVersion(): got (%v, %v), want (%v, %v)", gotVersion, gotOk, tc.wantVersion, tc.wantOk)
93			}
94		})
95	}
96}
97