stimpack 0.6.0 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e6ad8e03da3ba828985b82eccb3840f6f4122d5fe38bcbc7905734a566c7d4cc
4
- data.tar.gz: cba30db4834bd3e056ea19c76067945d73378c799b603f1cc931d43ac409cf59
3
+ metadata.gz: a0fa94f149c0035eb12465252c3262c7bea9df7fad9ea00efb5014d6484b00d1
4
+ data.tar.gz: a20a5aad13953de01410054b99e3bd2932df56a9e879edc6a14bd3601baca0ba
5
5
  SHA512:
6
- metadata.gz: d38f3c326b2fa6f327de5b0d2da6e77c1ea58e54c28df8e51887247743699ad3f8cc5e4dc5f769c50eaa80011a6ee8374fd21c4173e3c5c0b59a26ebb9609f82
7
- data.tar.gz: 20bd2048490efd0eaa00380263c31c98e1eededa46458e61195c62b4f2c2f4102db1f080eb03efaf3f9496bd5d1777ec06f48109d1cf2dab52332900e41cde98
6
+ metadata.gz: a7634623fc5fb02369e979fd62f1d816dafc734eacd0d2bab2a47acdce9ec1eeb1b29eccbb88193263623feb3eb94ccf1929065e919bd8d787fce43300dcafbb
7
+ data.tar.gz: 700f5b8c757d98b6340fb55dee4812c86d449162e65aa1a4946eb5557981ef5b37b306b2f7327703e6f6ddfcd5024db0eb60112c7b087170204704b1ba71fa70
data/README.md CHANGED
@@ -92,6 +92,35 @@ metadata:
92
92
  engine: true
