Skip to content
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

Minitest/GlobalExpectations triggers google-cloud-storage unit test failures #68

Closed
yahonda opened this issue Mar 15, 2020 · 2 comments
Closed

Comments

@yahonda
Copy link
Contributor

yahonda commented Mar 15, 2020

Follow up #65 (comment)


Expected behavior

Minitest/GlobalExpectations auto-correction does not cause unit test failures.

Actual behavior

Minitest/GlobalExpectations auto-correct causes two google-cloud-storage unit test failures.

% bundle exec rubocop --only Minitest/GlobalExpectations -a test/google/cloud/storage/project_anonymous_test.rb

Inspecting 1 file
C

Offenses:

test/google/cloud/storage/project_anonymous_test.rb:25:7: C: [Corrected] Minitest/GlobalExpectations: Prefer using _(args.first).must_be_nil.
      args.first.must_be_nil # project
      ^^^^^^^^^^^^^^^^^^^^^^
test/google/cloud/storage/project_anonymous_test.rb:36:7: C: [Corrected] Minitest/GlobalExpectations: Prefer using _(args.first).must_be_nil.
      args.first.must_be_nil # project
      ^^^^^^^^^^^^^^^^^^^^^^
test/google/cloud/storage/project_anonymous_test.rb:56:5: C: [Corrected] Minitest/GlobalExpectations: Prefer using _(bucket.name).must_equal bucket_name.
    bucket.name.must_equal bucket_name
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
test/google/cloud/storage/project_anonymous_test.rb:74:5: C: [Corrected] Minitest/GlobalExpectations: Prefer using _(files.size).must_equal num_files.
    files.size.must_equal num_files
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
test/google/cloud/storage/project_anonymous_test.rb:77:7: C: [Corrected] Minitest/GlobalExpectations: Prefer using _(file.user_project).must_be :nil?.
      file.user_project.must_be :nil?
      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
test/google/cloud/storage/project_anonymous_test.rb:96:5: C: [Corrected] Minitest/GlobalExpectations: Prefer using _(file.name).must_equal file_name.
    file.name.must_equal file_name
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
test/google/cloud/storage/project_anonymous_test.rb:97:5: C: [Corrected] Minitest/GlobalExpectations: Prefer using _(file.user_project).must_be :nil?.
    file.user_project.must_be :nil?
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

1 file inspected, 7 offenses detected, 7 offenses corrected
% bundle exec ruby -w -Itest test/google/cloud/storage/project_anonymous_test.rb                                                                                                         diag_rubocop_minitest_args
/Users/yahonda/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/google-api-client-0.37.2/lib/google/apis/core/api_command.rb:132: warning: assigned but unused variable - a
/Users/yahonda/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/google-api-client-0.37.2/lib/google/apis/core/batch.rb:171: warning: assigned but unused variable - length
/Users/yahonda/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/httpclient-2.8.3/lib/httpclient/ssl_config.rb:370: warning: assigned but unused variable - pathlen
/Users/yahonda/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/httpclient-2.8.3/lib/httpclient/ssl_config.rb:51: warning: method redefined; discarding old initialize
/Users/yahonda/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/httpclient-2.8.3/lib/httpclient/ssl_config.rb:58: warning: method redefined; discarding old add_cert
/Users/yahonda/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/httpclient-2.8.3/lib/httpclient/ssl_config.rb:58: warning: method redefined; discarding old add_file
/Users/yahonda/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/httpclient-2.8.3/lib/httpclient/ssl_config.rb:58: warning: method redefined; discarding old add_path
/Users/yahonda/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/google-api-client-0.37.2/lib/google/apis/core/base_service.rb:215: warning: method redefined; discarding old client
Run options: --seed 24803

# Running:

