Exemplo n.º 1
0
def test_prepare_for_datalad(tmpdir):
    pytest.importorskip("datalad", minversion=MIN_VERSION)
    studydir = tmpdir.join("PI").join("study")
    studydir_ = str(studydir)
    os.makedirs(studydir_)
    populate_bids_templates(studydir_)

    add_to_datalad(str(tmpdir), studydir_, None, False)

    from datalad.api import Dataset
    superds = Dataset(str(tmpdir))

    assert superds.is_installed()
    assert not superds.repo.dirty
    subdss = superds.subdatasets(recursive=True, result_xfm='relpaths')
    for ds_path in sorted(subdss):
        ds = Dataset(opj(superds.path, ds_path))
        assert ds.is_installed()
        assert not ds.repo.dirty

    # the last one should have been the study
    target_files = {
        '.gitattributes',
        '.datalad/config', '.datalad/.gitattributes',
        'dataset_description.json',
        'CHANGES', 'README'}
    assert set(ds.repo.get_indexed_files()) == target_files
    # and all are under git
    for f in target_files:
        assert not ds.repo.is_under_annex(f)
    assert not ds.repo.is_under_annex('.gitattributes')

    # Above call to add_to_datalad does not create .heudiconv subds since
    # directory does not exist (yet).
    # Let's first check that it is safe to call it again
    add_to_datalad(str(tmpdir), studydir_, None, False)
    assert not ds.repo.dirty

    old_hexsha = ds.repo.get_hexsha()
    # Now let's check that if we had previously converted data so that
    # .heudiconv was not a submodule, we still would not fail
    dsh_path = os.path.join(ds.path, '.heudiconv')
    dummy_path = os.path.join(dsh_path, 'dummy.nii.gz')

    create_file_if_missing(dummy_path, '')
    ds.add(dummy_path, message="added a dummy file")
    # next call must not fail, should just issue a warning
    add_to_datalad(str(tmpdir), studydir_, None, False)
    ds.repo.is_under_annex(dummy_path)
    assert not ds.repo.dirty
    assert '.heudiconv/dummy.nii.gz' in ds.repo.get_files()

    # Let's now roll back and make it a proper submodule
    ds.repo._git_custom_command([], ['git', 'reset', '--hard', old_hexsha])
    # now we do not add dummy to git
    create_file_if_missing(dummy_path, '')
    add_to_datalad(str(tmpdir), studydir_, None, False)
    assert '.heudiconv' in ds.subdatasets(result_xfm='relpaths')
    assert not ds.repo.dirty
    assert '.heudiconv/dummy.nii.gz' not in ds.repo.get_files()
Exemplo n.º 2
0
def test_create_file_if_missing(tmpdir):
    tf = tmpdir.join("README.txt")
    assert not tf.exists()
    create_file_if_missing(str(tf), "content")
    assert tf.exists()
    assert tf.read() == "content"
    create_file_if_missing(str(tf), "content2")
    # nothing gets changed
    assert tf.read() == "content"