Esempio n. 1
0
 def set_up(path, fast=False):
     path.mkdir()
     (path / 'dupe1.txt').write_bytes(b'equal')
     (path / 'dupe2.txt').write_bytes(b'equal')
     (path / 'unequal.txt').write_bytes(str(path).encode('utf8'))
     info = DirInfo(path)
     info.populate(fast=fast)
     info.save()
     return path
Esempio n. 2
0
 def set_up(path, extra_files=()):
     path.mkdir()
     (path / 'equal1.txt').write_bytes(b'equal1')
     (path / 'equal2.txt').write_bytes(b'equal2')
     (path / 'unequal.txt').write_bytes(str(path).encode('utf8'))
     for extra in extra_files:
         (path / extra).write_bytes(b'extra')
     info = DirInfo(path)
     info.populate()
     info.save()
     return path
Esempio n. 3
0
 def decorated(
     dirname: str,
     populate: bool,
     hash_empty: bool,
     fast: bool,
     resume: bool,
     threads: int,
 ):
     dirname = os.path.abspath(dirname)
     if resume or not populate:
         dirinfo = DirInfo.cached(dirname)
         if dirinfo.file_count == 0:
             populate = True
     else:
         dirinfo = DirInfo(dirname)
     if populate:
         dirinfo.populate(
             no_empty=not hash_empty,
             fast=fast,
             resume=resume,
             threads=threads,
             progress=tqdm,
         )
         filename = dirinfo.save()
         tqdm.write(f'Written {filename}')
     return func(dirinfo)
Esempio n. 4
0
def test_serialisation(tmp_path):
    """
    Test failure modes. Success is tested in check_everything.
    """
    subdir = (tmp_path / 'sub')
    jsonfile = (tmp_path / 'sub.dirinfo.json')
    (subdir / 'dir').mkdir(parents=True)
    dirinfo = DirInfo(subdir)
    assert dirinfo.save() == os.fspath(jsonfile)
    # Not exactly a requirement, but for the tests to work we need this.
    assert jsonfile.exists()
    # If this fails, then testing that the bad cases fail is kind of pointless.
    assert DirInfo.load(subdir).base == os.fspath(subdir)
    # Make sure the encoder isn't accidentally used for something it can't handle.
    with pytest.raises(TypeError):
        json.dumps(object(), cls=Encoder)

    # Make sure bad json file contents are rejected
    def bad_jsonfile(jsondata):
        with open(jsonfile, 'w', encoding='utf8') as outfile:
            json.dump(jsondata, outfile)
        with pytest.raises(ValueError):
            DirInfo.load(subdir)

    bad_jsonfile({'foo': 'bar'})
    bad_jsonfile(None)

    # If the serialised base doesn't match the actual location, then something
    # is wrong and we should refuse to load it.
    assert dirinfo.save() == os.fspath(jsonfile)
    with open(jsonfile, 'r', encoding='utf8') as infile:
        jsondata = json.load(infile)
    jsondata['base'] += 'X'
    bad_jsonfile(jsondata)

    # If there's no data then load() fails, but cached() succeeds.
    jsonfile.unlink()
    with pytest.raises(FileNotFoundError):
        DirInfo.load(subdir)
    assert DirInfo.cached(subdir).base == subdir