@@ -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,22 @@ 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+ if h , err := repo .ResolveRevision (ref ); err != nil {
666+ require .NoError (t , err )
667+ } else if w , err := repo .Worktree (); err != nil {
668+ require .NoError (t , err )
669+ } else if err := w .Checkout (& git.CheckoutOptions {Hash : plumbing .NewHash (h .String ())}); err != nil {
670+ require .NoError (t , err )
671+ }
672+ }
683673}
684674
685675func TestUpdateIndex (t * testing.T ) {
@@ -1220,68 +1210,6 @@ func TestInstallZipInvalidLibrary(t *testing.T) {
12201210 require .Contains (t , string (stderr ), "library not valid" )
12211211}
12221212
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-
12851213func TestUpgradeDoesNotTryToUpgradeBundledCoreLibrariesInSketchbook (t * testing.T ) {
12861214 env , cli := integrationtest .CreateArduinoCLIWithEnvironment (t )
12871215 defer env .CleanUp ()
@@ -1715,3 +1643,48 @@ func TestDependencyResolverNoOverwrite(t *testing.T) {
17151643 _ ,
_ ,
err = cli .
Run (
"lib" ,
"install" ,
"[email protected] " ,
"--no-overwrite" )
17161644 require .NoError (t , err )
17171645}
1646+
1647+ func parseGitArgURL (argURL string ) (string , string , plumbing.Revision , error ) {
1648+ // On Windows handle paths with backslashes in the form C:\Path\to\library
1649+ if path := paths .New (argURL ); path != nil && path .Exist () {
1650+ return path .Base (), argURL , "" , nil
1651+ }
1652+
1653+ // Handle commercial git-specific address in the form "[email protected] :arduino-libraries/SigFox.git" 1654+ prefixes := map [string ]string {
1655+ "[email protected] :" :
"https://github.com/" ,
1656+ "[email protected] :" :
"https://gitlab.com/" ,
1657+ "[email protected] :" :
"https://bitbucket.org/" ,
1658+ }
1659+ for prefix , replacement := range prefixes {
1660+ if strings .HasPrefix (argURL , prefix ) {
1661+ // We can't parse these as URLs
1662+ argURL = replacement + strings .TrimPrefix (argURL , prefix )
1663+ }
1664+ }
1665+
1666+ parsedURL , err := url .Parse (argURL )
1667+ if err != nil {
1668+ return "" , "" , "" , fmt .Errorf ("%s: %w" , i18n .Tr ("invalid git url" ), err )
1669+ }
1670+ if parsedURL .String () == "" {
1671+ return "" , "" , "" , errors .New (i18n .Tr ("invalid git url" ))
1672+ }
1673+
1674+ // Extract lib name from "https://github.com/arduino-libraries/SigFox.git#1.0.3"
1675+ // path == "/arduino-libraries/SigFox.git"
1676+ slash := strings .LastIndex (parsedURL .Path , "/" )
1677+ if slash == - 1 {
1678+ return "" , "" , "" , errors .New (i18n .Tr ("invalid git url" ))
1679+ }
1680+ libName := strings .TrimSuffix (parsedURL .Path [slash + 1 :], ".git" )
1681+ if libName == "" {
1682+ return "" , "" , "" , errors .New (i18n .Tr ("invalid git url" ))
1683+ }
1684+ // fragment == "1.0.3"
1685+ rev := plumbing .Revision (parsedURL .Fragment )
1686+ // gitURL == "https://github.com/arduino-libraries/SigFox.git"
1687+ parsedURL .Fragment = ""
1688+ gitURL := parsedURL .String ()
1689+ return libName , gitURL , rev , nil
1690+ }
0 commit comments