@@ -18,6 +18,7 @@ package cores
1818import (
1919 "testing"
2020
21+ "github.com/arduino/arduino-cli/arduino/resources"
2122 "github.com/stretchr/testify/require"
2223)
2324
@@ -26,71 +27,147 @@ func TestFlavorCompatibility(t *testing.T) {
2627 Os string
2728 Arch string
2829 }
29- windowsi386 := &os{"windows", "386"}
30- windowsx8664 := &os{"windows", "amd64"}
31- linuxi386 := &os{"linux", "386"}
32- linuxamd64 := &os{"linux", "amd64"}
33- linuxarm := &os{"linux", "arm"}
34- linuxarmbe := &os{"linux", "armbe"}
35- linuxarm64 := &os{"linux", "arm64"}
36- darwini386 := &os{"darwin", "386"}
37- darwinamd64 := &os{"darwin", "amd64"}
38- freebsdi386 := &os{"freebsd", "386"}
39- freebsdamd64 := &os{"freebsd", "amd64"}
30+ windows32 := &os{"windows", "386"}
31+ windows64 := &os{"windows", "amd64"}
32+ linux32 := &os{"linux", "386"}
33+ linux64 := &os{"linux", "amd64"}
34+ linuxArm := &os{"linux", "arm"}
35+ linuxArmbe := &os{"linux", "armbe"}
36+ linuxArm64 := &os{"linux", "arm64"}
37+ darwin32 := &os{"darwin", "386"}
38+ darwin64 := &os{"darwin", "amd64"}
39+ darwinArm64 := &os{"darwin", "arm64"}
40+ freebsd32 := &os{"freebsd", "386"}
41+ freebsd64 := &os{"freebsd", "amd64"}
4042 oses := []*os{
41- windowsi386,
42- windowsx8664,
43- linuxi386,
44- linuxamd64,
45- linuxarm,
46- linuxarmbe,
47- linuxarm64,
48- darwini386,
49- darwinamd64,
50- freebsdi386,
51- freebsdamd64,
43+ windows32,
44+ windows64,
45+ linux32,
46+ linux64,
47+ linuxArm,
48+ linuxArmbe,
49+ linuxArm64,
50+ darwin32,
51+ darwin64,
52+ darwinArm64,
53+ freebsd32,
54+ freebsd64,
5255 }
5356
5457 type test struct {
55- Flavour *Flavor
56- Positives []*os
58+ Flavour *Flavor
59+ Compatibles []*os
60+ ExactMatch []*os
5761 }
5862 tests := []*test{
59- {&Flavor{OS: "i686-mingw32"}, []*os{windowsi386, windowsx8664}},
60- {&Flavor{OS: "i386-apple-darwin11"}, []*os{darwini386, darwinamd64}},
61- {&Flavor{OS: "x86_64-apple-darwin"}, []*os{darwinamd64}},
63+ {&Flavor{OS: "i686-mingw32"}, []*os{windows32, windows64}, []*os{windows32}},
64+ {&Flavor{OS: "x86_64-mingw32"}, []*os{windows64}, []*os{windows64}},
65+ {&Flavor{OS: "i386-apple-darwin11"}, []*os{darwin32, darwin64, darwinArm64}, []*os{darwin32}},
66+ {&Flavor{OS: "x86_64-apple-darwin"}, []*os{darwin64, darwinArm64}, []*os{darwin64}},
67+ {&Flavor{OS: "arm64-apple-darwin"}, []*os{darwinArm64}, []*os{darwinArm64}},
6268
6369 // Raspberry PI, BBB or other ARM based host
6470 // PI: "arm-linux-gnueabihf"
6571 // Raspbian on PI2: "arm-linux-gnueabihf"
6672 // Ubuntu Mate on PI2: "arm-linux-gnueabihf"
6773 // Debian 7.9 on BBB: "arm-linux-gnueabihf"
6874 // Raspbian on PI Zero: "arm-linux-gnueabihf"
69- {&Flavor{OS: "arm-linux-gnueabihf"}, []*os{linuxarm, linuxarmbe }},
75+ {&Flavor{OS: "arm-linux-gnueabihf"}, []*os{linuxArm, linuxArmbe}, []*os{linuxArm, linuxArmbe }},
7076 // Arch-linux on PI2: "armv7l-unknown-linux-gnueabihf"
71- {&Flavor{OS: "armv7l-unknown-linux-gnueabihf"}, []*os{linuxarm, linuxarmbe }},
77+ {&Flavor{OS: "armv7l-unknown-linux-gnueabihf"}, []*os{linuxArm, linuxArmbe}, []*os{linuxArm, linuxArmbe }},
7278
73- {&Flavor{OS: "i686-linux-gnu"}, []*os{linuxi386 }},
74- {&Flavor{OS: "i686-pc-linux-gnu"}, []*os{linuxi386 }},
75- {&Flavor{OS: "x86_64-linux-gnu"}, []*os{linuxamd64 }},
76- {&Flavor{OS: "x86_64-pc-linux-gnu"}, []*os{linuxamd64 }},
77- {&Flavor{OS: "aarch64-linux-gnu"}, []*os{linuxarm64 }},
78- {&Flavor{OS: "arm64-linux-gnu"}, []*os{linuxarm64 }},
79+ {&Flavor{OS: "i686-linux-gnu"}, []*os{linux32}, []*os{linux32 }},
80+ {&Flavor{OS: "i686-pc-linux-gnu"}, []*os{linux32}, []*os{linux32 }},
81+ {&Flavor{OS: "x86_64-linux-gnu"}, []*os{linux64}, []*os{linux64 }},
82+ {&Flavor{OS: "x86_64-pc-linux-gnu"}, []*os{linux64}, []*os{linux64 }},
83+ {&Flavor{OS: "aarch64-linux-gnu"}, []*os{linuxArm64}, []*os{linuxArm64 }},
84+ {&Flavor{OS: "arm64-linux-gnu"}, []*os{linuxArm64}, []*os{linuxArm64 }},
7985 }
8086
81- check := func(test *test, os *os) {
82- for _, positiveOs := range test.Positives {
87+ checkCompatible := func(test *test, os *os) {
88+ // if the os is in the "positive" set iCompatibleWith must return true...
89+ res, _ := test.Flavour.isCompatibleWith(os.Os, os.Arch)
90+ for _, compatibleOs := range test.Compatibles {
91+ if compatibleOs == os {
92+ require.True(t, res, "'%s' tag compatible with '%s,%s' pair", test.Flavour.OS, os.Os, os.Arch)
93+ return
94+ }
95+ }
96+ // ...otherwise false
97+ require.False(t, res, "'%s' tag compatible with '%s,%s' pair", test.Flavour.OS, os.Os, os.Arch)
98+ }
99+ checkExactMatch := func(test *test, os *os) {
100+ // if the os is in the "positive" set iExactMatchWith must return true...
101+ for _, positiveOs := range test.ExactMatch {
83102 if positiveOs == os {
84- require.True(t, test.Flavour.isCompatibleWith (os.Os, os.Arch), "'%s' tag compatible with '%s,%s' pair", test.Flavour.OS, os.Os, os.Arch)
103+ require.True(t, test.Flavour.isExactMatchWith (os.Os, os.Arch), "'%s' tag exact match with '%s,%s' pair", test.Flavour.OS, os.Os, os.Arch)
85104 return
86105 }
87106 }
88- require.False(t, test.Flavour.isCompatibleWith(os.Os, os.Arch), "'%s' tag compatible with '%s,%s' pair", test.Flavour.OS, os.Os, os.Arch)
107+ // ...otherwise false
108+ require.False(t, test.Flavour.isExactMatchWith(os.Os, os.Arch), "'%s' tag exact match with '%s,%s' pair", test.Flavour.OS, os.Os, os.Arch)
89109 }
90110
91111 for _, test := range tests {
92112 for _, os := range oses {
93- check(test, os)
113+ checkCompatible(test, os)
114+ checkExactMatch(test, os)
94115 }
95116 }
96117}
118+
119+ func TestFlavorPrioritySelection(t *testing.T) {
120+ res := (&ToolRelease{
121+ Flavors: []*Flavor{
122+ {OS: "i386-apple-darwin11", Resource: &resources.DownloadResource{ArchiveFileName: "1"}},
123+ {OS: "x86_64-apple-darwin", Resource: &resources.DownloadResource{ArchiveFileName: "2"}},
124+ {OS: "arm64-apple-darwin", Resource: &resources.DownloadResource{ArchiveFileName: "3"}},
125+ },
126+ }).GetFlavourCompatibleWith("darwin", "arm64")
127+ require.NotNil(t, res)
128+ require.Equal(t, "3", res.ArchiveFileName)
129+
130+ res = (&ToolRelease{
131+ Flavors: []*Flavor{
132+ {OS: "i386-apple-darwin11", Resource: &resources.DownloadResource{ArchiveFileName: "1"}},
133+ {OS: "x86_64-apple-darwin", Resource: &resources.DownloadResource{ArchiveFileName: "2"}},
134+ },
135+ }).GetFlavourCompatibleWith("darwin", "arm64")
136+ require.NotNil(t, res)
137+ require.Equal(t, "2", res.ArchiveFileName)
138+
139+ res = (&ToolRelease{
140+ Flavors: []*Flavor{
141+ {OS: "x86_64-apple-darwin", Resource: &resources.DownloadResource{ArchiveFileName: "2"}},
142+ {OS: "i386-apple-darwin11", Resource: &resources.DownloadResource{ArchiveFileName: "1"}},
143+ },
144+ }).GetFlavourCompatibleWith("darwin", "arm64")
145+ require.NotNil(t, res)
146+ require.Equal(t, "2", res.ArchiveFileName)
147+
148+ res = (&ToolRelease{
149+ Flavors: []*Flavor{
150+ {OS: "i386-apple-darwin11", Resource: &resources.DownloadResource{ArchiveFileName: "1"}},
151+ },
152+ }).GetFlavourCompatibleWith("darwin", "arm64")
153+ require.NotNil(t, res)
154+ require.Equal(t, "1", res.ArchiveFileName)
155+
156+ res = (&ToolRelease{
157+ Flavors: []*Flavor{
158+ {OS: "i686-mingw32", Resource: &resources.DownloadResource{ArchiveFileName: "1"}},
159+ {OS: "x86_64-mingw32", Resource: &resources.DownloadResource{ArchiveFileName: "2"}},
160+ },
161+ }).GetFlavourCompatibleWith("windows", "amd64")
162+ require.NotNil(t, res)
163+ require.Equal(t, "2", res.ArchiveFileName)
164+
165+ res = (&ToolRelease{
166+ Flavors: []*Flavor{
167+ {OS: "x86_64-mingw32", Resource: &resources.DownloadResource{ArchiveFileName: "2"}},
168+ {OS: "i686-mingw32", Resource: &resources.DownloadResource{ArchiveFileName: "1"}},
169+ },
170+ }).GetFlavourCompatibleWith("windows", "amd64")
171+ require.NotNil(t, res)
172+ require.Equal(t, "2", res.ArchiveFileName)
173+ }
0 commit comments