def test_cache_config(self): file_path = os.path.join(temp_folder(), "whatever_cacert") save(file_path, "") conan_conf = os.path.join(temp_folder(), "conan.conf") save(conan_conf, normalize(default_client_conf)) replace_in_file(conan_conf, "# cacert_path", "cacert_path={}".format(file_path), output=TestBufferConanOutput()) config = ConanClientConfigParser(conan_conf) mocked_requester = MockRequesterGet() requester = ConanRequester(config, mocked_requester) requester.get(url="bbbb", verify=True) self.assertEqual(mocked_requester.verify, file_path)
def test_get_tag_no_git_repo(self): # Try to get tag out of a git repo git = Git(folder=temp_folder()) with six.assertRaisesRegex( self, ConanException, "Not a valid 'git' repository or 'git' not found"): git.get_tag()
def test_verify_ssl(self): class MyRunner(object): def __init__(self): self.calls = [] def __call__(self, *args, **kwargs): self.calls.append(args[0]) return "" runner = MyRunner() tmp = temp_folder() git = Git(tmp, username="******", password="******", verify_ssl=True, runner=runner, force_english=True) git.clone(url="https://myrepo.git") self.assertIn("git config http.sslVerify true", runner.calls[1]) runner = MyRunner() git = Git(tmp, username="******", password="******", verify_ssl=False, runner=runner, force_english=False) git.clone(url="https://myrepo.git") self.assertIn("git config http.sslVerify false", runner.calls[1])
def test_clone_git_shallow_revision(self): path, revision = create_local_git_repo({"myfile": "contents"}, commits=3, tags=["1.0"], branch="develop") tmp = temp_folder() git = Git(tmp) if Git.get_version() < "2.13": # older Git versions have known bugs with "git fetch origin <sha>": # https://github.com/git/git/blob/master/Documentation/RelNotes/2.13.0.txt # * "git fetch" that requests a commit by object name, when the other # side does not allow such an request, failed without much # explanation. # https://github.com/git/git/blob/master/Documentation/RelNotes/2.14.0.txt # * There is no good reason why "git fetch $there $sha1" should fail # when the $sha1 names an object at the tip of an advertised ref, # even when the other side hasn't enabled allowTipSHA1InWant. with self.assertRaises(subprocess.CalledProcessError): git.clone("file://" + path, branch=revision, shallow=True) else: git.clone("file://" + path, branch=revision, shallow=True) with self.assertRaises(subprocess.CalledProcessError): git.checkout(element="HEAD~1") self.assertTrue(os.path.exists(os.path.join(tmp, "myfile"))) self.assertEqual(git.get_revision(), revision) self.assertEqual(git.run("rev-list --all --count"), "1")
def _create_requesters(conan_user_home=temp_folder()): with environment_append({'CONAN_USER_HOME': conan_user_home}): conan_api, cache, _ = Conan.factory() requester = conan_api._requester mock_requester = MockRequesterGet() requester._requester = mock_requester return requester, mock_requester, cache
def test_clone_existing_folder_without_branch(self): tmp = temp_folder() save(os.path.join(tmp, "file"), "dummy contents") git = Git(tmp) with six.assertRaisesRegex(self, ConanException, "specify a branch to checkout"): git.clone("https://github.com/conan-community/conan-zlib.git")
def test_env_variable(self): file_path = os.path.join(temp_folder(), "whatever_cacert") save(file_path, "dummy content") with environment_append({"CONAN_CACERT_PATH": file_path}): requester, mocked_requester, _ = self._create_requesters() requester.get(url="aaa", verify=True) self.assertEqual(mocked_requester.verify, file_path)
def test_excluded_files(self): folder = temp_folder() save(os.path.join(folder, "file"), "some contents") git = Git(folder) with tools.environment_append({"PATH": ""}): excluded = git.excluded_files() self.assertEqual(excluded, [])
def test_non_existing_file(self): file_path = os.path.join(temp_folder(), "whatever_cacert") self.assertFalse(os.path.exists(file_path)) with environment_append({"CONAN_CACERT_PATH": file_path}): with six.assertRaisesRegex( self, ConanException, "Configured file for 'cacert_path'" " doesn't exist"): self._create_requesters()
def test_get_tag_no_git_repo(self): # Try to get tag out of a git repo tmp_folder = temp_folder() git = Git(folder=tmp_folder) pattern = "'{0}' is not a valid 'git' repository or 'git' not found".format( re.escape(tmp_folder)) with six.assertRaisesRegex(self, ConanException, pattern): git.get_tag()
def _create_paths(): tmp = temp_folder() submodule_path = os.path.join( tmp, os.path.basename(os.path.normpath(submodule))) subsubmodule_path = os.path.join( submodule_path, os.path.basename(os.path.normpath(subsubmodule))) return tmp, submodule_path, subsubmodule_path
def test_rename_folder_robocopy(): conanfile = ConanFileMock() tmp = temp_folder() old_folder = os.path.join(tmp, "old_folder") os.mkdir(old_folder) new_folder = os.path.join(tmp, "new_folder") rename(conanfile, old_folder, new_folder) assert not os.path.exists(old_folder) assert os.path.exists(new_folder)
def test_rename_file(): conanfile = ConanFileMock() tmp = temp_folder() save_files(tmp, {"file.txt": ""}) old_path = os.path.join(tmp, "file.txt") new_path = os.path.join(tmp, "kk.txt") rename(conanfile, old_path, new_path) assert not os.path.exists(old_path) assert os.path.exists(new_path)
def test_clone_git_shallow(self, element): path, revision = create_local_git_repo({"myfile": "contents"}, commits=3, tags=["1.0"], branch="develop") tmp = temp_folder() git = Git(tmp) git.clone("file://" + path, branch=element, shallow=True) # --depth is ignored in local clones with self.assertRaises(subprocess.CalledProcessError): git.checkout(element="HEAD~1") self.assertTrue(os.path.exists(os.path.join(tmp, "myfile"))) self.assertEqual(git.get_revision(), revision) self.assertEqual(git.run("rev-list --all --count"), "1")
def test_non_existing_default_file(self): conan_user_home = temp_folder() default_cacert_path = os.path.join(conan_user_home, CACERT_FILE) self.assertFalse(os.path.exists(default_cacert_path)) requester, mocked_requester, cache = self._create_requesters( conan_user_home) requester.get(url="aaa", verify=True) self.assertEqual(mocked_requester.verify, cache.config.cacert_path) self.assertEqual(cache.config.cacert_path, default_cacert_path)
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://*****:*****@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)
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://*****:*****@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)
def test_clone_existing_folder_git(self): path, commit = create_local_git_repo({"myfile": "contents"}, branch="my_release") tmp = temp_folder() save(os.path.join(tmp, "file"), "dummy contents") git = Git(tmp) git.clone(path, branch="my_release") self.assertTrue(os.path.exists(os.path.join(tmp, "myfile"))) # Checkout a commit git.checkout(commit) self.assertEqual(git.get_revision(), commit)
def test_user_agent(self): cache_folder = temp_folder() cache = ClientCache(cache_folder, TestBufferConanOutput()) mock_http_requester = MagicMock() requester = ConanRequester(cache.config, mock_http_requester) requester.get(url="aaa") headers = mock_http_requester.get.call_args[1]["headers"] self.assertIn("Conan/%s" % __version__, headers["User-Agent"]) requester.get(url="aaa", headers={"User-Agent": "MyUserAgent"}) headers = mock_http_requester.get.call_args[1]["headers"] self.assertEqual("MyUserAgent", headers["User-Agent"])
def test_cache_config(self): file_path = os.path.join(temp_folder(), "whatever_cacert") save(file_path, "dummy") requester, mocked_requester, cache = self._create_requesters() replace_in_file(cache.conan_conf_path, "# cacert_path", "cacert_path={}".format(file_path), output=TestBufferConanOutput()) cache.invalidate() requester.get(url="bbbb", verify=True) self.assertEqual(mocked_requester.verify, file_path)
def git_commit_message_test(self): client = TestClient() git_repo = temp_folder() client.runner("git init .", cwd=git_repo) client.runner('git config user.email "*****@*****.**"', cwd=git_repo) client.runner('git config user.name "Your Name"', cwd=git_repo) client.runner("git checkout -b dev", cwd=git_repo) git = Git(git_repo) self.assertIsNone(git.get_commit_message()) save(os.path.join(git_repo, "test"), "contents") client.runner("git add test", cwd=git_repo) client.runner('git commit -m "first commit"', cwd=git_repo) self.assertEqual("dev", git.get_branch()) self.assertEqual("first commit", git.get_commit_message())
def test_clone_git_shallow_with_local(self): path, revision = create_local_git_repo({"repofile": "contents"}, commits=3) tmp = temp_folder() save(os.path.join(tmp, "localfile"), "contents") save(os.path.join(tmp, "indexfile"), "contents") git = Git(tmp) git.run("init") git.run("add indexfile") git.clone("file://" + path, branch="master", shallow=True) # --depth is ignored in local clones self.assertTrue(os.path.exists(os.path.join(tmp, "repofile"))) self.assertTrue(os.path.exists(os.path.join(tmp, "localfile"))) self.assertTrue(os.path.exists(os.path.join(tmp, "indexfile"))) self.assertEqual(git.get_revision(), revision) self.assertEqual(git.run("rev-list --all --count"), "1")
def test_cache_config(self): cache = ClientCache(temp_folder(), TestBufferConanOutput()) file_path = os.path.join(cache.cache_folder, "whatever_cacert") replace_in_file(cache.conan_conf_path, "# cacert_path", "cacert_path={}".format(file_path), output=TestBufferConanOutput()) save(file_path, "") cache.invalidate() requester = ConanRequester(cache.config) mocked_requester = MockRequesterGet() requester._http_requester = mocked_requester requester.get(url="bbbb", verify=True) self.assertEqual(mocked_requester.verify, file_path)
def test_credentials(self): tmp = temp_folder() git = Git(tmp, username="******", password="******") url_credentials = git.get_url_with_credentials("https://some.url.com") self.assertEqual(url_credentials, "https://*****:*****@some.url.com")
def test_clone_git(self): path, _ = create_local_git_repo({"myfile": "contents"}) tmp = temp_folder() git = Git(tmp) git.clone(path) self.assertTrue(os.path.exists(os.path.join(tmp, "myfile")))
def test_is_local_repository(self): root_path, _ = create_local_git_repo({"myfile": "anything"}) git = Git(temp_folder()) git.clone(root_path) self.assertTrue(git.is_local_repository())
def git_helper_in_recipe_test(self): client = TestClient() git_repo = temp_folder() save(os.path.join(git_repo, "file.h"), "contents") client.runner("git init .", cwd=git_repo) client.runner('git config user.email "*****@*****.**"', cwd=git_repo) client.runner('git config user.name "Your Name"', cwd=git_repo) client.runner("git checkout -b dev", cwd=git_repo) client.runner("git add .", cwd=git_repo) client.runner('git commit -m "comm"', cwd=git_repo) conanfile = """ import os from conans import ConanFile, tools class HelloConan(ConanFile): name = "Hello" version = "0.1" exports_sources = "other" def source(self): git = tools.Git() git.clone("%s", "dev") def build(self): assert(os.path.exists("file.h")) """ % git_repo.replace("\\", "/") client.save({"conanfile.py": conanfile, "other": "hello"}) client.run("create . user/channel") # Now clone in a subfolder with later checkout conanfile = """ import os from conans import ConanFile, tools class HelloConan(ConanFile): name = "Hello" version = "0.1" exports_sources = "other" def source(self): tools.mkdir("src") git = tools.Git("./src") git.clone("%s") git.checkout("dev") def build(self): assert(os.path.exists(os.path.join("src", "file.h"))) """ % git_repo.replace("\\", "/") client.save({"conanfile.py": conanfile, "other": "hello"}) client.run("create . user/channel") # Base dir, with exports without subfolder and not specifying checkout fails conanfile = """ import os from conans import ConanFile, tools class HelloConan(ConanFile): name = "Hello" version = "0.1" exports_sources = "other" def source(self): git = tools.Git() git.clone("%s") def build(self): assert(os.path.exists("file.h")) """ % git_repo.replace("\\", "/") client.save({"conanfile.py": conanfile, "other": "hello"}) client.run("create . user/channel", assert_error=True) self.assertIn("specify a branch to checkout", client.out)
def _create_requesters(cache_folder=None): cache = ClientCache(cache_folder or temp_folder(), TestBufferConanOutput()) mock_requester = MockRequesterGet() requester = ConanRequester(cache.config, mock_requester) return requester, mock_requester, cache