def test_matches_with_string(self): grep = GrepCount(None, ANY_FILE_FILTER, None, 'ism', metrics_logger=Mock()) grep.pre_files_scan('test-module') grep.wants_file('test.log') self.assertTrue(grep.on_read_line('buddhism')) self.assertTrue(grep.on_read_line('ismypizzaready?')) self.assertTrue(grep.on_read_line('kissme')) self.assertTrue(grep.on_read_line('mismatch')) self.assertEqual(3, grep.value, 'should have 3 hits')
def __init__(self, fast: bool, plugins_utility=BundledPluginsUtility(), maven_callable=MavenCallable): plugins_stop_condition = ['plugins-version', 'pluginsVersion'] collectors = [GrepCount('plugins.version.one.plugins', lambda x: x == 'atlassian-plugin.xml', 'Should not be adding new version 1 plugins', ['pluginsVersion="1"', 'plugins-version="1"'], plugins_stop_condition), FileCount('plugins.count', lambda x: x == 'atlassian-plugin.xml', 'Count of plugins in JIRA codebase.').unchecked().neutral(), GrepCountWithActivator('plugins.version.web.panels.to.soy', lambda x: x == 'atlassian-plugin.xml', 'Should migrate web panels to soy templates', r'<\s*resource.+type\s*=\s*"velocity"', start_count=r'<\s*web-panel', stop_count=r'<\s*/\s*web-panel\s*>', use_regex=True), FileCount('bundled.jars.in.plugins', lambda x: x.endswith('.jar'), 'Try and use provided jars instead of bundling your own. See ' 'https://developer.atlassian.com/display/DOCS/How+to+Speed+Up+Plugin+Startup for more ' 'details'), MissingHostComponentsXml('plugins.missing.component.import.xml', 'Should be specifying a atlassian-plugins-host-components.xml to bypass host component ' 'scanning. See https://developer.atlassian.com/display/DOCS/How+to+Speed+Up+Plugin+Startup#HowtoSpeedUpPluginStartup-Bypasshostcomponentscanning ' 'for more details').unchecked(), MissingOsgiManifest('plugins.missing.osgi.instructions', 'Should be specifying a non-empty instructions element so the SDK will generate an OSGi ' 'manifest. See https://developer.atlassian.com/display/DOCS/How+to+Speed+Up+Plugin+Startup#HowtoSpeedUpPluginStartup-GeneratetheOSGimanifestatbuildtime ' 'for more details'), # Won't check the plugin xml minified until AMPS 4.2.4 is released. PluginXmlMinified('plugins.xml.not.minified', 'Should be minifiying your atlassian-plugin.xml using plugin SDK version 4.2.4 or later').unchecked().rising()] super().__init__(collectors, 'Scan Bundled plugins JARS') self.plugins_utility = plugins_utility self.plugin_zip = None self.plugin_xml_info = None self.fast = fast self.maven_callable = maven_callable
def test_stop_condition(self): grep = GrepCount(None, ANY_FILE_FILTER, None, 'cat', stop_object='lolcat', metrics_logger=Mock()) grep.pre_files_scan('test-module') grep.wants_file('test.log') self.assertTrue(grep.on_read_line('catalogue')) self.assertFalse(grep.on_read_line('lolcat')) self.assertEqual(2, grep.value, 'should have 2 hits')
def test_grep_matches_with_list_of_strings(self): grep_count_dates = GrepCount(None, ANY_FILE_FILTER, None, ['13', '2'], metrics_logger=Mock()) grep_count_dates.pre_files_scan('test-module') grep_count_dates.wants_file('test.log') #when self.assertTrue(grep_count_dates.on_read_line('sad 1345 sad ')) self.assertTrue(grep_count_dates.on_read_line('sad 434 sad ')) self.assertTrue(grep_count_dates.on_read_line('sad 12345 sad ')) #then self.assertEqual(grep_count_dates.value, 2, 'should have two hits')
def test_grep_file_filter(self): grep = GrepCount('test', lambda x: x.startswith('want'), grep_str='', metrics_logger=Mock()).configure(Mock(), False) self.assertTrue(grep.wants_file('want-a-lot')) self.assertTrue(grep.wants_file('want!!!')) self.assertTrue(grep.wants_file('wants')) self.assertFalse(grep.wants_file('do-not-want')) self.assertFalse(grep.wants_file('how-about-no'))
def test_grep_matches_with_function(self): grep_count_dates = GrepCount(None, ANY_FILE_FILTER, None, lambda line: '45' in line, metrics_logger=Mock()) grep_count_dates.pre_files_scan('test-module') grep_count_dates.wants_file('test.log') #when self.assertTrue(grep_count_dates.on_read_line('sad 1345 sad ')) self.assertTrue(grep_count_dates.on_read_line('sad 43445 sad ')) self.assertTrue(grep_count_dates.on_read_line('sad 12335 sad ')) #then self.assertEqual(grep_count_dates.value, 2, 'should have two hits')
def test_grep_regex_matches_with_list_of_regex_expressions(self): #having grep_count_dates = GrepCount(None, ANY_FILE_FILTER, None, ['1[1-9]{3}', '2+'], use_regex=True, metrics_logger=Mock()) grep_count_dates.pre_files_scan('test-module') grep_count_dates.wants_file('test.log') #when self.assertTrue(grep_count_dates.on_read_line('sad 1345 sad ')) self.assertTrue(grep_count_dates.on_read_line('sad 2345 sad ')) self.assertTrue(grep_count_dates.on_read_line('sad 12345 sad ')) #then self.assertEqual(grep_count_dates.value, 3, 'should have three hits')
def test_grep_with_regex_matches(self): #having grep_count_dates = GrepCount(None, ANY_FILE_FILTER, None, '1[1-9]{3}', use_regex=True, metrics_logger=Mock()) grep_count_dates.pre_files_scan('test-module') grep_count_dates.wants_file('test.log') #when self.assertTrue(grep_count_dates.on_read_line('sad 1345 sad ')) #then self.assertEqual(grep_count_dates.value, 1, 'should have one hit ')
def test_grep_with_regex_not_matches(self): #having grep_count_dates = GrepCount(None, ANY_FILE_FILTER, None, '1[1-9]{3]', use_regex=True, metrics_logger=Mock()) grep_count_dates.pre_files_scan('test-module') grep_count_dates.wants_file('test.log') #when should_continue = grep_count_dates.on_read_line('2345') self.assertTrue(should_continue) self.assertEqual(grep_count_dates.value, 0, 'should not have any hits ')
def __init__(self, fast: bool, file_utils: FileUtils=FileUtils()): collectors = [JavaPackageImports('usageof.CacheBuilder', 'Imports of CacheBuilder: prevent caches other than EhCache', 'com.google.common.cache.CacheBuilder').unchecked(), JavaPackageImports('usageof.java.util.Calendar', 'Stop using java.util.Calendar - use joda time instead', 'java.util.Calendar'), ManagersInJiraApi(), FileCountAndSize('velocity', lambda x: x.endswith('.vm'), 'Get rid of velocity files').unchecked(), FileCountAndSize('jsp', lambda x: x.endswith('.jsp'), 'Get rid of jsp files').unchecked(), GrepCount('usageof.css.important', lambda x: x.endswith(('.css', '.less')), 'Get rid of css !important usages', '!important'), GrepCount('usageof.system.getproperty', lambda x: x.endswith('.java') and not x.endswith('/SystemPropertiesAccessor.java'), 'Get rid of System.getProperty family usages ' '(use JiraSystemProperties class if you must)', ['System.getProperty', 'System.getProperties', 'Boolean.getBoolean', 'System.clearProperty', 'Integer.getInteger', 'Long.getLong', 'System.setProperty', 'System.setProperties']), GrepCount('usageof.actioncontext.getrequest', lambda x: x.endswith('.java'), 'Get rid of ActionContext.getRequest usages', 'ActionContext.getRequest'), GrepCount('usageof.webwork.action.jelly.tag', lambda x: x.endswith('.java'), 'Jelly dependencies on webwork actions blocks their removal', ('extends ActionTagSupport', 'extends UserAwareActionTagSupport', 'extends IssueSchemeAwareActionTagSupport', 'extends ProjectAwareActionTagSupport', 'extends PermissionSchemeAwareActionTagSupport')), GrepCount('usageof.not.component.accessor', lambda x: x.endswith('.java'), 'Should use component accessor to get components', ['import com.atlassian.jira.ComponentManager', 'import com.atlassian.jira.ManagerFactory', 'import com.atlassian.jira.util.ComponentLocator' 'import com.atlassian.jira.plugin.ComponentClassManager'], 'class'), GrepCount('tests.qunit.core.count', lambda x: x.endswith(('-test.js', '-tests.js')), 'Keep count of Qunit tests', ['test(', 'asyncTest(']).unchecked().rising(), GrepCount('frontend.number.of.soy.templates', lambda x: x.endswith('.soy'), 'Keep count of soy templates', '{/template}').unchecked().rising(), GrepCount('frontend.number.of.ww.tags', lambda x: x.endswith('.jsp'), 'Get rid of jsp webwork tags', '<ww:').unchecked(), GrepCount('frontend.number.of.velocity.macros', lambda x: x.endswith('.vm'), 'Rewrite velocity macros to soy', lambda x: x.strip().startswith('#macro')), GrepCount('frontend.number.of.css.rules', lambda x: x.endswith(('.css', '.less')), 'Keep track of styles complexity.', '{').unchecked(), GrepCount('frontend.number.of.lcss.mixins.invocations', lambda x: x.endswith('.less'), 'Keep track of mixin complexity.', lambda line: ');' in line and ':' not in line).unchecked(), FileCount('soy', lambda x: x.endswith('.soy'), 'Keep count of soy files.').unchecked().rising(), FileCount('frontend.helpers', lambda x: any(path in x for path in ['/webapp/template/aui/', 'webapp/template/standard', 'webapp/ui/aui-layout']), 'Number of files with JIRA exceptions in AUI').unchecked(), JavaPackageImports('usageof.generic.value.webwork.action', 'Should not be using GenericValue in webwork actions', 'org.ofbiz.core.entity.GenericValue', 'web.action'), JavaPackageImports('usageof.property.set.webwork.action', 'Should not be using Property Set in webwork actions', 'com.opensymphony.module.propertyset.PropertySet', 'web.action'), JavaPackageImports('usageof.crowd.user', 'use UserRename user', 'com.atlassian.crowd.embedded.api.User').unchecked(), JavaPackageImports('usageof.event.publisher.webwork.action', 'Should not be using Event Publisher in webwork actions', 'com.atlassian.event.api.EventPublisher', 'web.action'), JavaPackageImports('usageof.issue.event.dispatcher.webwork.action', 'Should not be using IssueEventDispatcher in webwork actions', 'com.atlassian.jira.event.issue.IssueEventDispatcher', 'web.action'), JavaPackageImports('usageof.issue.event.manager.webwork.action', 'Should not be using IssueEventManager in webwork actions', 'com.atlassian.jira.event.issue.IssueEventManager', 'web.action'), JavaPackageImports('usageof.ofbiz.delegator.interface.webwork.action', 'Should not be calling straight to Ofbiz in webwork actions ' '(DelegatorInterface)', 'org.ofbiz.core.entity.DelegatorInterface', 'web.action'), JavaPackageImports('usageof.ofbiz.delegator.webwork.action', 'Should not be calling straight to Ofbiz in webwork actions (OfBizDelegator)', 'com.atlassian.jira.ofbiz.OfBizDelegator', 'web.action'), JavaPackageImports('usageof.entity.engine.webwork.action', 'Should not be calling straight to Ofbiz in webwork actions (EntityEngine)', 'com.atlassian.jira.entity.EntityEngine', 'web.action'), JavaPackageImports('usageof.managers.webwork.action', 'Should not be using Managers in webwork actions', 'Manager', 'web.action', whitelist = ['.FeatureManager']), ModulesDescription.commonJUnitFinder, InvocationOfSoyTemplates().unchecked().rising(), InvocationOfVelocityMacros().unchecked()] + ([] if fast else [DeprecatedMethodUsage().unchecked()]) super().__init__(collectors, 'Scan JIRA source files') self.file_utils = file_utils