Realize that switches on production environment are untestable

This is part of the Semicolon&Sons Code Diary - consisting of lessons learned on the job. You're in the testing category.

Last Updated: 2024-03-28

I had the following code in my sitemap generator:

if Rails.env.production?
  SitemapGenerator::Sitemap.adapter =
    SitemapGenerator::AwsSdkAdapter.new(
      MyConfig['s3_bucket'],
      aws_access_key_id: MyConfig['s3_access_key'],
      aws_secret_access_key: MyConfig['s3_secret'],
      aws_region: MyConfig['s3_bucket_region']
    )
end

SitemapGenerator::Sitemap.create do
# ... actually generate sitemap
end

The integration tests worked for this and I deployed.

Later I discovered it had a production bug. That's because the AwsSdkAdapter class was not in scope.

The fix was as follows:

if Rails.env.production?
  #! NEW CODE #!
  require 'aws-sdk-s3'

  SitemapGenerator::Sitemap.adapter =
    SitemapGenerator::AwsSdkAdapter.new(
      MyConfig['s3_bucket'],
      aws_access_key_id: MyConfig['s3_access_key'],
      aws_secret_access_key: MyConfig['s3_secret'],
      aws_region: MyConfig['s3_bucket_region']
    )
end

Lesson