filenuke/tests/test.py

103 lines
3.0 KiB
Python
Raw Normal View History

2019-12-02 00:23:40 +00:00
import unittest
import sys
import os
import uuid
2019-12-02 09:04:17 +00:00
import shutil
2019-12-02 00:23:40 +00:00
sys.path.append(".")
import filenuke
from filenuke import nuke
def get_file():
return str(uuid.uuid4()) + '.tmp'
class TestErase(unittest.TestCase):
def test_single_delete(self):
f = get_file()
with open(f, "w") as test_f:
test_f.write('hello world')
2019-12-02 09:04:17 +00:00
filenuke.nuke._nuke(f)
2019-12-02 00:23:40 +00:00
self.assertFalse(os.path.exists(f))
def test_overwrite_single(self):
f = get_file()
with open(f, "w") as test_f:
test_f.write('hello world')
nuke._overwrite_file(f, 1)
2019-12-02 09:04:17 +00:00
with open(f, "rb") as test_f:
self.assertNotEqual(test_f.read(), b'hello world')
def test_nuke_single(self):
f = get_file()
with open(f, "w") as test_f:
test_f.write('hello world')
nuke._nuke(f)
self.assertFalse(os.path.exists(f))
def test_nuke(self):
try:
os.mkdir('test')
except FileExistsError: pass
with open('test/test.txt', 'w') as f:
f.write('test')
try:
os.mkdir('test/test2/')
except FileExistsError: pass
try:
os.mkdir('test/test2/test3/')
except FileExistsError: pass
with open('test/test2/test-f.txt', 'w') as f:
f.write('test')
nuke._nuke('test')
self.assertFalse(os.path.exists('test/'))
2019-12-02 00:23:40 +00:00
def test_rename(self):
2019-12-02 09:04:17 +00:00
f = os.path.basename(get_file())
2019-12-02 00:23:40 +00:00
with open(f, "w") as test_f:
test_f.write('hello world')
2019-12-02 09:04:17 +00:00
with open(f, "r") as test_f:
self.assertEqual(test_f.read(), 'hello world')
2019-12-02 00:23:40 +00:00
new_name = nuke._rename_inode(f)
self.assertFalse(os.path.exists(f))
self.assertTrue(os.path.exists(new_name))
for char in os.path.basename(new_name):
if char != '0':
break
else:
raise ValueError('New inode name is all zeros in default mode')
self.assertEqual(len(os.path.basename(new_name)), len(f))
self.assertGreater(len(os.path.basename(new_name)), 0)
2019-12-02 09:04:17 +00:00
2019-12-02 00:23:40 +00:00
with open(new_name, "r") as test_f:
self.assertEqual(test_f.read(), 'hello world')
def test_rename_zeros(self):
2019-12-02 09:04:17 +00:00
f = os.path.basename(get_file())
2019-12-02 00:23:40 +00:00
with open(f, "w") as test_f:
test_f.write('hello world')
new_name = nuke._rename_inode(f, zeros=True)
for char in os.path.basename(new_name):
if char != '0':
raise ValueError('New inode name has non zero in zeros mode')
self.assertFalse(os.path.exists(f))
self.assertTrue(os.path.exists(new_name))
self.assertEqual(len(os.path.basename(new_name)), len(f))
self.assertGreater(len(os.path.basename(new_name)), 0)
with open(new_name, "r") as test_f:
self.assertEqual(test_f.read(), 'hello world')
2019-12-02 09:04:17 +00:00
try:
os.mkdir('testdata')
except FileExistsError:
pass
os.chdir('testdata')
unittest.main()
os.chdir('..')
shutil.rmtree('testdata')