Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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
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
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))
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])
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())
from unittest.case import expectedFailure
from commoncode.testcase import FileBasedTesting
from conan import image
class TestDockerFormat10(FileBasedTesting):
test_data_dir = os.path.join(os.path.dirname(__file__), 'data')
def test_ImageV10(self):
test_dir = self.extract_test_tar('docker/v10_format/busybox.tgz')
image.Image(test_dir)
@expectedFailure
class TestDockerLayerOld(FileBasedTesting):
test_data_dir = os.path.join(os.path.dirname(__file__), 'data')
def test_LayerOld_sort(self):
# ordered in correct layer order, top to bottom
test_data = [
('511136ea3c5a64f264b78b5433614aec563103b4d4702f3ba7d4d2698e22c158', {'parent': None}),
('34e94e67e63a0f079d9336b3c2a52e814d138e5b3f1f614a0cfe273814ed7c0a', {'parent': '511136ea3c5a64f264b78b5433614aec563103b4d4702f3ba7d4d2698e22c158'}),
('0c752394b855e8f15d2dc1fba6f10f4386ff6c0ab6fc6a253285bcfbfdd214f5', {'parent': '34e94e67e63a0f079d9336b3c2a52e814d138e5b3f1f614a0cfe273814ed7c0a'}),
('045df3e66e28eadb9be8c9156f638a4f9cfe286a696dd0641153e0d76e3e6af1', {'parent': '0c752394b855e8f15d2dc1fba6f10f4386ff6c0ab6fc6a253285bcfbfdd214f5'}),
('c89045c0bfe8cd62c539d0cc227eaeab7f5445002b8a711c0d5f47ec7716ad51', {'parent': '045df3e66e28eadb9be8c9156f638a4f9cfe286a696dd0641153e0d76e3e6af1'}),
('766dd2d9abcf5a4cc87729e938c005b0714309659b197fca61e4fd9b775b6b7b', {'parent': 'c89045c0bfe8cd62c539d0cc227eaeab7f5445002b8a711c0d5f47ec7716ad51'}),
('0136554511a3841a6600fbfcd551c81a9fa268b73181b6b2ef708f2b0c04c1ef', {'parent': '766dd2d9abcf5a4cc87729e938c005b0714309659b197fca61e4fd9b775b6b7b'}),
('2f4f35287f9436748410cba5b5911c50063715860125c1ce7941cb5e5ed6bee3', {'parent': '0136554511a3841a6600fbfcd551c81a9fa268b73181b6b2ef708f2b0c04c1ef'}),
('c52368e26e7b720d70bb7a0846193ff1223fdc8d97d4de979a79f8b40ec3201b', {'parent': '2f4f35287f9436748410cba5b5911c50063715860125c1ce7941cb5e5ed6bee3'}),
('556b4e3b80dbc6e0586faedcfccf9aadaf6dfcd0f7a245cfb801887330992f28', {'parent': 'c52368e26e7b720d70bb7a0846193ff1223fdc8d97d4de979a79f8b40ec3201b'}),
('91908fbd9b30e4fdaffed472fa221b01373670779dbfa9644f0e13e767c4c8ec', {'parent': '556b4e3b80dbc6e0586faedcfccf9aadaf6dfcd0f7a245cfb801887330992f28'}),
from __future__ import print_function
from __future__ import unicode_literals
from os import path
import click
click.disable_unicode_literals_warning = True
from click.testing import CliRunner
from commoncode.testcase import FileBasedTesting
from scancode.cli import ScanCommand
from scancode.cli import ScanOption
from scancode.cli_test_utils import run_scan_click
class TestHelpGroups(FileBasedTesting):
test_data_dir = path.join(path.dirname(__file__), 'data')
def test_scan_help_without_custom_class(self):
@click.command(name='scan', cls=ScanCommand)
@click.option('--opt', is_flag=True, help='Help text for option')
def scan(opt):
pass
runner = CliRunner()
result = runner.invoke(scan, ['--help'])
assert 'misc:\n --opt Help text for option\n' in result.output
def test_scan_help_with_custom_class(self):
@click.command(name='scan', cls=ScanCommand)
@click.option('--opt', is_flag=True, help='Help text for option', cls=ScanOption)
from __future__ import print_function
from __future__ import unicode_literals
import os
from commoncode.testcase import FileBasedTesting
from commoncode import saneyaml
TEST_DATA_DIR = os.path.join(os.path.dirname(__file__), 'data')
import pytest
pytestmark = pytest.mark.scanpy3 # NOQA
class TestSaneyaml(FileBasedTesting):
test_data_dir = TEST_DATA_DIR
def test_load_with_and_without_tags(self):
test_file_with_tag = self.get_test_loc('saneyaml/metadata1')
test_file_without_tag = self.get_test_loc('saneyaml/metadata1.notag')
with_tags = saneyaml.load(open(test_file_with_tag, 'rb').read())
without_tags = saneyaml.load(open(test_file_without_tag, 'rb').read())
assert with_tags == without_tags
1: 4757 -1695 -644 429 -627 411 -602 368 -555 299 -470 206 -328 96 -125 -15
126 -105 391 -146 634 -120 762 -58 911 -13 1583 -8 1049 -28 1451 123 1377 -464
907 -603 -4056 -1955 -6769 -485 -5797 929 -4254 1413 -3251 1295 -2871 993
-2899 724
2: 4413 -932 -563 355 -566 354 -582 322 -597 258 -579 164 -499 45 -341 -84
-127 -192 93 -234 288 -157 190 -25 -145 65 1065 74 -1087 -40 -877 1058 -994 18
1208 694 -5540 -3840 -7658 -332 -4130 1732 -1668 1786 -634 1127 -525 501
-856 110
'''.splitlines()
for line in lines:
line, _ = copyrights_module.prep_line(line)
assert not copyrights_module.is_candidate(line)
class TestCopyrightDetector(FileBasedTesting):
test_data_dir = os.path.join(os.path.dirname(__file__), 'data')
def test_detect(self):
location = self.get_test_loc('copyrights_basic/essential_smoke-ibm_c.c')
expected = [
'Copyright IBM and others (c) 2008',
'Copyright Eclipse, IBM and others',
'(c) 2008',
]
copyrights, _, _ = cluecode_test_utils.copyright_detector(location)
assert expected == copyrights
def test_detect_with_lines(self):
location = self.get_test_loc('copyrights_basic/essential_smoke-ibm_c.c')
expected = [
('copyrights', u'Copyright IBM and others (c) 2008', 6, 6),
import json
from commoncode.testcase import FileBasedTesting
from licensedcode.spans import Span
from licensedcode import index
from licensedcode import match_seq
from licensedcode import models
from licensedcode.query import Query
from licensedcode.tracing import get_texts
TEST_DATA_DIR = os.path.join(os.path.dirname(__file__), 'data')
class IndexTesting(FileBasedTesting):
test_data_dir = TEST_DATA_DIR
def get_test_rules(self, base, subset=None):
base = self.get_test_loc(base)
test_files = sorted(os.listdir(base))
if subset:
test_files = [t for t in test_files if t in subset]
return [models.Rule(text_file=os.path.join(base, license_key), license_expression=license_key) for license_key in test_files]
class TestIndexing(IndexTesting):
def check_index_as_dict(self, idx, expected, regen=False):
as_dict = idx.to_dict()
expected = self.get_test_loc(expected)
if regen: