ninech / netbox-client-ruby

A ruby client library for Netbox v2.
MIT License
24 stars 21 forks source link

Reset config changes before each test #64

Closed rossta closed 11 months ago

rossta commented 11 months ago

Problem

While running tests locally with Ruby 3+, the logger sometimes outputs to $stdout which creates noisy test output.

To reproduce, run tests on Ruby 3.2 with bundle exec rspec --seed 27884:

$ be rspec --seed 27884

.............................................................................................................................................................................I, [2023-09-27T21:55:31.949858 #79021]  INFO -- request: GET http://netbox.test/api/virtualization/clusters.json?limit=42
I, [2023-09-27T21:55:31.949909 #79021]  INFO -- request: Authorization: "Token this-is-the-test-token"
User-Agent: "Faraday v1.10.3"
...I, [2023-09-27T21:55:31.957737 #79021]  INFO -- request: GET http://netbox.test/api/virtualization/cluster-types/1.json
I, [2023-09-27T21:55:31.957763 #79021]  INFO -- request: Authorization: "Token this-is-the-test-token"
User-Agent: "Faraday v1.10.3"
....I, [2023-09-27T21:55:31.964715 #79021]  INFO -- request: GET http://netbox.test/api/virtualization/virtual-machines.json?limit=42
I, [2023-09-27T21:55:31.964739 #79021]  INFO -- request: Authorization: "Token this-is-the-test-token"
User-Agent: "Faraday v1.10.3"
....

(etc)

The issue does not occur when tests are run on Ruby 3.2 with bundle exec rspec --seed 61562.

Solution

The root cause is from modifying the global configuration via NetboxClientRuby.configure in connection_spec.rb and netbox_spec.rb. These tests set a "logger" which bleeds over to other tests that are run after. Other configuration settings bleed over as well though their effect isn't observed.

The fix here is to reset all config changes before each test using the testing interface provided by Dry::Configurable. This change ensures that global configuration modifications from one test don't bleed into other tests.

https://dry-rb.org/gems/dry-configurable/1.0/testing/

thde commented 11 months ago

Thanks @rossta for the contribution!