aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKevin Daudt <kdaudt@alpinelinux.org>2021-04-27 13:20:56 +0200
committerKevin Daudt <kdaudt@alpinelinux.org>2021-04-27 17:41:39 +0200
commit51accaeec09b386f90e2ffb553952d053f62f6d9 (patch)
treead7e1d53618298313c62b48df86e4c7637ad60e4
parentbfee82ca3b3ef0cda0cdff3982667892ef78d022 (diff)
downloadgo-51accaeec09b386f90e2ffb553952d053f62f6d9.tar.gz
go-51accaeec09b386f90e2ffb553952d053f62f6d9.tar.bz2
go-51accaeec09b386f90e2ffb553952d053f62f6d9.tar.xz
releases: split fetch function
Split the Fetch function in 3: - NewFromBuffer, that parses the json and returns the parsed structure - NewFromURL, which downloads a file and passes it to `NewFromBuffer` - The original Fetch, which downloads it from the canonical location. This makes the API more flexible and also easier to test.
-rw-r--r--pkg/releases/releases.go25
-rw-r--r--pkg/releases/releases_test.go37
-rw-r--r--pkg/releases/testdata/releases.json28
3 files changed, 82 insertions, 8 deletions
diff --git a/pkg/releases/releases.go b/pkg/releases/releases.go
index 2576a4c..d807a04 100644
--- a/pkg/releases/releases.go
+++ b/pkg/releases/releases.go
@@ -36,26 +36,35 @@ type (
}
)
-// Fetch downloads and parses the Alpine Linux releases published at
-// https://alpinelinux.org/releases.json
-func Fetch() (releases *Releases, err error) {
- resp, err := http.Get("https://alpinelinux.org/releases.json")
+// NewFromBuffer parses
+func NewFromBuffer(buffer []byte) (releases *Releases, err error) {
+ releases = &Releases{}
+ err = json.Unmarshal(buffer, releases)
+ return
+}
+
+func NewFromURL(url string) (releases *Releases, err error) {
+ resp, err := http.Get(url)
if err != nil {
- return
+ return nil, err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
-
if err != nil {
return
}
- releases = &Releases{}
- err = json.Unmarshal(body, releases)
+ releases, err = NewFromBuffer(body)
+ return
+}
+// Fetch downloads and parses the Alpine Linux releases published at
+// https://alpinelinux.org/releases.json
+func Fetch() (releases *Releases, err error) {
+ releases, err = NewFromURL("https://alpinelinux.org/releases.json")
return
}
diff --git a/pkg/releases/releases_test.go b/pkg/releases/releases_test.go
new file mode 100644
index 0000000..15e3614
--- /dev/null
+++ b/pkg/releases/releases_test.go
@@ -0,0 +1,37 @@
+package releases
+
+import (
+ "io"
+ "os"
+ "testing"
+
+ "github.com/stretchr/testify/assert"
+ "github.com/stretchr/testify/require"
+)
+
+func TestNewFromBuffer(t *testing.T) {
+ assert := assert.New(t)
+ require := require.New(t)
+
+ file, err := os.Open("testdata/releases.json")
+ require.Nil(err)
+
+ buffer, err := io.ReadAll(file)
+ require.Nil(err)
+
+ releases, err := NewFromBuffer(buffer)
+ require.Nil(err)
+
+ assert.Len(releases.Architectures, 2)
+ require.Len(releases.ReleaseBranches, 2)
+
+ firstBranch := releases.ReleaseBranches[0]
+ assert.Equal("edge", firstBranch.RelBranch)
+ assert.Equal("master", firstBranch.GitBranch)
+ assert.Len(firstBranch.Repos, 3)
+
+ secondBranch := releases.ReleaseBranches[1]
+ assert.Equal("v3.13", secondBranch.RelBranch)
+ assert.Equal("3.13-stable", secondBranch.GitBranch)
+ assert.Equal("2022-11-01", secondBranch.Repos[1].EOLDate)
+}
diff --git a/pkg/releases/testdata/releases.json b/pkg/releases/testdata/releases.json
new file mode 100644
index 0000000..a1124c2
--- /dev/null
+++ b/pkg/releases/testdata/releases.json
@@ -0,0 +1,28 @@
+{
+ "architectures": ["x86_64", "aarch64"],
+ "release_branches": [{
+ "rel_branch": "edge",
+ "arches": ["x86_64", "aarch64"],
+ "git_branch": "master",
+ "repos": [
+ {"name": "main"},
+ {"name": "community"},
+ {"name": "testing"}
+ ]
+ },{
+ "rel_branch": "v3.13",
+ "arches": ["x86_64", "aarch64"],
+ "git_branch": "3.13-stable",
+ "repos": [
+ {"name": "main"},
+ {"name": "community", "eol_date": "2022-11-01"}
+ ],
+ "releases": [{
+ "notes": "posts/Alpine-3.13.0-released.md",
+ "version": "3.13.0",
+ "date": "2021-01-14"
+ }],
+ "branch_date": "2021-01-14",
+ "eol_date": "2022-11-01"
+ }]
+}