2022-05-09 11:45:50 -04:00
# Testing Ruby
2024-12-12 20:55:23 -05:00
All the commands below assume that you're running them from the `build/` directory made during [Building Ruby ](building_ruby.md ).
Most commands below should work with [GNU make ](https://www.gnu.org/software/make/ ) (the default on Linux and macOS), [BSD make ](https://man.freebsd.org/cgi/man.cgi?make(1 )) and [NMAKE ](https://learn.microsoft.com/en-us/cpp/build/reference/nmake-reference ), except where indicated otherwise.
2022-05-09 11:45:50 -04:00
## Test suites
There are several test suites in the Ruby codebase:
We can run any of the make scripts [in parallel ](building_ruby.md#label-Running+make+scripts+in+parallel ) to speed them up.
1. [bootstraptest/ ](https://github.com/ruby/ruby/tree/master/bootstraptest )
2024-12-12 20:55:23 -05:00
This is a small test suite that runs on [Miniruby ](building_ruby.md#label-Miniruby+vs+Ruby ). We can run it with:
2022-05-09 11:45:50 -04:00
2024-12-12 20:49:45 -05:00
```sh
2022-05-09 11:45:50 -04:00
make btest
```
2024-12-12 20:55:23 -05:00
To run individual bootstrap tests, we can either specify a list of filenames or use the `--sets` flag in the variable `BTESTS` :
2022-05-09 11:45:50 -04:00
2024-12-12 20:49:45 -05:00
```sh
2024-12-12 20:55:23 -05:00
make btest BTESTS="../bootstraptest/test_string.rb ../bootstraptest/test_class.rb"
make btest BTESTS="--sets=string,class"
2022-05-09 11:45:50 -04:00
```
2024-12-12 20:55:23 -05:00
To run these tests with verbose logging, we can add `-v` to the `OPTS` :
2022-07-27 13:18:25 +01:00
2024-12-12 20:49:45 -05:00
```sh
2024-12-12 20:55:23 -05:00
make btest OPTS="--sets=string,class -v"
2022-07-27 13:18:25 +01:00
```
2022-05-09 11:45:50 -04:00
If we want to run the bootstrap test suite on Ruby (not Miniruby), we can use:
2024-12-12 20:49:45 -05:00
```sh
2022-05-09 11:45:50 -04:00
make test
```
2024-12-12 20:55:23 -05:00
To run these tests with verbose logging, we can add `-v` to the `OPTS` :
2022-05-09 11:45:50 -04:00
2024-12-12 20:49:45 -05:00
```sh
2022-05-09 11:45:50 -04:00
make test OPTS=-v
```
2024-12-12 20:55:23 -05:00
(GNU make only) To run a specific file, we can use:
2022-05-30 23:50:39 -04:00
2024-12-12 20:49:45 -05:00
```sh
2024-12-12 20:55:23 -05:00
make ../test/ruby/test_string.rb
2022-05-30 23:50:39 -04:00
```
2024-12-12 20:55:23 -05:00
You can use the `-n` test option to run a specific test with a regex:
2022-05-09 11:45:50 -04:00
2024-12-12 20:49:45 -05:00
```sh
2024-12-12 20:55:23 -05:00
make ../test/ruby/test_string.rb TESTOPTS="-n /test_.*_to_s/"
2022-05-09 11:45:50 -04:00
```
2024-12-12 20:55:23 -05:00
2. [test/ ](https://github.com/ruby/ruby/tree/master/test )
This is a more comprehensive test suite that runs on Ruby. We can run it with:
2022-05-30 23:50:39 -04:00
2024-12-12 20:49:45 -05:00
```sh
2024-12-12 20:55:23 -05:00
make test-all
2022-05-30 23:50:39 -04:00
```
2024-12-12 20:55:23 -05:00
We can run a specific test file or directory in this suite using the `TESTS` option, for example:
2022-05-09 11:45:50 -04:00
2024-12-12 20:49:45 -05:00
```sh
2024-12-12 20:55:23 -05:00
make test-all TESTS="../test/ruby/"
make test-all TESTS="../test/ruby/test_string.rb"
2022-05-09 11:45:50 -04:00
```
2022-05-30 23:50:39 -04:00
We can run a specific test in this suite using the `TESTS` option, specifying
2022-05-09 11:45:50 -04:00
first the file name, and then the test name, prefixed with `--name` . For example:
2024-12-12 20:49:45 -05:00
```sh
2024-12-12 20:55:23 -05:00
make test-all TESTS="../test/ruby/test_string.rb --name=TestString#test_to_s "
2022-05-09 11:45:50 -04:00
```
2024-12-12 20:55:23 -05:00
To run these tests with verbose logging, we can add `-v` to `TESTS` :
2022-05-09 11:45:50 -04:00
2024-12-12 20:49:45 -05:00
```sh
2022-05-09 11:45:50 -04:00
make test-all TESTS=-v
```
2023-05-25 17:57:52 +02:00
We can display the help of the `TESTS` option:
2024-12-12 20:49:45 -05:00
```sh
2023-10-13 16:54:57 +02:00
make test-all TESTS=--help
2023-05-25 17:57:52 +02:00
```
2024-12-12 20:55:23 -05:00
We can run all the tests in `test/` , `bootstraptest/` and `spec/` (the `spec/` is explained in a later section) all together with:
2022-05-09 11:45:50 -04:00
2024-12-12 20:49:45 -05:00
```sh
2022-05-09 11:45:50 -04:00
make check
```
3. [spec/ruby ](https://github.com/ruby/ruby/tree/master/spec/ruby )
2024-12-12 20:55:23 -05:00
This is a test suite defined in [the Ruby spec repository ](https://github.com/ruby/spec ), and is periodically mirrored into the `spec/ruby` directory of this repository. It tests the behavior of the Ruby programming language. We can run this using:
2022-05-09 11:45:50 -04:00
2024-12-12 20:49:45 -05:00
```sh
2022-05-09 11:45:50 -04:00
make test-spec
```
2024-12-12 20:55:23 -05:00
We can run a specific test file or directory in this suite using the `SPECOPTS` option, for example:
2022-05-09 11:45:50 -04:00
2024-12-12 20:49:45 -05:00
```sh
2024-12-12 20:55:23 -05:00
make test-spec SPECOPTS="../spec/ruby/core/string/"
make test-spec SPECOPTS="../spec/ruby/core/string/to_s_spec.rb"
2022-05-09 11:45:50 -04:00
```
To run a specific test, we can use the `--example` flag to match against the test name:
2024-12-12 20:49:45 -05:00
```sh
2024-12-12 20:55:23 -05:00
make test-spec SPECOPTS="../spec/ruby/core/string/to_s_spec.rb --example='returns self when self.class == String'"
2022-05-09 11:45:50 -04:00
```
2024-12-12 20:55:23 -05:00
To run these specs with verbose logging, we can add `-v` to the `SPECOPTS` :
2022-05-09 11:45:50 -04:00
2024-12-12 20:49:45 -05:00
```sh
2024-12-12 20:55:23 -05:00
make test-spec SPECOPTS="../spec/ruby/core/string/to_s_spec.rb -Vfs"
2022-05-09 11:45:50 -04:00
```
2024-12-12 20:55:23 -05:00
(GNU make only) To run a ruby-spec file or directory, we can use
2022-05-30 23:50:39 -04:00
2024-12-12 20:49:45 -05:00
```sh
2024-12-12 20:55:23 -05:00
make ../spec/ruby/core/string/to_s_spec.rb
2022-05-30 23:50:39 -04:00
```
2022-05-09 11:45:50 -04:00
4. [spec/bundler ](https://github.com/ruby/ruby/tree/master/spec/bundler )
2024-12-12 20:55:23 -05:00
The bundler test suite is defined in [the RubyGems repository ](https://github.com/rubygems/rubygems/tree/master/bundler/spec ), and is periodically mirrored into the `spec/ruby` directory of this repository. We can run this using:
2022-05-09 11:45:50 -04:00
2024-12-12 20:49:45 -05:00
```sh
2022-05-09 11:45:50 -04:00
make test-bundler
```
2022-05-30 23:50:39 -04:00
To run a specific bundler spec file, we can use `BUNDLER_SPECS` as follows:
2024-12-12 20:49:45 -05:00
```sh
2023-10-13 16:54:57 +02:00
make test-bundler BUNDLER_SPECS=commands/exec_spec.rb
2022-05-30 23:50:39 -04:00
```
2023-10-09 17:05:51 +02:00
## Troubleshooting
### Running test suites on s390x CPU Architecture
If we see failing tests related to the zlib library on s390x CPU architecture, we can run the test suites with `DFLTCC=0` to pass:
2024-12-12 20:49:45 -05:00
```sh
2023-10-09 17:05:51 +02:00
DFLTCC=0 make check
```
The failures can happen with the zlib library applying the patch [madler/zlib#410 ](https://github.com/madler/zlib/pull/410 ) to enable the deflate algorithm producing a different compressed byte stream. We manage this issue at [[ruby-core:114942][Bug #19909 ]](https://bugs.ruby-lang.org/issues/19909).