Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions arduino/resources/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,16 @@ type IndexResource struct {
// IndexFileName returns the index file name as it is saved in data dir (package_xxx_index.json).
func (res *IndexResource) IndexFileName() (string, error) {
filename := path.Base(res.URL.Path) // == package_index.json[.gz] || packacge_index.tar.bz2
if filename == "." || filename == "" {
if filename == "." || filename == "" || filename == "/" {
return "", &arduino.InvalidURLError{}
}
if i := strings.Index(filename, "."); i != -1 {
filename = filename[:i]
switch {
case strings.HasSuffix(filename, ".json"):
return filename, nil
case strings.HasSuffix(filename, ".gz"):
return strings.TrimSuffix(filename, ".gz"), nil
case strings.HasSuffix(filename, ".tar.bz2"):
return strings.TrimSuffix(filename, ".tar.bz2") + ".json", nil
}
return filename + ".json", nil
}
Expand Down
23 changes: 23 additions & 0 deletions arduino/resources/resources_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package resources
import (
"crypto"
"encoding/hex"
"fmt"
"net"
"net/http"
"net/url"
Expand Down Expand Up @@ -148,3 +149,25 @@ func TestIndexDownloadAndSignatureWithinArchive(t *testing.T) {
require.False(t, invDestDir.Join("package_index.json").Exist())
require.False(t, invDestDir.Join("package_index.json.sig").Exist())
}

func TestIndexFileName(t *testing.T) {
tests := []struct {
url string
expected string
}{
{url: "package_index.json", expected: "package_index.json"},
{url: "package_index.json.gz", expected: "package_index.json"},
{url: "package_index.tar.bz2", expected: "package_index.json"},
// https://github.com/arduino/arduino-cli/issues/2345
{url: "package_arduino.cc_index.json", expected: "package_arduino.cc_index.json"},
{url: "package_arduino.cc_index.json.gz", expected: "package_arduino.cc_index.json"},
{url: "package_arduino.cc_index.tar.bz2", expected: "package_arduino.cc_index.json"},
{url: "http://drazzy.com/package_drazzy.com_index.json", expected: "package_drazzy.com_index.json"},
}
for _, tc := range tests {
ir := IndexResource{URL: &url.URL{Path: tc.url}}
name, err := ir.IndexFileName()
require.NoError(t, err, fmt.Sprintf("error trying url: %v", tc))
require.Equal(t, tc.expected, name)
}
}