|
| 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 | +} |
0 commit comments