1
0
mirror of https://github.com/arsenetar/send2trash.git synced 2026-03-14 19:41:39 +00:00

Replace plat_other with one supporting the XDG Trash spec

Added tests for plat_other
This commit is contained in:
gbn
2011-03-10 04:55:46 -05:00
parent a8a771c9bd
commit 13b3943c82
2 changed files with 185 additions and 41 deletions

80
test_plat_other.py Normal file
View File

@@ -0,0 +1,80 @@
import unittest
import os
from os import path as op
from send2trash.plat_other import send2trash
from configparser import ConfigParser
#
# Warning: This test will shit up your Trash folder with test.txt files.
#
class TestHomeTrash(unittest.TestCase):
def setUp(self):
self.filePath = op.expanduser("~/test.txt")
def test_trash(self):
os.system('touch ' + self.filePath)
send2trash(self.filePath)
self.assertFalse(op.exists(self.filePath))
#
# Following cases use sudo, require ramfs
#
class TestRamFs(unittest.TestCase):
def setUp(self):
# Create a ramfs thingy.
self.trashFolder = '/tmp/trashtest'
os.system('sudo mkdir ' + self.trashFolder)
os.system('sudo mount -t ramfs none ' + self.trashFolder)
self.fileName = 'test.txt'
self.filePath = op.join(self.trashFolder, self.fileName)
def tearDown(self):
os.system('sudo umount ' + self.trashFolder)
os.system('sudo rmdir ' + self.trashFolder)
class TestTopdirTrash(TestRamFs):
def setUp(self):
TestRamFs.setUp(self)
# Create a .Trash dir w/ a sticky bit
os.system('sudo chmod a+w ' + self.trashFolder)
os.system('sudo mkdir ' + op.join(self.trashFolder, '.Trash'))
os.system('sudo chmod a+wt ' + op.join(self.trashFolder, '.Trash'))
def test_trash(self):
os.system('touch ' + self.filePath)
send2trash(self.filePath)
self.assertFalse(op.exists(self.filePath))
self.assertTrue(op.exists(op.join(self.trashFolder, '.Trash', str(os.getuid()), 'files', self.fileName)))
self.assertTrue(op.exists(op.join(self.trashFolder, '.Trash', str(os.getuid()), 'info', self.fileName + '.trashinfo')))
# info relative path (if another test is added, with the same fileName/Path,
# then it gets renamed etc.)
cfg = ConfigParser()
cfg.read(op.join(self.trashFolder, '.Trash', str(os.getuid()), 'info', self.fileName + '.trashinfo'))
self.assertEqual(self.fileName, cfg.get('Trash Info', 'Path', 1))
# Test .Trash-UID
class TestTopdirTrashFallback(TestRamFs):
def setUp(self):
TestRamFs.setUp(self)
# DONT Create a .Trash dir, but make sure the topdir is writable for uid dir
os.system('sudo chmod a+w ' + self.trashFolder)
def test_trash(self):
os.system('touch ' + self.filePath)
send2trash(self.filePath)
self.assertFalse(op.exists(self.filePath))
self.assertTrue(op.exists(op.join(self.trashFolder, '.Trash-' + str(os.getuid()), 'files', self.fileName)))
# Test failure
class TestTopdirFailure(TestRamFs):
def test_trash(self):
# a file to call our own
os.system('sudo chmod o+w ' + self.trashFolder)
os.system('touch ' + self.filePath)
os.system('sudo chmod o-w ' + self.trashFolder)
with self.assertRaises(OSError):
send2trash(self.filePath)
self.assertTrue(op.exists(self.filePath))
if __name__ == '__main__':
unittest.main()