93
93
  ```
94
94
 
95
+ ### RSpec Integration
96
+ Simply add `--require stimpack/rspec` to your `.rspec`.
97
+ Or, if you'd like, pass it as an argument to `rspec`:
98
+
99
+ ```
100
+ $ rspec --require stimpack/rspec ...
101
+ ```
102
+
103
+ Integration will allow you to run tests as such:
104
+ ```
105
+ # Run all specs in your entire application (packs and all):
106
+ rspec
107
+
108
+ # Run just that one test:
109
+ rspec spec/some/specific_spec.rb
110
+
111
+ # Run all tests under the "foobar" pack and all the tests of its nested packs:
112
+ rspec packs/foobar
113
+
114
+ # Same as above but also adds the "binbaz" pack:
115
+ rspec packs/foobar pack/binbaz
116
+
117
+ # Run all test files inside the "packs/foobar/spec" directory:
118
+ rspec packs/foobar/spec
119
+
120
+ # Run all specs under the "packs/foobar/nested_pack" pack:
121
+ rspec packs/foobar/nested_pack
122
+ ```
123
+
95
124
  ## Contributing
96
125
 
97
126
  Bug reports and pull requests are welcome on GitHub at https://github.com/Gusto/stimpack.
@@ -1,10 +1,10 @@
1
1
  module Stimpack
2
2
  module Integrations
3
3
  class FactoryBot
4
- def self.install(app)
4
+ def initialize(app)
5
5
  return unless app.config.respond_to?(:factory_bot)
6
6
 
7
- Packs.each do |pack|
7
+ Packs.all.each do |pack|
8
8
  app.config.factory_bot.definition_file_paths << pack.relative_path.join("spec/factories").to_s
9
9
  end
10
10
  end
@@ -1,17 +1,53 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "active_support/inflections"
4
+
3
5
  module Stimpack
4
6
  module Integrations
5
7
  class Rails
6
- def self.install(app)
8
+ def initialize(app)
9
+ @app = app
10
+
7
11
  Stimpack.config.paths.freeze
12
+ create_engines
13
+ inject_paths
14
+ end
15
+
16
+ def create_engines
17
+ Packs.all.each do |pack|
18
+ next unless pack.config.engine?
19
+
20
+ pack.engine = create_engine(pack)
21
+ end
22
+ end
8
23
 
9
- Packs.each do |pack|
24
+ def inject_paths
25
+ Packs.all.each do |pack|
10
26
  Stimpack.config.paths.each do |path|
11
- app.paths[path] << pack.path.join(path)
27
+ @app.paths[path] << pack.path.join(path)
12
28
  end
13
29
  end
14
30
  end
31
+
32
+ private
33
+
34
+ def create_namespace(name)
35
+ namespace = ActiveSupport::Inflector.camelize(name)
36
+ namespace.split("::").reduce(Object) do |base, mod|
37
+ if base.const_defined?(mod, false)
38
+ base.const_get(mod, false)
39
+ else
40
+ base.const_set(mod, Module.new)
41
+ end
42
+ end
43
+ end
44
+
45
+ def create_engine(pack)
46
+ name = pack.path.relative_path_from(Stimpack::Packs.root)
47
+ namespace = create_namespace(pack.name)
48
+ stim = Stim.new(pack, namespace)
49
+ namespace.const_set("Engine", Class.new(::Rails::Engine)).include(stim)
50
+ end
15
51
  end
16
52
  end
17
53
  end
@@ -1,19 +1,46 @@
1
1
  module Stimpack
2
2
  module Integrations
3
3
  class RSpec
4
- def self.install(app)
5
- return unless defined?(::RSpec)
6
- # Sometimes, the `rspec-rails` gem is installed in the development
7
- # group. This means the ::RSpec module will be defined. However, this
8
- # doesn't mean that we've loaded rspec-core, or even about to run tests.
9
- # Let's make sure we are actually loading the test environment before
10
- # installing our integration. An easy of doing this is seeing of the
11
- # configuration method exists on RSpec.
12
- return unless ::RSpec.respond_to?(:configuration)
4
+ def initialize
5
+ # This is the list of directories RSpec was told to run.
6
+ to_run = ::RSpec.configuration.instance_variable_get(:@files_or_directories_to_run)
7
+ default_path = ::RSpec.configuration.default_path
13
8
 
14
- Packs.each do |pack|
15
- ::RSpec.configuration.pattern.concat(",#{pack.relative_path.join("spec/**/*_spec.rb")}")
9
+ if to_run == [default_path]
10
+ # This is the default case when you run `rspec`. We want to add all the pack's spec paths
11
+ # to the collection of directories to run.
12
+
13
+ pack_paths = Packs.all.map do |pack|
14
+ spec_path = pack.relative_path.join(default_path)
15
+ spec_path.to_s if spec_path.exist?
16
+ end
17
+
18
+ to_run.concat(pack_paths)
19
+ else
20
+ # This is when `rspec` is run with a list of directories or files. We scan this list to see
21
+ # if any of them matches a pack's directory. If it does, we concat the `default_path` to the
22
+ # end of it.
23
+ #
24
+ # packs/my_pack => packs/my_pack/spec
25
+ #
26
+ # If it doesn't match a pack path, we leave it alone.
27
+
28
+ to_run.map! do |path|
29
+ if pack = Packs.all_by_path[path]
30
+ [
31
+ pack,
32
+ *Packs.all(pack)
33
+ ].map do |pack|
34
+ spec_path = pack.relative_path.join(default_path)
35
+ spec_path.to_s if spec_path.exist?
36
+ end
37
+ else
38
+ path
39
+ end
40
+ end
16
41
  end
42
+
43
+ ::RSpec.configuration.files_or_directories_to_run = to_run.flatten.compact.uniq
17
44
  end
18
45
  end
19
46
  end
data/lib/stimpack/pack.rb CHANGED
@@ -1,53 +1,28 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "pathname"
4
+
3
5
  module Stimpack
4
6
  class Pack
5
7
  PACKAGE_FILE = "package.yml"
6
8
 
7
9
  autoload :Configuration, "stimpack/pack/configuration"
8
10
 
9
- attr_reader :name
10
11
  attr_reader :path
11
- attr_reader :engine
12
-
13
- def self.create(path)
14
- new(path) if path.join(PACKAGE_FILE).exist?
15
- end
12
+ attr_reader :name
13
+ attr_accessor :engine
16
14
 
17
15
  def initialize(path)
18
16
  @path = path
19
- @name = path.relative_path_from(Packs.root)
20
-
21
- if config.engine?
22
- @engine = create_engine
23
- end
17
+ @name = path.basename.to_s
24
18
  end
25
19
 
26
20
  def relative_path
27
- @relative_path ||= path.relative_path_from(Rails.root)
21
+ @relative_path ||= path.relative_path_from(Stimpack.root)
28
22
  end
29
23
 
30
24
  def config
31
25
  @config ||= Configuration.new(path.join(PACKAGE_FILE))
32
26
  end
33
-
34
- private
35
-
36
- def create_engine
37
- namespace = create_namespace(name)
38
- stim = Stim.new(self, namespace)
39
- namespace.const_set("Engine", Class.new(Rails::Engine)).include(stim)
40
- end
41
-
42
- def create_namespace(name)
43
- namespace = ActiveSupport::Inflector.camelize(name)
44
- namespace.split("::").reduce(Object) do |base, mod|
45
- if base.const_defined?(mod)
46
- base.const_get(mod)
47
- else
48
- base.const_set(mod, Module.new)
49
- end
50
- end
51
- end
52
27
  end
53
28
  end
@@ -1,43 +1,47 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "active_support"
4
- require "pathname"
5
- require "rails"
6
4
 
7
5
  module Stimpack
8
6
  module Packs
7
+ PACKAGE_GLOB_PATTERN = "*/#{Pack::PACKAGE_FILE}"
8
+
9
9
  class << self
10
10
  def root
11
- @root ||= Rails.root.join(Stimpack.config.root)
11
+ @root ||= Stimpack.root.join(Stimpack.config.root)
12
12
  end
13
13
 
14
- def resolve
15
- # Gather all the parent packs and the children packs.
16
- package_locations = root.glob('*/{package.yml,*/package.yml}').map(&:dirname)
17
- package_locations.sort!.each do |path|
18
- next unless pack = Pack.create(path)
19
- @packs[pack.name] = pack
14
+ def find(path)
15
+ @find_pack_paths ||= all_by_path.keys.sort_by(&:length).reverse!.map { |path| "#{path}/" }
16
+ path = "#{path}/"
17
+ @find_pack_paths.find do |pack_path|
18
+ path.start_with?(pack_path)
20
19
  end
21
- @packs.freeze
22
20
  end
23
21
 
24
- def find(path)
25
- path = "#{path}/"
22
+ def all(parent = nil)
23
+ @all ||= resolve(root).each_with_object({}) do |pack, map|
24
+ map[pack] = resolve(pack.path).freeze
25
+ end.freeze
26
26
 
27
- @packs.values.find do |pack|
28
- path.start_with?("#{pack.path}/")
27
+ if parent
28
+ @all[parent]
29
+ else
30
+ @all.keys + @all.values.flatten
29
31
  end
30
32
  end
31
33
 
32
- def [](name)
33
- @packs[name]
34
+ def all_by_path
35
+ @all_by_path ||= all.each_with_object({}) do |pack, map|
36
+ map[pack.relative_path.to_s] = pack
37
+ end
34
38
  end
35
39
 
36
- def each(*args, &block)
37
- @packs.each_value(*args, &block)
40
+ private
41
+
42
+ def resolve(directory)
43
+ directory.glob(PACKAGE_GLOB_PATTERN).map { |path| Pack.new(path.dirname) }
38
44
  end
39
45
  end
40
-
41
- @packs = {}
42
46
  end
43
47
  end
@@ -1,9 +1,11 @@
1
- require "rails"
1
+ require "rails/railtie"
2
2
 
3
3
  module Stimpack
4
4
  class Railtie < Rails::Railtie
5
5
  config.before_configuration do |app|
6
- Stimpack.load(app)
6
+ Integrations::Rails.new(app)
7
+ Integrations::FactoryBot.new(app)
8
+
7
9
  # This is not used within stimpack. Rather, this allows OTHER tools to
8
10
  # hook into Stimpack via ActiveSupport hooks.
9
11
  ActiveSupport.run_load_hooks(:stimpack, Packs)
@@ -0,0 +1,3 @@
1
+ require "stimpack"
2
+
3
+ Stimpack::Integrations::RSpec.new
@@ -1,3 +1,3 @@
1
1
  module Stimpack
2
- VERSION = "0.6.0".freeze
2
+ VERSION = "0.7.0".freeze
3
3
  end
data/lib/stimpack.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require "active_support"
2
+ require "rails/application"
2
3
 
3
4
  module Stimpack
4
5
  extend ActiveSupport::Autoload
@@ -14,16 +15,8 @@ module Stimpack
14
15
  class << self
15
16
  attr_reader :config
16
17
 
17
- def load(app)
18
- Packs.resolve
19
-
20
- Integrations::Rails.install(app)
21
- Integrations::FactoryBot.install(app)
22
- Integrations::RSpec.install(app)
23
- end
24
-
25
- def [](name)
26
- Packs[name.to_s]
18
+ def root
19
+ @root ||= Rails::Application.find_root(Dir.pwd)
27
20
  end
28
21
  end
29
22
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: stimpack
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.0
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ngan Pham
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-08-12 00:00:00.000000000 Z
11
+ date: 2022-08-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -115,6 +115,7 @@ files:
115
115
  - lib/stimpack/pack/configuration.rb
116
116
  - lib/stimpack/packs.rb
117
117
  - lib/stimpack/railtie.rb
118
+ - lib/stimpack/rspec.rb
118
119
  - lib/stimpack/stim.rb
119
120
  - lib/stimpack/version.rb
120
121
  homepage: https://github.com/Gusto/stimpack