def testIsGitRepo(self): loc = os.path.dirname(__file__) self.assertTrue(mooseutils.is_git_repo(loc)) loc = tempfile.mkdtemp() self.assertFalse(mooseutils.is_git_repo(loc)) os.rmdir(loc)
class Test(unittest.TestCase): def testIsGitRepo(self): loc = os.path.dirname(__file__) self.assertTrue(mooseutils.is_git_repo(loc)) loc = tempfile.mkdtemp() self.assertFalse(mooseutils.is_git_repo(loc)) os.rmdir(loc) @unittest.skipIf(not mooseutils.is_git_repo(), "Not a Git repository") def testGitCommit(self): c = mooseutils.git_commit() self.assertEqual(len(c), 40) @unittest.skipIf(not mooseutils.is_git_repo(), "Not a Git repository") def testGitCommitMessage(self): c = 'b863a496dbf1449853be6978c8ac1a9c242d389b' # beautiful commit msg = mooseutils.git_commit_message(c) self.assertIn('The name is, just so long', msg) @unittest.skipIf(not mooseutils.is_git_repo(), "Not a Git repository") def testGitMergeCommits(self): merges = mooseutils.git_merge_commits() self.assertEqual(len(merges[0]), 40) @unittest.skipIf(not mooseutils.is_git_repo(), "Not a Git repository") def testGitLsFiles(self): files = mooseutils.git_ls_files() self.assertIn(os.path.abspath(__file__), files) @unittest.skipIf(not mooseutils.is_git_repo(), "Not a Git repository") def testGitRootDir(self): root = mooseutils.git_root_dir() self.assertEqual( root, os.path.abspath( os.path.join(os.path.dirname(__file__), '..', '..', '..'))) @unittest.skipIf(not mooseutils.is_git_repo(), "Not a Git repository") def testGitSubmoduleStatus(self): root = mooseutils.git_root_dir() status = mooseutils.git_submodule_status(root) self.assertIn('large_media', status) self.assertIn('libmesh', status) self.assertIn('petsc', status) @mock.patch('subprocess.call') @mock.patch('mooseutils.gitutils.git_submodule_status') @unittest.skipIf(not mooseutils.is_git_repo(), "Not a Git repository") def testGitInitSubmodule(self, status_func, call_func): status_func.return_value = {'test': '-'} root = mooseutils.git_root_dir() mooseutils.git_init_submodule('test', root) status_func.assert_called_with(root) call_func.assert_called_with( ['git', 'submodule', 'update', '--init', 'test'], cwd=root)
def createToken(self, parent, info, page): content = info['inline'] if 'inline' in info else info['block'] if content: raise exceptions.MooseDocsException("Content is not supported for the 'git commit' command.") if not mooseutils.is_git_repo(): raise exceptions.MooseDocsException("The current working directory is not a git repository.") core.Word(parent, content=mooseutils.git_commit()) return parent
def execute(self, **kwargs): """Determine the status""" file_list = list() for working_dir in self.working_dirs: path = mooseutils.eval_path(working_dir) if mooseutils.is_git_repo(path): file_list += mooseutils.git_ls_files(path) else: file_list += glob.glob(os.path.join(path, '**', '*.*'), recursive=True) logger = check_documents(self.documents, file_list, **kwargs) return logger
def get_content(items, in_ext): """ Create a tree of files for processing. Inputs: items: [list[dict(),...] A list of dict items, each dict entry must contain the 'root_dir' and 'content' fields that are passed to the doc_import function. in_ext[tuple]: Set of extensions to be converted (e.g., ('.md', )). out_ext[str]: The extension of rendered result (e.g., '.html'). """ if not isinstance(items, list) or any(not isinstance(x, dict) for x in items): LOG.error( 'The supplied items must be a list of dict items, each with a "root_dir" and ' 'optionally a "content" entry.') return None roots = set() nodes = dict() for root, filename, external in get_files(items, in_ext): roots.add(root) key = filename.replace(root, '').strip('/') parts = key.split('/') # Create directory nodes if they don't exist for i in range(1, len(parts)): dir_key = os.path.join(*parts[:i]) if dir_key not in nodes: nodes[dir_key] = pages.Directory(dir_key, external=external, source=os.path.join( root, dir_key)) # Create the file node, if it doesn't already exist. This enforces that the first # item in the supplied content lists is the page that is rendered. if key not in nodes: nodes[key] = create_file_page(key, filename, in_ext) nodes[key].external = external # Update the project files for root in roots: if mooseutils.is_git_repo(root): MooseDocs.PROJECT_FILES.update( mooseutils.git_ls_files(mooseutils.git_root_dir(root))) else: MooseDocs.PROJECT_FILES.update(mooseutils.list_files(root)) return list(nodes.values())
def get_content(items, in_ext): """ Create a tree of files for processing. Inputs: items: [list[dict(),...] A list of dict items, each dict entry must contain the 'root_dir' and 'content' fields that are passed to the doc_import function. in_ext[tuple]: Set of extensions to be converted (e.g., ('.md', )). out_ext[str]: The extension of rendered result (e.g., '.html'). """ if not isinstance(items, list) or any(not isinstance(x, dict) for x in items): LOG.error('The supplied items must be a list of dict items, each with a "root_dir" and ' 'optionally a "content" entry.') return None roots = set() nodes = dict() for root, filename in get_files(items, in_ext): roots.add(root) key = filename.replace(root, '').strip('/') parts = key.split('/') # Create directory nodes if they don't exist for i in range(1, len(parts)): dir_key = os.path.join(*parts[:i]) if dir_key not in nodes: nodes[dir_key] = MooseDocs.tree.pages.Directory(dir_key, source=os.path.join(root, dir_key)) # Create the file node, if it doesn't already exist. This enforces that the first # item in the supplied content lists is the page that is rendered. if key not in nodes: nodes[key] = create_file_page(key, filename, in_ext) # Update the project files for root in roots: if mooseutils.is_git_repo(root): MooseDocs.PROJECT_FILES.update(mooseutils.git_ls_files(mooseutils.git_root_dir(root))) else: MooseDocs.PROJECT_FILES.update(mooseutils.list_files(root)) return nodes.values()
def check_documents(documents, file_list=None, **kwargs): """ Tool for checking SQA document deficiencies """ # Setup logger, assume the names of the documents with a "log_" prefix are the logging flags (see get_documents) for doc in documents: kwargs.setdefault("log_" + doc.name, logging.ERROR) logger = LogHelper(__name__, **kwargs) # Setup file_list, if not provided if (file_list is None) and (not mooseutils.is_git_repo()): msg = "If the 'file_list' is not provided then the working directory must be a git repository." raise ValueError(msg) elif file_list is None: root = mooseutils.git_root_dir() file_list = mooseutils.git_ls_files(root, recurse_submodules=False) # Perform document checks for doc in documents: _check_document(doc.name, doc.filename, file_list, logger) return logger
import os import sys import subprocess import mooseutils try: import hit except ImportError: # If the repository is not a git repository, then give up otherwise try to figure out what to do if not mooseutils.is_git_repo(os.path.dirname(__file__)): raise moose_dir = mooseutils.git_root_dir(os.path.dirname(__file__)) status = mooseutils.git_submodule_status(moose_dir) # Use framework/contrib/hit because moosetools submodule is not available if status['moosetools'] == '-': hit_dir = os.path.join(moose_dir, 'framework', 'contrib', 'hit') sys.path.append(hit_dir) try: import hit except ImportError: moose_test_dir = os.path.abspath(os.path.join(moose_dir, 'test')) subprocess.run(['make', 'hit'], cwd=moose_test_dir) import hit # Use hit in moosetools submodule else: hit_dir = os.path.join(moose_dir, 'moosetools', 'contrib', 'hit') sys.path.append(hit_dir)
class Test(unittest.TestCase): def testIsGitRepo(self): loc = os.path.dirname(__file__) self.assertTrue(mooseutils.is_git_repo(loc)) loc = tempfile.mkdtemp() self.assertFalse(mooseutils.is_git_repo(loc)) os.rmdir(loc) @unittest.skipIf(not mooseutils.is_git_repo(), "Not a Git repository") def testGitCommit(self): c = mooseutils.git_commit() self.assertEqual(len(c), 40) @unittest.skipIf(not mooseutils.is_git_repo(), "Not a Git repository") def testGitCommitMessage(self): c = 'b863a496dbf1449853be6978c8ac1a9c242d389b' # beautiful commit msg = mooseutils.git_commit_message(c) self.assertIn('The name is, just so long', msg) @unittest.skipIf(not mooseutils.is_git_repo(), "Not a Git repository") def testGitMergeCommits(self): merges = mooseutils.git_merge_commits() self.assertEqual(len(merges[0]), 40) @unittest.skipIf(not mooseutils.is_git_repo(), "Not a Git repository") def testGitLsFiles(self): files = mooseutils.git_ls_files() self.assertIn(os.path.abspath(__file__), files) @unittest.skipIf(not mooseutils.is_git_repo(), "Not a Git repository") def testGitRootDir(self): root = mooseutils.git_root_dir() self.assertEqual( root, os.path.abspath( os.path.join(os.path.dirname(__file__), '..', '..', '..'))) @unittest.skipIf(not mooseutils.is_git_repo(), "Not a Git repository") def testGitSubmoduleStatus(self): root = mooseutils.git_root_dir() status = mooseutils.git_submodule_status(root) self.assertIn('large_media', status) self.assertIn('libmesh', status) self.assertIn('petsc', status) @mock.patch('subprocess.call') @mock.patch('mooseutils.gitutils.git_submodule_status') @unittest.skipIf(not mooseutils.is_git_repo(), "Not a Git repository") def testGitInitSubmodule(self, status_func, call_func): status_func.return_value = {'test': '-'} root = mooseutils.git_root_dir() mooseutils.git_init_submodule('test', root) status_func.assert_called_with(root) call_func.assert_called_with( ['git', 'submodule', 'update', '--init', 'test'], cwd=root) def testGitVersion(self): ver = mooseutils.git_version() self.assertEqual(len(ver), 3) self.assertIsInstance(ver[0], int) self.assertIsInstance(ver[1], int) self.assertIsInstance(ver[2], int) @mock.patch('re.search') def testGitVersion2(self, re_func): re_func.return_value = None with self.assertRaises(SystemError): ver = mooseutils.git_version() @unittest.skip("Fails on CIVET and I can't reproduce it") def testGitAuthors(self): names = mooseutils.git_authors(mooseutils.__file__) self.assertIn('Andrew E. Slaughter', names) with self.assertRaises(OSError) as e: mooseutils.git_authors('wrong') @unittest.skip("Fails on CIVET and I can't reproduce it") def testCommitters(self): names = mooseutils.git_committers(mooseutils.__file__) self.assertIn('Andrew E. Slaughter', names) with self.assertRaises(OSError) as e: mooseutils.git_authors('wrong') names = mooseutils.git_committers(mooseutils.__file__, '--merges') self.assertIn('Logan Harbour', names) def testGitLines(self): with open(__file__, 'r') as fid: lines = fid.readlines() n_with_blank = len(lines) n_no_blank = n_with_blank - len([l for l in lines if not l.strip()]) counts = mooseutils.git_lines(__file__) self.assertIn('Andrew E. Slaughter', counts) self.assertTrue(counts['Andrew E. Slaughter'] > 0) self.assertEqual(n_no_blank, sum(list(counts.values()))) counts = mooseutils.git_lines(__file__, blank=True) self.assertIn('Andrew E. Slaughter', counts) self.assertTrue(counts['Andrew E. Slaughter'] > 0) self.assertEqual(n_with_blank, sum(list(counts.values()))) def testGitLocalPath(self): filename = os.path.abspath(__file__) local = mooseutils.git_localpath(filename) self.assertEqual(local, 'python/mooseutils/tests/test_gitutils.py') @mock.patch('subprocess.run') def testGitRepo(self, mock_out): mock_out.return_value.stdout = 'origin [email protected]:aeslaughter/moose.git (fetch)\n' \ 'origin [email protected]:aeslaughter/moose.git (push)\n' \ 'upstream [email protected]:idaholab/moose.git (fetch)' \ 'upstream [email protected]:idaholab/moose.git (push)' url = mooseutils.git_repo(os.path.dirname(__file__)) self.assertEqual(url, 'https://github.com/idaholab/moose') url = mooseutils.git_repo(os.path.dirname(__file__), remotes=['origin']) self.assertEqual(url, 'https://github.com/aeslaughter/moose') with self.assertRaises(OSError) as e: mooseutils.git_repo('wrong') self.assertEqual(str(e.exception), "The supplied location must be a directory: wrong") with self.assertRaises(OSError) as e: mooseutils.git_repo(os.path.dirname(__file__), remotes=['wrong']) self.assertEqual(str(e.exception), "Unable to locate a remote with the name(s): wrong")
def check_requirements(requirements, file_list=None, color_text=True, allowed_collections=None, **kwargs): """ Tool for checking Requirement for deficiencies """ # Create key values for the logging messages log_default = kwargs.get('log_default', logging.ERROR) kwargs.setdefault('log_deprecated_requirement', log_default) kwargs.setdefault('log_deprecated_design', log_default) kwargs.setdefault('log_deprecated_issues', log_default) kwargs.setdefault('log_deprecated_detail', log_default) kwargs.setdefault('log_deprecated_verification', log_default) kwargs.setdefault('log_deprecated_validation', log_default) kwargs.setdefault('log_deprecated_with_details', log_default) kwargs.setdefault('log_missing', log_default) kwargs.setdefault('log_missing_requirement', log_default) kwargs.setdefault('log_missing_design', log_default) kwargs.setdefault('log_missing_issues', log_default) kwargs.setdefault('log_empty_requirement', log_default) kwargs.setdefault('log_empty_design', log_default) kwargs.setdefault('log_empty_issues', log_default) kwargs.setdefault('log_empty_verification', log_default) kwargs.setdefault('log_empty_validation', log_default) kwargs.setdefault('log_top_level_detail', log_default) kwargs.setdefault('log_missing_detail', log_default) kwargs.setdefault('log_empty_detail', log_default) kwargs.setdefault('log_deprecated_detail', log_default) kwargs.setdefault('log_extra_requirement', log_default) kwargs.setdefault('log_extra_design', log_default) kwargs.setdefault('log_extra_issues', log_default) kwargs.setdefault('log_extra_collections', log_default) kwargs.setdefault('log_invalid_collection', log_default) kwargs.setdefault('log_issue_format', log_default) kwargs.setdefault('log_design_files', log_default) kwargs.setdefault('log_validation_files', log_default) kwargs.setdefault('log_verification_files', log_default) kwargs.setdefault('log_testable', log_default) kwargs.setdefault('log_duplicate_requirement', log_default) kwargs.setdefault('log_duplicate_detail', log_default) logger = RequirementLogHelper(__name__, **kwargs) RequirementLogHelper.COLOR_TEXT = color_text # Setup file_list, if not provided if (file_list is None) and (not mooseutils.is_git_repo()): msg = "If the 'file_list' is not provided then the working directory must be a git repository." raise ValueError(msg) elif file_list is None: root = mooseutils.git_root_dir() ver = mooseutils.git_version() file_list = mooseutils.git_ls_files(root, recurse_submodules=True) # Setup allowed collections if allowed_collections is None: allowed_collections = set(moosesqa.MOOSESQA_COLLECTIONS.keys()) # Storage container for duplicate detection requirement_dict = collections.defaultdict(set) # Check each Requirement object for deficiencies for req in requirements: _check_requirement(req, logger, file_list, allowed_collections) if req.requirement is not None: key = [req.requirement] for detail in req.details: if detail.detail is not None: key.append(detail.detail) requirement_dict['\n'.join(key)].add(req) # Duplicate checking for txt, value in requirement_dict.items(): if len(value) > 1: msg = 'Duplicate requirements found:' msg += '\n{}\n'.format(mooseutils.colorText(txt, 'GREY', colored=color_text)) for r in value: r.duplicate = True msg += RequirementLogHelper._colorTestInfo(r, None, None, None) LogHelper.log(logger, 'log_duplicate_requirement', msg.strip('\n')) return logger
class Test(unittest.TestCase): def testIsGitRepo(self): loc = os.path.dirname(__file__) self.assertTrue(mooseutils.is_git_repo(loc)) loc = tempfile.mkdtemp() self.assertFalse(mooseutils.is_git_repo(loc)) os.rmdir(loc) @unittest.skipIf(not mooseutils.is_git_repo(), "Not a Git repository") def testGitCommit(self): c = mooseutils.git_commit() self.assertEqual(len(c), 40) @unittest.skipIf(not mooseutils.is_git_repo(), "Not a Git repository") def testGitCommitMessage(self): c = 'b863a496dbf1449853be6978c8ac1a9c242d389b' # beautiful commit msg = mooseutils.git_commit_message(c) self.assertIn('The name is, just so long', msg) @unittest.skipIf(not mooseutils.is_git_repo(), "Not a Git repository") def testGitMergeCommits(self): merges = mooseutils.git_merge_commits() self.assertEqual(len(merges[0]), 40) @unittest.skipIf(not mooseutils.is_git_repo(), "Not a Git repository") def testGitLsFiles(self): files = mooseutils.git_ls_files() self.assertIn(os.path.abspath(__file__), files) @unittest.skipIf(not mooseutils.is_git_repo(), "Not a Git repository") def testGitRootDir(self): root = mooseutils.git_root_dir() self.assertEqual( root, os.path.abspath( os.path.join(os.path.dirname(__file__), '..', '..', '..'))) @unittest.skipIf(not mooseutils.is_git_repo(), "Not a Git repository") def testGitSubmoduleStatus(self): root = mooseutils.git_root_dir() status = mooseutils.git_submodule_status(root) self.assertIn('large_media', status) self.assertIn('libmesh', status) self.assertIn('petsc', status) @mock.patch('subprocess.call') @mock.patch('mooseutils.gitutils.git_submodule_status') @unittest.skipIf(not mooseutils.is_git_repo(), "Not a Git repository") def testGitInitSubmodule(self, status_func, call_func): status_func.return_value = {'test': '-'} root = mooseutils.git_root_dir() mooseutils.git_init_submodule('test', root) status_func.assert_called_with(root) call_func.assert_called_with( ['git', 'submodule', 'update', '--init', 'test'], cwd=root) def testGitVersion(self): ver = mooseutils.git_version() self.assertEqual(len(ver), 3) self.assertIsInstance(ver[0], int) self.assertIsInstance(ver[1], int) self.assertIsInstance(ver[2], int) @mock.patch('re.search') def testGitVersion2(self, re_func): re_func.return_value = None with self.assertRaises(SystemError): ver = mooseutils.git_version() @unittest.skip("Fails on CIVET and I can't reproduce it") def testGitAuthors(self): names = mooseutils.git_authors(mooseutils.__file__) self.assertIn('Andrew E. Slaughter', names) with self.assertRaises(OSError) as e: mooseutils.git_authors('wrong') def testGitLines(self): with open(__file__, 'r') as fid: lines = fid.readlines() n_with_blank = len(lines) n_no_blank = n_with_blank - len([l for l in lines if not l.strip()]) counts = mooseutils.git_lines(__file__) self.assertIn('Andrew E. Slaughter', counts) self.assertTrue(counts['Andrew E. Slaughter'] > 0) self.assertEqual(n_no_blank, sum(list(counts.values()))) counts = mooseutils.git_lines(__file__, blank=True) self.assertIn('Andrew E. Slaughter', counts) self.assertTrue(counts['Andrew E. Slaughter'] > 0) self.assertEqual(n_with_blank, sum(list(counts.values())))
import sys import subprocess import logging if sys.version_info < (3, 6): print('"MOOSEDocs" requires python version 3.6 or greater, version {}.{} is being used.' \ .format(sys.version_info[0], sys.version_info[1])) sys.exit(1) import mooseutils # Current logging level, used to allow for debug only type checking, etc. LOG_LEVEL = logging.NOTSET # The repository root location is_git_repo = mooseutils.is_git_repo() if is_git_repo: os.environ.setdefault('ROOT_DIR', mooseutils.git_root_dir()) elif 'ROOT_DIR' not in os.environ: print( 'MooseDocs requires the ROOT_DIR environment variable to set when operating outside of a git repository.' ) sys.exit(1) ROOT_DIR = os.environ['ROOT_DIR'] # Setup MOOSE_DIR/ROOT_DIR MOOSE_DIR = os.getenv('MOOSE_DIR', None) if MOOSE_DIR is None: print( "The MOOSE_DIR environment must be set, this should be set within moosedocs.py."