def test_copy_file_errors(dspath1, dspath2, nondspath): ds1 = Dataset(dspath1) # nothing given assert_raises(ValueError, copy_file) # no target directory given assert_raises(ValueError, ds1.copy_file, 'somefile') # using multiple sources and --specs-from assert_raises(ValueError, ds1.copy_file, ['1', '2', '3'], specs_from='-') # trying to copy to a dir that is not in a dataset ds1.create() assert_status( 'error', ds1.copy_file('somepath', target_dir=nondspath, on_failure='ignore')) # copy into a dataset that is not in the reference dataset ds2 = Dataset(dspath2).create() assert_status( 'error', ds1.copy_file('somepath', target_dir=dspath2, on_failure='ignore')) # attempt to copy from a directory, but no recursion is enabled. # use no reference ds to excercise a different code path assert_status('impossible', copy_file([nondspath, dspath1], on_failure='ignore')) # attempt to copy a file that doesn't exist assert_status('impossible', copy_file(['funky', dspath1], on_failure='ignore')) # attempt to copy a file without a destination given assert_raises(ValueError, copy_file, 'somepath') assert_status('impossible', copy_file(specs_from=['somepath'], on_failure='ignore'))
def test_copy_file_recursion(srcdir, destdir): src_ds = Dataset(srcdir).create(force=True) src_ds.save() dest_ds = Dataset(destdir).create() copy_file([src_ds.pathobj / 'subdir', dest_ds.pathobj], recursive=True) # structure is mirrored ok_file_has_content(dest_ds.pathobj / 'subdir' / 'file1', '123') ok_file_has_content(dest_ds.pathobj / 'subdir' / 'file2', 'abc')
def test_copy_file_into_nonannex(workdir): workdir = Path(workdir) src_ds = Dataset(workdir / 'src').create() (src_ds.pathobj / 'present.txt').write_text('123') (src_ds.pathobj / 'gone.txt').write_text('abc') src_ds.save() src_ds.drop('gone.txt', check=False) # destination has no annex dest_ds = Dataset(workdir / 'dest').create(annex=False) # no issue copying a file that has content copy_file([src_ds.pathobj / 'present.txt', dest_ds.pathobj]) ok_file_has_content(dest_ds.pathobj / 'present.txt', '123') # but cannot handle a dropped file, no chance to register # availability info in an annex assert_status( 'impossible', copy_file([src_ds.pathobj / 'gone.txt', dest_ds.pathobj], on_failure='ignore'))
def test_copy_file_datalad_specialremote(workdir, webdir, weburl): workdir = Path(workdir) src_ds = Dataset(workdir / 'src').create() # enable datalad special remote src_ds.repo.init_remote(DATALAD_SPECIAL_REMOTE, [ 'encryption=none', 'type=external', 'externaltype={}'.format(DATALAD_SPECIAL_REMOTE), 'autoenable=true' ]) # put files into the dataset by URL src_ds.download_url('/'.join((weburl, 'webfile1')), path='myfile1.txt') src_ds.download_url('/'.join((weburl, 'webfile2')), path='myfile2.txt') # approx test that the file is known to a remote # that is not the web remote assert_in_results( src_ds.repo.whereis('myfile1.txt', output='full').values(), here=False, description='[{}]'.format(DATALAD_SPECIAL_REMOTE), ) # now a new dataset dest_ds = Dataset(workdir / 'dest').create() # no special remotes eq_(dest_ds.repo.get_special_remotes(), {}) copy_file([src_ds.pathobj / 'myfile1.txt', dest_ds.pathobj]) # we have an special remote in the destination dataset now assert_in_results( dest_ds.repo.get_special_remotes().values(), externaltype=DATALAD_SPECIAL_REMOTE, ) # and it works dest_ds.drop('myfile1.txt') dest_ds.repo.get('myfile1.txt', remote='datalad') ok_file_has_content(dest_ds.pathobj / 'myfile1.txt', '123') # now replace file in dest with a different content at the same path copy_file( [src_ds.pathobj / 'myfile2.txt', dest_ds.pathobj / 'myfile1.txt']) dest_ds.drop('myfile1.txt') dest_ds.repo.get('myfile1.txt', remote='datalad') # no gets the "same path" but yields different content ok_file_has_content(dest_ds.pathobj / 'myfile1.txt', 'abc')
def test_copy_file_specs_from(srcdir, destdir): srcdir = Path(srcdir) destdir = Path(destdir) files = [p for p in srcdir.glob('**/*') if not p.is_dir()] # plain list of absolute path objects r_srcabs, res = _check_copy_file_specs_from(srcdir, destdir / 'srcabs', files) # same, but with relative paths with chpwd(srcdir): r_srcrel, res = _check_copy_file_specs_from( srcdir, destdir / 'srcrel', [p.relative_to(srcdir) for p in files]) # same, but as strings r_srcabs_str, res = _check_copy_file_specs_from(srcdir, destdir / 'srcabs_str', [str(p) for p in files]) with chpwd(srcdir): r_srcrel_str, res = _check_copy_file_specs_from( srcdir, destdir / 'srcrel_str', [str(p.relative_to(srcdir)) for p in files]) # same, but with src/dest pairs r_srcdestabs_str, res = _check_copy_file_specs_from( srcdir, destdir / 'srcdestabs_str', [ '{}\0{}'.format(str(p), str(destdir / 'srcdestabs_str' / p.name)) for p in files ]) # all methods lead to the same dataset structure for a, b in ((r_srcabs, r_srcrel), (r_srcabs, r_srcabs_str), (r_srcabs, r_srcrel_str), (r_srcabs, r_srcdestabs_str)): eq_(*[ sorted( r for r in d.status(result_xfm='relpaths', result_renderer=None)) for d in (a, b) ]) # fail on destination outside of the dest repo res = copy_file(specs_from=[ '{}\0{}'.format(str(p), str(destdir / 'srcdest_wrong' / p.relative_to(srcdir))) for p in files ], on_failure='ignore') assert_status('error', res)