def test_Filepaths_handles_directories(tmp_path): # Create directory with 2 files content = tmp_path / 'content' content.mkdir() for f in ('a', 'b'): (content / f).write_text('<data>') fps = utils.Filepaths((content, )) assert fps == (content / 'a', content / 'b') # Replace one file with multilevel subdirectory subdir = content / 'b' subdir.unlink() subdir.mkdir() for f in ('c', 'd'): (subdir / f).write_text('<subdata>') subsubdir = subdir / 'subsubdir' subsubdir.mkdir() for f in ('e', 'f'): (subsubdir / f).write_text('<subdata>') fps[1] = content / 'b' assert fps == (content / 'a', subdir / 'c', subdir / 'd', subsubdir / 'e', subsubdir / 'f') # Replace subdirectory with file again for f in (subdir / 'c', subdir / 'd', subsubdir / 'e', subsubdir / 'f'): f.unlink() subsubdir.rmdir() subdir.rmdir() (content / 'b').write_text('I AM BACK') fps[1] = content / 'b' assert fps == (content / 'a', content / 'b')
def test_Filepaths_treats_relative_paths_as_equal_to_their_absolute_versions(tmp_path): (tmp_path / 'cwd').mkdir() cwd = os.getcwd() try: os.chdir(tmp_path / 'cwd') fps = utils.Filepaths((Path('foo'),)) assert fps == ('foo',) fps.append(tmp_path / 'cwd' / 'foo') assert fps == ('foo',) fps.append(tmp_path / 'cwd' / 'bar') fps.append('bar') assert fps == ('foo', tmp_path / 'cwd' / 'bar') finally: os.chdir(cwd)
def test_Filepaths_calls_callback_after_clearing(): cb = mock.MagicMock() fps = utils.Filepaths(('path/to/foo.jpg', ), callback=cb) fps.clear() cb.assert_called_once_with(fps)
def test_Filepaths_calls_callback_after_inserting(): cb = mock.MagicMock() fps = utils.Filepaths(('path/to/foo.jpg', ), callback=cb) fps.insert(0, 'path/to/baz.jpg') cb.assert_called_once_with(fps)
def test_Filepaths_calls_callback_after_removing(): cb = mock.MagicMock() fps = utils.Filepaths(('path/to/foo.jpg', ), callback=cb) del fps[0] cb.assert_called_once_with(fps)
def test_Filepaths_deduplicates_when_inserting(): fps = utils.Filepaths(('path/to/foo.jpg', 'path/to/bar.jpg')) fps.insert(0, 'path/to/bar.jpg') assert fps == (Path('path/to/foo.jpg'), Path('path/to/bar.jpg'))
def test_Filepaths_deduplicates_when_setting(): fps = utils.Filepaths(('path/to/foo.jpg', 'path/to/bar.jpg')) fps.append('path/to/foo.jpg') fps.extend(('path/to/bar.jpg', )) assert fps == (Path('path/to/foo.jpg'), Path('path/to/bar.jpg'))
def test_Filepaths_deduplicates_when_initializing(): fps = utils.Filepaths( ('path/to/foo.jpg', 'path/to/bar.jpg', 'path/to/foo.jpg')) assert fps == (Path('path/to/foo.jpg'), Path('path/to/bar.jpg'))
def test_Filepaths_accepts_string_or_iterable(): assert utils.Filepaths('path/to/foo.jpg') == [Path('path/to/foo.jpg')] assert utils.Filepaths(('path/to/foo.jpg', )) == [Path('path/to/foo.jpg')] assert utils.Filepaths(['path/to/foo.jpg']) == [Path('path/to/foo.jpg')]