FDEPRECATED: global use of wont_be from test/google/cloud/storage/project_anonymous_test.rb:57. Use _(obj).wont_be instead. This will fail in Minitest 6.
/Users/yahonda/Desktop/google-cloud-ruby/google-cloud-storage/lib/google/cloud/storage/bucket.rb:2210: warning: instance variable @lazy not initialized
.DEPRECATED: global use of must_be_kind_of from test/google/cloud/storage/project_anonymous_test.rb:76. Use _(obj).must_be_kind_of instead. This will fail in Minitest 6.
DEPRECATED: global use of must_be_kind_of from test/google/cloud/storage/project_anonymous_test.rb:76. Use _(obj).must_be_kind_of instead. This will fail in Minitest 6.
DEPRECATED: global use of must_be_kind_of from test/google/cloud/storage/project_anonymous_test.rb:76. Use _(obj).must_be_kind_of instead. This will fail in Minitest 6.
.FDEPRECATED: global use of wont_be from test/google/cloud/storage/project_anonymous_test.rb:98. Use _(obj).wont_be instead. This will fail in Minitest 6.
/Users/yahonda/Desktop/google-cloud-ruby/google-cloud-storage/lib/google/cloud/storage/file.rb:1722: warning: instance variable @lazy not initialized
.DEPRECATED: global use of must_be_kind_of from /Users/yahonda/.rbenv/versions/2.6.5/lib/ruby/2.6.0/delegate.rb:83. Use _(obj).must_be_kind_of instead. This will fail in Minitest 6.
.

Finished in 0.025759s, 232.9283 runs/s, 582.3207 assertions/s.

  1) Failure:
Google::Cloud::Storage::Project::anonymous::mock_storage#test_0001_raises when creating a bucket without authentication [test/google/cloud/storage/project_anonymous_test.rb:30]:
[Google::Cloud::UnauthenticatedError] exception expected, not
Class: <NoMethodError>
Message: <"undefined method `_' for #<Object:0x00007fb26e827788>">
---Backtrace---
test/google/cloud/storage/project_anonymous_test.rb:25:in `insert_bucket'
/Users/yahonda/Desktop/google-cloud-ruby/google-cloud-storage/lib/google/cloud/storage/service.rb:98:in `block in insert_bucket'
/Users/yahonda/Desktop/google-cloud-ruby/google-cloud-storage/lib/google/cloud/storage/service.rb:631:in `execute'
/Users/yahonda/Desktop/google-cloud-ruby/google-cloud-storage/lib/google/cloud/storage/service.rb:97:in `insert_bucket'
/Users/yahonda/Desktop/google-cloud-ruby/google-cloud-storage/lib/google/cloud/storage/project.rb:379:in `create_bucket'
test/google/cloud/storage/project_anonymous_test.rb:30:in `block (3 levels) in <main>'
---------------

  2) Failure:
Google::Cloud::Storage::Project::anonymous::mock_storage#test_0002_raises when listing buckets without authentication [test/google/cloud/storage/project_anonymous_test.rb:41]:
[Google::Cloud::UnauthenticatedError] exception expected, not
Class: <NoMethodError>
Message: <"undefined method `_' for #<Object:0x00007fb26e91c120>">
---Backtrace---
test/google/cloud/storage/project_anonymous_test.rb:36:in `list_buckets'
/Users/yahonda/Desktop/google-cloud-ruby/google-cloud-storage/lib/google/cloud/storage/service.rb:76:in `block in list_buckets'
/Users/yahonda/Desktop/google-cloud-ruby/google-cloud-storage/lib/google/cloud/storage/service.rb:631:in `execute'
/Users/yahonda/Desktop/google-cloud-ruby/google-cloud-storage/lib/google/cloud/storage/service.rb:75:in `list_buckets'
/Users/yahonda/Desktop/google-cloud-ruby/google-cloud-storage/lib/google/cloud/storage/project.rb:153:in `buckets'
test/google/cloud/storage/project_anonymous_test.rb:41:in `block (3 levels) in <main>'
---------------

6 runs, 15 assertions, 2 failures, 0 errors, 0 skips
%

Steps to reproduce the problem

