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

[CI] Run tests for individual gems in GitHub Actions #569

Merged
merged 79 commits into from
Nov 22, 2023

Conversation

jagthedrummer
Copy link
Contributor

In #550 I realized that I was making the same changes over and over to each individual gem and that instead we should probably ship some base controller/models in the main bullet_train gem. So this is an attempt at doing that.

@jagthedrummer jagthedrummer force-pushed the jeremy/github-gem-tests-redux branch 2 times, most recently from 6d2872e to 93be509 Compare November 15, 2023 17:40
@jagthedrummer
Copy link
Contributor Author

jagthedrummer commented Nov 15, 2023

OK, I finally got this all working, but this situation is a little ridiculous for a single PR:

CleanShot 2023-11-15 at 12 13 33

There are a few main things that this PR does, and I'm going to try to split it into smaller PRs as best I can.

  • Adds some missing dependencies to .gemspec files. These dependencies end up getting resolved when we're installing things within the starter repo, but then some gem tests fail when we run them outside of the starter repo context.
  • Add some dependencies to various Gemfiles. These make it so that in dev/test each gem will use the peer version of the gems in the core repo.
  • Adds some "missing" files like Account::ApplicationController. These are files that we ship in the starter repo, and which various gems expect to be present. So when we try to test the gems outside of the starter repo we need to provide those files in some way. We could potentially put a copy of them in test/dummy for each gem, but that seems unwieldy. Instead I'm adding them directly to the gem that seems appropriate and then the copy in the starter repo will be as if that file has been ejected.
  • Some CI plumbing in the .github diretory
  • A few random things that I had to change here and there to get things to work. These are probably the changes most likely to cause problems or to be things that we don't want to do, and it would be much easier to review them if they're not mixed in with all this other stuff.

jagthedrummer added a commit that referenced this pull request Nov 15, 2023
In the course of [trying to be able to run all of the tests for
individual gems in
CI](#569) I've
discovered that we don't specify all of our dependencies fully.

In the starter repo the problems don't appear because all of the things
that we need end up being dependencies of something or other, but when
we try to run tests for individual gems we'll get errors about
unsatisfied dependencies.

There are only a few gems where we needed to declare a missing
dependency in the `.gemspec`. And those are the only thing in this PR
that could have the chance to impact downsteam apps.

All of the changes to `Gemfile` and `Gemfile.lock` in each gem won't
impact gem consumers at all. They just come into play in local
development and CI. The changes in each `Gemfile` ensure that when
developing or testing these gems that they'll use their peer gems from
inside the `core` repo insted of using whatever version happens to be
latest.
jagthedrummer added a commit that referenced this pull request Nov 15, 2023
In the course of [trying to be able to run all of the tests for
individual gems in CI](#569) I've
discovered that we don't specify all of our dependencies fully.

In the starter repo the problems don't appear because all of the things
that we need end up being dependencies of something or other, but when
we try to run tests for individual gems we'll get errors about
unsatisfied dependencies.

There are only a few gems where we needed to declare a missing
dependency in the `.gemspec`. And those are the only thing in this PR
that could have the chance to impact downsteam apps.

All of the changes to `Gemfile` and `Gemfile.lock` in each gem won't
impact gem consumers at all. They just come into play in local
development and CI. The changes in each `Gemfile` ensure that when
developing or testing these gems that they'll use their peer gems from
inside the `core` repo insted of using whatever version happens to be
latest.
@jagthedrummer jagthedrummer force-pushed the jeremy/github-gem-tests-redux branch 2 times, most recently from dc3ea6f to 7c94fc5 Compare November 15, 2023 22:06
@jagthedrummer
Copy link
Contributor Author

I handled the .gemspec and Gemfile dependencies in #667 and #668. Then rebased this branch and things aren't quite as bad:

CleanShot 2023-11-15 at 17 02 52

Tomorrow I'll see what I can do to split this up further. And I'm sure there are some bits that I need to clean up.

@@ -0,0 +1,3 @@
class Api::V1::ApplicationController < ActionController::API
include Api::Controllers::Base
end
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This and the next couple of files are just providing a default implementation of files that we ship in the starter repo. We can think of the files that we ship in the starter repo as being "ejected" versions of these files. I added these in the gem itself so that we don't have to supply these files in test/dummy of every singe gem that ends up needing them.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I remember this being really annoying because the tests complained about the files not being there. This seems like a good option!

I think it would be helpful to provide a comment in these files briefly explaining the situation so in the future someone on the Bullet Train team doesn't come across it and mistake it for a "duplicated" file in the starter repository.

@jagthedrummer jagthedrummer force-pushed the jeremy/github-gem-tests-redux branch from 7eb90ed to 8eead89 Compare November 16, 2023 18:45
@@ -1,6 +1,6 @@
class Webhooks::Incoming::BulletTrainWebhooksController < Webhooks::Incoming::WebhooksController
def create
Webhooks::Incoming::BulletTrainWebhook.create(data: JSON.parse(request.body.read)).process_async
Webhooks::Incoming::BulletTrainWebhook.create(data: JSON.parse(request.raw_post)).process_async
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For some reason request.body.read is returning an empty string '' which then causes JSON.parse to throw an error. Using raw_post seems to work. At least for tests...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a "default implementation" of something we ship with the starter repo. Putting it in this specific spot feels kinda wrong, but that's what I had to do to get tests to pass.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This and the next few files are just setting up the database for the test env of bullet_train-incoming_webhooks.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another "default implementation" that feels like it's not in quite the right place, but it had to be here for tests to work.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This and the next few files are just setting up the database for the test env of bullet_train-outgoing_webhooks.

@@ -3,6 +3,9 @@
class Api::V1::Webhooks::Outgoing::EndpointsControllerTest < ActionDispatch::IntegrationTest
setup do
@controller = Api::V1::Webhooks::Outgoing::EndpointsController

@team = Team.first
@endpoint = @team.webhooks_outgoing_endpoints.first
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Grabbing a couple of objects for reference later in the tests. Doing this since we can't (and shouldn't try to) rely on hard coded ids.

t.references :team
t.string :url, null: false
t.string :name, null: false
t.integer :event_type_ids, array: true, default: []
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sqlite3 doesn't seem to support array: true in CI, so I just changed this gem to test against postgres which is what we target for deployment anyway.


5.times do |n|
Webhooks::Outgoing::Endpoint.create! team_id: Team.first.id, name: "Generic name #{n}", url: "http://example.com/webhook-#{n}"
end
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved these into the setup method so that they're created within the transaction for running tests. When they were outside of the setup method in the setup/active_record.rb file the records created would stay in the DB between test runs.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a missing binstub.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed this so that we use the default implementation from the core gems.

Copy link
Contributor Author

@jagthedrummer jagthedrummer Nov 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a default implementation of this file so that it doesn't need to be in test/dummy for each gem. The next few files are also default implementations.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed a bunch of */test/dummy/app/controllers/application_controller.rb */test/dummy/app/models/application_record.rb files so that gems will use the default implementation that we ship.

@@ -2,6 +2,7 @@ module Users::Base
extend ActiveSupport::Concern

included do
extend Devise::Models
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Explicitly including this so that things will work in tests when the rail ties don't do the thing.

@@ -24,7 +24,7 @@ def enable_bulk_invitations
end

def incoming_webhooks_parent_class_name
@@config&.incoming_webhooks_parent_class_name
@@config&.incoming_webhooks_parent_class_name || "ApplicationRecord"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to supply a default value here or we get a failure like this:

Error:
Webhooks::Incoming::BulletTrainWebhooksControllerTest#test_should_get_incoming_webhook:
NoMethodError: undefined method `constantize' for nil:NilClass
    /Users/jgreen/projects/bullet-train-co/bullet_train-core/bullet_train-incoming_webhooks/app/models/webhooks/incoming/bullet_train_webhook.rb:1:in `<top (required)>'
    <internal:/Users/jgreen/.asdf/installs/ruby/3.2.2/lib/ruby/3.2.0/rubygems/core_ext/kernel_require.rb>:37:in `require'
    <internal:/Users/jgreen/.asdf/installs/ruby/3.2.2/lib/ruby/3.2.0/rubygems/core_ext/kernel_require.rb>:37:in `require'
    /Users/jgreen/.asdf/installs/ruby/3.2.2/lib/ruby/gems/3.2.0/gems/zeitwerk-2.6.12/lib/zeitwerk/kernel.rb:30:in `require'
    /Users/jgreen/projects/bullet-train-co/bullet_train-core/bullet_train-incoming_webhooks/app/controllers/webhooks/incoming/bullet_train_webhooks_controller.rb:3:in `create'

@jagthedrummer jagthedrummer marked this pull request as ready for review November 16, 2023 20:53
@jagthedrummer jagthedrummer force-pushed the jeremy/github-gem-tests-redux branch from 5dfbe87 to 672fa00 Compare November 22, 2023 15:32
@jagthedrummer jagthedrummer merged commit fce0791 into main Nov 22, 2023
18 checks passed
@jagthedrummer jagthedrummer deleted the jeremy/github-gem-tests-redux branch November 22, 2023 15:58
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

Successfully merging this pull request may close these issues.

2 participants