How to use commoncode - 10 common examples

To help you get started, we’ve selected a few commoncode examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github nexB / scancode-toolkit / tests / cluecode / test_finder.py View on Github external
def test_find_urls_does_not_crash_on_mojibake_bytes(self):
        lines = [
            '    // as defined in https://tools.ietf.org/html/rfc2821#section-4.1.2”.',
        ]
        expected = ['https://tools.ietf.org/html/rfc2821#section-4.1.2']
        result = find_urls_tester(lines)
        assert expected == result

    def test_find_in_go_does_not_crash_with_unicode_error(self):
        test_file = self.get_test_loc('finder/url/verify.go')
        patterns = [('urls', urls_regex(),)]
        for _key, url, _line, _lineno in find(test_file, patterns):
            assert type(url) == compat.unicode


class TestSearch(FileBasedTesting):
    test_data_dir = os.path.join(os.path.dirname(__file__), 'data')

    def test_search_is_non_unique_by_default(self):
        test_dir = self.get_test_loc('finder/search', copy=True)
        pattern = 'Copyright'
        tests = [
            (u'addr.c', [u'Copyright', u'Copyright']),
            (u'CommandLine.java', [u'Copyright', u'Copyright']),
            (u'CustomFileFilter.java', [u'Copyright', u'Copyright']),
            (u'diskio.c', [u'copyright', u'copyright', u'copyright', u'Copyright']),
            (u'getopt_long.c', [u'Copyright']),
        ]
        for test_file, expected in tests:
            location = os.path.join(test_dir, test_file)
            result = list(s for s, _ln in finder.find_pattern(location, pattern))
            assert expected == result
github nexB / scancode-toolkit / tests / packagedcode / test_rubygems.py View on Github external
from commoncode import compat
from commoncode.system import py2
from commoncode.system import py3
from commoncode import text
from commoncode.testcase import FileBasedTesting
from packagedcode import rubygems


# TODO: Add test with https://rubygems.org/gems/pbox2d/versions/1.0.3-java
# this is a multiple personality package (Java  and Ruby)
# see also https://rubygems.org/downloads/jaro_winkler-1.5.1-java.gem

# NOTE: this needs to be implemented first
@expectedFailure
class TestRubyGemspec(FileBasedTesting):
    test_data_dir = os.path.join(os.path.dirname(__file__), 'data')

    def check_gemspec(self, test_loc, expected_loc, regen=False):
        test_loc = self.get_test_loc(test_loc)
        expected_loc = self.get_test_loc(expected_loc)
        results = rubygems.get_gemspec_data(test_loc)

        try:
            # fix absolute paths for testing
            rel_path = results['loaded_from']
            rel_path = [p for p in rel_path.split('/') if p]
            rel_path = '/'.join(rel_path[-2:])
            results['loaded_from'] = rel_path
        except:
            pass
github nexB / scancode-toolkit / tests / licensedcode / test_performance.py View on Github external
class TestIndexingPerformance(FileBasedTesting):
    test_data_dir = TEST_DATA_DIR

    @skip('Use only for local profiling')
    def test_build_index_performance_profiling(self):
        import cProfile as profile
        import pstats
        stats = 'build_index_performance_profile_log.txt'
        test_py = 'cache.get_index()'
        profile.runctx(test_py, globals(), locals(), stats)
        p = pstats.Stats(stats)
        p.sort_stats('time').print_stats(40)


class TestTokenizingPerformance(FileBasedTesting):
    test_data_dir = TEST_DATA_DIR

    @skip('Use only for local profiling')
    def test_get_all_rules_performance_timing(self):
        from timeit import timeit
        print()
        print('With Object or namedtuple')
        print(timeit(stmt='from licensedcode.models import get_all_rules;get_all_rules()', number=10))
github nexB / scancode-toolkit / tests / licensedcode / test_detection_validate.py View on Github external
def make_validation_test(rule, test_name):
    """
    Build and return a test function closing on tests arguments.
    """
    if py2 and isinstance(test_name, compat.unicode):
        test_name = test_name.encode('utf-8')
    if py3 and isinstance(test_name, bytes):
        test_name = test_name.decode('utf-8')

    if rule.is_negative or rule.is_false_positive:
        def closure_test_function(*args, **kwargs):
            check_special_rule_can_be_detected(rule)
    else:
        def closure_test_function(*args, **kwargs):
            check_rule_or_license_can_be_self_detected_exactly(rule)
            check_ignorable_clues(rule)

    closure_test_function.__name__ = test_name
    closure_test_function.funcname = test_name

    return closure_test_function
