Replies: 1 comment 6 replies
-
@drewj-tp The import armi
armi.configure(armi.apps.App())
from armi.conftest import bootstrapArmiTestEnv
bootstrapArmiTestEnv()
from armi.utils.directoryChangers import TemporaryDirectoryChanger
with TemporaryDirectoryChanger() as td:
passOr, obviously, you could just copy/paste your code directly into a unit test. As you pointed out, there are hundreds of examples of that code you wrote working exactly as-is inside a unit test: armi/armi/cases/tests/test_cases.py Line 91 in e38fa1a armi/armi/cases/tests/test_cases.py Lines 122 to 123 in e38fa1a armi/armi/cases/tests/test_cases.py Lines 165 to 166 in e38fa1a So, you started your statement by saying:
I posit that if you only ever use the |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I want to rely on the
TemporaryDirectoryChangerfor testing because it's a great encapsulation forBut I frequently hit some snags that make it a frustrating experience. This is my attempt to document them, get some more guidance, and maybe talk about how to improve the functionality.
All the commands below are in a fresh ipython environment, no configured application, and with armi e38fa1a
My expectations / debugging process
I expect that I can use the class like
and directory changer will do its magic. There are no required arguments to the class so I'm led to believe this should work.
But this fails with
Which is immediately odd. If the destruction of the temporary directory is also trying to remove the parent directory, that's definitely bad. But if the directory changer is managing the creation of a temporary directory, I expect that it should always be able to delete it.
(aside) After looking at the code base, you can infer that
rootin this case is my present working directory if the fast path is not activated. Definitely don't want to delete that. But the error doesn't tell me whatrootis here, so I usually assume it made a temporary directory that was not safe to delete. Which, huh?Next step usually involves providing some paths. What about my current working directory.
But, why? If it's trying to remove
C:\Users\anjohnson\codes\armi, yeah definitely don't do that. Buttemp-wkzeRrM5D2should be fair game for deletion.Okay what about the fast path?
Code base indicates the initial fast path is just your current directory
armi/armi/context.py
Line 139 in e38fa1a
until someone activates it. Usually the Operator
armi/armi/operators/operator.py
Lines 257 to 272 in e38fa1a
That one works.
Working use cases
It seems the most reliable way to use the temporary directory change is to pass the activated fast path. If that is correct and the intended workflow, I would recommend we add that to the
TemporaryDirectoryChangerdocstring.But then I look at ARMI's testing, and I see calls like
armi/armi/nuclearDataIO/cccc/tests/test_fixsrc.py
Line 36 in e38fa1a
Where the temp dir is created without arguments. And, without arguments, it uses the fast path
armi/armi/utils/directoryChangers.py
Lines 278 to 279 in e38fa1a
I presume this works because the fast path is configured at the start of testing
armi/armi/conftest.py
Lines 63 to 69 in e38fa1a
This would be good guidance to include in the temporary directory changer docstring.
Recommendations
These recommendations are predicated on my understanding of what ARMI is doing, and if what I'm trying to do is correct. I can make a PR with these recommendations, but I want to make sure they're correct.
rootandcontext.getFastPathin the constructor for theTemporaryDirectoryChangerTemporaryDirectoryChanger.__exit__in aRaisessection to provide guidance on why these errors may be raised and how to mitigate them.pathTools.cleanPathcall errors to explain why the error and/or how to get around the error.Exception: You tried to delete C:\Users\anjohnson\codes\armi\temp-b7yYigWnFV, but it does not seem safe to do so.doesn't help with mitigation. Especially when I'm trying to delete that temp directory and it should be safe to do so. Why is it not safe?ValueError: Temporary directory not in a safe location for deletion.message to aid in debugging.Extensions
I think it's reasonable to expect
TemporaryDirectoryChanger()to work always, even if the fast path is not activated. Python has thetempfilemodule for making temporary files and directories.I think it's reasonable to expect
TemporaryDirectoryChanger(cwd)to work always. I have asked to make a temporary directory in my current working directory. That temporary directory should be fair game for deletion. Same for any base directory that I have the ability to make a directory and write files.I think it's reasonable for the
TemporaryDirectoryChanger, upon failing to remove the temporary directory, to log a warning rather than raise a fatal error. So long as we've returned the caller to their original working directory, does it really matter ARMI did not remove the temporary files? If werunLog.warningto alert to the un-removed temporary directory, the user can still clean them up after the fact. Alternatively, it can document what type of exception (other than a baseException) it would raise and allow clients totry/exceptaround this behavior.Beta Was this translation helpful? Give feedback.
All reactions