Skip to content

Commit fee4414

Browse files
authored
Merge pull request #112 from JoshDevHub/exercise_generator
Add an exercise generator to bootstrap directory structure for new exercises
2 parents 9ed5f67 + c23543b commit fee4414

File tree

8 files changed

+304
-0
lines changed

8 files changed

+304
-0
lines changed

README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,32 @@ If your shell reports that it cannot find the rspec binary, it may be necessary
4040
## Usage
4141

4242
Each directory contains a read me with instructions for the exercises within them.
43+
44+
## Contributing
45+
46+
Make sure that when changing an exercise or its tests that you also make the required changes to that exercise's solution under the `solutions/` directory.
47+
48+
If you're writing an entirely new exercise, there's a script provided to help generate the correct directory structure and boilerplate. An example use for if you wanted to create a new exercise in `ruby_basics` for working with Ruby blocks:
49+
50+
```bash
51+
bin/generate_exercise ruby_basics 13_blocks block_exercises
52+
```
53+
54+
This will setup the following structure automatically:
55+
56+
```bash
57+
.
58+
├── ruby_basics
59+
│ └── 13_blocks
60+
│ ├── exercises
61+
│ │ └── block_exercises.rb
62+
│ └── spec
63+
│ ├── block_exercises_spec.rb
64+
│ └── spec_helper.rb
65+
└── solutions
66+
└── ruby_basics
67+
└── 13_blocks
68+
├── exercises
69+
└── spec
70+
└── spec_helper.rb
71+
```

bin/generate_exercise

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/usr/bin/env ruby
2+
3+
require_relative "../generators/exercise_generator"
4+
5+
ExerciseGenerator.new(*ARGV).generate

generators/.rspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
--require spec_helper

generators/exercise_generator.rb

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
require "fileutils"
2+
3+
class ExerciseGenerator
4+
def initialize(namespace, exercise_group_name, exercise_name)
5+
@namespace = namespace
6+
@exercise_group_name = exercise_group_name
7+
@exercise_name = exercise_name
8+
end
9+
10+
TEMPLATE_DIR_PATH = "generators/exercise_template".freeze
11+
12+
def generate
13+
unless Dir.exist?(exercise_group_path)
14+
make_exercise_directory
15+
make_solutions_directory
16+
end
17+
18+
FileUtils.touch(exercise_file_path)
19+
File.write(spec_file_path, spec_template)
20+
end
21+
22+
private
23+
24+
attr_reader :namespace, :exercise_group_name, :exercise_name
25+
26+
def exercise_group_path
27+
File.join(namespace, exercise_group_name)
28+
end
29+
30+
def exercise_directory_path
31+
File.join(exercise_group_path, "exercises")
32+
end
33+
34+
def exercise_file_path
35+
File.join(exercise_directory_path, "#{exercise_name}.rb")
36+
end
37+
38+
def spec_file_path
39+
File.join(exercise_group_path, "spec", "#{exercise_name}_spec.rb")
40+
end
41+
42+
def spec_template
43+
<<~SPEC
44+
require 'spec_helper'
45+
require_relative '../exercises/#{exercise_name}'
46+
47+
RSpec.describe '' do
48+
end
49+
SPEC
50+
end
51+
52+
def make_exercise_directory
53+
FileUtils::mkdir_p(exercise_directory_path)
54+
FileUtils::copy_entry(TEMPLATE_DIR_PATH, exercise_group_path)
55+
end
56+
57+
def make_solutions_directory
58+
solutions_group_path = File.join("solutions", exercise_group_path)
59+
solutions_exercise_path = File.join("solutions", exercise_directory_path)
60+
61+
FileUtils::mkdir_p(solutions_exercise_path)
62+
FileUtils::copy_entry(TEMPLATE_DIR_PATH, solutions_group_path)
63+
end
64+
end

generators/exercise_template/.rspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
--require spec_helper --format documentation --color
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
RSpec.configure do |config|
2+
config.expect_with :rspec do |expectations|
3+
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
4+
end
5+
6+
config.mock_with :rspec do |mocks|
7+
mocks.verify_partial_doubles = true
8+
end
9+
10+
config.shared_context_metadata_behavior = :apply_to_host_groups
11+
end
12+
13+
module FormatterOverrides
14+
def dump_pending(_)
15+
end
16+
end
17+
18+
RSpec::Core::Formatters::DocumentationFormatter.prepend FormatterOverrides
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
require_relative "../exercise_generator"
2+
3+
describe ExerciseGenerator do
4+
describe "#generate" do
5+
subject(:generator) { described_class.new("test_namespace", "1_test_group", "test_exercise") }
6+
7+
before do
8+
allow(FileUtils).to receive(:mkdir_p)
9+
allow(FileUtils).to receive(:copy_entry)
10+
allow(FileUtils).to receive(:touch)
11+
allow(File).to receive(:write)
12+
end
13+
14+
it "sends a `::touch` message to `FileUtils` to create the exercise file in the `exercises` subdir of the exercise directory" do
15+
generator.generate
16+
17+
expected_filename = "test_namespace/1_test_group/exercises/test_exercise.rb"
18+
expect(FileUtils).to have_received(:touch).with(expected_filename)
19+
end
20+
21+
it "sends a `::write` message to `File` to create the spec file in the `spec` subdir of the exercise directory" do
22+
generator.generate
23+
24+
expected_filename = "test_namespace/1_test_group/spec/test_exercise_spec.rb"
25+
expected_content = <<~FILE_CONTENT
26+
require 'spec_helper'
27+
require_relative '../exercises/test_exercise'
28+
29+
RSpec.describe '' do
30+
end
31+
FILE_CONTENT
32+
33+
expect(File).to have_received(:write).with(expected_filename, expected_content)
34+
end
35+
36+
context "when the directory structure does not exist for the exercise" do
37+
before do
38+
allow(Dir).to receive(:exist?).and_return(false)
39+
end
40+
41+
it "sends a `::mkdir_p` message to `FileUtils` to create exercise path" do
42+
generator.generate
43+
44+
expected_path = "test_namespace/1_test_group/exercises"
45+
expect(FileUtils).to have_received(:mkdir_p).with(expected_path)
46+
end
47+
48+
it "sends a `::copy_entry` message to `FileUtils` to copy boilerplate into exercise directory" do
49+
generator.generate
50+
51+
expected_source = "generators/exercise_template"
52+
expected_target = "test_namespace/1_test_group"
53+
expect(FileUtils).to have_received(:copy_entry).with(expected_source, expected_target)
54+
end
55+
56+
it "sends a `::mkdir_p` message to `FileUtils` to create solutions path" do
57+
generator.generate
58+
59+
expected_path = "solutions/test_namespace/1_test_group/exercises"
60+
expect(FileUtils).to have_received(:mkdir_p).with(expected_path)
61+
end
62+
63+
it "sends a `::copy_entry` message to `FileUtils` to copy boilerplate into solutions directory" do
64+
generator.generate
65+
66+
expected_source = "generators/exercise_template"
67+
expected_target = "solutions/test_namespace/1_test_group"
68+
expect(FileUtils).to have_received(:copy_entry).with(expected_source, expected_target)
69+
end
70+
end
71+
72+
context "when the directory structure already exists for exercise" do
73+
before do
74+
allow(Dir).to receive(:exist?).and_return(true)
75+
end
76+
77+
it "does not send `::mkdir_p` message to `FileUtils`" do
78+
generator.generate
79+
expect(FileUtils).not_to have_received(:mkdir_p)
80+
end
81+
82+
it "does not send a `::copy_entry` message to `FileUtils`" do
83+
generator.generate
84+
expect(FileUtils).not_to have_received(:copy_entry)
85+
end
86+
end
87+
end
88+
end

