Python Forum
Remove function and unit test - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Remove function and unit test (/thread-23584.html)



Remove function and unit test - ftg - Jan-07-2020

Hello everyone,

let's say I want to create a 'utils' package with several functions
working on files and folders. I want to do it once properly so I can use
them in all my personal projects without feeling to re invent the wheel
each time.
I also want to learn 2 items by the way:
- exception handling, already read a lot about EAFP vs LBYL, I think the
following function suits well to EAFP.
- unit tests with pytest tool, it seems simple enough to start with.

My function is:

def removeanything(src):
			"""
			remove files or folders
			"""
			try:
				os.remove(src)
				print('File is removed')
			except IsADirectoryError:
				shutil.rmtree(src)
				print('Folder is removed')
			except FileNotFoundError:
				print('File/folder is not existing')
			except PermissionError:
				print('Not allowed to suppress this file')


Regarding the function it self:
do you think it is worth handling each possible exception or should I just
handle IsADirectoryError for folder removing and all the other one in a unique
one?
What would be the interest to raise an exception in that case to be caught by the caller?
Are there some exceptions I didn't handle and that could possibly occur?

Regarding the testing:
I need to test whether a file or a folder is well removed and the exception are
well handled:
- file exists and permissions are ok => file is removed
- folder exists and permissions are ok => folder is removed
- neither file or folder exist => FileNOtFoundError
- permissions issues => PermissionError
My main concern is to create the conditions of the test (files/folders to be
removed with good permissions) and to remove them in a clean way once test is over
or has been interrupted.
What is the skeletton of those steps?

Sorry for this long post.

Thanks by adavnce for your answers.


RE: Remove function and unit test - ndc85430 - Jan-07-2020

I'm glad you're looking into unit testing. I've not used pytest, but I suspect it has functionality to mark a function to be run before tests and after tests so you can do the setting up and clearing up in that way (it's common to want to do that, so I'd be surprised if pytest didn't have such facilities).

As for creating the files and folders in the first place, take a look at this - the tempfile module has facilities to create temporary files. If you're on Unix, then look at the os module (not listed in the link because its scope is more general than just the file system) which has a chmod function for setting permissions (I don't know what one would do on Windows I'm afraid!).


RE: Remove function and unit test - Gribouillis - Jan-07-2020

If you are in linux, you can very easily create a ram disk where you would store the temporary files and directories needed for the tests. It would make the tests run much faster and also avoid unnecessary disk accesses. Here is a recipe to create a ramdisk of size 512MB
Output:
mkdir /media/ramdisk chmod 777 /media/ramdisk sudo mount -t tmpfs -o size=512M tmpfs /media/ramdisk
It can also be automounted at boot time by adding this line to /etc/fstab
Output:
none /media/ramdisk tmpfs nodev,nosuid,noexec,nodiratime,size=512M 0 0
Then create the necessary files and directories for the tests as subdirectories of /media/ramdisk


RE: Remove function and unit test - ftg - Jan-07-2020

Thanks for the answer.

I am indeed under linux OS.
Your solution means an additional step in order to drive the tests.
Intuitively, I would say that I prefer the test to remain "under" python control.

By the way if I use tempfile.TemporaryFile context manager, will I be able to remove the files with my function during all the time it is activated?


RE: Remove function and unit test - ftg - Jan-07-2020

Btw I have found this feature from pytest:
https://pytest.org/en/latest/tmpdir.html#tmpdir-handling


RE: Remove function and unit test - ndc85430 - Jan-07-2020

(Jan-07-2020, 12:54 PM)ftg Wrote: Intuitively, I would say that I prefer the test to remain "under" python control.

I think that's a good approach. Having the extra step somewhere else means it's a bit hidden and it's easy to forget about it.

Quote:By the way if I use tempfile.TemporaryFile context manager, will I be able to remove the files with my function during all the time it is activated?

I'm not sure what you mean really. Even without the context manager, you can create and remove the files in the set up/tear down steps.