I’m writing a unit test, and I’m trying to find a way to test whether my class has properly closed a file handle (it uses old-style open() and close()). Ideally I’d like to be able to do it without having access to the actual handle – I want to be able to check a file in the file system and determine whether it is currently open elsewhere.
I’ve tried doing it through exclusive file locks, but I haven’t had much luck. Besides, file locks are very much not cross-platform (this needs to work on Solaris, Linux and Windows).
Any suggestions?
It’s not a proper unit-test if you’re relying on opening \ closing files like this – such tests can randomly fail for a variety of reasons, leading to false positives and general confusion.
Instead, consider abstracting away the file operations and ensuring that close is called on your representation of the file.