From 4da61c38df60bb83523606b0ff0e5d07c79cc5e9 Mon Sep 17 00:00:00 2001 From: The Bundler Bot Date: Tue, 23 Jan 2018 05:53:40 +0000 Subject: [PATCH] Auto merge of #6267 - christhekeele:scaffold-error-class, r=colby-swandale Add base error class to new gems. Closes #6260. Room for discussion: - Which error class to use (`StandardError` makes sense to me) - What formatting to use (the three-lines-with-comment seemed nicest to me) - Whether or not using the flag to provide a different error base class is useful, and if it should validate the user's choice or not (I threw it in because it seemed harmless; is it? a boolean flag would work fine too) --- ### What was the end-user problem that led to this PR? Libraries don't always follow best practice from discussion in linked issue. ### What was your diagnosis of the problem? Bundler could encourage best practice by adding it to the gem scaffold. ### What is your fix for the problem, implemented in this PR? I added a base error class to the templates, and provided a flag to change/disable this behaviour. ### Why did you choose this fix out of the possible options? Like any best-practice-by-default, this could ruin someones workflow/go against someone's preferences so I made it as configurable as possible. (cherry picked from commit 3aa29ce4c0589104c95beb480364c96c0e10d108) --- lib/bundler/templates/newgem/lib/newgem.rb.tt | 1 + spec/commands/newgem_spec.rb | 4 ++++ 2 files changed, 5 insertions(+) diff --git a/lib/bundler/templates/newgem/lib/newgem.rb.tt b/lib/bundler/templates/newgem/lib/newgem.rb.tt index 7d8ad90ab0c..f441eab5f26 100644 --- a/lib/bundler/templates/newgem/lib/newgem.rb.tt +++ b/lib/bundler/templates/newgem/lib/newgem.rb.tt @@ -6,6 +6,7 @@ require "<%= config[:namespaced_path] %>/<%= config[:underscored_name] %>" <%- config[:constant_array].each_with_index do |c, i| -%> <%= " " * i %>module <%= c %> <%- end -%> +<%= " " * config[:constant_array].size %>class Error < StandardError; end %> <%= " " * config[:constant_array].size %># Your code goes here... <%- (config[:constant_array].size-1).downto(0) do |i| -%> <%= " " * i %>end diff --git a/spec/commands/newgem_spec.rb b/spec/commands/newgem_spec.rb index 1a3e8236b65..f4642787cbf 100644 --- a/spec/commands/newgem_spec.rb +++ b/spec/commands/newgem_spec.rb @@ -310,6 +310,10 @@ def create_temporary_dir(dir) expect(bundled_app("test_gem/lib/test_gem.rb").read).to match(%r{require "test_gem/version"}) end + it "creates a base error class" do + expect(bundled_app("test_gem/lib/test_gem.rb").read).to include("class Error < StandardError") + end + it "runs rake without problems" do system_gems ["rake-10.0.2"]