rbenv install 2.6.5 ; rbenv global 2.6.5 # or whatever use Ruby 2.6 because google-cloud-storage gem does not support Ruby 2.7 yet
gem install bundler
git clone -b diag_rubocop_minitest_args https://github.com/yahonda/google-cloud-ruby
cd google-cloud-ruby/google-cloud-storage/
bundle install
bundle exec ruby -w -Itest test/google/cloud/storage/project_anonymous_test.rb # to confirm this test passes before auto-correct
bundle exec rubocop --only Minitest/GlobalExpectations -a test/google/cloud/storage/project_anonymous_test.rb
bundle exec ruby -w -Itest test/google/cloud/storage/project_anonymous_test.rb

RuboCop version

$ [bundle exec] rubocop -V
0.74.0 (using Parser 2.6.4.0, running on ruby 2.6.4 x86_64-darwin17)

I understand this is not the latest version of RuboCop, which is restricted by https://github.com/googleapis/ruby-style/blob/master/google-style.gemspec#L31

@koic
Copy link
Member

koic commented Mar 15, 2020

First of all, Thanks for your feedback!

As far as I've investigated, I think Minitest warnings cannot be fixed with simple substitutions.
The _ method defined by Minitest::Spec::DSL::InstanceMethods, and Object does not defined the method.

   it "raises when creating a bucket without authentication" do
+    p self.class.to_s #=> "Google::Cloud::Storage::Project::anonymous::mock_storage"
+    p self.respond_to?(:_) #=> true
+    p self.method(:_).owner #=> Minitest::Spec::DSL::InstanceMethods
     stub = Object.new
     def stub.insert_bucket *args
+      p self.class.to_s #=> "Object"
+      p self.respond_to?(:_) #=> false
       args.first.must_be_nil # project
       raise Google::Apis::AuthorizationError.new("unauthorized", status_code: 401)
     end

There are several possible approaches for the issue.

  • Change minitest usage for google-cloud-storage (Suggestion for google-gould-storage's code)
  • Make an Object instance respond to _ for Minitest (Suggestion for google-gould-storage or Minitest?)

I think the former is realistic.
Here is a simple quick fixes, but there is probably a better clean fix another approach.

diff --git a/google-cloud-storage/test/google/cloud/storage/project_anonymous_test.rb b/google-cloud-storage/test/google/cloud/storage/project_anonymous_test.rb
index 2767f361f..5fe3c3e35 100644
--- a/google-cloud-storage/test/google/cloud/storage/project_anonymous_test.rb
+++ b/google-cloud-storage/test/google/cloud/storage/project_anonymous_test.rb
@@ -20,7 +20,7 @@ describe Google::Cloud::Storage::Project, :anonymous, :mock_storage do
   let(:anonymous_storage) { Google::Cloud::Storage.anonymous }

   it "raises when creating a bucket without authentication" do
-    stub = Object.new
+    stub = self
     def stub.insert_bucket *args
       _(args.first).must_be_nil # project
       raise Google::Apis::AuthorizationError.new("unauthorized", status_code: 401)
@@ -31,7 +31,7 @@ describe Google::Cloud::Storage::Project, :anonymous, :mock_storage do
   end

   it "raises when listing buckets without authentication" do
-    stub = Object.new
+    stub = self
     def stub.list_buckets *args
       _(args.first).must_be_nil # project
       raise Google::Apis::AuthorizationError.new("unauthorized", status_code: 401)

Either way, I think it's difficult to solve with RuboCop Minitest. Thank you.

@yahonda
Copy link
Contributor Author

yahonda commented Mar 15, 2020

Thanks for the deep investigation. Agreed that these two failures should be addressed by google-cloud-storage gem or minitest.

@yahonda yahonda closed this as completed Mar 15, 2020
yahonda added a commit to yahonda/google-cloud-ruby that referenced this issue Apr 12, 2020
…/storage/project_anonymous_test.rb`

Because object of `Object` class does not have `_` method defined
It is owned by `Minitest::Spec::DSL::InstanceMethods`.

Refer rubocop/rubocop-minitest#68 (comment)

```
% bundle exec ruby -w -Itest test/google/cloud/storage/project_anonymous_test.rb -n /test_0001_raises\|test_0002_raises/
... snip ...

FF

Finished in 0.002656s, 753.0120 runs/s, 753.0120 assertions/s.

  1) Failure:
Google::Cloud::Storage::Project::anonymous::mock_storage#test_0002_raises when listing buckets without authentication [test/google/cloud/storage/project_anonymous_test.rb:41]:
[Google::Cloud::UnauthenticatedError] exception expected, not
Class: <NoMethodError>
Message: <"undefined method `_' for #<Object:0x00007fae26010460>">
---Backtrace---
test/google/cloud/storage/project_anonymous_test.rb:36:in `list_buckets'
/Users/yahonda/src/github.com/googleapis/google-cloud-ruby/google-cloud-storage/lib/google/cloud/storage/service.rb:76:in `block in list_buckets'
/Users/yahonda/src/github.com/googleapis/google-cloud-ruby/google-cloud-storage/lib/google/cloud/storage/service.rb:631:in `execute'
/Users/yahonda/src/github.com/googleapis/google-cloud-ruby/google-cloud-storage/lib/google/cloud/storage/service.rb:75:in `list_buckets'
/Users/yahonda/src/github.com/googleapis/google-cloud-ruby/google-cloud-storage/lib/google/cloud/storage/project.rb:153:in `buckets'
test/google/cloud/storage/project_anonymous_test.rb:41:in `block (3 levels) in <main>'
---------------

  2) Failure:
Google::Cloud::Storage::Project::anonymous::mock_storage#test_0001_raises when creating a bucket without authentication [test/google/cloud/storage/project_anonymous_test.rb:30]:
[Google::Cloud::UnauthenticatedError] exception expected, not
Class: <NoMethodError>
Message: <"undefined method `_' for #<Object:0x00007fae658703c8>">
---Backtrace---
test/google/cloud/storage/project_anonymous_test.rb:25:in `insert_bucket'
/Users/yahonda/src/github.com/googleapis/google-cloud-ruby/google-cloud-storage/lib/google/cloud/storage/service.rb:98:in `block in insert_bucket'
/Users/yahonda/src/github.com/googleapis/google-cloud-ruby/google-cloud-storage/lib/google/cloud/storage/service.rb:631:in `execute'
/Users/yahonda/src/github.com/googleapis/google-cloud-ruby/google-cloud-storage/lib/google/cloud/storage/service.rb:97:in `insert_bucket'
/Users/yahonda/src/github.com/googleapis/google-cloud-ruby/google-cloud-storage/lib/google/cloud/storage/project.rb:379:in `create_bucket'
test/google/cloud/storage/project_anonymous_test.rb:30:in `block (3 levels) in <main>'
---------------

2 runs, 2 assertions, 2 failures, 0 errors, 0 skips
%
```

2 Revert false positives of `google-cloud-storage/test/google/cloud/storage/project_anonymous_test.rb

```
% bundle exec ruby -w -Itest test/google/cloud/storage/project_anonymous_test.rb -n /test_0006_downloads/
... snip ...

  1) Failure:
Google::Cloud::Storage::Project::anonymous::mock_storage#test_0006_downloads a public file [test/google/cloud/storage/project_anonymous_test.rb:125]:
Expected #<Tempfile:/var/folders/4d/1hfsdkkj2772brj5d70vtbm40000gn/T/google-cloud20200410-6349-nvppgj> to be a kind of File, not Tempfile.

1 runs, 1 assertions, 1 failures, 0 errors, 0 skips
%
```

* Reverting auto-correct generates these warnings as expected.

```
DEPRECATED: global use of must_be_kind_of from /Users/yahonda/.rbenv/versions/2.6.6/lib/ruby/2.6.0/delegate.rb:83. Use _(obj).must_be_kind_of instead. This will fail in Minitest 6.
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants