-
Notifications
You must be signed in to change notification settings - Fork 970
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove credentials from 'url' for SCM replacement (#4207)
* add 'remove_credentials' argument to SCMBase.get_remote_url * modify comments * do not remove credentials if it is a local path
- Loading branch information
Showing
9 changed files
with
151 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# coding=utf-8 | ||
|
||
import unittest | ||
import textwrap | ||
|
||
from conans.test.utils.tools import TestClient | ||
from conans.test.utils.tools import NO_SETTINGS_PACKAGE_ID, TestClient, TestServer, \ | ||
create_local_git_repo | ||
from conans.model.ref import ConanFileReference, PackageReference | ||
|
||
|
||
class RemoveCredentials(unittest.TestCase): | ||
|
||
conanfile = textwrap.dedent("""\ | ||
from conans import ConanFile | ||
class Lib(ConanFile): | ||
scm = {"type": "git", "url": "auto"} | ||
""") | ||
|
||
def setUp(self): | ||
self.ref = ConanFileReference.loads("lib/1.0@lasote/testing") | ||
self.path, _ = create_local_git_repo({"conanfile.py": self.conanfile}) | ||
self.client = TestClient() | ||
self.client.current_folder = self.path | ||
self.client.runner("git remote add origin https://url.to.be.sustituted", cwd=self.path) | ||
|
||
def test_https(self): | ||
expected_url = 'https://myrepo.com.git' | ||
origin_url = 'https://username:password@myrepo.com.git' | ||
|
||
self.client.runner("git remote set-url origin {}".format(origin_url), cwd=self.path) | ||
self.client.run("export . {}".format(self.ref)) | ||
self.assertIn("Repo origin deduced by 'auto': {}".format(expected_url), self.client.out) |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# coding=utf-8 | ||
|
||
import unittest | ||
|
||
from conans.client.tools.scm import Git | ||
from conans.test.utils.tools import temp_folder | ||
|
||
|
||
class GitRemoteUrlTest(unittest.TestCase): | ||
|
||
def test_remove_credentials(self): | ||
""" Check that the 'remove_credentials' argument is taken into account """ | ||
expected_url = 'https://myrepo.com/path/to/repo.git' | ||
origin_url = 'https://username:password@myrepo.com/path/to/repo.git' | ||
|
||
git = Git(folder=temp_folder()) | ||
git.run("init .") | ||
git.run("remote add origin {}".format(origin_url)) | ||
|
||
self.assertEqual(git.get_remote_url(), origin_url) | ||
self.assertEqual(git.get_remote_url(remove_credentials=True), expected_url) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# coding=utf-8 | ||
|
||
import unittest | ||
|
||
from conans.client.tools.scm import SCMBase | ||
|
||
|
||
class RemoveCredentialsTest(unittest.TestCase): | ||
|
||
def test_http(self): | ||
expected_url = 'https://myrepo.com/path/to/repo.git' | ||
test_urls = ['https://myrepo.com/path/to/repo.git', | ||
'https://username:password@myrepo.com/path/to/repo.git', | ||
'https://username@myrepo.com/path/to/repo.git', | ||
'https://gitlab-ci-token:1324@myrepo.com/path/to/repo.git', | ||
] | ||
|
||
for it in test_urls: | ||
self.assertEqual(expected_url, SCMBase._remove_credentials_url(it)) | ||
|
||
def test_http_with_port_number(self): | ||
self.assertEqual('https://myrepo.com:8000/path/to/repo.git', | ||
SCMBase._remove_credentials_url( | ||
'https://username@myrepo.com:8000/path/to/repo.git')) | ||
|
||
def test_ssh(self): | ||
# Here, for ssh, we don't want to remove the user ('git' in this example) | ||
self.assertEqual('git@github.com:conan-io/conan.git', | ||
SCMBase._remove_credentials_url( | ||
'git@github.com:conan-io/conan.git')) | ||
|
||
def test_local_unix(self): | ||
self.assertEqual('file:///srv/git/project.git', | ||
SCMBase._remove_credentials_url('file:///srv/git/project.git')) | ||
self.assertEqual('file:///srv/git/PROJECT.git', | ||
SCMBase._remove_credentials_url('file:///srv/git/PROJECT.git')) | ||
|
||
def test_local_windows(self): | ||
self.assertEqual('file:///c:/srv/git/PROJECT', | ||
SCMBase._remove_credentials_url('file:///c:/srv/git/PROJECT')) | ||
self.assertEqual('file:///C:/srv/git/PROJECT', | ||
SCMBase._remove_credentials_url('file:///C:/srv/git/PROJECT')) | ||
|
||
def test_svn_ssh(self): | ||
self.assertEqual('svn+ssh://10.106.191.164/home/svn/shproject', | ||
SCMBase._remove_credentials_url( | ||
'svn+ssh://username:password@10.106.191.164/home/svn/shproject')) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# coding=utf-8 | ||
|
||
import unittest | ||
|
||
from mock import patch | ||
|
||
from conans.client.tools.scm import SVN | ||
from conans.test.utils.tools import temp_folder | ||
|
||
|
||
class SVNRemoteUrlTest(unittest.TestCase): | ||
|
||
def test_remove_credentials(self): | ||
""" Check that the 'remove_credentials' argument is taken into account """ | ||
expected_url = 'https://myrepo.com/path/to/repo' | ||
origin_url = 'https://username:password@myrepo.com/path/to/repo' | ||
|
||
svn = SVN(folder=temp_folder()) | ||
|
||
# Mocking, as we cannot change SVN remote to a non-existing url | ||
with patch.object(svn, '_show_item', return_value=origin_url): | ||
self.assertEqual(svn.get_remote_url(), origin_url) | ||
self.assertEqual(svn.get_remote_url(remove_credentials=True), expected_url) |