diff --git a/.rubocop.yml b/.rubocop.yml index 133dee5f..d17626ca 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -1,4 +1,5 @@ require: + - rubocop-factory_bot - rubocop-minitest - rubocop-rails diff --git a/Gemfile b/Gemfile index cf9e8526..4ecd3a41 100644 --- a/Gemfile +++ b/Gemfile @@ -50,6 +50,7 @@ end group :development do gem 'annotate', '~> 3.2' # Remove workaround in lib/tasks/annotate.rb when https://github.com/ctran/annotate_models/issues/696 is fixed + gem 'rubocop-factory_bot', '~> 2.24' gem 'rubocop-minitest', '~> 0.32.2' gem 'rubocop-rails', '~> 2.21' end diff --git a/Gemfile.lock b/Gemfile.lock index 581d63a3..e1354134 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -227,6 +227,8 @@ GEM unicode-display_width (>= 2.4.0, < 3.0) rubocop-ast (1.29.0) parser (>= 3.2.1.0) + rubocop-factory_bot (2.24.0) + rubocop (~> 1.33) rubocop-minitest (0.32.2) rubocop (>= 1.39, < 2.0) rubocop-rails (2.21.2) @@ -277,6 +279,7 @@ DEPENDENCIES pundit (~> 2.3) rack-cors (~> 2.0) rails (~> 7.0) + rubocop-factory_bot (~> 2.24) rubocop-minitest (~> 0.32.2) rubocop-rails (~> 2.21) simplecov (~> 0.21) diff --git a/gemset.nix b/gemset.nix index 727b1fb9..ae01bc36 100644 --- a/gemset.nix +++ b/gemset.nix @@ -859,6 +859,17 @@ }; version = "1.29.0"; }; + rubocop-factory_bot = { + dependencies = ["rubocop"]; + groups = ["development"]; + platforms = []; + source = { + remotes = ["https://rubygems.org"]; + sha256 = "1y79flwjwlaslyhfpg84di9n756ir6bm52n964620xsj658d661h"; + type = "gem"; + }; + version = "2.24.0"; + }; rubocop-minitest = { dependencies = ["rubocop"]; groups = ["development"]; diff --git a/test/controllers/albums_controller_test.rb b/test/controllers/albums_controller_test.rb index 56e11625..3efbf542 100644 --- a/test/controllers/albums_controller_test.rb +++ b/test/controllers/albums_controller_test.rb @@ -2,7 +2,7 @@ class AlbumsControllerTest < ActionDispatch::IntegrationTest setup do - @album = create :album, :with_image + @album = create(:album, :with_image) sign_in_as create(:user) end @@ -22,7 +22,7 @@ class AlbumsControllerTest < ActionDispatch::IntegrationTest test 'should create album for moderator' do sign_in_as create(:moderator) - album = build :album + album = build(:album) image = { data: Base64.encode64(Rails.root.join('test/files/image.jpg').read), @@ -46,7 +46,7 @@ class AlbumsControllerTest < ActionDispatch::IntegrationTest test 'should not create album without title' do sign_in_as create(:moderator) - album = build :album + album = build(:album) assert_difference('Album.count', 0) do post albums_url, params: { album: { release: album.release } } @@ -57,7 +57,7 @@ class AlbumsControllerTest < ActionDispatch::IntegrationTest test 'should create dependent album_labels' do sign_in_as create(:moderator) - album = build :album + album = build(:album) album_labels = (1..5).map do |_| { @@ -82,7 +82,7 @@ class AlbumsControllerTest < ActionDispatch::IntegrationTest test 'should create dependent album_artists' do sign_in_as create(:moderator) - album = build :album + album = build(:album) album_artists = (1..5).map do |i| { @@ -147,7 +147,7 @@ class AlbumsControllerTest < ActionDispatch::IntegrationTest test 'should update album for moderator' do sign_in_as create(:moderator) - album = create :album + album = create(:album) image = { data: Base64.encode64(Rails.root.join('test/files/image.jpg').read), @@ -165,7 +165,7 @@ class AlbumsControllerTest < ActionDispatch::IntegrationTest test 'should destroy previous image when image is replaced' do sign_in_as create(:moderator) - album = create :album, :with_image + album = create(:album, :with_image) image = { data: Base64.encode64(Rails.root.join('test/files/image.jpg').read), @@ -185,7 +185,7 @@ class AlbumsControllerTest < ActionDispatch::IntegrationTest test 'should destroy previous image when image is cleared' do sign_in_as create(:moderator) - album = create :album, :with_image + album = create(:album, :with_image) image = { data: nil, @@ -234,8 +234,8 @@ class AlbumsControllerTest < ActionDispatch::IntegrationTest test 'should destroy empty albums for moderator' do sign_in_as create(:moderator) - album2 = create :album - create :track, album: album2 + album2 = create(:album) + create(:track, album: album2) assert_difference('Image.count', -1) do assert_difference('ActiveStorage::Blob.count', -1) do assert_difference('Album.count', -1) do diff --git a/test/controllers/artists_controller_test.rb b/test/controllers/artists_controller_test.rb index 9d5eb30e..a5beed05 100644 --- a/test/controllers/artists_controller_test.rb +++ b/test/controllers/artists_controller_test.rb @@ -2,7 +2,7 @@ class ArtistsControllerTest < ActionDispatch::IntegrationTest setup do - @artist = create :artist, :with_image + @artist = create(:artist, :with_image) sign_in_as create(:user) end @@ -22,7 +22,7 @@ class ArtistsControllerTest < ActionDispatch::IntegrationTest test 'should create artist for moderator' do sign_in_as create(:moderator) - artist = build :artist + artist = build(:artist) image = { data: Base64.encode64(Rails.root.join('test/files/image.jpg').read), @@ -94,7 +94,7 @@ class ArtistsControllerTest < ActionDispatch::IntegrationTest test 'should update artist for moderator' do sign_in_as create(:moderator) - artist = create :artist + artist = create(:artist) image = { data: Base64.encode64(Rails.root.join('test/files/image.jpg').read), @@ -112,7 +112,7 @@ class ArtistsControllerTest < ActionDispatch::IntegrationTest test 'should destroy previous image when image is replaced' do sign_in_as create(:moderator) - artist = create :artist, :with_image + artist = create(:artist, :with_image) image = { data: Base64.encode64(Rails.root.join('test/files/image.jpg').read), @@ -132,7 +132,7 @@ class ArtistsControllerTest < ActionDispatch::IntegrationTest test 'should destroy previous image when image is cleared' do sign_in_as create(:moderator) - artist = create :artist, :with_image + artist = create(:artist, :with_image) image = { data: nil, @@ -182,8 +182,8 @@ class ArtistsControllerTest < ActionDispatch::IntegrationTest test 'should destroy empty artists for moderator (track_artist)' do sign_in_as create(:moderator) - artist2 = create :artist - create :track_artist, artist: artist2 + artist2 = create(:artist) + create(:track_artist, artist: artist2) assert_difference('Image.count', -1) do assert_difference('ActiveStorage::Blob.count', -1) do @@ -202,8 +202,8 @@ class ArtistsControllerTest < ActionDispatch::IntegrationTest test 'should destroy empty artists for moderator (album_artist)' do sign_in_as create(:moderator) - artist2 = create :artist - create :album_artist, artist: artist2 + artist2 = create(:artist) + create(:album_artist, artist: artist2) assert_difference('Image.count', -1) do assert_difference('ActiveStorage::Blob.count', -1) do diff --git a/test/controllers/auth_tokens_controller_test.rb b/test/controllers/auth_tokens_controller_test.rb index 1fbf18f9..1e169dc9 100644 --- a/test/controllers/auth_tokens_controller_test.rb +++ b/test/controllers/auth_tokens_controller_test.rb @@ -49,7 +49,7 @@ class AuthTokensControllerTest < ActionDispatch::IntegrationTest test 'should show auth_token' do sign_in_as(@user) - auth_token = create :auth_token, user: @user + auth_token = create(:auth_token, user: @user) get auth_token_url(auth_token) assert_response :success @@ -57,7 +57,7 @@ class AuthTokensControllerTest < ActionDispatch::IntegrationTest test 'should destroy auth_token' do sign_in_as(@user) - auth_token = create :auth_token, user: @user + auth_token = create(:auth_token, user: @user) assert_difference('AuthToken.count', -1) do delete auth_token_url(auth_token) end diff --git a/test/controllers/cover_filenames_controller_test.rb b/test/controllers/cover_filenames_controller_test.rb index ba0df0ac..7dc1eac8 100644 --- a/test/controllers/cover_filenames_controller_test.rb +++ b/test/controllers/cover_filenames_controller_test.rb @@ -27,7 +27,7 @@ class CoverFilenamesControllerTest < ActionDispatch::IntegrationTest end test 'should not create cover_filename for user' do - cover_filename = build :cover_filename + cover_filename = build(:cover_filename) assert_difference('CoverFilename.count', 0) do post cover_filenames_url, params: { cover_filename: { filename: cover_filename.filename } } end @@ -46,7 +46,7 @@ class CoverFilenamesControllerTest < ActionDispatch::IntegrationTest test 'should create cover_filename for moderator' do sign_in_as create(:moderator) - cover_filename = build :cover_filename + cover_filename = build(:cover_filename) assert_difference('CoverFilename.count', 1) do post cover_filenames_url, params: { cover_filename: { filename: cover_filename.filename } } end @@ -56,7 +56,7 @@ class CoverFilenamesControllerTest < ActionDispatch::IntegrationTest test 'should create cover_filename for admin' do sign_in_as create(:admin) - cover_filename = build :cover_filename + cover_filename = build(:cover_filename) assert_difference('CoverFilename.count', 1) do post cover_filenames_url, params: { cover_filename: { filename: cover_filename.filename } } end diff --git a/test/controllers/genres_controller_test.rb b/test/controllers/genres_controller_test.rb index 59a15ccc..01d3a4f7 100644 --- a/test/controllers/genres_controller_test.rb +++ b/test/controllers/genres_controller_test.rb @@ -122,8 +122,8 @@ class GenresControllerTest < ActionDispatch::IntegrationTest test 'should destroy empty genres for moderator' do sign_in_as(create(:moderator)) - genre2 = create :genre - track = create :track + genre2 = create(:genre) + track = create(:track) track.update(genres: [genre2]) assert_difference('Genre.count', -1) do @@ -137,8 +137,8 @@ class GenresControllerTest < ActionDispatch::IntegrationTest test 'should destroy empty genres for admin' do sign_in_as(create(:admin)) - genre2 = create :genre - track = create :track + genre2 = create(:genre) + track = create(:track) track.update(genres: [genre2]) assert_difference('Genre.count', -1) do diff --git a/test/controllers/labels_controller_test.rb b/test/controllers/labels_controller_test.rb index 7c450e6a..cf10be2d 100644 --- a/test/controllers/labels_controller_test.rb +++ b/test/controllers/labels_controller_test.rb @@ -124,8 +124,8 @@ class LabelsControllerTest < ActionDispatch::IntegrationTest test 'should destroy empty labels for moderator' do sign_in_as(create(:moderator)) - label2 = create :label - create :album_label, label: label2 + label2 = create(:label) + create(:album_label, label: label2) assert_difference('Label.count', -1) do post destroy_empty_labels_url @@ -139,8 +139,8 @@ class LabelsControllerTest < ActionDispatch::IntegrationTest test 'should destroy empty labels for admin' do sign_in_as(create(:admin)) - label2 = create :label - create :album_label, label: label2 + label2 = create(:label) + create(:album_label, label: label2) assert_difference('Label.count', -1) do post destroy_empty_labels_url diff --git a/test/controllers/locations_controller_test.rb b/test/controllers/locations_controller_test.rb index 2be36599..9eafe705 100644 --- a/test/controllers/locations_controller_test.rb +++ b/test/controllers/locations_controller_test.rb @@ -2,7 +2,7 @@ class LocationsControllerTest < ActionDispatch::IntegrationTest setup do - @location = create :location + @location = create(:location) sign_in_as create(:user) end @@ -27,7 +27,7 @@ class LocationsControllerTest < ActionDispatch::IntegrationTest end test 'should not create location for user' do - location = build :location + location = build(:location) assert_difference('Location.count', 0) do post locations_url, params: { location: { path: location.path } } end @@ -46,7 +46,7 @@ class LocationsControllerTest < ActionDispatch::IntegrationTest test 'should create location for moderator' do sign_in_as create(:moderator) - location = build :location + location = build(:location) assert_difference('Location.count', 1) do post locations_url, params: { location: { path: location.path } } end @@ -56,7 +56,7 @@ class LocationsControllerTest < ActionDispatch::IntegrationTest test 'should create location for admin' do sign_in_as create(:admin) - location = build :location + location = build(:location) assert_difference('Location.count', 1) do post locations_url, params: { location: { path: location.path } } end diff --git a/test/controllers/tracks_controller_test.rb b/test/controllers/tracks_controller_test.rb index 916d8e0c..159c9b6a 100644 --- a/test/controllers/tracks_controller_test.rb +++ b/test/controllers/tracks_controller_test.rb @@ -190,7 +190,7 @@ class TracksControllerTest < ActionDispatch::IntegrationTest test 'should destroy empty tracks for moderator' do sign_in_as(create(:moderator)) - create :track + create(:track) assert_difference('Track.count', -1) do post destroy_empty_tracks_url @@ -309,7 +309,7 @@ class TracksControllerAudioTest < ActionDispatch::IntegrationTest test 'should not create transcoded_item if it already exists but should queue if file is gone' do io = StringIO.new Rails.root.join('test/files/base.flac').read AudioFile.any_instance.stubs(:convert).returns(io) - codec_conversion = create :codec_conversion + codec_conversion = create(:codec_conversion) location = Location.create(path: Rails.root.join('test/files')) flac = Codec.create(mimetype: 'audio/flac', extension: 'flac') audio_file = create(:audio_file, location:, filename: '/base.flac', codec: flac) @@ -330,7 +330,7 @@ class TracksControllerAudioTest < ActionDispatch::IntegrationTest test 'should not create transcoded_item if it already exists' do io = StringIO.new Rails.root.join('test/files/base.flac').read AudioFile.any_instance.stubs(:convert).returns(io) - codec_conversion = create :codec_conversion + codec_conversion = create(:codec_conversion) location = Location.create(path: Rails.root.join('test/files')) flac = Codec.create(mimetype: 'audio/flac', extension: 'flac') audio_file = create(:audio_file, location:, filename: '/base.flac', codec: flac) diff --git a/test/factories/albums.rb b/test/factories/albums.rb index 1855eaf5..a03b3abb 100644 --- a/test/factories/albums.rb +++ b/test/factories/albums.rb @@ -21,7 +21,7 @@ release { Faker::Date.backward(days: 365 * 100) } trait :with_image do - association :image, factory: :image + image factory: %i[image] end transient do diff --git a/test/factories/artists.rb b/test/factories/artists.rb index af7ca632..b190898a 100644 --- a/test/factories/artists.rb +++ b/test/factories/artists.rb @@ -17,7 +17,7 @@ review_comment { Faker::Lorem.word } trait :with_image do - association :image, factory: :image + image factory: %i[image] end end end diff --git a/test/factories/auth_tokens.rb b/test/factories/auth_tokens.rb index a3ceefc3..339756cc 100644 --- a/test/factories/auth_tokens.rb +++ b/test/factories/auth_tokens.rb @@ -15,11 +15,11 @@ user_agent { Faker::Internet.user_agent } factory :moderator_auth_token do - association :user, factory: :moderator + user factory: %i[moderator] end factory :admin_auth_token do - association :user, factory: :admin + user factory: %i[admin] end end end diff --git a/test/factories/codec_conversions.rb b/test/factories/codec_conversions.rb index 31e3536e..2053a582 100644 --- a/test/factories/codec_conversions.rb +++ b/test/factories/codec_conversions.rb @@ -10,7 +10,7 @@ FactoryBot.define do factory :codec_conversion do - association :resulting_codec, factory: :codec + resulting_codec factory: %i[codec] name { Faker::Lorem.unique.word } ffmpeg_params { Faker::Lorem.words(number: 10) } end diff --git a/test/factories/playlist_items.rb b/test/factories/playlist_items.rb index 7d62c31b..e3b9b00b 100644 --- a/test/factories/playlist_items.rb +++ b/test/factories/playlist_items.rb @@ -17,17 +17,17 @@ trait :for_album do playlist { association :playlist, playlist_type: :album } - association :item, factory: :album + item factory: %i[album] end trait :for_artist do playlist { association :playlist, playlist_type: :artist } - association :item, factory: :artist + item factory: %i[artist] end trait :for_track do playlist { association :playlist, playlist_type: :track } - association :item, factory: :track + item factory: %i[track] end end end diff --git a/test/factories/tracks.rb b/test/factories/tracks.rb index 1188dec3..a439e884 100644 --- a/test/factories/tracks.rb +++ b/test/factories/tracks.rb @@ -20,7 +20,7 @@ number { Random.rand(20) } review_comment { Faker::Lorem.word } trait :with_audio_file do - association :audio_file, factory: :audio_file + audio_file factory: %i[audio_file] end end end diff --git a/test/models/rescan_runner_test.rb b/test/models/rescan_runner_test.rb index c5cc5c25..9b6db5ca 100644 --- a/test/models/rescan_runner_test.rb +++ b/test/models/rescan_runner_test.rb @@ -42,7 +42,7 @@ class RescanRunnerTest < ActiveSupport::TestCase end test 'schedule_all should create one task per runner' do - 3.times { create(:rescan_runner) } + create_list(:rescan_runner, 3) prev = RescanRunner.all.map(&:finished_at) RescanRunner.schedule_all @@ -627,7 +627,7 @@ class RescanRunnerTest < ActiveSupport::TestCase end test 'should not fail if genre is duplicated in tag and genre exists' do - create :genre, name: 'genre' + create(:genre, name: 'genre') @runner.location.update(path: Rails.root.join('test/files/success-duplicate-genre'))