Skip to content

Run Tests Without Dir.chdir - #52619

Merged
Hamms merged 1 commit into
stagingfrom
run-tests-without-dir-chdir
Jul 6, 2023
Merged

Run Tests Without Dir.chdir#52619
Hamms merged 1 commit into
stagingfrom
run-tests-without-dir-chdir

Conversation

@Hamms

@Hamms Hamms commented Jul 5, 2023

Copy link
Copy Markdown
Contributor

Dir.chdir is inherently thread-unsafe, which is a problem for us because the two rake tasks we have which use it also get run in parallel threads:

# Run the eyes tests and ui test suites in parallel. If one of these suites
# raises, allow the other suite to complete, then make sure this task raises.
timed_task_with_logging :ui_all do
Parallel.each([:eyes_ui, :regular_ui], in_threads: 3) do |target|
Rake::Task["test:#{target}"].invoke
end
end

This hasn't harmed us so far because although one of the two calls to Dir.chdir will "override" the other, they're both targeting the same directory so in practice the overridden one will continue to work just fine.

However, Ruby starting in 3.0 will be much more proactive about preventing the user from making this call in the first place and the test:ui_all Rake task will begin failing in that version with conflicting chdir during another chdir block. Fortunately, we don't actually need to change the working directory at the Ruby level, we just want to change the directory from which the child process we're kicking off is executed, which we can do with a simple cd in the child process itself.

I recommend reviewing with whitespace changes disabled

Links

Testing story

Verified locally that with this change (plus a tweak to have each test suite run only a single arbitrarily-chosen test rather than the entire test suite), we are successfully able to run bundle exec rake test:ui_all on Ruby 3.0

`Dir.chdir` is inherently thread-unsafe, which is a problem for us because the two rake tasks we have which use it also get run in parallel threads: https://github.com/code-dot-org/code-dot-org/blob/e0aa3b37133f99a70f7ff0dffaad9e93da35e286/lib/rake/test.rake#L88-L94

This hasn't harmed us so far because although the two calls to `Dir.chdir` will overwrite one another, they're both in practice targeting the same directory so the "overwritten" one will continue to work just fine. However, Ruby starting in 3.0 will be much more proactive about preventing the user from making this call in the first place and the `test:ui_all` Rake task will begin failing in that version with `conflicting chdir during another chdir block`.

Fortunately, we don't actually need to change the working directory at the *Ruby* level, we just want to change the directory from which the child process we're kicking off is executed, which we can do with a simple `cd` in the child process itself.

- https://coderscat.com/ruby-change-current-working-directory/
- https://bugs.ruby-lang.org/issues/9785
- https://bugs.ruby-lang.org/issues/15661
@Hamms Hamms added the Ruby Update Everything related to work to update the version of Ruby our codebase runs on label Jul 5, 2023
@Hamms
Hamms marked this pull request as ready for review July 5, 2023 20:48
@Hamms
Hamms requested review from a team July 5, 2023 20:48
@Hamms
Hamms merged commit 016f982 into staging Jul 6, 2023
@Hamms
Hamms deleted the run-tests-without-dir-chdir branch July 6, 2023 18:55
Hamms added a commit that referenced this pull request Jul 6, 2023
Hamms added a commit that referenced this pull request Jul 6, 2023
Hamms added a commit that referenced this pull request Jul 6, 2023
Hamms added a commit that referenced this pull request Jul 7, 2023
* Revert "Revert "Run Tests Without `Dir.chdir` (#52619)" (#52659)"

This reverts commit bcf2d0d.

* find features from the test/ui directory so we get relative rather than absolute pathnames
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Ruby Update Everything related to work to update the version of Ruby our codebase runs on

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants