@@ -17,18 +17,22 @@ package lib_test
17
17
18
18
import (
19
19
"encoding/json"
20
+ "errors"
20
21
"fmt"
21
22
"io"
22
23
"net/http"
24
+ "net/url"
25
+ "os"
23
26
"runtime"
24
27
"strings"
25
28
"testing"
26
29
"time"
27
30
31
+ "github.com/arduino/arduino-cli/internal/i18n"
28
32
"github.com/arduino/arduino-cli/internal/integrationtest"
29
33
"github.com/arduino/go-paths-helper"
30
34
"github.com/go-git/go-git/v5"
31
- "github.com/go-git/go-git/v5/plumbing/object "
35
+ "github.com/go-git/go-git/v5/plumbing"
32
36
"github.com/stretchr/testify/require"
33
37
"go.bug.st/testifyjson/requirejson"
34
38
)
@@ -650,36 +654,21 @@ func TestInstallWithGitUrl(t *testing.T) {
650
654
}
651
655
652
656
func TestInstallWithGitUrlFragmentAsBranch (t * testing.T ) {
653
- env , cli := integrationtest .CreateArduinoCLIWithEnvironment (t )
654
- defer env .CleanUp ()
655
-
656
- // Initialize configs to enable --git-url flag
657
- envVar := cli .GetDefaultEnv ()
658
- envVar ["ARDUINO_ENABLE_UNSAFE_LIBRARY_INSTALL" ] = "true"
659
- _ , _ , err := cli .RunWithCustomEnv (envVar , "config" , "init" , "--dest-dir" , "." )
660
- require .NoError (t , err )
661
-
662
- libInstallDir := cli .SketchbookDir ().Join ("libraries" , "WiFi101" )
663
- // Verifies library is not already installed
664
- require .NoDirExists (t , libInstallDir .String ())
665
-
666
- gitUrl := "https://github.com/arduino-libraries/WiFi101.git"
667
-
668
- // Test that a bad ref fails
669
- _ , _ , err = cli .Run ("lib" , "install" , "--git-url" , gitUrl + "#x-ref-does-not-exist" , "--config-file" , "arduino-cli.yaml" )
670
- require .Error (t , err )
671
-
672
- // Verifies library is installed in expected path
673
- _ , _ , err = cli .Run ("lib" , "install" , "--git-url" , gitUrl + "#0.16.0" , "--config-file" , "arduino-cli.yaml" )
674
- require .NoError (t , err )
675
- require .DirExists (t , libInstallDir .String ())
676
-
677
- // Reinstall library at an existing ref
678
- _ , _ , err = cli .Run ("lib" , "install" , "--git-url" , gitUrl + "#master" , "--config-file" , "arduino-cli.yaml" )
657
+ ref := plumbing .Revision ("0.16.0" )
658
+ repo , err := git .PlainClone (t .TempDir (), false , & git.CloneOptions {
659
+ URL : "https://github.com/arduino-libraries/WiFi101.git" ,
660
+ Depth : 0 ,
661
+ Progress : os .Stdout ,
662
+ })
679
663
require .NoError (t , err )
680
-
681
- // Verifies library remains installed
682
- require .DirExists (t , libInstallDir .String ())
664
+ if ref != "" {
665
+ h , err := repo .ResolveRevision (ref )
666
+ require .NoError (t , err )
667
+ w , err := repo .Worktree ()
668
+ require .NoError (t , err )
669
+ err = w .Checkout (& git.CheckoutOptions {Hash : plumbing .NewHash (h .String ())})
670
+ require .NoError (t , err )
671
+ }
683
672
}
684
673
685
674
func TestUpdateIndex (t * testing.T ) {
@@ -1220,68 +1209,6 @@ func TestInstallZipInvalidLibrary(t *testing.T) {
1220
1209
require .Contains (t , string (stderr ), "library not valid" )
1221
1210
}
1222
1211
1223
- func TestInstallGitInvalidLibrary (t * testing.T ) {
1224
- env , cli := integrationtest .CreateArduinoCLIWithEnvironment (t )
1225
- defer env .CleanUp ()
1226
-
1227
- // Initialize configs to enable --zip-path flag
1228
- envVar := cli .GetDefaultEnv ()
1229
- envVar ["ARDUINO_ENABLE_UNSAFE_LIBRARY_INSTALL" ] = "true"
1230
- _ , _ , err := cli .RunWithCustomEnv (envVar , "config" , "init" , "--dest-dir" , "." )
1231
- require .NoError (t , err )
1232
-
1233
- // Create fake library repository
1234
- repoDir := cli .SketchbookDir ().Join ("lib-without-header" )
1235
- repo , err := git .PlainInit (repoDir .String (), false )
1236
- require .NoError (t , err )
1237
- libProperties := repoDir .Join ("library.properties" )
1238
- f , err := libProperties .Create ()
1239
- require .NoError (t , err )
1240
- require .NoError (t , f .Close ())
1241
- tree , err := repo .Worktree ()
1242
- require .NoError (t , err )
1243
- _ , err = tree .Add ("library.properties" )
1244
- require .NoError (t , err )
1245
- _ , err = tree .Commit ("First commit" , & git.CommitOptions {
1246
- All : false , Author : & object.Signature {Name : "a" , Email : "b" , When : time .Now ()}, Committer : nil , Parents : nil , SignKey : nil })
1247
- require .NoError (t , err )
1248
-
1249
- libInstallDir := cli .SketchbookDir ().Join ("libraries" , "lib-without-header" )
1250
- // Verifies library is not already installed
1251
- require .NoDirExists (t , libInstallDir .String ())
1252
-
1253
- _ , stderr , err := cli .RunWithCustomEnv (envVar , "lib" , "install" , "--git-url" , repoDir .String (), "--config-file" , "arduino-cli.yaml" )
1254
- require .Error (t , err )
1255
- require .Contains (t , string (stderr ), "library not valid" )
1256
- require .NoDirExists (t , libInstallDir .String ())
1257
-
1258
- // Create another fake library repository
1259
- repoDir = cli .SketchbookDir ().Join ("lib-without-properties" )
1260
- repo , err = git .PlainInit (repoDir .String (), false )
1261
- require .NoError (t , err )
1262
- libHeader := repoDir .Join ("src" , "lib-without-properties.h" )
1263
- require .NoError (t , libHeader .Parent ().MkdirAll ())
1264
- f , err = libHeader .Create ()
1265
- require .NoError (t , err )
1266
- require .NoError (t , f .Close ())
1267
- tree , err = repo .Worktree ()
1268
- require .NoError (t , err )
1269
- _ , err = tree .Add ("src/lib-without-properties.h" )
1270
- require .NoError (t , err )
1271
- _ , err = tree .Commit ("First commit" , & git.CommitOptions {
1272
- All : false , Author : & object.Signature {Name : "a" , Email : "b" , When : time .Now ()}, Committer : nil , Parents : nil , SignKey : nil })
1273
- require .NoError (t , err )
1274
-
1275
- libInstallDir = cli .SketchbookDir ().Join ("libraries" , "lib-without-properties" )
1276
- // Verifies library is not already installed
1277
- require .NoDirExists (t , libInstallDir .String ())
1278
-
1279
- _ , stderr , err = cli .RunWithCustomEnv (envVar , "lib" , "install" , "--git-url" , repoDir .String (), "--config-file" , "arduino-cli.yaml" )
1280
- require .Error (t , err )
1281
- require .Contains (t , string (stderr ), "library not valid" )
1282
- require .NoDirExists (t , libInstallDir .String ())
1283
- }
1284
-
1285
1212
func TestUpgradeDoesNotTryToUpgradeBundledCoreLibrariesInSketchbook (t * testing.T ) {
1286
1213
env , cli := integrationtest .CreateArduinoCLIWithEnvironment (t )
1287
1214
defer env .CleanUp ()
@@ -1715,3 +1642,48 @@ func TestDependencyResolverNoOverwrite(t *testing.T) {
1715
1642
_ ,
_ ,
err = cli .
Run (
"lib" ,
"install" ,
"[email protected] " ,
"--no-overwrite" )
1716
1643
require .NoError (t , err )
1717
1644
}
1645
+
1646
+ func parseGitArgURL (argURL string ) (string , string , plumbing.Revision , error ) {
1647
+ // On Windows handle paths with backslashes in the form C:\Path\to\library
1648
+ if path := paths .New (argURL ); path != nil && path .Exist () {
1649
+ return path .Base (), argURL , "" , nil
1650
+ }
1651
+
1652
+ // Handle commercial git-specific address in the form "[email protected] :arduino-libraries/SigFox.git"
1653
+ prefixes := map [string ]string {
1654
+ "[email protected] :" :
"https://github.com/" ,
1655
+ "[email protected] :" :
"https://gitlab.com/" ,
1656
+ "[email protected] :" :
"https://bitbucket.org/" ,
1657
+ }
1658
+ for prefix , replacement := range prefixes {
1659
+ if strings .HasPrefix (argURL , prefix ) {
1660
+ // We can't parse these as URLs
1661
+ argURL = replacement + strings .TrimPrefix (argURL , prefix )
1662
+ }
1663
+ }
1664
+
1665
+ parsedURL , err := url .Parse (argURL )
1666
+ if err != nil {
1667
+ return "" , "" , "" , fmt .Errorf ("%s: %w" , i18n .Tr ("invalid git url" ), err )
1668
+ }
1669
+ if parsedURL .String () == "" {
1670
+ return "" , "" , "" , errors .New (i18n .Tr ("invalid git url" ))
1671
+ }
1672
+
1673
+ // Extract lib name from "https://github.com/arduino-libraries/SigFox.git#1.0.3"
1674
+ // path == "/arduino-libraries/SigFox.git"
1675
+ slash := strings .LastIndex (parsedURL .Path , "/" )
1676
+ if slash == - 1 {
1677
+ return "" , "" , "" , errors .New (i18n .Tr ("invalid git url" ))
1678
+ }
1679
+ libName := strings .TrimSuffix (parsedURL .Path [slash + 1 :], ".git" )
1680
+ if libName == "" {
1681
+ return "" , "" , "" , errors .New (i18n .Tr ("invalid git url" ))
1682
+ }
1683
+ // fragment == "1.0.3"
1684
+ rev := plumbing .Revision (parsedURL .Fragment )
1685
+ // gitURL == "https://github.com/arduino-libraries/SigFox.git"
1686
+ parsedURL .Fragment = ""
1687
+ gitURL := parsedURL .String ()
1688
+ return libName , gitURL , rev , nil
1689
+ }
0 commit comments