@@ -17,18 +17,22 @@ package lib_test
1717
1818import (
1919 "encoding/json"
20+ "errors"
2021 "fmt"
2122 "io"
2223 "net/http"
24+ "net/url"
25+ "os"
2326 "runtime"
2427 "strings"
2528 "testing"
2629 "time"
2730
31+ "github.com/arduino/arduino-cli/internal/i18n"
2832 "github.com/arduino/arduino-cli/internal/integrationtest"
2933 "github.com/arduino/go-paths-helper"
3034 "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"
3236 "github.com/stretchr/testify/require"
3337 "go.bug.st/testifyjson/requirejson"
3438)
@@ -650,36 +654,21 @@ func TestInstallWithGitUrl(t *testing.T) {
650654}
651655
652656func 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+ })
679663 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+ }
683672}
684673
685674func TestUpdateIndex (t * testing.T ) {
@@ -1220,68 +1209,6 @@ func TestInstallZipInvalidLibrary(t *testing.T) {
12201209 require .Contains (t , string (stderr ), "library not valid" )
12211210}
12221211
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-
12851212func TestUpgradeDoesNotTryToUpgradeBundledCoreLibrariesInSketchbook (t * testing.T ) {
12861213 env , cli := integrationtest .CreateArduinoCLIWithEnvironment (t )
12871214 defer env .CleanUp ()
@@ -1715,3 +1642,48 @@ func TestDependencyResolverNoOverwrite(t *testing.T) {
17151642 _ , _ , err = cli .Run ("lib" , "install" , "EncoderTool@2.2.0" , "--no-overwrite" )
17161643 require .NoError (t , err )
17171644}
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 "git@xxxxx.com:arduino-libraries/SigFox.git"
1653+ prefixes := map [string ]string {
1654+ "git@github.com:" : "https://github.com/" ,
1655+ "git@gitlab.com:" : "https://gitlab.com/" ,
1656+ "git@bitbucket.org:" : "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