def test_copytree_to_from_dir_w_manifest(self): num_test_objs = 10 test_obj_size = 100 with NamedTemporaryDirectory(change_dir=True) as tmp_d: self.create_dataset(tmp_d, num_test_objs, test_obj_size) # Make a nested file and an empty directory for testing purposes tmp_d = Path(tmp_d) os.mkdir(tmp_d / 'my_dir') open(tmp_d / 'my_dir' / 'empty_file', 'w').close() os.mkdir(tmp_d / 'my_dir' / 'empty_dir') stor.copytree('.', self.test_dir, use_manifest=True) # Validate the contents of the manifest file manifest_contents = utils.get_data_manifest_contents(self.test_dir) expected_contents = self.get_dataset_obj_names(num_test_objs) expected_contents.extend( ['my_dir/empty_file', 'my_dir/empty_dir/']) expected_contents = [Path('test') / c for c in expected_contents] self.assertEquals(set(manifest_contents), set(expected_contents)) with NamedTemporaryDirectory(change_dir=True) as tmp_d: # Download the results successfully Path(self.test_dir).copytree('test', use_manifest=True) # Now delete one of the objects from s3. A second download # will fail with a condition error Path(self.test_dir / 'my_dir' / 'empty_dir/').remove() with self.assertRaises(exceptions.ConditionNotMetError): Path(self.test_dir).copytree('test', use_manifest=True, num_retries=0)
def test_upload_download_remove(self): num_test_objs = 10 min_obj_size = 50 with NamedTemporaryDirectory(change_dir=True) as tmp_d: self.create_dataset(tmp_d, num_test_objs, min_obj_size) self.test_dir.upload(['.']) which_obj = self.get_dataset_obj_names(num_test_objs)[-1] dx_p = self.test_dir / which_obj file_h = dxpy.DXFile(dxid=dx_p.canonical_resource, project=dx_p.canonical_project) file_h.wait_on_close(20) # wait for file to go to closed state for which_obj in self.get_dataset_obj_names(num_test_objs): self.assertTrue((self.test_dir / which_obj).exists()) with NamedTemporaryDirectory(change_dir=True) as tmp_d: self.test_dir.download(tmp_d) for which_obj in self.get_dataset_obj_names(num_test_objs): self.assertCorrectObjectContents(which_obj, which_obj, min_obj_size) (self.test_dir / which_obj).remove() # consistency check while (self.test_dir / which_obj).exists(): time.sleep(.5) self.assertFalse((self.test_dir / which_obj).exists())
def test_temp_url(self): basic_file = 'test.txt' complex_file = 'my test?file=special_chars.txt' with NamedTemporaryDirectory(change_dir=True) as tmp_d: nested_tmp_dir = stor.join(tmp_d, 'tmp') os.mkdir(nested_tmp_dir) basic_file_p = stor.join(nested_tmp_dir, basic_file) complex_file_p = stor.join(nested_tmp_dir, 'my test?file=special_chars.txt') with stor.open(basic_file_p, 'w') as f: f.write('basic test') with stor.open(complex_file_p, 'w') as f: f.write('complex test') self.test_container.upload(['.']) with NamedTemporaryDirectory(change_dir=True) as tmp_d: basic_obj = stor.Path( stor.join(self.test_container, 'tmp', basic_file)) basic_temp_url = basic_obj.temp_url(inline=False, filename=basic_file) r = requests.get(basic_temp_url) self.assertEquals(r.content, 'basic test') self.assertEquals(r.headers['Content-Disposition'], 'attachment; filename="test.txt"; filename*=UTF-8\'\'test.txt') complex_obj = stor.Path( stor.join(self.test_container, 'tmp', complex_file)) complex_temp_url = complex_obj.temp_url(inline=False, filename=complex_file) r = requests.get(complex_temp_url) self.assertEquals(r.content, 'complex test') self.assertEquals(r.headers['Content-Disposition'], 'attachment; filename="my test%3Ffile%3Dspecial_chars.txt"; filename*=UTF-8\'\'my%20test%3Ffile%3Dspecial_chars.txt') # noqa
def test_copytree_to_from_dir(self): num_test_objs = 10 test_obj_size = 100 with NamedTemporaryDirectory(change_dir=True) as tmp_d: self.create_dataset(tmp_d, num_test_objs, test_obj_size) stor.copytree('.', self.test_dir) time.sleep(10) # for uploaded files to close with NamedTemporaryDirectory(change_dir=True) as tmp_d: self.test_dir.copytree('test') # Verify contents of all downloaded test objects for which_obj in self.get_dataset_obj_names(num_test_objs): obj_path = Path('test') / which_obj self.assertCorrectObjectContents(obj_path, which_obj, test_obj_size)
def test_download(self): with NamedTemporaryDirectory(change_dir=True): os.mkdir('dir') os.mkdir('dir/a') open('dir/a/a.txt', 'w').close() self.test_dir.upload(['.']) with NamedTemporaryDirectory(change_dir=True): open('dir', 'w').close() open('a', 'w').close() with self.assertRaises(OSError): self.test_dir.download('.') with self.assertRaises(OSError): (self.test_dir / 'dir').download('.')
def test_over_1000_files(self): num_test_objs = 1234 min_obj_size = 0 with NamedTemporaryDirectory(change_dir=True) as tmp_d: self.create_dataset(tmp_d, num_test_objs, min_obj_size) self.test_dir.upload(['.']) self.assertEquals(1234, len(self.test_dir.list())) self.assertEquals(1200, len(self.test_dir.list(limit=1200))) self.assertTrue(self.test_dir.isdir()) with NamedTemporaryDirectory(change_dir=True) as tmp_d: self.test_dir.download('./') self.assertEquals(1234, len(os.listdir(tmp_d)))
def test_list_glob(self): num_test_objs = 20 test_obj_size = 100 test_dir = self.test_container / 'test' with NamedTemporaryDirectory(change_dir=True) as tmp_d: self.create_dataset(tmp_d, num_test_objs, test_obj_size) Path('.').copytree(test_dir) objs = set( test_dir.list( condition=lambda results: len(results) == num_test_objs)) expected_objs = { test_dir / obj_name for obj_name in self.get_dataset_obj_names(num_test_objs) } self.assertEquals(len(objs), num_test_objs) self.assertEquals(objs, expected_objs) expected_glob = { test_dir / obj_name for obj_name in self.get_dataset_obj_names(num_test_objs) if obj_name.startswith('1') } self.assertTrue(len(expected_glob) > 1) globbed_objs = set( test_dir.glob( '1*', condition=lambda results: len(results) == len(expected_glob))) self.assertEquals(globbed_objs, expected_glob)
def test_makedirs(self): with NamedTemporaryDirectory() as tmpdir: d = Path(tmpdir) # Placeholder file so that when removedirs() is called, # it doesn't remove the temporary directory itself. tempf = d / 'temp.txt' with tempf.open('w') as fp: fp.write('blah') try: foo = d / 'foo' boz = foo / 'bar' / 'baz' / 'boz' boz.makedirs() try: assert boz.isdir() finally: foo.rmtree() assert not foo.exists() assert d.exists() foo.mkdir(0o750) boz.makedirs(0o700) try: assert boz.isdir() finally: foo.rmtree() assert not foo.exists() assert d.exists() finally: os.remove(tempf)
def test_list_methods(self): fake_bucket = Path('s3://stor-test-bucket2') with self.assertRaises(exceptions.NotFoundError): fake_bucket.list() fake_folder = self.test_bucket / 'not_a_dir' self.assertEquals([], fake_folder.list()) with NamedTemporaryDirectory(change_dir=True): open('file1.txt', 'w').close() open('file2.txt', 'w').close() os.mkdir('nested_dir') os.mkdir('nested_dir/dir') open('nested_dir/dir/file3.txt', 'w').close() self.test_dir.upload(['.']) file_list = self.test_dir.list() starts_with_list = self.test_bucket.list(starts_with='test') self.assertEquals(set(file_list), set(starts_with_list)) self.assertEquals( set(file_list), set([ self.test_dir / 'file1.txt', self.test_dir / 'file2.txt', self.test_dir / 'nested_dir/dir/file3.txt' ])) dir_list = self.test_dir.listdir() self.assertEquals( set(dir_list), set([ self.test_dir / 'file1.txt', self.test_dir / 'file2.txt', self.test_dir / 'nested_dir/' ])) self.assertTrue(self.test_dir.listdir() == (self.test_dir + '/').listdir())
def test_rmtree(self): with NamedTemporaryDirectory(change_dir=True) as tmp_d: # Make a couple empty test files and nested files tmp_d = Path(tmp_d) os.mkdir(tmp_d / 'my_dir') open(tmp_d / 'my_dir' / 'dir_file1', 'w').close() open(tmp_d / 'my_dir' / 'dir_file2', 'w').close() open(tmp_d / 'base_file1', 'w').close() open(tmp_d / 'base_file2', 'w').close() stor.copytree( '.', self.test_container, use_manifest=True) swift_dir = self.test_container / 'my_dir' self.assertEquals(len(swift_dir.list()), 2) swift_dir.rmtree() self.assertEquals(len(swift_dir.list()), 0) base_contents = self.test_container.list() self.assertTrue((self.test_container / 'base_file1') in base_contents) self.assertTrue((self.test_container / 'base_file1') in base_contents) self.test_container.rmtree() # TODO figure out a better way to test that the container no longer exists. with self.assertRaises(swift.NotFoundError): # Replication may have not happened yet for container deletion. Keep # listing in intervals until a NotFoundError is thrown for i in (0, 1, 3): time.sleep(i) self.test_container.list()
def test_listdir_other_encoding(self): """ Some filesystems allow non-character sequences in path names. ``.listdir`` should still function in this case. See issue #61 for details. """ raise unittest.SkipTest('test copied over was broken, need to fix') with NamedTemporaryDirectory() as tmpdir: assert Path(tmpdir).listdir() == [] tmpdir_bytes = str(tmpdir).encode('ascii') filename = 'r\xe9\xf1emi'.encode('latin-1') pathname = os.path.join(tmpdir_bytes, filename) with open(pathname, 'w'): pass # first demonstrate that os.listdir works assert os.listdir(tmpdir_bytes) # now try with path.py results = Path(tmpdir).listdir() assert len(results) == 1 res, = results assert isinstance(res, Path) # OS X seems to encode the bytes in the filename as %XX characters. if platform.system() == 'Darwin': assert res.basename() == 'r%E9%F1emi' return assert len(res.basename()) == len(filename)
def test_condition(self): num_test_objs = 20 test_obj_size = 100 with NamedTemporaryDirectory(change_dir=True) as tmp_d: self.create_dataset(tmp_d, num_test_objs, test_obj_size) Path('.').copytree(self.test_dir) # Verify a ConditionNotMet exception is thrown when attempting to list # a file that hasn't been uploaded expected_objs = { self.test_dir / which_obj for which_obj in self.get_dataset_obj_names(num_test_objs + 1) } with self.assertRaises(exceptions.ConditionNotMetError): self.test_dir.list( condition=lambda results: expected_objs == set(results)) # Verify that the condition passes when excluding the non-extant file correct_objs = { self.test_dir / which_obj for which_obj in self.get_dataset_obj_names(num_test_objs) } objs = self.test_dir.list( condition=lambda results: correct_objs == set(results)) self.assertEquals(correct_objs, set(objs))
def test_static_large_obj_copy_and_segment_container(self): with NamedTemporaryDirectory(change_dir=True) as tmp_d: segment_size = 1048576 obj_size = segment_size * 4 + 100 self.create_dataset(tmp_d, 1, obj_size) obj_path = stor.join(tmp_d, self.get_dataset_obj_names(1)[0]) options = {'swift:upload': {'segment_size': segment_size}} with settings.use(options): obj_path.copy(self.test_container / 'large_object.txt') # Verify there is a segment container and that it can be ignored when listing a dir segment_container = Path(self.test_container.parent) / ('.segments_%s' % self.test_container.name) # noqa containers = Path(self.test_container.parent).listdir(ignore_segment_containers=False) self.assertTrue(segment_container in containers) self.assertTrue(self.test_container in containers) containers = Path(self.test_container.parent).listdir(ignore_segment_containers=True) self.assertFalse(segment_container in containers) self.assertTrue(self.test_container in containers) # Verify there are five segments objs = set(segment_container.list(condition=lambda results: len(results) == 5)) self.assertEquals(len(objs), 5) # Copy back the large object and verify its contents obj_path = Path(tmp_d) / 'large_object.txt' Path(self.test_container / 'large_object.txt').copy(obj_path) self.assertCorrectObjectContents(obj_path, self.get_dataset_obj_names(1)[0], obj_size)
def test_condition_failures(self): num_test_objs = 20 test_obj_size = 100 test_dir = self.test_container / 'test' with NamedTemporaryDirectory(change_dir=True) as tmp_d: self.create_dataset(tmp_d, num_test_objs, test_obj_size) Path('.').copytree(test_dir) # Verify a ConditionNotMet exception is thrown when attempting to list # a file that hasn't been uploaded expected_objs = { test_dir / which_obj for which_obj in self.get_dataset_obj_names(num_test_objs + 1) } num_retries = settings.get()['swift']['num_retries'] with mock.patch('time.sleep') as mock_sleep: with self.assertRaises(swift.ConditionNotMetError): test_dir.list(condition=lambda results: expected_objs == set(results)) self.assertTrue(num_retries > 0) self.assertEquals(len(mock_sleep.call_args_list), num_retries) # Verify that the condition passes when excluding the non-extant file expected_objs = { test_dir / which_obj for which_obj in self.get_dataset_obj_names(num_test_objs) } objs = test_dir.list(condition=lambda results: expected_objs == set(results)) self.assertEquals(expected_objs, set(objs))
def test_glob(self): with NamedTemporaryDirectory(change_dir=True): open('file.txt', 'w').close() open('file2.txt', 'w').close() open('file3', 'w').close() files = stor.glob('.', '*.txt') self.assertEquals(set(files), set(['./file.txt', './file2.txt']))
def test_upload_w_headers(self): test_file = self.test_dir / 'a.txt' with NamedTemporaryDirectory(change_dir=True): open('a.txt', 'w').close() self.test_dir.upload(['.'], headers={'ContentLanguage': 'en'}) self.assertTrue(test_file.exists()) self.assertEquals(test_file.stat()['ContentLanguage'], 'en')
def test_copytree_to_from_dir(self): num_test_objs = 10 test_obj_size = 100 with NamedTemporaryDirectory(change_dir=True) as tmp_d: self.create_dataset(tmp_d, num_test_objs, test_obj_size) stor.copytree('.', self.test_dir) with NamedTemporaryDirectory(change_dir=True) as tmp_d: self.test_dir.copytree( 'test', condition=lambda results: len(results) == num_test_objs) # Verify contents of all downloaded test objects for which_obj in self.get_dataset_obj_names(num_test_objs): obj_path = Path('test') / which_obj self.assertCorrectObjectContents(obj_path, which_obj, test_obj_size)
def test_copytree_w_headers(self): with NamedTemporaryDirectory(change_dir=True) as tmp_d: open(tmp_d / 'test_obj', 'w').close() stor.copytree('.', self.test_container, headers=['X-Delete-After:1000']) obj = stor.join(self.test_container, 'test_obj') stat_results = obj.stat() self.assertTrue('x-delete-at' in stat_results['headers'])
def test_hidden_file_nested_dir_copytree(self): with NamedTemporaryDirectory(change_dir=True): open('.hidden_file', 'w').close() os.symlink('.hidden_file', 'symlink') os.mkdir('.hidden_dir') os.mkdir('.hidden_dir/nested') open('.hidden_dir/nested/file1', 'w').close() open('.hidden_dir/nested/file2', 'w').close() Path('.').copytree(self.test_dir) time.sleep(10) # for uploaded files to close with NamedTemporaryDirectory(change_dir=True): self.test_dir.copytree('test') self.assertTrue(Path('test/.hidden_file').isfile()) self.assertTrue(Path('test/symlink').isfile()) self.assertTrue(Path('test/.hidden_dir').isdir()) self.assertTrue(Path('test/.hidden_dir/nested').isdir()) self.assertTrue(Path('test/.hidden_dir/nested/file1').isfile()) self.assertTrue(Path('test/.hidden_dir/nested/file2').isfile())
def test_context_manager(self): """Can be used as context manager for chdir.""" with NamedTemporaryDirectory() as tmpdir: d = Path(tmpdir) subdir = d / 'subdir' subdir.makedirs() old_dir = os.getcwd() with subdir: assert os.getcwd() == os.path.realpath(subdir) assert os.getcwd() == old_dir
def test_hidden_file_nested_dir_copytree(self): with NamedTemporaryDirectory(change_dir=True): open('.hidden_file', 'w').close() os.symlink('.hidden_file', 'symlink') os.mkdir('.hidden_dir') os.mkdir('.hidden_dir/nested') open('.hidden_dir/nested/file1', 'w').close() open('.hidden_dir/nested/file2', 'w').close() Path('.').copytree(self.test_dir) with NamedTemporaryDirectory(change_dir=True): self.test_dir.copytree( 'test', condition=lambda results: len(results) == 4) self.assertTrue(Path('test/.hidden_file').isfile()) self.assertTrue(Path('test/symlink').isfile()) self.assertTrue(Path('test/.hidden_dir').isdir()) self.assertTrue(Path('test/.hidden_dir/nested').isdir()) self.assertTrue(Path('test/.hidden_dir/nested/file1').isfile()) self.assertTrue(Path('test/.hidden_dir/nested/file2').isfile())
def test_copy_to_from_dir(self): num_test_objs = 5 min_obj_size = 100 with NamedTemporaryDirectory(change_dir=True) as tmp_d: self.create_dataset(tmp_d, num_test_objs, min_obj_size) for which_obj in self.get_dataset_obj_names(num_test_objs): obj_path = stor.join(self.test_dir, '%s.txt' % which_obj) stor.copy(which_obj, obj_path) stor.copy(obj_path, 'copied_file') self.assertCorrectObjectContents('copied_file', which_obj, min_obj_size)
def test_multipart_transfer(self): logger = six.StringIO() handler = logging.StreamHandler(logger) logging.getLogger('botocore').setLevel(logging.DEBUG) logging.getLogger('botocore').addHandler(handler) handler.setLevel(logging.DEBUG) with NamedTemporaryDirectory(change_dir=True) as tmp_d: self.create_dataset(tmp_d, 1, 10 * 1024 * 1024) self.test_dir.upload(['.']) self.assertEquals(1, len(self.test_dir.listdir())) with NamedTemporaryDirectory(change_dir=True) as tmp_d: self.test_dir.download('.') self.assertEquals(1, len(Path('.').listdir())) self.assertIn("CompleteMultipartUploadResult", logger.getvalue()) # Check for multipart download by checking for multiple 206 GET requests # to the object self.assertRegexpMatches(logger.getvalue(), '"GET /test/0 HTTP/1.1" 206')
def test_upload_download_remove(self): num_test_objs = 10 min_obj_size = 50 with NamedTemporaryDirectory(change_dir=True) as tmp_d: self.create_dataset(tmp_d, num_test_objs, min_obj_size) self.test_dir.upload(['.']) for which_obj in self.get_dataset_obj_names(num_test_objs): self.assertTrue((self.test_dir / which_obj).exists()) with NamedTemporaryDirectory(change_dir=True) as tmp_d: self.test_dir.download(tmp_d) for which_obj in self.get_dataset_obj_names(num_test_objs): self.assertCorrectObjectContents(which_obj, which_obj, min_obj_size) (self.test_dir / which_obj).remove() # consistency check while (self.test_dir / which_obj).exists(): time.sleep(.5) self.assertFalse((self.test_dir / which_obj).exists())
def test_walkfiles(self): with NamedTemporaryDirectory(change_dir=True): open('file1.txt', 'w').close() os.mkdir('dir') os.mkdir('dir/dir2') open('dir/file2.txt', 'w').close() open('dir/dir2/file3', 'w').close() open('dir/dir2/file4.txt', 'w').close() files = Path('.').walkfiles(pattern='*.txt') self.assertEquals(set(files), set(['./file1.txt', './dir/file2.txt', './dir/dir2/file4.txt']))
def test_copy_to_from_dir(self): num_test_objs = 5 min_obj_size = 100 with NamedTemporaryDirectory(change_dir=True) as tmp_d: self.create_dataset(tmp_d, num_test_objs, min_obj_size) for which_obj in self.get_dataset_obj_names(num_test_objs): obj_path = stor.join(self.test_dir, '%s.txt' % which_obj) stor.copy(which_obj, obj_path) file_h = dxpy.DXFile(dxid=obj_path.canonical_resource, project=obj_path.canonical_project) file_h.wait_on_close(20) # wait for file to go to closed state stor.copy(obj_path, 'copied_file') self.assertCorrectObjectContents('copied_file', which_obj, min_obj_size)
def test_dir_markers(self): with NamedTemporaryDirectory(change_dir=True): os.mkdir('empty') os.mkdir('dir') open('a.txt', 'w').close() open('dir/b.txt', 'w').close() self.test_dir.upload(['.']) self.assertEquals( set(self.test_dir.list()), { self.test_dir / 'a.txt', self.test_dir / 'dir/b.txt', self.test_dir / 'empty/' }) self.assertEquals( set(self.test_dir.list(ignore_dir_markers=True)), {self.test_dir / 'a.txt', self.test_dir / 'dir/b.txt'}) self.assertTrue((self.test_dir / 'empty').isdir()) with NamedTemporaryDirectory(change_dir=True): self.test_dir.download('.') self.assertTrue(os.path.isdir('empty')) self.assertTrue(os.path.exists('dir/b.txt')) self.assertTrue(os.path.exists('a.txt'))
def test_upload_multiple_dirs(self): with NamedTemporaryDirectory(change_dir=True) as tmp_d: num_test_objs = 10 tmp_d = Path(tmp_d) # Create files filled with random data. path1 = tmp_d / 'dir1' os.mkdir(path1) self.create_dataset(path1, num_test_objs, 10) # Create empty dir and file. path2 = tmp_d / 'dir2' os.mkdir(path2) os.mkdir(path2 / 'my_dir') open(path2 / 'my_dir' / 'included_file', 'w').close() open(path2 / 'my_dir' / 'excluded_file', 'w').close() os.mkdir(path2 / 'my_dir' / 'included_dir') os.mkdir(path2 / 'my_dir' / 'excluded_dir') # Create file in the top level directory. open(tmp_d / 'top_level_file', 'w').close() to_upload = [ 'dir1', 'dir2/my_dir/included_file', 'dir2/my_dir/included_dir', 'top_level_file', ] with tmp_d: dx_p = self.test_dir / 'subdir' dx_p.upload(to_upload) # Validate the contents of the manifest file uploaded_contents = stor.list(dx_p) expected_contents = [ Path('dir1') / name for name in self.get_dataset_obj_names(num_test_objs) ] expected_contents.extend([ 'dir2/my_dir/included_file', 'top_level_file', ]) expected_contents = [dx_p / c for c in expected_contents] self.assertEquals(set(uploaded_contents), set(expected_contents)) empty_dir = dx_p / 'dir2/my_dir/included_dir' self.assertTrue(stor.isdir(empty_dir))
def test_walkfiles(self): with NamedTemporaryDirectory(change_dir=True): # Make a dataset with files that will match a particular pattern (*.sh) # and also empty directories that should be ignored when calling walkfiles open('aabc.sh', 'w').close() open('aabc', 'w').close() os.mkdir('b') open('b/c.sh', 'w').close() os.mkdir('empty') open('b/d', 'w').close() open('b/abbbc', 'w').close() Path('.').copytree(self.test_dir) unfiltered_files = list(self.test_dir.walkfiles()) self.assertEquals( set(unfiltered_files), set([ stor.join(self.test_dir, 'aabc.sh'), stor.join(self.test_dir, 'aabc'), stor.join(self.test_dir, 'b/c.sh'), stor.join(self.test_dir, 'b/d'), stor.join(self.test_dir, 'b/abbbc'), ])) prefix_files = list(self.test_dir.walkfiles('*.sh')) self.assertEquals( set(prefix_files), set([ stor.join(self.test_dir, 'aabc.sh'), stor.join(self.test_dir, 'b/c.sh'), ])) double_infix_files = list(self.test_dir.walkfiles('a*b*c')) self.assertEquals( set(double_infix_files), set([ stor.join(self.test_dir, 'aabc'), stor.join(self.test_dir, 'b/abbbc'), ])) suffix_files = list(self.test_dir.walkfiles('a*')) self.assertEquals( set(suffix_files), set([ stor.join(self.test_dir, 'aabc.sh'), stor.join(self.test_dir, 'aabc'), stor.join(self.test_dir, 'b/abbbc'), ])) # should still *make* an empty directory assert stor.exists(stor.join(self.test_dir, 'empty'))
def test_is_methods(self): with NamedTemporaryDirectory(change_dir=True) as tmpdir: dirname = tmpdir / 'blah' self.assertFalse(dirname.isdir()) self.assertFalse(dirname.isfile()) dirname.mkdir() self.assertTrue(dirname.isdir()) self.assertFalse(dirname.isfile()) filename = os.path.join(dirname, 'test.txt') with filename.open('wb') as fp: fp.write(b'somedata') self.assertFalse(filename.isdir()) self.assertTrue(filename.isfile()) self.assertFalse(filename.islink()) self.assertTrue(filename.abspath().isabs()) self.assertFalse(Path('.').isabs()) self.assertEqual(dirname.listdir(), [filename])