Skip to content

Commit d99c7c4

Browse files
committed
feat: Add test for cache clean
Clean cache files with command `arduino-cli cache clean`.
1 parent 3345c6d commit d99c7c4

File tree

2 files changed

+43
-28
lines changed

2 files changed

+43
-28
lines changed

Diff for: cli/cache/clean.go

+2-28
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,12 @@ package cache
1717

1818
import (
1919
"os"
20-
"path/filepath"
21-
"runtime"
2220

2321
"github.com/arduino/arduino-cli/cli/errorcodes"
2422
"github.com/arduino/arduino-cli/cli/feedback"
25-
"github.com/arduino/go-win32-utils"
2623
"github.com/sirupsen/logrus"
2724
"github.com/spf13/cobra"
25+
"github.com/spf13/viper"
2826
)
2927

3028
func initCleanCommand() *cobra.Command {
@@ -42,34 +40,10 @@ func initCleanCommand() *cobra.Command {
4240
func runCleanCommand(cmd *cobra.Command, args []string) {
4341
logrus.Info("Executing `arduino cache clean`")
4442

45-
cachePath := getDefaultArduinoDataDir() + "/staging"
43+
cachePath := viper.GetString("directories.Downloads")
4644
err := os.RemoveAll(cachePath)
4745
if err != nil {
4846
feedback.Errorf("Error cleaning caches: %v", err)
4947
os.Exit(errorcodes.ErrGeneric)
5048
}
5149
}
52-
53-
func getDefaultArduinoDataDir() string {
54-
userHomeDir, err := os.UserHomeDir()
55-
if err != nil {
56-
feedback.Errorf("Unable to get user home dir: %v", err)
57-
return "."
58-
}
59-
60-
switch runtime.GOOS {
61-
case "linux":
62-
return filepath.Join(userHomeDir, ".arduino15")
63-
case "darwin":
64-
return filepath.Join(userHomeDir, "Library", "Arduino15")
65-
case "windows":
66-
localAppDataPath, err := win32.GetLocalAppDataFolder()
67-
if err != nil {
68-
feedback.Errorf("Unable to get Local App Data Folder: %v", err)
69-
return "."
70-
}
71-
return filepath.Join(localAppDataPath, "Arduino15")
72-
default:
73-
return "."
74-
}
75-
}

Diff for: test/test_cache.py

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# This file is part of arduino-cli.
2+
#
3+
# Copyright 2020 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 modify or
12+
# otherwise use the software for commercial activities involving the Arduino
13+
# software without disclosing the source code of your own applications. To purchase
14+
# a commercial license, send an email to [email protected].
15+
import os
16+
import platform
17+
18+
19+
def test_cache_clean(run_command):
20+
"""
21+
Clean the cache under arduino caching file directory which is
22+
"<Arduino configure file path>/staging"
23+
"""
24+
result = run_command("cache clean")
25+
assert result.ok
26+
27+
# Generate /staging directory
28+
result = run_command("lib list")
29+
assert result.ok
30+
31+
result = run_command("cache clean")
32+
assert result.ok
33+
34+
running_platform = platform.system()
35+
homeDir = os.path.expanduser("~")
36+
if running_platform == "Linux":
37+
assert not (os.path.isdir(homeDir + ".arduino15/staging"))
38+
elif running_platform == "Darwin":
39+
assert not (os.path.isdir(homeDir + "Library/Arduino15/staging"))
40+
elif running_platform == "Windows":
41+
assert not (os.path.isdir(homeDir + "Arduino15/staging"))

0 commit comments

Comments
 (0)