-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Sort versions from releases JSON response (#37)
* Sort versions from releases JSON response * bump Go requirement to 1.16 (for go-cmp) * add E2E test for Versions.List
- Loading branch information
1 parent
489c6f6
commit af09eee
Showing
10 changed files
with
124 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
1.15.0 | ||
1.16.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
//go:build !windows | ||
// +build !windows | ||
|
||
package fs | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
//go:build !windows | ||
// +build !windows | ||
|
||
package fs | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package releasesjson | ||
|
||
import "github.com/hashicorp/go-version" | ||
|
||
// ProductVersion is a wrapper around a particular product version like | ||
// "consul 0.5.1". A ProductVersion may have one or more builds. | ||
type ProductVersion struct { | ||
Name string `json:"name"` | ||
RawVersion string `json:"version"` | ||
Version *version.Version `json:"-"` | ||
SHASUMS string `json:"shasums,omitempty"` | ||
SHASUMSSig string `json:"shasums_signature,omitempty"` | ||
SHASUMSSigs []string `json:"shasums_signatures,omitempty"` | ||
Builds ProductBuilds `json:"builds"` | ||
} | ||
|
||
type ProductVersionsMap map[string]*ProductVersion | ||
|
||
type ProductVersions []*ProductVersion | ||
|
||
func (pv ProductVersions) Len() int { | ||
return len(pv) | ||
} | ||
|
||
func (pv ProductVersions) Less(i, j int) bool { | ||
return pv[i].Version.LessThan(pv[j].Version) | ||
} | ||
|
||
func (pv ProductVersions) Swap(i, j int) { | ||
pv[i], pv[j] = pv[j], pv[i] | ||
} | ||
|
||
func (pvm ProductVersionsMap) AsSlice() ProductVersions { | ||
versions := make(ProductVersions, 0) | ||
|
||
for _, pVersion := range pvm { | ||
versions = append(versions, pVersion) | ||
} | ||
|
||
return versions | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package releases | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
"github.com/hashicorp/go-version" | ||
"github.com/hashicorp/hc-install/internal/testutil" | ||
"github.com/hashicorp/hc-install/product" | ||
"github.com/hashicorp/hc-install/src" | ||
) | ||
|
||
func TestVersions_List(t *testing.T) { | ||
testutil.EndToEndTest(t) | ||
|
||
cons, err := version.NewConstraint(">= 1.0.0, < 1.0.10") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
versions := &Versions{ | ||
Product: product.Terraform, | ||
Constraints: cons, | ||
} | ||
|
||
ctx := context.Background() | ||
sources, err := versions.List(ctx) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
expectedVersions := []string{ | ||
"1.0.0", | ||
"1.0.1", | ||
"1.0.2", | ||
"1.0.3", | ||
"1.0.4", | ||
"1.0.5", | ||
"1.0.6", | ||
"1.0.7", | ||
"1.0.8", | ||
"1.0.9", | ||
} | ||
if diff := cmp.Diff(expectedVersions, sourcesToRawVersions(sources)); diff != "" { | ||
t.Fatalf("unexpected versions: %s", diff) | ||
} | ||
} | ||
|
||
func sourcesToRawVersions(srcs []src.Source) []string { | ||
rawVersions := make([]string, len(srcs)) | ||
|
||
for idx, src := range srcs { | ||
source := src.(*ExactVersion) | ||
rawVersions[idx] = source.Version.String() | ||
} | ||
|
||
return rawVersions | ||
} |