|
| 1 | +// This file is part of arduino-cli. |
| 2 | +// |
| 3 | +// Copyright 2022 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 sketch_test |
| 17 | + |
| 18 | +import ( |
| 19 | + "archive/zip" |
| 20 | + "fmt" |
| 21 | + "strings" |
| 22 | + "testing" |
| 23 | + |
| 24 | + "github.com/arduino/arduino-cli/internal/integrationtest" |
| 25 | + "github.com/arduino/go-paths-helper" |
| 26 | + "github.com/stretchr/testify/require" |
| 27 | +) |
| 28 | + |
| 29 | +type archiveTest struct { |
| 30 | + SubTestName string |
| 31 | + SketchPathParam string |
| 32 | + TargetPathParam string |
| 33 | + WorkingDir *paths.Path |
| 34 | + ExpectedArchivePath *paths.Path |
| 35 | +} |
| 36 | + |
| 37 | +func TestSketchNew(t *testing.T) { |
| 38 | + env, cli := integrationtest.CreateArduinoCLIWithEnvironment(t) |
| 39 | + defer env.CleanUp() |
| 40 | + |
| 41 | + // Create a test sketch in current directory |
| 42 | + currentPath := cli.WorkingDir() |
| 43 | + sketchName := "SketchNewIntegrationTest" |
| 44 | + currentSketchPath := currentPath.Join(sketchName) |
| 45 | + stdout, _, err := cli.Run("sketch", "new", sketchName) |
| 46 | + require.NoError(t, err) |
| 47 | + require.Contains(t, string(stdout), "Sketch created in: "+currentSketchPath.String()) |
| 48 | + require.FileExists(t, currentSketchPath.Join(sketchName).String()+".ino") |
| 49 | + |
| 50 | + // Create a test sketch in current directory but using an absolute path |
| 51 | + sketchName = "SketchNewIntegrationTestAbsolute" |
| 52 | + currentSketchPath = currentPath.Join(sketchName) |
| 53 | + stdout, _, err = cli.Run("sketch", "new", currentSketchPath.String()) |
| 54 | + require.NoError(t, err) |
| 55 | + require.Contains(t, string(stdout), "Sketch created in: "+currentSketchPath.String()) |
| 56 | + require.FileExists(t, currentSketchPath.Join(sketchName).String()+".ino") |
| 57 | + |
| 58 | + // Create a test sketch in current directory subpath but using an absolute path |
| 59 | + sketchName = "SketchNewIntegrationTestSubpath" |
| 60 | + sketchSubpath := paths.New("subpath", sketchName) |
| 61 | + currentSketchPath = currentPath.JoinPath(sketchSubpath) |
| 62 | + stdout, _, err = cli.Run("sketch", "new", sketchSubpath.String()) |
| 63 | + require.NoError(t, err) |
| 64 | + require.Contains(t, string(stdout), "Sketch created in: "+currentSketchPath.String()) |
| 65 | + require.FileExists(t, currentSketchPath.Join(sketchName).String()+".ino") |
| 66 | + |
| 67 | + // Create a test sketch in current directory using .ino extension |
| 68 | + sketchName = "SketchNewIntegrationTestDotIno" |
| 69 | + currentSketchPath = currentPath.Join(sketchName) |
| 70 | + stdout, _, err = cli.Run("sketch", "new", sketchName+".ino") |
| 71 | + require.NoError(t, err) |
| 72 | + require.Contains(t, string(stdout), "Sketch created in: "+currentSketchPath.String()) |
| 73 | + require.FileExists(t, currentSketchPath.Join(sketchName).String()+".ino") |
| 74 | +} |
| 75 | + |
| 76 | +func verifyZipContainsSketchExcludingBuildDir(t *testing.T, files []*zip.File) { |
| 77 | + require.Len(t, files, 8) |
| 78 | + require.Equal(t, paths.New("sketch_simple", "doc.txt").String(), files[0].Name) |
| 79 | + require.Equal(t, paths.New("sketch_simple", "header.h").String(), files[1].Name) |
| 80 | + require.Equal(t, paths.New("sketch_simple", "merged_sketch.txt").String(), files[2].Name) |
| 81 | + require.Equal(t, paths.New("sketch_simple", "old.pde").String(), files[3].Name) |
| 82 | + require.Equal(t, paths.New("sketch_simple", "other.ino").String(), files[4].Name) |
| 83 | + require.Equal(t, paths.New("sketch_simple", "s_file.S").String(), files[5].Name) |
| 84 | + require.Equal(t, paths.New("sketch_simple", "sketch_simple.ino").String(), files[6].Name) |
| 85 | + require.Equal(t, paths.New("sketch_simple", "src", "helper.h").String(), files[7].Name) |
| 86 | +} |
| 87 | + |
| 88 | +func verifyZipContainsSketchIncludingBuildDir(t *testing.T, files []*zip.File) { |
| 89 | + require.Len(t, files, 13) |
| 90 | + require.Equal(t, paths.New("sketch_simple", "doc.txt").String(), files[5].Name) |
| 91 | + require.Equal(t, paths.New("sketch_simple", "header.h").String(), files[6].Name) |
| 92 | + require.Equal(t, paths.New("sketch_simple", "merged_sketch.txt").String(), files[7].Name) |
| 93 | + require.Equal(t, paths.New("sketch_simple", "old.pde").String(), files[8].Name) |
| 94 | + require.Equal(t, paths.New("sketch_simple", "other.ino").String(), files[9].Name) |
| 95 | + require.Equal(t, paths.New("sketch_simple", "s_file.S").String(), files[10].Name) |
| 96 | + require.Equal(t, paths.New("sketch_simple", "sketch_simple.ino").String(), files[11].Name) |
| 97 | + require.Equal(t, paths.New("sketch_simple", "src", "helper.h").String(), files[12].Name) |
| 98 | + require.Equal(t, paths.New("sketch_simple", "build", "adafruit.samd.adafruit_feather_m0", "sketch_simple.ino.hex").String(), files[0].Name) |
| 99 | + require.Equal(t, paths.New("sketch_simple", "build", "adafruit.samd.adafruit_feather_m0", "sketch_simple.ino.map").String(), files[1].Name) |
| 100 | + require.Equal(t, paths.New("sketch_simple", "build", "arduino.avr.uno", "sketch_simple.ino.eep").String(), files[2].Name) |
| 101 | + require.Equal(t, paths.New("sketch_simple", "build", "arduino.avr.uno", "sketch_simple.ino.hex").String(), files[3].Name) |
| 102 | + require.Equal(t, paths.New("sketch_simple", "build", "arduino.avr.uno", "sketch_simple.ino.with_bootloader.hex").String(), files[4].Name) |
| 103 | +} |
| 104 | + |
| 105 | +func TestSketchArchive(t *testing.T) { |
| 106 | + env, cli := integrationtest.CreateArduinoCLIWithEnvironment(t) |
| 107 | + defer env.CleanUp() |
| 108 | + |
| 109 | + sketchSimple := cli.CopySketch("sketch_simple") |
| 110 | + |
| 111 | + // Creates a folder where to save the zip |
| 112 | + archivesFolder := cli.WorkingDir().Join("my_archives") |
| 113 | + require.NoError(t, archivesFolder.Mkdir()) |
| 114 | + |
| 115 | + archiveTests := []archiveTest{ |
| 116 | + { |
| 117 | + SubTestName: "ArchiveNoArgs", |
| 118 | + SketchPathParam: "", |
| 119 | + TargetPathParam: "", |
| 120 | + WorkingDir: sketchSimple, |
| 121 | + ExpectedArchivePath: env.RootDir().Join("sketch_simple.zip"), |
| 122 | + }, |
| 123 | + { |
| 124 | + SubTestName: "ArchiveDotArg", |
| 125 | + SketchPathParam: ".", |
| 126 | + TargetPathParam: "", |
| 127 | + WorkingDir: sketchSimple, |
| 128 | + ExpectedArchivePath: env.RootDir().Join("sketch_simple.zip"), |
| 129 | + }, |
| 130 | + { |
| 131 | + SubTestName: "DotArgRelativeZipPath", |
| 132 | + SketchPathParam: ".", |
| 133 | + TargetPathParam: "../my_archives", |
| 134 | + WorkingDir: sketchSimple, |
| 135 | + ExpectedArchivePath: archivesFolder.Join("sketch_simple.zip"), |
| 136 | + }, |
| 137 | + { |
| 138 | + SubTestName: "DotArgAbsoluteZiptPath", |
| 139 | + SketchPathParam: ".", |
| 140 | + TargetPathParam: archivesFolder.String(), |
| 141 | + WorkingDir: sketchSimple, |
| 142 | + ExpectedArchivePath: archivesFolder.Join("sketch_simple.zip"), |
| 143 | + }, |
| 144 | + { |
| 145 | + SubTestName: "ArchiveDotArgRelativeZipPathAndNameWithoutExtension", |
| 146 | + SketchPathParam: ".", |
| 147 | + TargetPathParam: "../my_archives/my_custom_sketch", |
| 148 | + WorkingDir: sketchSimple, |
| 149 | + ExpectedArchivePath: archivesFolder.Join("my_custom_sketch.zip"), |
| 150 | + }, |
| 151 | + { |
| 152 | + SubTestName: "ArchiveDotArgAbsoluteZipPathAndNameWithoutExtension", |
| 153 | + SketchPathParam: ".", |
| 154 | + TargetPathParam: archivesFolder.Join("my_custom_sketch").String(), |
| 155 | + WorkingDir: sketchSimple, |
| 156 | + ExpectedArchivePath: archivesFolder.Join("my_custom_sketch.zip"), |
| 157 | + }, |
| 158 | + { |
| 159 | + SubTestName: "ArchiveDotArgCustomZipPathAndNameWithExtension", |
| 160 | + SketchPathParam: ".", |
| 161 | + TargetPathParam: archivesFolder.Join("my_custom_sketch.zip").String(), |
| 162 | + WorkingDir: sketchSimple, |
| 163 | + ExpectedArchivePath: archivesFolder.Join("my_custom_sketch.zip"), |
| 164 | + }, |
| 165 | + { |
| 166 | + SubTestName: "ArchiveRelativeSketchPath", |
| 167 | + SketchPathParam: "./sketch_simple", |
| 168 | + TargetPathParam: "", |
| 169 | + WorkingDir: env.RootDir(), |
| 170 | + ExpectedArchivePath: env.RootDir().Join("sketch_simple.zip"), |
| 171 | + }, |
| 172 | + { |
| 173 | + SubTestName: "ArchiveAbsoluteSketchPath", |
| 174 | + SketchPathParam: env.RootDir().Join("sketch_simple").String(), |
| 175 | + TargetPathParam: "", |
| 176 | + WorkingDir: env.RootDir(), |
| 177 | + ExpectedArchivePath: env.RootDir().Join("sketch_simple.zip"), |
| 178 | + }, |
| 179 | + { |
| 180 | + SubTestName: "ArchiveRelativeSketchPathWithRelativeZipPath", |
| 181 | + SketchPathParam: "./sketch_simple", |
| 182 | + TargetPathParam: "./my_archives", |
| 183 | + WorkingDir: env.RootDir(), |
| 184 | + ExpectedArchivePath: archivesFolder.Join("sketch_simple.zip"), |
| 185 | + }, |
| 186 | + { |
| 187 | + SubTestName: "ArchiveRelativeSketchPathWithAbsoluteZipPath", |
| 188 | + SketchPathParam: "./sketch_simple", |
| 189 | + TargetPathParam: archivesFolder.String(), |
| 190 | + WorkingDir: env.RootDir(), |
| 191 | + ExpectedArchivePath: archivesFolder.Join("sketch_simple.zip"), |
| 192 | + }, |
| 193 | + { |
| 194 | + SubTestName: "ArchiveRelativeSketchPathWithRelativeZipPathAndNameWithoutExtension", |
| 195 | + SketchPathParam: "./sketch_simple", |
| 196 | + TargetPathParam: "./my_archives/my_custom_sketch", |
| 197 | + WorkingDir: env.RootDir(), |
| 198 | + ExpectedArchivePath: archivesFolder.Join("my_custom_sketch.zip"), |
| 199 | + }, |
| 200 | + { |
| 201 | + SubTestName: "ArchiveRelativeSketchPathWithRelativeZipPathAndNameWithExtension", |
| 202 | + SketchPathParam: "./sketch_simple", |
| 203 | + TargetPathParam: "./my_archives/my_custom_sketch.zip", |
| 204 | + WorkingDir: env.RootDir(), |
| 205 | + ExpectedArchivePath: archivesFolder.Join("my_custom_sketch.zip"), |
| 206 | + }, |
| 207 | + { |
| 208 | + SubTestName: "ArchiveRelativeSketchPathWithAbsoluteZipPathAndNameWithoutExtension", |
| 209 | + SketchPathParam: "./sketch_simple", |
| 210 | + TargetPathParam: archivesFolder.Join("my_custom_sketch").String(), |
| 211 | + WorkingDir: env.RootDir(), |
| 212 | + ExpectedArchivePath: archivesFolder.Join("my_custom_sketch.zip"), |
| 213 | + }, |
| 214 | + { |
| 215 | + SubTestName: "ArchiveRelativeSketchPathWithAbsoluteZipPathAndNameWithoutExtension", |
| 216 | + SketchPathParam: "./sketch_simple", |
| 217 | + TargetPathParam: archivesFolder.Join("my_custom_sketch.zip").String(), |
| 218 | + WorkingDir: env.RootDir(), |
| 219 | + ExpectedArchivePath: archivesFolder.Join("my_custom_sketch.zip"), |
| 220 | + }, |
| 221 | + { |
| 222 | + SubTestName: "ArchiveAbsoluteSketchPathWithRelativeZipPath", |
| 223 | + SketchPathParam: cli.WorkingDir().Join("sketch_simple").String(), |
| 224 | + TargetPathParam: "./my_archives", |
| 225 | + WorkingDir: env.RootDir(), |
| 226 | + ExpectedArchivePath: archivesFolder.Join("sketch_simple.zip"), |
| 227 | + }, |
| 228 | + { |
| 229 | + SubTestName: "ArchiveAbsoluteSketchPathWithAbsoluteZipPath", |
| 230 | + SketchPathParam: env.RootDir().Join("sketch_simple").String(), |
| 231 | + TargetPathParam: archivesFolder.String(), |
| 232 | + WorkingDir: sketchSimple, |
| 233 | + ExpectedArchivePath: archivesFolder.Join("sketch_simple.zip"), |
| 234 | + }, |
| 235 | + { |
| 236 | + SubTestName: "ArchiveAbsoluteSketchPathWithRelativeZipPathAndNameWithoutExtension", |
| 237 | + SketchPathParam: cli.WorkingDir().Join("sketch_simple").String(), |
| 238 | + TargetPathParam: "./my_archives/my_custom_sketch", |
| 239 | + WorkingDir: env.RootDir(), |
| 240 | + ExpectedArchivePath: archivesFolder.Join("my_custom_sketch.zip"), |
| 241 | + }, |
| 242 | + { |
| 243 | + SubTestName: "ArchiveAbsoluteSketchPathWithRelativeZipPathAndNameWithExtension", |
| 244 | + SketchPathParam: cli.WorkingDir().Join("sketch_simple").String(), |
| 245 | + TargetPathParam: "./my_archives/my_custom_sketch.zip", |
| 246 | + WorkingDir: env.RootDir(), |
| 247 | + ExpectedArchivePath: archivesFolder.Join("my_custom_sketch.zip"), |
| 248 | + }, |
| 249 | + { |
| 250 | + SubTestName: "ArchiveAbsoluteSketchPathWithAbsoluteZipPathAndNameWithoutExtension", |
| 251 | + SketchPathParam: env.RootDir().Join("sketch_simple").String(), |
| 252 | + TargetPathParam: archivesFolder.Join("my_custom_sketch").String(), |
| 253 | + WorkingDir: sketchSimple, |
| 254 | + ExpectedArchivePath: archivesFolder.Join("my_custom_sketch.zip"), |
| 255 | + }, |
| 256 | + { |
| 257 | + SubTestName: "ArchiveAbsoluteSketchPathWithAbsoluteZipPathAndNameWithExtension", |
| 258 | + SketchPathParam: env.RootDir().Join("sketch_simple").String(), |
| 259 | + TargetPathParam: archivesFolder.Join("my_custom_sketch.zip").String(), |
| 260 | + WorkingDir: sketchSimple, |
| 261 | + ExpectedArchivePath: archivesFolder.Join("my_custom_sketch.zip"), |
| 262 | + }, |
| 263 | + } |
| 264 | + |
| 265 | + for _, test := range archiveTests { |
| 266 | + t.Run(fmt.Sprint(test.SubTestName), func(t *testing.T) { |
| 267 | + var err error |
| 268 | + cli.SetWorkingDir(test.WorkingDir) |
| 269 | + if test.TargetPathParam == "" { |
| 270 | + if test.SketchPathParam == "" { |
| 271 | + _, _, err = cli.Run("sketch", "archive") |
| 272 | + } else { |
| 273 | + _, _, err = cli.Run("sketch", "archive", test.SketchPathParam) |
| 274 | + } |
| 275 | + } else { |
| 276 | + _, _, err = cli.Run("sketch", "archive", test.SketchPathParam, test.TargetPathParam) |
| 277 | + } |
| 278 | + require.NoError(t, err) |
| 279 | + |
| 280 | + archive, err := zip.OpenReader(test.ExpectedArchivePath.String()) |
| 281 | + require.NoError(t, err) |
| 282 | + defer require.NoError(t, archive.Close()) |
| 283 | + defer require.NoError(t, test.ExpectedArchivePath.Remove()) |
| 284 | + verifyZipContainsSketchExcludingBuildDir(t, archive.File) |
| 285 | + }) |
| 286 | + t.Run(fmt.Sprint(test.SubTestName+"WithIncludeBuildDirFlag"), func(t *testing.T) { |
| 287 | + var err error |
| 288 | + cli.SetWorkingDir(test.WorkingDir) |
| 289 | + if test.TargetPathParam == "" { |
| 290 | + if test.SketchPathParam == "" { |
| 291 | + _, _, err = cli.Run("sketch", "archive", "--include-build-dir") |
| 292 | + } else { |
| 293 | + _, _, err = cli.Run("sketch", "archive", test.SketchPathParam, "--include-build-dir") |
| 294 | + } |
| 295 | + } else { |
| 296 | + _, _, err = cli.Run("sketch", "archive", test.SketchPathParam, test.TargetPathParam, "--include-build-dir") |
| 297 | + } |
| 298 | + require.NoError(t, err) |
| 299 | + |
| 300 | + archive, err := zip.OpenReader(test.ExpectedArchivePath.String()) |
| 301 | + require.NoError(t, err) |
| 302 | + defer require.NoError(t, archive.Close()) |
| 303 | + defer require.NoError(t, test.ExpectedArchivePath.Remove()) |
| 304 | + verifyZipContainsSketchIncludingBuildDir(t, archive.File) |
| 305 | + }) |
| 306 | + } |
| 307 | +} |
| 308 | + |
| 309 | +func TestSketchArchiveWithPdeMainFile(t *testing.T) { |
| 310 | + env, cli := integrationtest.CreateArduinoCLIWithEnvironment(t) |
| 311 | + defer env.CleanUp() |
| 312 | + |
| 313 | + sketchName := "sketch_pde_main_file" |
| 314 | + sketchDir := cli.CopySketch(sketchName) |
| 315 | + sketchFile := sketchDir.Join(sketchName + ".pde") |
| 316 | + relPath, err := sketchFile.RelFrom(sketchDir) |
| 317 | + require.NoError(t, err) |
| 318 | + cli.SetWorkingDir(sketchDir) |
| 319 | + _, stderr, err := cli.Run("sketch", "archive") |
| 320 | + require.NoError(t, err) |
| 321 | + require.Contains(t, string(stderr), "Sketches with .pde extension are deprecated, please rename the following files to .ino") |
| 322 | + require.Contains(t, string(stderr), relPath.String()) |
| 323 | + cli.SetWorkingDir(env.RootDir()) |
| 324 | + |
| 325 | + archive, err := zip.OpenReader(cli.WorkingDir().Join(sketchName + ".zip").String()) |
| 326 | + require.NoError(t, err) |
| 327 | + defer require.NoError(t, archive.Close()) |
| 328 | + require.Contains(t, archive.File[0].Name, paths.New(sketchName, sketchName+".pde").String()) |
| 329 | +} |
| 330 | + |
| 331 | +func TestSketchArchiveWithMultipleMainFiles(t *testing.T) { |
| 332 | + env, cli := integrationtest.CreateArduinoCLIWithEnvironment(t) |
| 333 | + defer env.CleanUp() |
| 334 | + |
| 335 | + sketchName := "sketch_multiple_main_files" |
| 336 | + sketchDir := cli.CopySketch(sketchName) |
| 337 | + sketchFile := sketchDir.Join(sketchName + ".pde") |
| 338 | + relPath, err := sketchFile.RelFrom(sketchDir) |
| 339 | + require.NoError(t, err) |
| 340 | + cli.SetWorkingDir(sketchDir) |
| 341 | + _, stderr, err := cli.Run("sketch", "archive") |
| 342 | + require.Error(t, err) |
| 343 | + require.Contains(t, string(stderr), "Sketches with .pde extension are deprecated, please rename the following files to .ino") |
| 344 | + require.Contains(t, string(stderr), relPath.String()) |
| 345 | + require.Contains(t, string(stderr), "Error archiving: Can't open sketch: multiple main sketch files found") |
| 346 | +} |
| 347 | + |
| 348 | +func TestSketchArchiveCaseMismatchFails(t *testing.T) { |
| 349 | + env, cli := integrationtest.CreateArduinoCLIWithEnvironment(t) |
| 350 | + defer env.CleanUp() |
| 351 | + |
| 352 | + sketchName := "ArchiveSketchCaseMismatch" |
| 353 | + sketchPath := cli.SketchbookDir().Join(sketchName) |
| 354 | + |
| 355 | + _, _, err := cli.Run("sketch", "new", sketchPath.String()) |
| 356 | + require.NoError(t, err) |
| 357 | + |
| 358 | + // Rename main .ino file so casing is different from sketch name |
| 359 | + require.NoError(t, sketchPath.Join(sketchName+".ino").Rename(sketchPath.Join(strings.ToLower(sketchName)+".ino"))) |
| 360 | + |
| 361 | + _, stderr, err := cli.Run("sketch", "archive", sketchPath.String()) |
| 362 | + require.Error(t, err) |
| 363 | + require.Contains(t, string(stderr), "Error archiving: Can't open sketch:") |
| 364 | +} |
0 commit comments