|
| 1 | +// This file is part of arduino-app-cli. |
| 2 | +// |
| 3 | +// Copyright 2025 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-app-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 license@arduino.cc. |
| 15 | + |
| 16 | +package orchestrator |
| 17 | + |
| 18 | +import ( |
| 19 | + "context" |
| 20 | + "testing" |
| 21 | + |
| 22 | + "github.com/arduino/go-paths-helper" |
| 23 | + "github.com/stretchr/testify/assert" |
| 24 | + "github.com/stretchr/testify/require" |
| 25 | + |
| 26 | + "github.com/arduino/arduino-app-cli/internal/orchestrator/app" |
| 27 | +) |
| 28 | + |
| 29 | +func TestListSketchLibraries(t *testing.T) { |
| 30 | + t.Run("fail to list libraries if the sketch folder is missing", func(t *testing.T) { |
| 31 | + pythonApp, err := app.Load(createTestAppPythonOnly(t)) |
| 32 | + require.NoError(t, err) |
| 33 | + |
| 34 | + libs, err := ListSketchLibraries(context.Background(), pythonApp) |
| 35 | + require.Error(t, err) |
| 36 | + assert.Contains(t, err.Error(), "cannot list libraries. Missing sketch folder") |
| 37 | + assert.Empty(t, libs) |
| 38 | + }) |
| 39 | + |
| 40 | + t.Run("fail to add library if the sketch folder is missing", func(t *testing.T) { |
| 41 | + pythonApp, err := app.Load(createTestAppPythonOnly(t)) |
| 42 | + require.NoError(t, err) |
| 43 | + |
| 44 | + libs, err := AddSketchLibrary(context.Background(), pythonApp, LibraryReleaseID{}, false) |
| 45 | + require.Error(t, err) |
| 46 | + assert.Contains(t, err.Error(), "cannot add a library. Missing sketch folder") |
| 47 | + assert.Empty(t, libs) |
| 48 | + }) |
| 49 | + |
| 50 | + t.Run("fail to remove library if the sketch folder is missing", func(t *testing.T) { |
| 51 | + pythonApp, err := app.Load(createTestAppPythonOnly(t)) |
| 52 | + require.NoError(t, err) |
| 53 | + |
| 54 | + id, err := RemoveSketchLibrary(context.Background(), pythonApp, LibraryReleaseID{}) |
| 55 | + require.Error(t, err) |
| 56 | + assert.Contains(t, err.Error(), "cannot remove a library. Missing sketch folder") |
| 57 | + assert.Empty(t, id) |
| 58 | + }) |
| 59 | +} |
| 60 | + |
| 61 | +// Helper function to create a test app without sketch path (Python-only) |
| 62 | +func createTestAppPythonOnly(t *testing.T) *paths.Path { |
| 63 | + tempDir := t.TempDir() |
| 64 | + |
| 65 | + appYaml := paths.New(tempDir, "app.yaml") |
| 66 | + require.NoError(t, appYaml.WriteFile([]byte(` |
| 67 | +name: test-python-app |
| 68 | +version: 1.0.0 |
| 69 | +description: Test Python-only app |
| 70 | +`))) |
| 71 | + |
| 72 | + // Create python directory and file |
| 73 | + pythonDir := paths.New(tempDir, "python") |
| 74 | + require.NoError(t, pythonDir.MkdirAll()) |
| 75 | + |
| 76 | + pythonFile := pythonDir.Join("main.py") |
| 77 | + require.NoError(t, pythonFile.WriteFile([]byte(` |
| 78 | +import time |
| 79 | +
|
| 80 | +def main(): |
| 81 | + print("Hello from Python!") |
| 82 | + time.sleep(1) |
| 83 | +
|
| 84 | +if __name__ == "__main__": |
| 85 | + main() |
| 86 | +`))) |
| 87 | + return paths.New(tempDir) |
| 88 | +} |
0 commit comments