generators/spec/spec_helper.rb

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
# This file was generated by the `rspec --init` command. Conventionally, all
2+
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3+
# The generated `.rspec` file contains `--require spec_helper` which will cause
4+
# this file to always be loaded, without a need to explicitly require it in any
5+
# files.
6+
#
7+
# Given that it is always loaded, you are encouraged to keep this file as
8+
# light-weight as possible. Requiring heavyweight dependencies from this file
9+
# will add to the boot time of your test suite on EVERY test run, even for an
10+
# individual file that may not need all of that loaded. Instead, consider making
11+
# a separate helper file that requires the additional dependencies and performs
12+
# the additional setup, and require it from the spec files that actually need
13+
# it.
14+
#
15+
# See https://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
16+
RSpec.configure do |config|
17+
# rspec-expectations config goes here. You can use an alternate
18+
# assertion/expectation library such as wrong or the stdlib/minitest
19+
# assertions if you prefer.
20+
config.expect_with :rspec do |expectations|
21+
# This option will default to `true` in RSpec 4. It makes the `description`
22+
# and `failure_message` of custom matchers include text for helper methods
23+
# defined using `chain`, e.g.:
24+
# be_bigger_than(2).and_smaller_than(4).description
25+
# # => "be bigger than 2 and smaller than 4"
26+
# ...rather than:
27+
# # => "be bigger than 2"
28+
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
29+
end
30+
31+
# rspec-mocks config goes here. You can use an alternate test double
32+
# library (such as bogus or mocha) by changing the `mock_with` option here.
33+
config.mock_with :rspec do |mocks|
34+
# Prevents you from mocking or stubbing a method that does not exist on
35+
# a real object. This is generally recommended, and will default to
36+
# `true` in RSpec 4.
37+
mocks.verify_partial_doubles = true
38+
end
39+
40+
# This option will default to `:apply_to_host_groups` in RSpec 4 (and will
41+
# have no way to turn it off -- the option exists only for backwards
42+
# compatibility in RSpec 3). It causes shared context metadata to be
43+
# inherited by the metadata hash of host groups and examples, rather than
44+
# triggering implicit auto-inclusion in groups with matching metadata.
45+
config.shared_context_metadata_behavior = :apply_to_host_groups
46+
47+
# The settings below are suggested to provide a good initial experience
48+
# with RSpec, but feel free to customize to your heart's content.
49+
=begin
50+
# This allows you to limit a spec run to individual examples or groups
51+
# you care about by tagging them with `:focus` metadata. When nothing
52+
# is tagged with `:focus`, all examples get run. RSpec also provides
53+
# aliases for `it`, `describe`, and `context` that include `:focus`
54+
# metadata: `fit`, `fdescribe` and `fcontext`, respectively.
55+
config.filter_run_when_matching :focus
56+
57+
# Allows RSpec to persist some state between runs in order to support
58+
# the `--only-failures` and `--next-failure` CLI options. We recommend
59+
# you configure your source control system to ignore this file.
60+
config.example_status_persistence_file_path = "spec/examples.txt"
61+
62+
# Limits the available syntax to the non-monkey patched syntax that is
63+
# recommended. For more details, see:
64+
# https://rspec.info/features/3-12/rspec-core/configuration/zero-monkey-patching-mode/
65+
config.disable_monkey_patching!
66+
67+
# This setting enables warnings. It's recommended, but in some cases may
68+
# be too noisy due to issues in dependencies.
69+
config.warnings = true
70+
71+
# Many RSpec users commonly either run the entire suite or an individual
72+
# file, and it's useful to allow more verbose output when running an
73+
# individual spec file.
74+
if config.files_to_run.one?
75+
# Use the documentation formatter for detailed output,
76+
# unless a formatter has already been configured
77+
# (e.g. via a command-line flag).
78+
config.default_formatter = "doc"
79+
end
80+
81+
# Print the 10 slowest examples and example groups at the
82+
# end of the spec run, to help surface which specs are running
83+
# particularly slow.
84+
config.profile_examples = 10
85+
86+
# Run specs in random order to surface order dependencies. If you find an
87+
# order dependency and want to debug it, you can fix the order by providing
88+
# the seed, which is printed after each run.
89+
# --seed 1234
90+
config.order = :random
91+
92+
# Seed global randomization in this process using the `--seed` CLI option.
93+
# Setting this allows you to use `--seed` to deterministically reproduce
94+
# test failures related to randomization by passing the same `--seed` value
95+
# as the one that triggered the failure.
96+
Kernel.srand config.seed
97+
=end
98+
end

0 commit comments

Comments
 (0)