|
| 1 | +package resources |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "path" |
| 6 | + "path/filepath" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/arduino/go-paths-helper" |
| 10 | + |
| 11 | + "github.com/stretchr/testify/require" |
| 12 | +) |
| 13 | + |
| 14 | +func TestInstallPlatform(t *testing.T) { |
| 15 | + t.Run("ignore __MACOSX folder", func(t *testing.T) { |
| 16 | + testFileName := "platform_with_root_and__MACOSX_folder.tar.bz2" |
| 17 | + testFilePath := filepath.Join("testdata/valid", testFileName) |
| 18 | + |
| 19 | + downloadDir, tempPath, destDir := paths.New(t.TempDir()), paths.New(t.TempDir()), paths.New(t.TempDir()) |
| 20 | + |
| 21 | + // copy testfile in the download dir |
| 22 | + origin, err := os.ReadFile(testFilePath) |
| 23 | + require.NoError(t, err) |
| 24 | + require.NoError(t, os.WriteFile(path.Join(downloadDir.String(), testFileName), origin, 0644)) |
| 25 | + |
| 26 | + r := &DownloadResource{ |
| 27 | + ArchiveFileName: testFileName, |
| 28 | + Checksum: "SHA-256:600ad56b6260352e0b2cee786f60749e778e179252a0594ba542f0bd1f8adee5", |
| 29 | + Size: 157, |
| 30 | + } |
| 31 | + |
| 32 | + require.NoError(t, r.Install(downloadDir, tempPath, destDir)) |
| 33 | + }) |
| 34 | + |
| 35 | + tests := []struct { |
| 36 | + testName string |
| 37 | + downloadResource *DownloadResource |
| 38 | + error string |
| 39 | + }{ |
| 40 | + { |
| 41 | + testName: "multiple root folders not allowed", |
| 42 | + downloadResource: &DownloadResource{ |
| 43 | + ArchiveFileName: "platform_with_multiple_root_folders.tar.bz2", |
| 44 | + Checksum: "SHA-256:8b3fc6253c5ac2f3ba684eba0d62bb8a4ee93469fa822f81e2cd7d1b959c4044", |
| 45 | + Size: 148, |
| 46 | + }, |
| 47 | + error: "no unique root dir in archive", |
| 48 | + }, |
| 49 | + { |
| 50 | + testName: "root folder not present", |
| 51 | + downloadResource: &DownloadResource{ |
| 52 | + ArchiveFileName: "platform_without_root_folder.tar.bz2", |
| 53 | + Checksum: "SHA-256:bc00db9784e20f50d7a5fceccb6bd95ebff4a3e847aac88365b95a6851a24963", |
| 54 | + Size: 177, |
| 55 | + }, |
| 56 | + error: "files in archive must be placed in a subdirectory", |
| 57 | + }, |
| 58 | + } |
| 59 | + for _, test := range tests { |
| 60 | + t.Run(test.testName, func(t *testing.T) { |
| 61 | + downloadDir, tempPath, destDir := paths.New(t.TempDir()), paths.New(t.TempDir()), paths.New(t.TempDir()) |
| 62 | + testFileName := test.downloadResource.ArchiveFileName |
| 63 | + testFilePath := filepath.Join("testdata/invalid", testFileName) |
| 64 | + |
| 65 | + // copy testfile in the download dir |
| 66 | + origin, err := os.ReadFile(testFilePath) |
| 67 | + require.NoError(t, err) |
| 68 | + require.NoError(t, os.WriteFile(path.Join(downloadDir.String(), testFileName), origin, 0644)) |
| 69 | + |
| 70 | + err = test.downloadResource.Install(downloadDir, tempPath, destDir) |
| 71 | + require.Error(t, err) |
| 72 | + require.Contains(t, err.Error(), test.error) |
| 73 | + }) |
| 74 | + } |
| 75 | +} |
0 commit comments