Skip to content

Commit af029ca

Browse files
authored
Merge pull request #79 from opal/elia/project-initializer
Add a project initializer (+ fixes)
2 parents 39abf07 + ca2408c commit af029ca

File tree

7 files changed

+85
-6
lines changed

7 files changed

+85
-6
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
# Opal-RSpec Changelog
2+
3+
## 0.7.1 - unreleased
4+
5+
- add a project initializer: `opal-rspec --init`
6+
7+
18
## 0.7.0 - 2019-02-03
29

310
- Drop support for the legacy async syntax

exe/opal-rspec

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ require 'optparse'
1010

1111
options = {}
1212
OptionParser.new do |parser|
13+
parser.on('--init', 'Initialize your project with Opal-RSpec.') do |_cmd|
14+
Opal::RSpec::ProjectInitializer.new.run
15+
exit
16+
end
17+
1318
parser.on('-c', '--[no-]color', '--[no-]colour', 'Enable color in the output.') do |o|
1419
options[:color] = o
1520
end
@@ -23,17 +28,21 @@ OptionParser.new do |parser|
2328
options[:formatters] ||= []
2429
options[:formatters] << [o]
2530
end
31+
2632
parser.on('-r', '--require PATH', 'Require a file.') do |path|
2733
options[:requires] ||= []
2834
options[:requires] << path
2935
end
36+
3037
parser.on('-I PATH', 'Specify PATH to add to $LOAD_PATH (may be used more than once).') do |dir|
3138
options[:includes] ||= []
3239
options[:includes] << dir
3340
end
41+
3442
parser.on('-P', '--pattern PATTERN', 'Load files matching pattern (default: "spec-opal/**/*_spec.rb").') do |o|
3543
options[:pattern] = o
3644
end
45+
3746
parser.on('--default-path PATH', 'Set the default path where RSpec looks for examples (can',
3847
' be a path to a file or a directory).') do |path|
3948
options[:default_path] = path
@@ -44,9 +53,7 @@ OptionParser.new do |parser|
4453
parser.on('-R', '--runner NAME', 'Use a different JS runner (default is nodejs)') do |name|
4554
options[:runner] = name
4655
end
47-
4856
end.parse!
49-
# p options: options
5057

5158
runner = Opal::RSpec::Runner.new do |server, config|
5259
spec_opts = []
@@ -55,7 +62,8 @@ runner = Opal::RSpec::Runner.new do |server, config|
5562
raise "can accept only one formatter at this time" if options[:formatters] && options[:formatters].size != 1
5663
spec_opts += ['--format', options[:formatters].flatten.first] if options[:formatters]
5764
raw_files = ARGV
58-
# p raw_files: ARGV
65+
raw_files = ['spec-opal'] if raw_files.empty?
66+
5967
files = raw_files.flat_map do |file|
6068
if File.directory? file
6169
Dir[File.join(file, '**{,/*/**}/*_spec{.js,}.rb')]
@@ -65,14 +73,12 @@ runner = Opal::RSpec::Runner.new do |server, config|
6573
raise "Can't stat path: #{file}"
6674
end
6775
end
68-
# p files: files
6976

7077
options[:includes].each {|dir| server.append_path dir} if options[:includes]
7178
config.default_path = options[:default_path] if options[:default_path]
7279
config.spec_opts = spec_opts.shelljoin
7380
config.files = files
7481
end
7582

76-
# puts runner.command
7783
runner.run
7884

lib/opal/rspec.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
module Opal
2020
module RSpec
21+
autoload :ProjectInitializer, 'opal/rspec/project_initializer'
22+
2123
def self.spec_opts_code(spec_opts)
2224
code = []
2325
if spec_opts && !spec_opts.empty?

lib/opal/rspec/locator.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ module RSpec
99
class Locator
1010
include ::RSpec::Core::RubyProject
1111

12-
DEFAULT_PATTERN = 'spec-opal/**/*_spec{.js,}.{rb,opal}'
12+
DEFAULT_GLOB = '**{,/*/**}/*_spec{.js,}.{rb,opal}'
13+
DEFAULT_PATTERN = "spec-opal/#{DEFAULT_GLOB}"
1314
DEFAULT_DEFAULT_PATH = 'spec-opal'
1415

1516
attr_accessor :spec_pattern, :spec_exclude_pattern, :spec_files, :default_path

lib/opal/rspec/project_initializer.rb

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
require 'fileutils'
2+
3+
module Opal
4+
module RSpec
5+
# @private
6+
# Generates conventional files for an rspec project
7+
class ProjectInitializer
8+
attr_reader :destination, :stream, :template_path
9+
10+
DOT_RSPEC_FILE = '.rspec-opal'
11+
SPEC_HELPER_FILE = 'spec-opal/spec_helper.rb'
12+
13+
def initialize(opts={})
14+
@destination = opts.fetch(:destination, Dir.getwd)
15+
@stream = opts.fetch(:report_stream, $stdout)
16+
@template_path = opts.fetch(:template_path) do
17+
File.expand_path("../project_initializer", __FILE__)
18+
end
19+
end
20+
21+
def run
22+
copy_template DOT_RSPEC_FILE
23+
copy_template SPEC_HELPER_FILE
24+
end
25+
26+
private
27+
28+
def copy_template(file)
29+
destination_file = File.join(destination, file)
30+
return report_exists(file) if File.exist?(destination_file)
31+
32+
report_creating(file)
33+
FileUtils.mkdir_p(File.dirname(destination_file))
34+
File.open(destination_file, 'w') do |f|
35+
f.write File.read(File.join(template_path, file))
36+
end
37+
end
38+
39+
def report_exists(file)
40+
stream.puts " exist #{file}"
41+
end
42+
43+
def report_creating(file)
44+
stream.puts " create #{file}"
45+
end
46+
end
47+
end
48+
end
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
--color
2+
--require spec_helper
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
RSpec.configure do |config|
2+
# Run specs in random order to surface order dependencies. If you find an
3+
# order dependency and want to debug it, you can fix the order by providing
4+
# the seed, which is printed after each run.
5+
# --seed 1234
6+
config.order = :random
7+
8+
# Seed global randomization in this process using the `--seed` CLI option.
9+
# Setting this allows you to use `--seed` to deterministically reproduce
10+
# test failures related to randomization by passing the same `--seed` value
11+
# as the one that triggered the failure.
12+
Kernel.srand config.seed
13+
end

0 commit comments

Comments
 (0)