Skip to content

Commit dc5c56e

Browse files
fix wrong IndexFile truncate (#2346)
1 parent 6aa1be0 commit dc5c56e

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

Diff for: arduino/resources/index.go

+8-3
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,16 @@ type IndexResource struct {
4242
// IndexFileName returns the index file name as it is saved in data dir (package_xxx_index.json).
4343
func (res *IndexResource) IndexFileName() (string, error) {
4444
filename := path.Base(res.URL.Path) // == package_index.json[.gz] || packacge_index.tar.bz2
45-
if filename == "." || filename == "" {
45+
if filename == "." || filename == "" || filename == "/" {
4646
return "", &arduino.InvalidURLError{}
4747
}
48-
if i := strings.Index(filename, "."); i != -1 {
49-
filename = filename[:i]
48+
switch {
49+
case strings.HasSuffix(filename, ".json"):
50+
return filename, nil
51+
case strings.HasSuffix(filename, ".gz"):
52+
return strings.TrimSuffix(filename, ".gz"), nil
53+
case strings.HasSuffix(filename, ".tar.bz2"):
54+
return strings.TrimSuffix(filename, ".tar.bz2") + ".json", nil
5055
}
5156
return filename + ".json", nil
5257
}

Diff for: arduino/resources/resources_test.go

+23
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package resources
1818
import (
1919
"crypto"
2020
"encoding/hex"
21+
"fmt"
2122
"net"
2223
"net/http"
2324
"net/url"
@@ -148,3 +149,25 @@ func TestIndexDownloadAndSignatureWithinArchive(t *testing.T) {
148149
require.False(t, invDestDir.Join("package_index.json").Exist())
149150
require.False(t, invDestDir.Join("package_index.json.sig").Exist())
150151
}
152+
153+
func TestIndexFileName(t *testing.T) {
154+
tests := []struct {
155+
url string
156+
expected string
157+
}{
158+
{url: "package_index.json", expected: "package_index.json"},
159+
{url: "package_index.json.gz", expected: "package_index.json"},
160+
{url: "package_index.tar.bz2", expected: "package_index.json"},
161+
// https://github.com/arduino/arduino-cli/issues/2345
162+
{url: "package_arduino.cc_index.json", expected: "package_arduino.cc_index.json"},
163+
{url: "package_arduino.cc_index.json.gz", expected: "package_arduino.cc_index.json"},
164+
{url: "package_arduino.cc_index.tar.bz2", expected: "package_arduino.cc_index.json"},
165+
{url: "http://drazzy.com/package_drazzy.com_index.json", expected: "package_drazzy.com_index.json"},
166+
}
167+
for _, tc := range tests {
168+
ir := IndexResource{URL: &url.URL{Path: tc.url}}
169+
name, err := ir.IndexFileName()
170+
require.NoError(t, err, fmt.Sprintf("error trying url: %v", tc))
171+
require.Equal(t, tc.expected, name)
172+
}
173+
}

0 commit comments

Comments
 (0)