2023-01-27 21:37:20 +00:00
|
|
|
// Copyright (c) Tailscale Inc & AUTHORS
|
|
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
2020-06-12 21:28:20 +01:00
|
|
|
|
2022-11-29 04:08:35 +00:00
|
|
|
package version_test
|
2020-06-12 21:28:20 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/google/go-cmp/cmp"
|
2021-10-28 00:21:44 +01:00
|
|
|
"tailscale.com/tstest"
|
2022-11-29 04:08:35 +00:00
|
|
|
"tailscale.com/version"
|
2020-06-12 21:28:20 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestParse(t *testing.T) {
|
2022-11-29 04:08:35 +00:00
|
|
|
parse := version.ExportParse
|
|
|
|
type parsed = version.ExportParsed
|
|
|
|
|
2020-06-12 21:28:20 +01:00
|
|
|
tests := []struct {
|
|
|
|
version string
|
|
|
|
parsed parsed
|
|
|
|
want bool
|
|
|
|
}{
|
|
|
|
{"1", parsed{Major: 1}, true},
|
|
|
|
{"1.2", parsed{Major: 1, Minor: 2}, true},
|
|
|
|
{"1.2.3", parsed{Major: 1, Minor: 2, Patch: 3}, true},
|
|
|
|
{"1.2.3-4", parsed{Major: 1, Minor: 2, Patch: 3, ExtraCommits: 4}, true},
|
|
|
|
{"1.2-4", parsed{Major: 1, Minor: 2, ExtraCommits: 4}, true},
|
|
|
|
{"1.2.3-4-extra", parsed{Major: 1, Minor: 2, Patch: 3, ExtraCommits: 4}, true},
|
|
|
|
{"1.2.3-4a-test", parsed{Major: 1, Minor: 2, Patch: 3}, true},
|
|
|
|
{"1.2-extra", parsed{Major: 1, Minor: 2}, true},
|
|
|
|
{"1.2.3-extra", parsed{Major: 1, Minor: 2, Patch: 3}, true},
|
|
|
|
{"date.20200612", parsed{Datestamp: 20200612}, true},
|
|
|
|
{"borkbork", parsed{}, false},
|
|
|
|
{"1a.2.3", parsed{}, false},
|
2021-07-01 21:45:07 +01:00
|
|
|
{"", parsed{}, false},
|
2020-06-12 21:28:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
gotParsed, got := parse(test.version)
|
|
|
|
if got != test.want {
|
|
|
|
t.Errorf("version(%q) = %v, want %v", test.version, got, test.want)
|
|
|
|
}
|
|
|
|
if diff := cmp.Diff(gotParsed, test.parsed); diff != "" {
|
|
|
|
t.Errorf("parse(%q) diff (-got+want):\n%s", test.version, diff)
|
|
|
|
}
|
2021-10-28 00:21:44 +01:00
|
|
|
err := tstest.MinAllocsPerRun(t, 0, func() {
|
2021-07-01 21:45:07 +01:00
|
|
|
gotParsed, got = parse(test.version)
|
2021-10-28 00:21:44 +01:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("parse(%q): %v", test.version, err)
|
2021-07-01 21:45:07 +01:00
|
|
|
}
|
2020-06-12 21:28:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAtLeast(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
v, m string
|
|
|
|
want bool
|
|
|
|
}{
|
|
|
|
{"1", "1", true},
|
|
|
|
{"1.2", "1", true},
|
|
|
|
{"1.2.3", "1", true},
|
|
|
|
{"1.2.3-4", "1", true},
|
|
|
|
{"0.98-0", "0.98", true},
|
|
|
|
{"0.97.1-216", "0.98", false},
|
|
|
|
{"0.94", "0.98", false},
|
|
|
|
{"0.98", "0.98", true},
|
|
|
|
{"0.98.0-0", "0.98", true},
|
|
|
|
{"1.2.3-4", "1.2.4-4", false},
|
|
|
|
{"1.2.3-4", "1.2.3-4", true},
|
|
|
|
{"date.20200612", "date.20200612", true},
|
|
|
|
{"date.20200701", "date.20200612", true},
|
|
|
|
{"date.20200501", "date.20200612", false},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
2022-11-29 04:08:35 +00:00
|
|
|
got := version.AtLeast(test.v, test.m)
|
2020-06-12 21:28:20 +01:00
|
|
|
if got != test.want {
|
|
|
|
t.Errorf("AtLeast(%q, %q) = %v, want %v", test.v, test.m, got, test.want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|