Skip to content

Commit 2a5c83a

Browse files
Ignore __MACOSX folder in tool archives when launching core install (#2162)
* Ignore folder when installing packages * Propose a small refactoring of the func * Add missing license header
1 parent ebdef4d commit 2a5c83a

5 files changed

+100
-13
lines changed

Diff for: arduino/resources/install.go

+10-13
Original file line numberDiff line numberDiff line change
@@ -120,19 +120,16 @@ func findPackageRoot(parent *paths.Path) (*paths.Path, error) {
120120
if err != nil {
121121
return nil, fmt.Errorf(tr("reading package root dir: %s", err))
122122
}
123-
var root *paths.Path
124-
for _, file := range files {
125-
if !file.IsDir() {
126-
continue
127-
}
128-
if root == nil {
129-
root = file
130-
} else {
131-
return nil, fmt.Errorf(tr("no unique root dir in archive, found '%[1]s' and '%[2]s'", root, file))
132-
}
133-
}
134-
if root == nil {
123+
124+
files.FilterDirs()
125+
files.FilterOutPrefix("__MACOSX")
126+
127+
if len(files) == 0 {
135128
return nil, fmt.Errorf(tr("files in archive must be placed in a subdirectory"))
136129
}
137-
return root, nil
130+
if len(files) > 1 {
131+
return nil, fmt.Errorf(tr("no unique root dir in archive, found '%[1]s' and '%[2]s'", files[0], files[1]))
132+
}
133+
134+
return files[0], nil
138135
}

Diff for: arduino/resources/install_test.go

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
// This file is part of arduino-cli.
2+
//
3+
// Copyright 2023 ARDUINO SA (http://www.arduino.cc/)
4+
//
5+
// This software is released under the GNU General Public License version 3,
6+
// which covers the main part of arduino-cli.
7+
// The terms of this license can be found at:
8+
// https://www.gnu.org/licenses/gpl-3.0.en.html
9+
//
10+
// You can be released from the requirements of the above licenses by purchasing
11+
// a commercial license. Buying such a license is mandatory if you want to
12+
// modify or otherwise use the software for commercial activities involving the
13+
// Arduino software without disclosing the source code of your own applications.
14+
// To purchase a commercial license, send an email to [email protected].
15+
16+
package resources
17+
18+
import (
19+
"os"
20+
"path"
21+
"path/filepath"
22+
"testing"
23+
24+
"github.com/arduino/go-paths-helper"
25+
26+
"github.com/stretchr/testify/require"
27+
)
28+
29+
func TestInstallPlatform(t *testing.T) {
30+
t.Run("ignore __MACOSX folder", func(t *testing.T) {
31+
testFileName := "platform_with_root_and__MACOSX_folder.tar.bz2"
32+
testFilePath := filepath.Join("testdata/valid", testFileName)
33+
34+
downloadDir, tempPath, destDir := paths.New(t.TempDir()), paths.New(t.TempDir()), paths.New(t.TempDir())
35+
36+
// copy testfile in the download dir
37+
origin, err := os.ReadFile(testFilePath)
38+
require.NoError(t, err)
39+
require.NoError(t, os.WriteFile(path.Join(downloadDir.String(), testFileName), origin, 0644))
40+
41+
r := &DownloadResource{
42+
ArchiveFileName: testFileName,
43+
Checksum: "SHA-256:600ad56b6260352e0b2cee786f60749e778e179252a0594ba542f0bd1f8adee5",
44+
Size: 157,
45+
}
46+
47+
require.NoError(t, r.Install(downloadDir, tempPath, destDir))
48+
})
49+
50+
tests := []struct {
51+
testName string
52+
downloadResource *DownloadResource
53+
error string
54+
}{
55+
{
56+
testName: "multiple root folders not allowed",
57+
downloadResource: &DownloadResource{
58+
ArchiveFileName: "platform_with_multiple_root_folders.tar.bz2",
59+
Checksum: "SHA-256:8b3fc6253c5ac2f3ba684eba0d62bb8a4ee93469fa822f81e2cd7d1b959c4044",
60+
Size: 148,
61+
},
62+
error: "no unique root dir in archive",
63+
},
64+
{
65+
testName: "root folder not present",
66+
downloadResource: &DownloadResource{
67+
ArchiveFileName: "platform_without_root_folder.tar.bz2",
68+
Checksum: "SHA-256:bc00db9784e20f50d7a5fceccb6bd95ebff4a3e847aac88365b95a6851a24963",
69+
Size: 177,
70+
},
71+
error: "files in archive must be placed in a subdirectory",
72+
},
73+
}
74+
for _, test := range tests {
75+
t.Run(test.testName, func(t *testing.T) {
76+
downloadDir, tempPath, destDir := paths.New(t.TempDir()), paths.New(t.TempDir()), paths.New(t.TempDir())
77+
testFileName := test.downloadResource.ArchiveFileName
78+
testFilePath := filepath.Join("testdata/invalid", testFileName)
79+
80+
// copy testfile in the download dir
81+
origin, err := os.ReadFile(testFilePath)
82+
require.NoError(t, err)
83+
require.NoError(t, os.WriteFile(path.Join(downloadDir.String(), testFileName), origin, 0644))
84+
85+
err = test.downloadResource.Install(downloadDir, tempPath, destDir)
86+
require.Error(t, err)
87+
require.Contains(t, err.Error(), test.error)
88+
})
89+
}
90+
}
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)