@@ -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,31 @@ 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 ())
657+ libraryName , gitURL , ref , err := parseGitArgURL ("https://github.com/arduino-libraries/WiFi101.git#0.16.0" )
658+ if err != nil {
659+ require .NoError (t , err )
660+ }
661+ tmpInstallPath := paths .New (t .TempDir ()).Join (libraryName )
676662
677- // Reinstall library at an existing ref
678- _ , _ , err = cli .Run ("lib" , "install" , "--git-url" , gitUrl + "#master" , "--config-file" , "arduino-cli.yaml" )
663+ depth := 1
664+ if ref != "" {
665+ depth = 0
666+ }
667+ repo , err := git .PlainClone (tmpInstallPath .String (), false , & git.CloneOptions {
668+ URL : gitURL ,
669+ Depth : depth ,
670+ Progress : os .Stdout ,
671+ })
679672 require .NoError (t , err )
680-
681- // Verifies library remains installed
682- require .DirExists (t , libInstallDir .String ())
673+ if ref != "" {
674+ if h , err := repo .ResolveRevision (ref ); err != nil {
675+ require .NoError (t , err )
676+ } else if w , err := repo .Worktree (); err != nil {
677+ require .NoError (t , err )
678+ } else if err := w .Checkout (& git.CheckoutOptions {Hash : plumbing .NewHash (h .String ())}); err != nil {
679+ require .NoError (t , err )
680+ }
681+ }
683682}
684683
685684func TestUpdateIndex (t * testing.T ) {
@@ -1220,68 +1219,6 @@ func TestInstallZipInvalidLibrary(t *testing.T) {
12201219 require .Contains (t , string (stderr ), "library not valid" )
12211220}
12221221
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-
12851222func TestUpgradeDoesNotTryToUpgradeBundledCoreLibrariesInSketchbook (t * testing.T ) {
12861223 env , cli := integrationtest .CreateArduinoCLIWithEnvironment (t )
12871224 defer env .CleanUp ()
@@ -1715,3 +1652,48 @@ func TestDependencyResolverNoOverwrite(t *testing.T) {
17151652 _ , _ , err = cli .Run ("lib" , "install" , "EncoderTool@2.2.0" , "--no-overwrite" )
17161653 require .NoError (t , err )
17171654}
1655+
1656+ func parseGitArgURL (argURL string ) (string , string , plumbing.Revision , error ) {
1657+ // On Windows handle paths with backslashes in the form C:\Path\to\library
1658+ if path := paths .New (argURL ); path != nil && path .Exist () {
1659+ return path .Base (), argURL , "" , nil
1660+ }
1661+
1662+ // Handle commercial git-specific address in the form "git@xxxxx.com:arduino-libraries/SigFox.git"
1663+ prefixes := map [string ]string {
1664+ "git@github.com:" : "https://github.com/" ,
1665+ "git@gitlab.com:" : "https://gitlab.com/" ,
1666+ "git@bitbucket.org:" : "https://bitbucket.org/" ,
1667+ }
1668+ for prefix , replacement := range prefixes {
1669+ if strings .HasPrefix (argURL , prefix ) {
1670+ // We can't parse these as URLs
1671+ argURL = replacement + strings .TrimPrefix (argURL , prefix )
1672+ }
1673+ }
1674+
1675+ parsedURL , err := url .Parse (argURL )
1676+ if err != nil {
1677+ return "" , "" , "" , fmt .Errorf ("%s: %w" , i18n .Tr ("invalid git url" ), err )
1678+ }
1679+ if parsedURL .String () == "" {
1680+ return "" , "" , "" , errors .New (i18n .Tr ("invalid git url" ))
1681+ }
1682+
1683+ // Extract lib name from "https://github.com/arduino-libraries/SigFox.git#1.0.3"
1684+ // path == "/arduino-libraries/SigFox.git"
1685+ slash := strings .LastIndex (parsedURL .Path , "/" )
1686+ if slash == - 1 {
1687+ return "" , "" , "" , errors .New (i18n .Tr ("invalid git url" ))
1688+ }
1689+ libName := strings .TrimSuffix (parsedURL .Path [slash + 1 :], ".git" )
1690+ if libName == "" {
1691+ return "" , "" , "" , errors .New (i18n .Tr ("invalid git url" ))
1692+ }
1693+ // fragment == "1.0.3"
1694+ rev := plumbing .Revision (parsedURL .Fragment )
1695+ // gitURL == "https://github.com/arduino-libraries/SigFox.git"
1696+ parsedURL .Fragment = ""
1697+ gitURL := parsedURL .String ()
1698+ return libName , gitURL , rev , nil
1699+ }
0 commit comments