mirror of
https://github.com/arsenetar/send2trash.git
synced 2025-05-08 01:39:51 +00:00
Fix silently failing on Windows (#33)
* Fix #31: Silently failing on Windows * Update Windows test * Fix test folders not getting removed
This commit is contained in:
parent
8f684a9c8b
commit
66afce7252
@ -7,7 +7,8 @@
|
|||||||
from __future__ import unicode_literals
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
from ctypes import (windll, Structure, byref, c_uint,
|
from ctypes import (windll, Structure, byref, c_uint,
|
||||||
create_unicode_buffer, addressof)
|
create_unicode_buffer, addressof,
|
||||||
|
GetLastError, FormatError)
|
||||||
from ctypes.wintypes import HWND, UINT, LPCWSTR, BOOL
|
from ctypes.wintypes import HWND, UINT, LPCWSTR, BOOL
|
||||||
import os.path as op
|
import os.path as op
|
||||||
|
|
||||||
@ -49,6 +50,11 @@ def get_short_path_name(long_name):
|
|||||||
if not long_name.startswith('\\\\?\\'):
|
if not long_name.startswith('\\\\?\\'):
|
||||||
long_name = '\\\\?\\' + long_name
|
long_name = '\\\\?\\' + long_name
|
||||||
buf_size = GetShortPathNameW(long_name, None, 0)
|
buf_size = GetShortPathNameW(long_name, None, 0)
|
||||||
|
# FIX: https://github.com/hsoft/send2trash/issues/31
|
||||||
|
# If buffer size is zero, an error has occurred.
|
||||||
|
if not buf_size:
|
||||||
|
err_no = GetLastError()
|
||||||
|
raise WindowsError(err_no, FormatError(err_no), long_name[4:])
|
||||||
output = create_unicode_buffer(buf_size)
|
output = create_unicode_buffer(buf_size)
|
||||||
GetShortPathNameW(long_name, output, buf_size)
|
GetShortPathNameW(long_name, output, buf_size)
|
||||||
return output.value[4:] # Remove '\\?\' for SHFileOperationW
|
return output.value[4:] # Remove '\\?\' for SHFileOperationW
|
||||||
@ -83,4 +89,4 @@ def send2trash(path):
|
|||||||
fileop.lpszProgressTitle = None
|
fileop.lpszProgressTitle = None
|
||||||
result = SHFileOperationW(byref(fileop))
|
result = SHFileOperationW(byref(fileop))
|
||||||
if result:
|
if result:
|
||||||
raise WindowsError(None, None, path, result)
|
raise WindowsError(result, FormatError(result), path)
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
# coding: utf-8
|
# coding: utf-8
|
||||||
import os
|
import os
|
||||||
|
import shutil
|
||||||
import sys
|
import sys
|
||||||
import unittest
|
import unittest
|
||||||
from os import path as op
|
from os import path as op
|
||||||
@ -8,12 +9,37 @@ from tempfile import gettempdir
|
|||||||
from send2trash import send2trash as s2t
|
from send2trash import send2trash as s2t
|
||||||
|
|
||||||
|
|
||||||
|
@unittest.skipIf(sys.platform != 'win32', 'Windows only')
|
||||||
|
class TestNormal(unittest.TestCase):
|
||||||
|
def setUp(self):
|
||||||
|
self.dirname = '\\\\?\\' + op.join(gettempdir(), 'python.send2trash')
|
||||||
|
self.file = op.join(self.dirname, 'testfile.txt')
|
||||||
|
self._create_tree(self.file)
|
||||||
|
|
||||||
|
def tearDown(self):
|
||||||
|
shutil.rmtree(self.dirname, ignore_errors=True)
|
||||||
|
|
||||||
|
def _create_tree(self, path):
|
||||||
|
dirname = op.dirname(path)
|
||||||
|
if not op.isdir(dirname):
|
||||||
|
os.makedirs(dirname)
|
||||||
|
with open(path, 'w') as writer:
|
||||||
|
writer.write('send2trash test')
|
||||||
|
|
||||||
|
def test_trash_file(self):
|
||||||
|
s2t(self.file)
|
||||||
|
self.assertFalse(op.exists(self.file))
|
||||||
|
|
||||||
|
def test_file_not_found(self):
|
||||||
|
file = op.join(self.dirname, 'otherfile.txt')
|
||||||
|
self.assertRaises(WindowsError, s2t, file)
|
||||||
|
|
||||||
@unittest.skipIf(sys.platform != 'win32', 'Windows only')
|
@unittest.skipIf(sys.platform != 'win32', 'Windows only')
|
||||||
class TestLongPath(unittest.TestCase):
|
class TestLongPath(unittest.TestCase):
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
filename = 'A' * 100
|
filename = 'A' * 100
|
||||||
self.dirname = '\\\\?\\' + os.path.join(gettempdir(), filename)
|
self.dirname = '\\\\?\\' + op.join(gettempdir(), filename)
|
||||||
self.file = os.path.join(
|
self.file = op.join(
|
||||||
self.dirname,
|
self.dirname,
|
||||||
filename,
|
filename,
|
||||||
filename, # From there, the path is not trashable from Explorer
|
filename, # From there, the path is not trashable from Explorer
|
||||||
@ -22,14 +48,11 @@ class TestLongPath(unittest.TestCase):
|
|||||||
self._create_tree(self.file)
|
self._create_tree(self.file)
|
||||||
|
|
||||||
def tearDown(self):
|
def tearDown(self):
|
||||||
try:
|
shutil.rmtree(self.dirname, ignore_errors=True)
|
||||||
os.remove(self.dirname)
|
|
||||||
except OSError:
|
|
||||||
pass
|
|
||||||
|
|
||||||
def _create_tree(self, path):
|
def _create_tree(self, path):
|
||||||
dirname = os.path.dirname(path)
|
dirname = op.dirname(path)
|
||||||
if not os.path.isdir(dirname):
|
if not op.isdir(dirname):
|
||||||
os.makedirs(dirname)
|
os.makedirs(dirname)
|
||||||
with open(path, 'w') as writer:
|
with open(path, 'w') as writer:
|
||||||
writer.write('Looong filename!')
|
writer.write('Looong filename!')
|
||||||
|
Loading…
x
Reference in New Issue
Block a user