From ab85764a5cb5fe207b745a64c5222669ee21fc41 Mon Sep 17 00:00:00 2001 From: darronschall Date: Thu, 12 Dec 2024 09:22:54 -0500 Subject: [PATCH 1/2] Generate tests via `hook_for :test_framework` Leverage the [`hook_for`](https://api.rubyonrails.org/classes/Rails/Generators/Base.html#method-c-hook_for) API to separate the test framework generation. This follows the rails pattern, and paves the way for adding other test framework templates (e.g. rspec). Along the way, move the `users.yml` fixture into a `fixtures` subfolder and convert the `copy_file` to a `template` for consistency. --- .../authentication_generator.rb | 10 +------- .../authentication_generator.rb | 25 +++++++++++++++++++ .../email_verifications_controller_test.rb.tt | 0 .../api/identity/emails_controller_test.rb.tt | 0 .../password_resets_controller_test.rb.tt | 0 .../api/passwords_controller_test.rb.tt | 0 .../api/registrations_controller_test.rb.tt | 0 .../api/sessions_controller_test.rb.tt | 0 .../email_verifications_controller_test.rb.tt | 0 .../identity/emails_controller_test.rb.tt | 0 .../password_resets_controller_test.rb.tt | 0 .../html/passwords_controller_test.rb.tt | 0 .../html/registrations_controller_test.rb.tt | 0 .../html/sessions_controller_test.rb.tt | 0 .../templates/test/fixtures}/users.yml | 0 .../test}/mailers/user_mailer_test.rb.tt | 0 .../templates/test}/test_helper.rb.tt | 0 17 files changed, 26 insertions(+), 9 deletions(-) create mode 100644 lib/generators/test_unit/authentication/authentication_generator.rb rename lib/generators/{authentication/templates/test_unit => test_unit/authentication/templates/test}/controllers/api/identity/email_verifications_controller_test.rb.tt (100%) rename lib/generators/{authentication/templates/test_unit => test_unit/authentication/templates/test}/controllers/api/identity/emails_controller_test.rb.tt (100%) rename lib/generators/{authentication/templates/test_unit => test_unit/authentication/templates/test}/controllers/api/identity/password_resets_controller_test.rb.tt (100%) rename lib/generators/{authentication/templates/test_unit => test_unit/authentication/templates/test}/controllers/api/passwords_controller_test.rb.tt (100%) rename lib/generators/{authentication/templates/test_unit => test_unit/authentication/templates/test}/controllers/api/registrations_controller_test.rb.tt (100%) rename lib/generators/{authentication/templates/test_unit => test_unit/authentication/templates/test}/controllers/api/sessions_controller_test.rb.tt (100%) rename lib/generators/{authentication/templates/test_unit => test_unit/authentication/templates/test}/controllers/html/identity/email_verifications_controller_test.rb.tt (100%) rename lib/generators/{authentication/templates/test_unit => test_unit/authentication/templates/test}/controllers/html/identity/emails_controller_test.rb.tt (100%) rename lib/generators/{authentication/templates/test_unit => test_unit/authentication/templates/test}/controllers/html/identity/password_resets_controller_test.rb.tt (100%) rename lib/generators/{authentication/templates/test_unit => test_unit/authentication/templates/test}/controllers/html/passwords_controller_test.rb.tt (100%) rename lib/generators/{authentication/templates/test_unit => test_unit/authentication/templates/test}/controllers/html/registrations_controller_test.rb.tt (100%) rename lib/generators/{authentication/templates/test_unit => test_unit/authentication/templates/test}/controllers/html/sessions_controller_test.rb.tt (100%) rename lib/generators/{authentication/templates/test_unit => test_unit/authentication/templates/test/fixtures}/users.yml (100%) rename lib/generators/{authentication/templates/test_unit => test_unit/authentication/templates/test}/mailers/user_mailer_test.rb.tt (100%) rename lib/generators/{authentication/templates/test_unit => test_unit/authentication/templates/test}/test_helper.rb.tt (100%) diff --git a/lib/generators/authentication/authentication_generator.rb b/lib/generators/authentication/authentication_generator.rb index ad0b807e..090d9c98 100644 --- a/lib/generators/authentication/authentication_generator.rb +++ b/lib/generators/authentication/authentication_generator.rb @@ -76,10 +76,6 @@ def create_models template "models/user.rb", "app/models/user.rb" end - def create_fixture_file - copy_file "test_unit/users.yml", "test/fixtures/users.yml" - end - def create_controllers template "controllers/#{format}/authentications/events_controller.rb", "app/controllers/authentications/events_controller.rb" if options.trackable? @@ -206,11 +202,7 @@ def add_routes route 'get "sign_in", to: "sessions#new"' unless options.api? end - def create_test_files - directory "test_unit/controllers/#{format}", "test/controllers" - directory "test_unit/mailers/", "test/mailers" - template "test_unit/test_helper.rb", "test/test_helper.rb", force: true - end + hook_for :test_framework private def format diff --git a/lib/generators/test_unit/authentication/authentication_generator.rb b/lib/generators/test_unit/authentication/authentication_generator.rb new file mode 100644 index 00000000..19ef046b --- /dev/null +++ b/lib/generators/test_unit/authentication/authentication_generator.rb @@ -0,0 +1,25 @@ +require "rails/generators/test_unit" + +module TestUnit # :nodoc: + class AuthenticationGenerator < Rails::Generators::Base # :nodoc: + source_root File.expand_path("templates", __dir__) + + class_option :api, type: :boolean, desc: "Generates API authentication" + + def create_fixture_file + template "test/fixtures/users.yml" + end + + def create_test_files + directory "test/controllers/#{format}", "test/controllers" + directory "test/mailers/", "test/mailers" + template "test/test_helper.rb", "test/test_helper.rb", force: true + end + + private + + def format + options.api? ? "api" : "html" + end + end +end \ No newline at end of file diff --git a/lib/generators/authentication/templates/test_unit/controllers/api/identity/email_verifications_controller_test.rb.tt b/lib/generators/test_unit/authentication/templates/test/controllers/api/identity/email_verifications_controller_test.rb.tt similarity index 100% rename from lib/generators/authentication/templates/test_unit/controllers/api/identity/email_verifications_controller_test.rb.tt rename to lib/generators/test_unit/authentication/templates/test/controllers/api/identity/email_verifications_controller_test.rb.tt diff --git a/lib/generators/authentication/templates/test_unit/controllers/api/identity/emails_controller_test.rb.tt b/lib/generators/test_unit/authentication/templates/test/controllers/api/identity/emails_controller_test.rb.tt similarity index 100% rename from lib/generators/authentication/templates/test_unit/controllers/api/identity/emails_controller_test.rb.tt rename to lib/generators/test_unit/authentication/templates/test/controllers/api/identity/emails_controller_test.rb.tt diff --git a/lib/generators/authentication/templates/test_unit/controllers/api/identity/password_resets_controller_test.rb.tt b/lib/generators/test_unit/authentication/templates/test/controllers/api/identity/password_resets_controller_test.rb.tt similarity index 100% rename from lib/generators/authentication/templates/test_unit/controllers/api/identity/password_resets_controller_test.rb.tt rename to lib/generators/test_unit/authentication/templates/test/controllers/api/identity/password_resets_controller_test.rb.tt diff --git a/lib/generators/authentication/templates/test_unit/controllers/api/passwords_controller_test.rb.tt b/lib/generators/test_unit/authentication/templates/test/controllers/api/passwords_controller_test.rb.tt similarity index 100% rename from lib/generators/authentication/templates/test_unit/controllers/api/passwords_controller_test.rb.tt rename to lib/generators/test_unit/authentication/templates/test/controllers/api/passwords_controller_test.rb.tt diff --git a/lib/generators/authentication/templates/test_unit/controllers/api/registrations_controller_test.rb.tt b/lib/generators/test_unit/authentication/templates/test/controllers/api/registrations_controller_test.rb.tt similarity index 100% rename from lib/generators/authentication/templates/test_unit/controllers/api/registrations_controller_test.rb.tt rename to lib/generators/test_unit/authentication/templates/test/controllers/api/registrations_controller_test.rb.tt diff --git a/lib/generators/authentication/templates/test_unit/controllers/api/sessions_controller_test.rb.tt b/lib/generators/test_unit/authentication/templates/test/controllers/api/sessions_controller_test.rb.tt similarity index 100% rename from lib/generators/authentication/templates/test_unit/controllers/api/sessions_controller_test.rb.tt rename to lib/generators/test_unit/authentication/templates/test/controllers/api/sessions_controller_test.rb.tt diff --git a/lib/generators/authentication/templates/test_unit/controllers/html/identity/email_verifications_controller_test.rb.tt b/lib/generators/test_unit/authentication/templates/test/controllers/html/identity/email_verifications_controller_test.rb.tt similarity index 100% rename from lib/generators/authentication/templates/test_unit/controllers/html/identity/email_verifications_controller_test.rb.tt rename to lib/generators/test_unit/authentication/templates/test/controllers/html/identity/email_verifications_controller_test.rb.tt diff --git a/lib/generators/authentication/templates/test_unit/controllers/html/identity/emails_controller_test.rb.tt b/lib/generators/test_unit/authentication/templates/test/controllers/html/identity/emails_controller_test.rb.tt similarity index 100% rename from lib/generators/authentication/templates/test_unit/controllers/html/identity/emails_controller_test.rb.tt rename to lib/generators/test_unit/authentication/templates/test/controllers/html/identity/emails_controller_test.rb.tt diff --git a/lib/generators/authentication/templates/test_unit/controllers/html/identity/password_resets_controller_test.rb.tt b/lib/generators/test_unit/authentication/templates/test/controllers/html/identity/password_resets_controller_test.rb.tt similarity index 100% rename from lib/generators/authentication/templates/test_unit/controllers/html/identity/password_resets_controller_test.rb.tt rename to lib/generators/test_unit/authentication/templates/test/controllers/html/identity/password_resets_controller_test.rb.tt diff --git a/lib/generators/authentication/templates/test_unit/controllers/html/passwords_controller_test.rb.tt b/lib/generators/test_unit/authentication/templates/test/controllers/html/passwords_controller_test.rb.tt similarity index 100% rename from lib/generators/authentication/templates/test_unit/controllers/html/passwords_controller_test.rb.tt rename to lib/generators/test_unit/authentication/templates/test/controllers/html/passwords_controller_test.rb.tt diff --git a/lib/generators/authentication/templates/test_unit/controllers/html/registrations_controller_test.rb.tt b/lib/generators/test_unit/authentication/templates/test/controllers/html/registrations_controller_test.rb.tt similarity index 100% rename from lib/generators/authentication/templates/test_unit/controllers/html/registrations_controller_test.rb.tt rename to lib/generators/test_unit/authentication/templates/test/controllers/html/registrations_controller_test.rb.tt diff --git a/lib/generators/authentication/templates/test_unit/controllers/html/sessions_controller_test.rb.tt b/lib/generators/test_unit/authentication/templates/test/controllers/html/sessions_controller_test.rb.tt similarity index 100% rename from lib/generators/authentication/templates/test_unit/controllers/html/sessions_controller_test.rb.tt rename to lib/generators/test_unit/authentication/templates/test/controllers/html/sessions_controller_test.rb.tt diff --git a/lib/generators/authentication/templates/test_unit/users.yml b/lib/generators/test_unit/authentication/templates/test/fixtures/users.yml similarity index 100% rename from lib/generators/authentication/templates/test_unit/users.yml rename to lib/generators/test_unit/authentication/templates/test/fixtures/users.yml diff --git a/lib/generators/authentication/templates/test_unit/mailers/user_mailer_test.rb.tt b/lib/generators/test_unit/authentication/templates/test/mailers/user_mailer_test.rb.tt similarity index 100% rename from lib/generators/authentication/templates/test_unit/mailers/user_mailer_test.rb.tt rename to lib/generators/test_unit/authentication/templates/test/mailers/user_mailer_test.rb.tt diff --git a/lib/generators/authentication/templates/test_unit/test_helper.rb.tt b/lib/generators/test_unit/authentication/templates/test/test_helper.rb.tt similarity index 100% rename from lib/generators/authentication/templates/test_unit/test_helper.rb.tt rename to lib/generators/test_unit/authentication/templates/test/test_helper.rb.tt From 3a64253bcd69da7fd35d05ae02c3d1720ac316c4 Mon Sep 17 00:00:00 2001 From: darronschall Date: Thu, 12 Dec 2024 13:20:14 -0500 Subject: [PATCH 2/2] Convert users.yml to actual template Since we're using `template` for this file now, convert it to an actual template. --- .../templates/test/fixtures/{users.yml => users.yml.tt} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename lib/generators/test_unit/authentication/templates/test/fixtures/{users.yml => users.yml.tt} (69%) diff --git a/lib/generators/test_unit/authentication/templates/test/fixtures/users.yml b/lib/generators/test_unit/authentication/templates/test/fixtures/users.yml.tt similarity index 69% rename from lib/generators/test_unit/authentication/templates/test/fixtures/users.yml rename to lib/generators/test_unit/authentication/templates/test/fixtures/users.yml.tt index 4c5f5d7d..450e58fb 100644 --- a/lib/generators/test_unit/authentication/templates/test/fixtures/users.yml +++ b/lib/generators/test_unit/authentication/templates/test/fixtures/users.yml.tt @@ -2,5 +2,5 @@ lazaro_nixon: email: lazaronixon@hotmail.com - password_digest: <%= BCrypt::Password.create("Secret1*3*5*") %> + password_digest: <%%= BCrypt::Password.create("Secret1*3*5*") %> verified: true