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_submodule_git(self): subsubmodule, _ = create_local_git_repo({"subsubmodule": "contents"}) submodule, _ = create_local_git_repo({"submodule": "contents"}, submodules=[subsubmodule]) path, commit = create_local_git_repo({"myfile": "contents"}, submodules=[submodule]) 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 # Check old (default) behaviour tmp, submodule_path, _ = _create_paths() git = Git(tmp) git.clone(path) self.assertTrue(os.path.exists(os.path.join(tmp, "myfile"))) self.assertFalse( os.path.exists(os.path.join(submodule_path, "submodule"))) # Check invalid value tmp, submodule_path, _ = _create_paths() git = Git(tmp) git.clone(path) with six.assertRaisesRegex( self, ConanException, "Invalid 'submodule' attribute value in the 'scm'."): git.checkout(commit, submodule="invalid") # Check shallow tmp, submodule_path, subsubmodule_path = _create_paths() git = Git(tmp) git.clone(path) git.checkout(commit, submodule="shallow") self.assertTrue(os.path.exists(os.path.join(tmp, "myfile"))) self.assertTrue( os.path.exists(os.path.join(submodule_path, "submodule"))) self.assertFalse( os.path.exists(os.path.join(subsubmodule_path, "subsubmodule"))) # Check recursive tmp, submodule_path, subsubmodule_path = _create_paths() git = Git(tmp) git.clone(path) git.checkout(commit, submodule="recursive") self.assertTrue(os.path.exists(os.path.join(tmp, "myfile"))) self.assertTrue( os.path.exists(os.path.join(submodule_path, "submodule"))) self.assertTrue( os.path.exists(os.path.join(subsubmodule_path, "subsubmodule")))
def test_repo_root(self): root_path, _ = create_local_git_repo({"myfile": "anything"}) # Initialized in the root folder git = Git(root_path) self.assertEqual(root_path, git.get_repo_root()) # Initialized elsewhere subfolder = os.path.join(root_path, 'subfolder') os.makedirs(subfolder) git = Git(subfolder) self.assertEqual(root_path, git.get_repo_root())
def test_local_source(self): curdir = self.client.current_folder conanfile = base_git.format(url="auto", revision="auto") conanfile += """ def source(self): self.output.warn("SOURCE METHOD CALLED") """ self.client.save({"conanfile.py": conanfile, "myfile.txt": "My file is copied"}) create_local_git_repo(folder=self.client.current_folder) self.client.save({"aditional_file.txt": "contents"}) self.client.run("source . --source-folder=./source") self.assertTrue(os.path.exists(os.path.join(curdir, "source", "myfile.txt"))) self.assertIn("SOURCE METHOD CALLED", self.client.out) # Even the not commited files are copied self.assertTrue(os.path.exists(os.path.join(curdir, "source", "aditional_file.txt"))) self.assertIn("Getting sources from folder: %s" % curdir, self.client.out) # Export again but now with absolute reference, so no pointer file is created nor kept git = Git(curdir.replace("\\", "/")) conanfile = base_git.format(url=curdir.replace("\\", "/"), revision=git.get_revision()) conanfile += """ def source(self): self.output.warn("SOURCE METHOD CALLED") """ self.client.save({"conanfile.py": conanfile, "myfile2.txt": "My file is copied"}) create_local_git_repo(folder=self.client.current_folder) self.client.run("source . --source-folder=./source2") # myfile2 is no in the specified commit self.assertFalse(os.path.exists(os.path.join(curdir, "source2", "myfile2.txt"))) self.assertTrue(os.path.exists(os.path.join(curdir, "source2", "myfile.txt"))) self.assertIn("Getting sources from url: '%s'" % curdir.replace("\\", "/"), self.client.out) self.assertIn("SOURCE METHOD CALLED", self.client.out)
def test_auto_git(self): curdir = get_cased_path(self.client.current_folder).replace("\\", "/") conanfile = base_git.format(directory="None", url=_quoted("auto"), revision="auto") self.client.save({"conanfile.py": conanfile, "myfile.txt": "My file is copied"}) create_local_git_repo(folder=self.client.current_folder) self.client.run("export . user/channel", assert_error=True) self.assertIn("Repo origin cannot be deduced", self.client.out) self.client.run_command('git remote add origin https://myrepo.com.git') # Create the package, will copy the sources from the local folder self.client.run("create . user/channel") self.assertIn("Repo origin deduced by 'auto': https://myrepo.com.git", self.client.out) self.assertIn("Revision deduced by 'auto'", self.client.out) self.assertIn("SCM: Getting sources from folder: %s" % curdir, self.client.out) self.assertIn("My file is copied", self.client.out) # check blank lines are respected in replacement self.client.run("get lib/0.1@user/channel") self.assertIn("""} def build(self):""", self.client.out) # Export again but now with absolute reference, so no sources are copied from the local dir git = Git(curdir) self.client.save({"conanfile.py": base_git.format(url=_quoted(curdir), revision=git.get_revision())}) self.client.run("create . user/channel") self.assertNotIn("Repo origin deduced by 'auto'", self.client.out) self.assertNotIn("Revision deduced by 'auto'", self.client.out) self.assertNotIn("Getting sources from folder: %s" % curdir, self.client.out) self.assertIn("SCM: Getting sources from url: '%s'" % curdir, self.client.out) self.assertIn("My file is copied", self.client.out)
def create_local_git_repo(files=None, branch=None, submodules=None, folder=None, commits=1, tags=None): tmp = folder or temp_folder() tmp = get_cased_path(tmp) if files: save_files(tmp, files) git = Git(tmp) git.run("init .") git.run('config user.email "*****@*****.**"') git.run('config user.name "Your Name"') if branch: git.run("checkout -b %s" % branch) git.run("add .") for i in range(0, commits): git.run('commit --allow-empty -m "commiting"') tags = tags or [] for tag in tags: git.run("tag %s" % tag) if submodules: for submodule in submodules: git.run('submodule add "%s"' % submodule) git.run('commit -m "add submodules"') return tmp.replace("\\", "/"), git.get_revision()
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 test_no_tag(self): """ No tags has been created in repo """ git = Git(folder=self.folder) tag = git.get_tag() self.assertIsNone(tag)
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_user_space_with_local_sources(self): output = TestBufferConanOutput() # Need a real repo to get a working SCM object local_sources_path = os.path.join(self.tmp_dir, 'git_repo').replace('\\', '/') create_local_git_repo(files={'file1': "content"}, folder=local_sources_path) git = Git(local_sources_path) url = "https://remote.url" git.run("remote add origin \"{}\"".format(url)) # Mock the conanfile (return scm_data) conanfile = mock.MagicMock() conanfile.scm = {'type': 'git', 'url': 'auto', 'revision': 'auto'} # Mock functions called from inside _run_scm (tests will be here) def merge_directories(src, dst, excluded=None): src = os.path.normpath(src) dst = os.path.normpath(dst) self.assertEqual(src.replace('\\', '/'), local_sources_path) self.assertEqual(dst, self.src_folder) with mock.patch("conans.client.source.merge_directories", side_effect=merge_directories): _run_local_scm(conanfile, conanfile_folder=local_sources_path, src_folder=self.src_folder, output=output) self.assertIn( "getting sources from folder: {}".format( local_sources_path).lower(), str(output).lower())
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_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_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 test_in_tag(self): """ Current checkout is on a tag """ git = Git(folder=self.folder) git.run("tag 0.0.0") tag = git.get_tag() self.assertEqual("0.0.0", tag)
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_in_branch_with_tag(self): """ Tag is defined but current commit is ahead of it """ git = Git(folder=self.folder) git.run("tag 0.0.0") save(os.path.join(self.folder, "file.txt"), "") git.run("add .") git.run("commit -m \"new file\"") tag = git.get_tag() self.assertIsNone(tag)
def _get_repo(self): repo = { "git": Git(self.repo_folder, verify_ssl=self._data.verify_ssl, username=self._data.username, password=self._data.password) }.get(self._data.type) if not repo: raise ConanException("SCM not supported: %s" % self._data.type) return repo
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 setUp(self): ref = ConanFileReference.loads("name/version@user/channel") tmp_dir = temp_folder() # Need a real repo to get a working SCM object self.conanfile_dir = os.path.join(tmp_dir, 'git_repo').replace('\\', '/') self.git = Git(folder=self.conanfile_dir) self.origin, self.rev = create_local_git_repo(files={'file1': "content"}, folder=self.git.folder) # Mock the cache item (return the cache_ref_folder) self.cache_ref_folder = os.path.join(temp_folder(), ref.dir_repr())
def create_local_git_repo(files, branch=None): tmp = temp_folder() save_files(tmp, files) git = Git(tmp) git.run("init .") if branch: git.run("checkout -b %s" % branch) git.run("add .") git.run('config user.email "*****@*****.**"') git.run('config user.name "Your Name"') git.run('commit -m "message"') return tmp.replace("\\", "/"), git.get_revision()
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 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_is_pristine(self): root_path, _ = create_local_git_repo({"myfile": "anything"}) git = Git(root_path) self.assertTrue(git.is_pristine()) save(os.path.join(root_path, "other_file"), "content") self.assertFalse(git.is_pristine()) git.run("add .") self.assertFalse(git.is_pristine()) git.run('commit -m "commit"') self.assertTrue(git.is_pristine())
def ensure_cache_preserved(): cache_directory = os.environ["CONAN_USER_HOME"] git = Git(folder=cache_directory) with open(os.path.join(cache_directory, '.gitignore'), 'w') as gitignore: gitignore.write(".conan/data/") git.run("init .") git.run("add .") try: yield finally: r = git.run("diff") if r: writeln_console(">>> " + colorama.Fore.RED + "This is example modifies the cache!") writeln_console(r) raise Exception("Example modifies cache!")
def test_auto_git(self): curdir = self.client.current_folder.replace("\\", "/") conanfile = base.format(directory="None", url="auto", revision="auto") self.client.save({ "conanfile.py": conanfile, "myfile.txt": "My file is copied" }) self._commit_contents() error = self.client.run("export . user/channel", ignore_error=True) self.assertTrue(error) self.assertIn("Repo origin cannot be deduced by 'auto'", self.client.out) self.client.runner('git remote add origin https://myrepo.com.git', cwd=curdir) # Create the package, will copy the sources from the local folder self.client.run("create . user/channel") sources_dir = self.client.client_cache.scm_folder(self.reference) self.assertEquals(load(sources_dir), curdir) self.assertIn("Repo origin deduced by 'auto': https://myrepo.com.git", self.client.out) self.assertIn("Revision deduced by 'auto'", self.client.out) self.assertIn("Getting sources from folder: %s" % curdir, self.client.out) self.assertIn("My file is copied", self.client.out) # Export again but now with absolute reference, so no pointer file is created nor kept git = Git(curdir) self.client.save({ "conanfile.py": base.format(url=curdir, revision=git.get_revision()) }) self.client.run("create . user/channel") sources_dir = self.client.client_cache.scm_folder(self.reference) self.assertFalse(os.path.exists(sources_dir)) self.assertNotIn("Repo origin deduced by 'auto'", self.client.out) self.assertNotIn("Revision deduced by 'auto'", self.client.out) self.assertIn("Getting sources from url: '%s'" % curdir, self.client.out) self.assertIn("My file is copied", self.client.out)
def basic_test(self): server = TestServer() servers = {"default": server} # Create a project with git files = { 'conanfile.py': """from conans import ConanFile class MyPkg(ConanFile): name= "package" version = "1.0" scm = { "type": "git", "subfolder": "hello/to/you", "url": "auto", "revision": "auto" } """ } path, _ = create_local_git_repo(files) client = TestClient(servers=servers, users={"default": [("lasote", "mypass")]}) git = Git(client.current_folder) git.clone(path) # Upload to 'remote' client.run("export . lasote/stable") client.run("upload package/1.0@lasote/stable") # Consume the project client = TestClient(servers=servers, users={"default": [("lasote", "mypass")]}) error = client.run("install package/1.0@lasote/stable --build=package") self.assertFalse(error)
def create_local_git_repo(files=None, branch=None, submodules=None, folder=None): tmp = folder or temp_folder() if files: save_files(tmp, files) git = Git(tmp) git.run("init .") git.run('config user.email "*****@*****.**"') git.run('config user.name "Your Name"') if branch: git.run("checkout -b %s" % branch) git.run("add .") git.run('commit -m "commiting"') if submodules: for submodule in submodules: git.run('submodule add "%s"' % submodule) git.run('commit -m "add submodules"') return tmp.replace("\\", "/"), git.get_revision()
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")))