|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +RSpec.describe AnnotateRb::ModelAnnotator::RelatedFilesListBuilder do |
| 4 | + describe "#build" do |
| 5 | + subject { described_class.new(*args).build } |
| 6 | + |
| 7 | + let(:args) { [file, model_name, table_name, options] } |
| 8 | + let(:file) { "app/models/test_default.rb" } |
| 9 | + let(:model_name) { "test_default" } |
| 10 | + let(:table_name) { "test_defaults" } |
| 11 | + let(:options) { AnnotateRb::Options.new({}) } |
| 12 | + let(:include_nothing_options) do |
| 13 | + { |
| 14 | + exclude_tests: true, |
| 15 | + exclude_fixtures: true, |
| 16 | + exclude_factories: true, |
| 17 | + exclude_serializers: true, |
| 18 | + exclude_scaffolds: true, |
| 19 | + exclude_controllers: true, |
| 20 | + exclude_helpers: true, |
| 21 | + active_admin: false, |
| 22 | + additional_file_patterns: [], |
| 23 | + root_dir: [""] |
| 24 | + } |
| 25 | + end |
| 26 | + |
| 27 | + context "when not adding any related files with an empty project", :isolated_environment do |
| 28 | + let(:options) do |
| 29 | + AnnotateRb::Options.new(**include_nothing_options) |
| 30 | + end |
| 31 | + |
| 32 | + it { is_expected.to be_empty } |
| 33 | + end |
| 34 | + |
| 35 | + context "when not adding any related files with existing files", :isolated_environment do |
| 36 | + let(:options) do |
| 37 | + AnnotateRb::Options.new(**include_nothing_options) |
| 38 | + end |
| 39 | + |
| 40 | + let(:model_name) { "test_default" } |
| 41 | + |
| 42 | + before do |
| 43 | + # Add test file |
| 44 | + FileUtils.mkdir_p("spec/models") |
| 45 | + FileUtils.touch("spec/models/test_default_spec.rb") |
| 46 | + |
| 47 | + # Add test fixtures |
| 48 | + FileUtils.mkdir_p("spec/fixtures") |
| 49 | + FileUtils.touch("test_defaults.yml") |
| 50 | + |
| 51 | + # Add factories |
| 52 | + FileUtils.mkdir_p("spec/factories") |
| 53 | + FileUtils.touch("spec/factories/test_default_factory.rb") |
| 54 | + |
| 55 | + # Add serializers |
| 56 | + FileUtils.mkdir_p("spec/serializers") |
| 57 | + FileUtils.touch("spec/serializers/test_default_serializer_spec.rb") |
| 58 | + |
| 59 | + # Add scaffolds |
| 60 | + FileUtils.mkdir_p("spec/requests") |
| 61 | + FileUtils.touch("spec/requests/test_defaults_spec.rb") |
| 62 | + |
| 63 | + # Add controllers |
| 64 | + FileUtils.mkdir_p("app/controllers") |
| 65 | + FileUtils.touch("app/controllers/test_defaults_controller.rb") |
| 66 | + |
| 67 | + # Add helpers |
| 68 | + FileUtils.mkdir_p("app/helpers") |
| 69 | + FileUtils.touch("app/helpers/test_defaults_helper.rb") |
| 70 | + |
| 71 | + # Add active admin |
| 72 | + FileUtils.mkdir_p("app/admin") |
| 73 | + FileUtils.touch("app/admin/test_default.rb") |
| 74 | + end |
| 75 | + |
| 76 | + it { is_expected.to be_empty } |
| 77 | + end |
| 78 | + |
| 79 | + context "when including tests", :isolated_environment do |
| 80 | + let(:options) { AnnotateRb::Options.new(**include_nothing_options.merge({exclude_tests: false})) } |
| 81 | + |
| 82 | + let(:model_name) { "test_default" } |
| 83 | + let(:test_directory) { "spec/models" } |
| 84 | + let(:test_file_name) { "test_default_spec.rb" } |
| 85 | + let(:relative_file_path) { File.join(test_directory, test_file_name) } |
| 86 | + let(:position_key) { :position_in_test } |
| 87 | + |
| 88 | + before do |
| 89 | + FileUtils.mkdir_p(test_directory) |
| 90 | + FileUtils.touch(relative_file_path) |
| 91 | + end |
| 92 | + |
| 93 | + it "returns the test file and the position key" do |
| 94 | + expect(subject).to eq([[relative_file_path, position_key]]) |
| 95 | + end |
| 96 | + end |
| 97 | + |
| 98 | + context "when including fixtures", :isolated_environment do |
| 99 | + let(:options) { AnnotateRb::Options.new(**include_nothing_options.merge({exclude_fixtures: false})) } |
| 100 | + |
| 101 | + let(:model_name) { "test_default" } |
| 102 | + let(:fixture_directory) { "spec/fixtures" } |
| 103 | + let(:fixture_file_name) { "test_defaults.yml" } |
| 104 | + let(:relative_file_path) { File.join(fixture_directory, fixture_file_name) } |
| 105 | + let(:position_key) { :position_in_fixture } |
| 106 | + |
| 107 | + before do |
| 108 | + FileUtils.mkdir_p(fixture_directory) |
| 109 | + FileUtils.touch(relative_file_path) |
| 110 | + end |
| 111 | + |
| 112 | + it "returns the test file and the position key" do |
| 113 | + expect(subject).to eq([[relative_file_path, position_key]]) |
| 114 | + end |
| 115 | + end |
| 116 | + |
| 117 | + context "when including factories", :isolated_environment do |
| 118 | + let(:options) { AnnotateRb::Options.new(**include_nothing_options.merge({exclude_factories: false})) } |
| 119 | + |
| 120 | + let(:model_name) { "test_default" } |
| 121 | + let(:factory_directory) { "spec/factories" } |
| 122 | + let(:factory_file_name) { "test_default_factory.rb" } |
| 123 | + let(:relative_file_path) { File.join(factory_directory, factory_file_name) } |
| 124 | + let(:position_key) { :position_in_factory } |
| 125 | + |
| 126 | + before do |
| 127 | + FileUtils.mkdir_p(factory_directory) |
| 128 | + FileUtils.touch(relative_file_path) |
| 129 | + end |
| 130 | + |
| 131 | + it "returns the test file and the position key" do |
| 132 | + expect(subject).to eq([[relative_file_path, position_key]]) |
| 133 | + end |
| 134 | + end |
| 135 | + |
| 136 | + context "when including serializers", :isolated_environment do |
| 137 | + let(:options) { AnnotateRb::Options.new(**include_nothing_options.merge({exclude_serializers: false})) } |
| 138 | + |
| 139 | + let(:model_name) { "test_default" } |
| 140 | + let(:serializer_directory) { "spec/serializers" } |
| 141 | + let(:serializer_file_name) { "test_default_serializer_spec.rb" } |
| 142 | + let(:relative_file_path) { File.join(serializer_directory, serializer_file_name) } |
| 143 | + let(:position_key) { :position_in_serializer } |
| 144 | + |
| 145 | + before do |
| 146 | + FileUtils.mkdir_p(serializer_directory) |
| 147 | + FileUtils.touch(relative_file_path) |
| 148 | + end |
| 149 | + |
| 150 | + it "returns the test file and the position key" do |
| 151 | + expect(subject).to eq([[relative_file_path, position_key]]) |
| 152 | + end |
| 153 | + end |
| 154 | + |
| 155 | + context "when including scaffolds", :isolated_environment do |
| 156 | + let(:options) { AnnotateRb::Options.new(**include_nothing_options.merge({exclude_scaffolds: false})) } |
| 157 | + |
| 158 | + let(:model_name) { "test_default" } |
| 159 | + let(:scaffolded_requests_directory) { "spec/requests" } |
| 160 | + let(:scaffolded_request_spec_file_name) { "test_defaults_spec.rb" } |
| 161 | + let(:relative_file_path) { File.join(scaffolded_requests_directory, scaffolded_request_spec_file_name) } |
| 162 | + let(:position_key) { :position_in_scaffold } |
| 163 | + |
| 164 | + before do |
| 165 | + FileUtils.mkdir_p(scaffolded_requests_directory) |
| 166 | + FileUtils.touch(relative_file_path) |
| 167 | + end |
| 168 | + |
| 169 | + it "returns the test file and the position key" do |
| 170 | + expect(subject).to eq([[relative_file_path, position_key]]) |
| 171 | + end |
| 172 | + end |
| 173 | + |
| 174 | + context "when including controllers", :isolated_environment do |
| 175 | + let(:options) { AnnotateRb::Options.new(**include_nothing_options.merge({exclude_controllers: false})) } |
| 176 | + |
| 177 | + let(:model_name) { "test_default" } |
| 178 | + let(:controllers_directory) { "app/controllers" } |
| 179 | + let(:controller_file_name) { "test_defaults_controller.rb" } |
| 180 | + let(:relative_file_path) { File.join(controllers_directory, controller_file_name) } |
| 181 | + let(:position_key) { :position_in_controller } |
| 182 | + |
| 183 | + before do |
| 184 | + FileUtils.mkdir_p(controllers_directory) |
| 185 | + FileUtils.touch(relative_file_path) |
| 186 | + end |
| 187 | + |
| 188 | + it "returns the test file and the position key" do |
| 189 | + expect(subject).to eq([[relative_file_path, position_key]]) |
| 190 | + end |
| 191 | + end |
| 192 | + |
| 193 | + context "when including helpers", :isolated_environment do |
| 194 | + let(:options) { AnnotateRb::Options.new(**include_nothing_options.merge({exclude_helpers: false})) } |
| 195 | + |
| 196 | + let(:model_name) { "test_default" } |
| 197 | + let(:helpers_directory) { "app/helpers" } |
| 198 | + let(:helper_file_name) { "test_defaults_helper.rb" } |
| 199 | + let(:relative_file_path) { File.join(helpers_directory, helper_file_name) } |
| 200 | + let(:position_key) { :position_in_helper } |
| 201 | + |
| 202 | + before do |
| 203 | + FileUtils.mkdir_p(helpers_directory) |
| 204 | + FileUtils.touch(relative_file_path) |
| 205 | + end |
| 206 | + |
| 207 | + it "returns the test file and the position key" do |
| 208 | + expect(subject).to eq([[relative_file_path, position_key]]) |
| 209 | + end |
| 210 | + end |
| 211 | + |
| 212 | + context "when including active admin models", :isolated_environment do |
| 213 | + let(:options) { AnnotateRb::Options.new(**include_nothing_options.merge({active_admin: true})) } |
| 214 | + |
| 215 | + let(:model_name) { "test_default" } |
| 216 | + let(:admin_directory) { "app/admin" } |
| 217 | + let(:active_admin_model_file_name) { "test_default.rb" } |
| 218 | + let(:relative_file_path) { File.join(admin_directory, active_admin_model_file_name) } |
| 219 | + let(:position_key) { :position_in_admin } |
| 220 | + |
| 221 | + before do |
| 222 | + FileUtils.mkdir_p(admin_directory) |
| 223 | + FileUtils.touch(relative_file_path) |
| 224 | + end |
| 225 | + |
| 226 | + it "returns the test file and the position key" do |
| 227 | + expect(subject).to eq([[relative_file_path, position_key]]) |
| 228 | + end |
| 229 | + end |
| 230 | + |
| 231 | + context "when including additional file patterns", :isolated_environment do |
| 232 | + let(:patterns) { ["spec/custom/%MODEL_NAME%_custom.rb"] } |
| 233 | + let(:options) { AnnotateRb::Options.new(**include_nothing_options.merge({additional_file_patterns: patterns})) } |
| 234 | + |
| 235 | + let(:model_name) { "test_default" } |
| 236 | + let(:custom_directory) { "spec/custom" } |
| 237 | + let(:additional_file_name) { "test_default_custom.rb" } |
| 238 | + let(:relative_file_path) { File.join(custom_directory, additional_file_name) } |
| 239 | + let(:position_key) { :position_in_additional_file_patterns } |
| 240 | + |
| 241 | + before do |
| 242 | + FileUtils.mkdir_p(custom_directory) |
| 243 | + FileUtils.touch(relative_file_path) |
| 244 | + end |
| 245 | + |
| 246 | + it "returns the test file and the position key" do |
| 247 | + expect(subject).to eq([[relative_file_path, position_key]]) |
| 248 | + end |
| 249 | + end |
| 250 | + end |
| 251 | +end |
0 commit comments