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 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
|
// Copyright 2015 The Go Authors. All rights reserved.
// Use of this source code is governed by the Apache 2.0
// license that can be found in the LICENSE file.
package dl
import (
"sort"
"strings"
"testing"
)
func TestParseVersion(t *testing.T) {
for _, c := range []struct {
in string
maj, min int
tail string
}{
{"go1.5", 5, 0, ""},
{"go1.5beta1", 5, 0, "beta1"},
{"go1.5.1", 5, 1, ""},
{"go1.5.1rc1", 5, 1, "rc1"},
} {
maj, min, tail := parseVersion(c.in)
if maj != c.maj || min != c.min || tail != c.tail {
t.Errorf("parseVersion(%q) = %v, %v, %q; want %v, %v, %q",
c.in, maj, min, tail, c.maj, c.min, c.tail)
}
}
}
func TestFileOrder(t *testing.T) {
fs := []File{
{Filename: "go1.3.src.tar.gz", Version: "go1.3", OS: "", Arch: "", Kind: "source"},
{Filename: "go1.3.1.src.tar.gz", Version: "go1.3.1", OS: "", Arch: "", Kind: "source"},
{Filename: "go1.3.linux-amd64.tar.gz", Version: "go1.3", OS: "linux", Arch: "amd64", Kind: "archive"},
{Filename: "go1.3.1.linux-amd64.tar.gz", Version: "go1.3.1", OS: "linux", Arch: "amd64", Kind: "archive"},
{Filename: "go1.3.darwin-amd64.tar.gz", Version: "go1.3", OS: "darwin", Arch: "amd64", Kind: "archive"},
{Filename: "go1.3.darwin-amd64.pkg", Version: "go1.3", OS: "darwin", Arch: "amd64", Kind: "installer"},
{Filename: "go1.3.darwin-386.tar.gz", Version: "go1.3", OS: "darwin", Arch: "386", Kind: "archive"},
{Filename: "go1.3beta1.linux-amd64.tar.gz", Version: "go1.3beta1", OS: "linux", Arch: "amd64", Kind: "archive"},
{Filename: "go1.3beta2.linux-amd64.tar.gz", Version: "go1.3beta2", OS: "linux", Arch: "amd64", Kind: "archive"},
{Filename: "go1.3rc1.linux-amd64.tar.gz", Version: "go1.3rc1", OS: "linux", Arch: "amd64", Kind: "archive"},
{Filename: "go1.2.linux-amd64.tar.gz", Version: "go1.2", OS: "linux", Arch: "amd64", Kind: "archive"},
{Filename: "go1.2.2.linux-amd64.tar.gz", Version: "go1.2.2", OS: "linux", Arch: "amd64", Kind: "archive"},
}
sort.Sort(fileOrder(fs))
var s []string
for _, f := range fs {
s = append(s, f.Filename)
}
got := strings.Join(s, "\n")
want := strings.Join([]string{
"go1.3.1.src.tar.gz",
"go1.3.1.linux-amd64.tar.gz",
"go1.3.src.tar.gz",
"go1.3.darwin-386.tar.gz",
"go1.3.darwin-amd64.tar.gz",
"go1.3.darwin-amd64.pkg",
"go1.3.linux-amd64.tar.gz",
"go1.2.2.linux-amd64.tar.gz",
"go1.2.linux-amd64.tar.gz",
"go1.3rc1.linux-amd64.tar.gz",
"go1.3beta2.linux-amd64.tar.gz",
"go1.3beta1.linux-amd64.tar.gz",
}, "\n")
if got != want {
t.Errorf("sort order is\n%s\nwant:\n%s", got, want)
}
}
func TestFilesToReleases(t *testing.T) {
fs := []File{
{Version: "go1.7.4", OS: "darwin"},
{Version: "go1.7.4", OS: "windows"},
{Version: "go1.7", OS: "darwin"},
{Version: "go1.7", OS: "windows"},
{Version: "go1.6.2", OS: "darwin"},
{Version: "go1.6.2", OS: "windows"},
{Version: "go1.6", OS: "darwin"},
{Version: "go1.6", OS: "windows"},
{Version: "go1.5.2", OS: "darwin"},
{Version: "go1.5.2", OS: "windows"},
{Version: "go1.5", OS: "darwin"},
{Version: "go1.5", OS: "windows"},
{Version: "go1.5beta1", OS: "windows"},
}
stable, unstable, archive := filesToReleases(fs)
if got, want := list(stable), "go1.7.4, go1.6.2"; got != want {
t.Errorf("stable = %q; want %q", got, want)
}
if got, want := list(unstable), ""; got != want {
t.Errorf("unstable = %q; want %q", got, want)
}
if got, want := list(archive), "go1.7, go1.6, go1.5.2, go1.5"; got != want {
t.Errorf("archive = %q; want %q", got, want)
}
}
func TestOldUnstableNotShown(t *testing.T) {
fs := []File{
{Version: "go1.7.4"},
{Version: "go1.7"},
{Version: "go1.7beta1"},
}
_, unstable, _ := filesToReleases(fs)
if len(unstable) != 0 {
t.Errorf("got unstable, want none")
}
}
// A new beta should show up under unstable, but not show up under archive. See golang.org/issue/29669.
func TestNewUnstableShownOnce(t *testing.T) {
fs := []File{
{Version: "go1.12beta2"},
{Version: "go1.11.4"},
{Version: "go1.11"},
{Version: "go1.10.7"},
{Version: "go1.10"},
{Version: "go1.9"},
}
stable, unstable, archive := filesToReleases(fs)
if got, want := list(stable), "go1.11.4, go1.10.7"; got != want {
t.Errorf("stable = %q; want %q", got, want)
}
if got, want := list(unstable), "go1.12beta2"; got != want {
t.Errorf("unstable = %q; want %q", got, want)
}
if got, want := list(archive), "go1.11, go1.10, go1.9"; got != want {
t.Errorf("archive = %q; want %q", got, want)
}
}
func TestUnstableShown(t *testing.T) {
fs := []File{
{Version: "go1.8beta2"},
{Version: "go1.8rc1"},
{Version: "go1.7.4"},
{Version: "go1.7"},
{Version: "go1.7beta1"},
}
_, unstable, _ := filesToReleases(fs)
// Show RCs ahead of betas.
if got, want := list(unstable), "go1.8rc1"; got != want {
t.Errorf("unstable = %q; want %q", got, want)
}
}
// list returns a version list string for the given releases.
func list(rs []Release) string {
var s string
for i, r := range rs {
if i > 0 {
s += ", "
}
s += r.Version
}
return s
}
|