-
Notifications
You must be signed in to change notification settings - Fork 1.2k
TestFoundation: change CWD to TMPDIR and restore previous CWD when test finishes #2606
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Does Termux sets a |
Yes, I've modified a libdispatch test before to use it, swiftlang/swift-corelibs-libdispatch@06e4da811.
Well, after this change, there's no hard-coded path used in Termux: I'm just not changing the working directory to /data/local/tmp on Termux. Moving to that directory surprisingly makes no difference to all subsequent tests but one, TestFileHandle.test_readToEndOfFileAndNotify_readError(), which I mentioned before causes TestFoundation to segfault in Termux, turns out because of this one prior line. I'm unsure exactly why this change of directory to /data/local/tmp was made before, so I just avoid it in Termux, by checking to see if the PREFIX variable is set and that it's the Termux app. I can check for some other environment variable or use TMPDIR instead if you let me know what exactly you need on your end, as on my end, I'm simply avoiding this unnecessary change of directory to /data/local/tmp. |
If |
As for |
I am not the original author of the changes, but it looks to me that As I tried to explain (badly) in my previous comment, I would love to not have "flavors" of Android in the code, so finding a solution that works for everyone would be good. My proposal was changing the current code for something like:
Which should work for people using ADB to test and for people coding directly in their devices (like Termux, but maybe also solutions like Crostini or GNURoot), and does not mention any of the flavors by name anywhere. Additionally, from the clue that a test in @spevans, since you are already here: #2521 should also avoid the environment of one test method from leaking into the next, and the problem that we are experiencing here. I didn't received any concern or thumbs up about merging that one. |
I was unsure if this was intentional for Android/adb or not, ie if it wasn't reverted because several other tests needed that directory change too on Android with adb. I'll look into whether this change is even necessary anymore, but I don't use adb, so you'll have to try any pull I come up with on Android/adb and let me know. |
If termux sets
I was thinking about how we use temporary directories in other tests and there is inconsistent setup code uses all over the place. I think in a future PR we could change it to something like: Add the following into
and then each test that needs a temporary directory can simply do:
|
I went further and just dropped the path change completely, as that was part of a really old pull, #1189, and may not be needed anymore. If I keep it and simply change the setup for TestFileHandle instead, so that it doesn't try to open a file handle to the working directory, ie /data/local/tmp:
then the tests all pass for me again, even though I can't write to /data/local/tmp from Termux.
Sounds like a good idea, but there's only two other tests where the current directory is changed to a valid path, the one Daniel mentioned above that is reset and this seemingly spurious one in @drodriguez, let me know if this pull now causes any problems for you in Android with adb, and if the pasted patch above or other patches are needed. By simply removing this directory change, it should shake out what other tests were inadvertently relying on it in Android/adb. |
@buttaface So are you saying the tests pass on termux with this PR (which removes the chdir) and the above patch which uses I dont see an issue with changing the filehandle test as it still opening a directory so future reads fail. So just to confirm, under Termux, |
I'm saying the tests pass with either this pull alone or the TestFileHandle change alone in Termux, or obviously with both. But I'm unsure what would be needed in Android/adb after this pull, so Daniel will need to tell us.
Yes and yes. |
I will need to check again, but I am afraid that the current working directory for a command line invocation (like The problem with the current working directory being One can check that this also happens in other Unix systems by I am afraid that the trick needs to stay for most adb Android setups (and hopefully it doesn't affect a lot other Android setups like Termux). |
Are you sure? At least in the case of that Python script, it runs executables in /data/local/tmp, so that should be the working directory already.
If you're worried about it possibly being / under some other Android/adb invocations, I can change it to Let me know what you prefer. |
Yes, the executables are created in subdirectories of
If you don’t mind doing the necessary changes, I would prefer that solution, and I would be glad to review. |
Done. |
TestFoundation/TestURL.swift
Outdated
@@ -396,6 +396,8 @@ class TestURL : XCTestCase { | |||
let lengthOfRelativePath = Int(strlen(TestURL.gFileDoesNotExistName)) | |||
let relativePath = fileSystemRep.advanced(by: Int(TestURL.gRelativeOffsetFromBaseCurrentWorkingDirectory)) | |||
XCTAssertTrue(strncmp(TestURL.gFileDoesNotExistName, relativePath, lengthOfRelativePath) == 0, "fileSystemRepresentation of file path is wrong") | |||
|
|||
FileManager.default.changeCurrentDirectoryPath(TestURL.gSavedPath) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not necessary since it's reset in the next test, but keeps these tests independent so if someone shuffles the order of the tests someday, that won't matter.
I would recommend putting the added lines into a @swift-ci please test Linux platform |
Use NSTemporaryDirectory() instead and reset the working directory when done.
Done, CI failed at git checkout because of unrelated ICU issue. |
@swift-ci test linux |
@swift-ci please test Linux platform |
Updated the title and the content to reflect better the final solution. Merging. |
The working directory was changed to /data/local/tmp on Android assuming that adb is being used, but this isn't necessary in the Termux app. Additionally the working directory was not reset back, so later tests were affected by the change.
Along with the still-unmerged #2145, this finally gets all the TestFoundation tests passing for me on Android.
Pinging @drodriguez, who did a lot of this Android porting work.