github nexB / scancode-toolkit / tests / commoncode / test_ignore.py View on Github external
def test_fileset_is_included_with_default_ignore_does_not_skip_one_char_names(self):
        # use fileset directly to work on strings not locations
        from commoncode import fileset
        tests = [c for c in 'HFS+ Private Data'] + 'HFS+ Private Data'.split()
        result = [(t,
            fileset.is_included(t, excludes=ignore.default_ignores, includes={}))
            for t in tests]
        expected = [
            ('H', True),
            ('F', True),
            ('S', True),
            ('+', True),
            (' ', False),
            ('P', True),
            ('r', True),
            ('i', True),
            ('v', True),
            ('a', True),
            ('t', True),
            ('e', True),
            (' ', False),
            ('D', True),
github nexB / conan / tests / test_rootfs.py View on Github external
def test_rebuild_rootfs_simple(self):
        test_dir = self.extract_test_tar('rootfs/hello-world.tar')
        img = list(image.Image.get_images_from_dir(test_dir))[0]
        target_dir = self.get_temp_dir()
        rebuild_rootfs(img, target_dir)
        results = sorted([p.replace(target_dir, '')
            for p in fileutils.resource_iter(target_dir)])
        expected = ['/hello']
        assert expected == results
github nexB / scancode-toolkit / tests / commoncode / test_fileutils.py View on Github external
def test_file_base_name_on_path_and_location_9(self):
        test_dir = self.get_test_loc('fileutils/basename')
        test_file = 'f.a/'
        expected_name = 'f.a'
        result = fileutils.file_base_name(test_file)
        assert expected_name == result
        result = fileutils.file_base_name((os.path.join(test_dir, test_file)))
        assert expected_name == result
github nexB / scancode-toolkit / tests / commoncode / test_ignore.py View on Github external
not_ignored = []
            for d in dirs:
                p = os.path.join(top, d)
                ign = ignore.is_ignored(p, ignore.default_ignores, {})
                tp = fileutils.as_posixpath(p.replace(test_dir, ''))
                result.append((tp, ign,))
                if not ign:
                    not_ignored.append(d)

            # skip ignored things
            dirs[:] = not_ignored

            for f in files:
                p = os.path.join(top, f)
                ign = ignore.is_ignored(p, ignore.default_ignores, {})
                tp = fileutils.as_posixpath(p.replace(test_dir, ''))
                result.append((tp, ign,))

        expected = [
            ('/vcs', False),
            ('/vcs/.bzr', True),
            ('/vcs/.git', True),
            ('/vcs/.hg', True),
            ('/vcs/.repo', True),
            ('/vcs/.svn', True),
            ('/vcs/CVS', True),
            ('/vcs/_darcs', True),
            ('/vcs/_MTN', True),
            ('/vcs/.bzrignore', True),
            ('/vcs/.cvsignore', True),
            ('/vcs/.gitignore', True),
            ('/vcs/.hgignore', True),
github nexB / scancode-toolkit / tests / licensedcode / test_match_chunk.py View on Github external
u'permission please contact': [19],
             u'redistribution materials provided': [4],
             u'software is provided': [53],
             u'used to endorse': [12]},
            {u'derived from this software': [24],
             u'must not be used': [9],
             u'written permission please contact': [18]},
            {u'implied in no event shall': [63],
             u'products derived from this software': [23],
             u'software may not be called': [27],
             u'used to endorse or promote': [12]}
        ]
        assert expected == self.index_multigrams_as_str(result, idx)


class TestMatchChunk(FileBasedTesting):
    test_data_dir = TEST_DATA_DIR

    def test_match_template_with_few_tokens_around_gaps_is_wholly_chunk_matched(self):
        # was failing when a gapped token (from a template) starts at a
        # beginning of an index doc. We may still skip that, but capture a large match anyway.

        rule_text = u'''
            Copyright {{some copyright}} 
            THIS IS FROM {{THE CODEHAUS}} AND CONTRIBUTORS
            IN NO EVENT SHALL {{THE CODEHAUS}} OR ITS CONTRIBUTORS BE LIABLE
            EVEN IF ADVISED OF THE {{POSSIBILITY OF SUCH}} DAMAGE
        '''

        rule = Rule(_text=rule_text, licenses=['test'],)
        idx = index.LicenseIndex([rule])
github nexB / deltacode / tests / test_utils.py View on Github external
import deltacode
from deltacode import utils
from deltacode import models


unique_categories = set([
    'Commercial',
    'Copyleft',
    'Copyleft Limited',
    'Free Restricted',
    'Patent License',
    'Proprietary Free'
])


class TestUtils(FileBasedTesting):

    test_data_dir = os.path.join(os.path.dirname(__file__), 'data')

    def test_update_from_license_info_empty(self):
        test_delta = deltacode.Delta()

        utils.update_from_license_info(test_delta, set())

        assert test_delta.score == 0

    def test_update_from_license_info_non_modified(self):
        test_file = models.File({'path':'/test/path.txt', 'name': 'path.txt'})
        test_delta = deltacode.Delta(old_file=test_file)

        utils.update_from_license_info(test_delta, set())