def test_branch_exceptions(self): """ This wil create conditions to exercise bad paths in the switch_branch function. """ # create bare repo that we can mess with and attempt an import bare_repo = os.path.abspath('{0}/{1}'.format(settings.TEST_ROOT, 'bare.git')) os.mkdir(bare_repo) self.addCleanup(shutil.rmtree, bare_repo) subprocess.check_output([ 'git', '--bare', 'init', ], stderr=subprocess.STDOUT, cwd=bare_repo) # Build repo dir repo_dir = self.git_repo_dir if not os.path.isdir(repo_dir): os.mkdir(repo_dir) self.addCleanup(shutil.rmtree, repo_dir) rdir = '{0}/bare'.format(repo_dir) with self.assertRaises(GitImportErrorBadRepo): git_import.add_repo('file://{0}'.format(bare_repo), None, None) # Get logger for checking strings in logs output = StringIO() test_log_handler = logging.StreamHandler(output) test_log_handler.setLevel(logging.DEBUG) glog = git_import.log glog.addHandler(test_log_handler) # Move remote so fetch fails shutil.move(bare_repo, '{0}/not_bare.git'.format(settings.TEST_ROOT)) try: git_import.switch_branch('master', rdir) except GitImportError: self.assertIn('Unable to fetch remote', output.getvalue()) shutil.move('{0}/not_bare.git'.format(settings.TEST_ROOT), bare_repo) output.truncate(0) # Replace origin with a different remote subprocess.check_output([ 'git', 'remote', 'rename', 'origin', 'blah', ], stderr=subprocess.STDOUT, cwd=rdir) with self.assertRaises(GitImportError): git_import.switch_branch('master', rdir) self.assertIn('Getting a list of remote branches failed', output.getvalue())
def import_mongo_course(self, gitloc, branch): """ Imports course using management command and captures logging output at debug level for display in template """ msg = u'' log.debug(u'Adding course using git repo %s', gitloc) # Grab logging output for debugging imports output = StringIO() import_log_handler = logging.StreamHandler(output) import_log_handler.setLevel(logging.DEBUG) logger_names = [ 'xmodule.modulestore.xml_importer', 'lms.djangoapps.dashboard.git_import', 'xmodule.modulestore.xml', 'xmodule.seq_module', ] loggers = [] for logger_name in logger_names: logger = logging.getLogger(logger_name) logger.setLevel(logging.DEBUG) logger.addHandler(import_log_handler) loggers.append(logger) error_msg = '' try: git_import.add_repo(gitloc, None, branch) except GitImportError as ex: error_msg = str(ex) ret = output.getvalue() # Remove handler hijacks for logger in loggers: logger.setLevel(logging.NOTSET) logger.removeHandler(import_log_handler) if error_msg: msg_header = error_msg color = 'red' else: msg_header = _('Added Course') color = 'blue' msg = HTML(u"<h4 style='color:{0}'>{1}</h4>").format(color, msg_header) msg += HTML(u"<pre>{0}</pre>").format(escape(ret)) return msg
def test_add_repo(self): """ Various exit path tests for test_add_repo """ with pytest.raises(GitImportErrorNoDir): git_import.add_repo(self.TEST_REPO, None, None) os.mkdir(self.git_repo_dir) self.addCleanup(shutil.rmtree, self.git_repo_dir) with pytest.raises(GitImportErrorUrlBad): git_import.add_repo('foo', None, None) with pytest.raises(GitImportErrorCannotPull): git_import.add_repo('file:///foobar.git', None, None) # Test git repo that exists, but is "broken" bare_repo = os.path.abspath('{}/{}'.format(settings.TEST_ROOT, 'bare.git')) os.mkdir(bare_repo) self.addCleanup(shutil.rmtree, bare_repo) subprocess.check_output([ 'git', '--bare', 'init', ], stderr=subprocess.STDOUT, cwd=bare_repo) with pytest.raises(GitImportErrorBadRepo): git_import.add_repo(f'file://{bare_repo}', None, None)
def handle(self, *args, **options): """Check inputs and run the command""" if isinstance(modulestore, XMLModuleStore): raise CommandError('This script requires a mongo module store') rdir_arg = None branch = None if options['directory_path']: rdir_arg = options['directory_path'] if options['repository_branch']: branch = options['repository_branch'] try: git_import.add_repo(options['repository_url'], rdir_arg, branch) except git_import.GitImportError as ex: raise CommandError(str(ex)) # lint-amnesty, pylint: disable=raise-missing-from
def test_detached_repo(self): """ Test repo that is in detached head state. """ repo_dir = self.git_repo_dir # Test successful import from command try: os.mkdir(repo_dir) except OSError: pass self.addCleanup(shutil.rmtree, repo_dir) git_import.add_repo(self.TEST_REPO, repo_dir / 'edx4edx_lite', None) subprocess.check_output([ 'git', 'checkout', 'HEAD~2', ], stderr=subprocess.STDOUT, cwd=repo_dir / 'edx4edx_lite') with pytest.raises(GitImportErrorCannotPull): git_import.add_repo(self.TEST_REPO, repo_dir / 'edx4edx_lite', None)
def test_branching(self): """ Exercise branching code of import """ repo_dir = self.git_repo_dir # Test successful import from command if not os.path.isdir(repo_dir): os.mkdir(repo_dir) self.addCleanup(shutil.rmtree, repo_dir) # Checkout non existent branch with pytest.raises(GitImportErrorRemoteBranchMissing): git_import.add_repo(self.TEST_REPO, repo_dir / 'edx4edx_lite', 'asdfasdfasdf') # Checkout new branch git_import.add_repo(self.TEST_REPO, repo_dir / 'edx4edx_lite', self.TEST_BRANCH) def_ms = modulestore() # Validate that it is different than master assert def_ms.get_course(self.TEST_BRANCH_COURSE) is not None # Attempt to check out the same branch again to validate branch choosing # works git_import.add_repo(self.TEST_REPO, repo_dir / 'edx4edx_lite', self.TEST_BRANCH) # Delete to test branching back to master def_ms.delete_course(self.TEST_BRANCH_COURSE, ModuleStoreEnum.UserID.test) assert def_ms.get_course(self.TEST_BRANCH_COURSE) is None git_import.add_repo(self.TEST_REPO, repo_dir / 'edx4edx_lite', 'master') assert def_ms.get_course(self.TEST_BRANCH_COURSE) is None assert def_ms.get_course(CourseKey.from_string( self.TEST_COURSE)) is not None