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_scan_info_returns_correct_full_root_with_single_file():
test_file = test_env.get_test_loc('info/basic.tgz')
result_file = test_env.get_temp_file('json')
result = run_scan_click(['--info', '--full-root', test_file, result_file])
assert result.exit_code == 0
assert 'Scanning done' in result.output
result_data = json.loads(open(result_file, 'rb').read())
files = result_data['files']
# we have a single file
assert len(files) == 1
scanned_file = files[0]
# and we check that the path is the full path without repeating the file name
assert fileutils.as_posixpath(test_file) == scanned_file['path']
scancode_root_abs = abspath(scancode_root)
test_src_dir = tempfile.mkdtemp(dir=scancode_tmp).replace(scancode_root_abs, '').strip('\\/')
test_file = test_env.get_test_loc('extract_relative_path/basic.zip')
shutil.copy(test_file, test_src_dir)
test_src_file = join(test_src_dir, 'basic.zip')
test_tgt_dir = join(scancode_root, test_src_file) + extractcode.EXTRACT_SUFFIX
runner = CliRunner()
monkeypatch.setattr(click._termui_impl, 'isatty', lambda _: True)
result = runner.invoke(extract_cli.extractcode, [test_src_file])
assert result.exit_code == 0
assert 'Extracting done' in result.output
assert not 'WARNING' in result.output
assert not 'ERROR' in result.output
expected = ['/c/a/a.txt', '/c/b/a.txt', '/c/c/a.txt']
file_result = [as_posixpath(f.replace(test_tgt_dir, '')) for f in fileutils.resource_iter(test_tgt_dir, with_dirs=False)]
assert sorted(expected) == sorted(file_result)
finally:
fileutils.delete(test_src_dir)
def test_parent_directory_on_path_and_location_7(self):
test_dir = self.get_test_loc('fileutils/basename')
test_file = 'a/'
expected_name = '/'
result = fileutils.parent_directory(test_file)
result = fileutils.as_posixpath(result)
assert expected_name == result
result = fileutils.parent_directory((os.path.join(test_dir, test_file)))
result = fileutils.as_posixpath(result)
assert result.endswith(expected_name)
def collect_extracted_path(self, test_dir):
result = []
td = fileutils.as_posixpath(test_dir)
for t, dirs, files in os.walk(test_dir):
t = fileutils.as_posixpath(t)
for d in dirs:
nd = posixpath.join(t, d).replace(td, '') + '/'
result.append(nd)
for f in files:
nf = posixpath.join(t, f).replace(td, '')
result.append(nf)
result = sorted(result)
return result
def test_extractcode_command_can_extract_shallow(monkeypatch):
test_dir = test_env.get_test_loc('extract_shallow', copy=True)
monkeypatch.setattr(click._termui_impl, 'isatty', lambda _: True)
runner = CliRunner()
result = runner.invoke(extract_cli.extractcode, ['--shallow', test_dir])
assert result.exit_code == 0
file_result = [f for f in map(as_posixpath, resource_iter(test_dir, with_dirs=False)) if not f.endswith('unicodepath.tgz')]
file_result = [''.join(f.partition('/top.zip-extract/')[1:]) for f in file_result]
file_result = [f for f in file_result if f]
# this checks that the zip in top.zip are not extracted
expected = [
'/top.zip-extract/some3.zip',
'/top.zip-extract/some2.zip',
'/top.zip-extract/some1.zip',
]
assert sorted(expected) == sorted(file_result)
def patch_info(location):
"""
Yield an iterable of tuples of (src_path, target_path, patch_text)
for each patch segment of a patch file at location.
Raise an exception if the file is not a patch file or cannot be parsed.
"""
patchset = pythonpatch.fromfile(location)
if not patchset:
msg = 'Unable to parse patch file: %(location)s' % locals()
raise ExtractErrorFailedToExtract(msg)
for ptch in patchset.items:
src = fileutils.as_posixpath(ptch.source.strip())
tgt = fileutils.as_posixpath(ptch.target.strip())
text = [l.strip() for l in patch_text(ptch) if l]
yield src, tgt, text
def patch_text(ptch):
"""
Return the patch text content as an iterable of lines given a ptch 'patch
item'.
The content is re-formatted as unified diff.
"""
for head in ptch.header:
yield head
yield '--- ' + fileutils.as_posixpath(ptch.source)
yield '+++ ' + fileutils.as_posixpath(ptch.target)
hk = '@@ -%(startsrc)d,%(linessrc)d +%(starttgt)d,%(linestgt)d @@ %(desc)s'
def hunk_data(hnk):
return dict(
startsrc=hnk.startsrc,
linessrc=hnk.linessrc,
starttgt=hnk.starttgt,
linestgt=hnk.linestgt,
desc=text.as_unicode(hnk.desc),
)
for hunk in ptch.hunks:
yield hk % hunk_data(hunk)
for line in hunk.text:
yield line
def patch_info(location):
"""
Yield an iterable of tuples of (src_path, target_path, patch_text)
for each patch segment of a patch file at location.
Raise an exception if the file is not a patch file or cannot be parsed.
"""
patchset = pythonpatch.fromfile(location)
if not patchset:
msg = 'Unable to parse patch file: %(location)s' % locals()
raise ExtractErrorFailedToExtract(msg)
for ptch in patchset.items:
src = fileutils.as_posixpath(ptch.source.strip())
tgt = fileutils.as_posixpath(ptch.target.strip())
text = [l.strip() for l in patch_text(ptch) if l]
yield src, tgt, text