rspec_in_context 1.0.1.2 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: '08306b0c06181ef96f4acdc36980f27792715378cb624eb7ba633c4945e90c7e'
4
- data.tar.gz: 2cd8c33b01f3ebcb9720e43bcc4cba0fc4562808681f040ca10cc47816e33c27
3
+ metadata.gz: 1483e9b55b3cabffad15491bec3e5c3f006b421c4fa62099cce8effc4bf428b3
4
+ data.tar.gz: 92e35ec980fa84397bb5b33f39e6302825c8ae60aaa581cc009befa507f9b98e
5
5
  SHA512:
6
- metadata.gz: 0c91690c0ad28afc64c1999a1bcdfa6c0e51556f808b7cf337ea9d2ec59ebf01b50d03a492bd96957596d319a1a69204232a001bc432c6001c5cd6a69e0b4316
7
- data.tar.gz: 4d3b90906fbe61f9485c6f415e86b24dbb7d44343e87c0015915899f384bbae10f1044fbcf4b90cb585ef238fbbee65edb1e4b573968804d8f77a8a7203233be
6
+ metadata.gz: 6f2b2c8e5a4f5b65692b73cb81558a4aaaed3797da5b3aded5b3dc1ff8e943d3968f68df69007d9d15864ca1d14b934f7c51a57fc2a48d6a2757ab63c494ba4f
7
+ data.tar.gz: 6fd7be08a125054917af01ba365d34a2dcae63895f4aba95731e001dbd7fd757d4d617049e6f9d36ca576d817d35c543f76ae8fb10dc0172ff94bd2289e06217
@@ -6,7 +6,10 @@ on:
6
6
  - main
7
7
 
8
8
  jobs:
9
- test-2_5:
9
+ tests:
10
+ strategy:
11
+ matrix:
12
+ ruby: [2.5, 2.6, 2.7, 3.0]
10
13
  runs-on: ubuntu-latest
11
14
 
12
15
  steps:
@@ -14,52 +17,7 @@ jobs:
14
17
  - name: Set up Ruby
15
18
  uses: ruby/setup-ruby@v1
16
19
  with:
17
- ruby-version: 2.5
18
- - name: Install dependencies
19
- run: bundle install
20
- - name: Run linter
21
- run: bundle exec rubocop
22
- - name: Run tests
23
- run: bundle exec rspec
24
- test-2_6:
25
- runs-on: ubuntu-latest
26
-
27
- steps:
28
- - uses: actions/checkout@v2
29
- - name: Set up Ruby
30
- uses: ruby/setup-ruby@v1
31
- with:
32
- ruby-version: 2.6
33
- - name: Install dependencies
34
- run: bundle install
35
- - name: Run linter
36
- run: bundle exec rubocop
37
- - name: Run tests
38
- run: bundle exec rspec
39
- test-2_7:
40
- runs-on: ubuntu-latest
41
-
42
- steps:
43
- - uses: actions/checkout@v2
44
- - name: Set up Ruby
45
- uses: ruby/setup-ruby@v1
46
- with:
47
- ruby-version: 2.7
48
- - name: Install dependencies
49
- run: bundle install
50
- - name: Run linter
51
- run: bundle exec rubocop
52
- - name: Run tests
53
- run: bundle exec rspec
54
- test-3_0:
55
- runs-on: ubuntu-latest
56
-
57
- steps:
58
- - uses: actions/checkout@v2
59
- - name: Set up Ruby
60
- uses: ruby/setup-ruby@v1
61
- with:
62
- ruby-version: 3.0
20
+ ruby-version: ${{ matrix.ruby }}
63
21
  - name: Install dependencies
64
22
  run: bundle install
65
23
  - name: Run linter
@@ -67,7 +25,7 @@ jobs:
67
25
  - name: Run tests
68
26
  run: bundle exec rspec
69
27
  release:
70
- needs: [test-2_5, test-2_6, test-2_7, test-3_0]
28
+ needs: [tests]
71
29
  runs-on: ubuntu-latest
72
30
 
73
31
  steps:
@@ -84,5 +42,7 @@ jobs:
84
42
  run: "mkdir -p ~/.gem && echo -e \"---\\r\\n:rubygems_api_key: $RUBYGEM_KEY\" > ~/.gem/credentials && chmod 0600 ~/.gem/credentials"
85
43
  - name: Setup username/email
86
44
  run: "git config --global user.email zaratan@hey.com && git config --global user.name \"Denis <Zaratan> Pasin\""
87
- - name: Publish
88
- run: rake release
45
+ - name: Fetch tags from remote
46
+ run: "git fetch -t"
47
+ - name: Publish if version change
48
+ run: 'git diff `git tag | tail -1` lib/rspec_in_context/version.rb | grep -E "^\+.*VERSION" && rake release || echo "No release for now"'
@@ -6,7 +6,10 @@ on:
6
6
  - main
7
7
 
8
8
  jobs:
9
- test-2_5:
9
+ tests:
10
+ strategy:
11
+ matrix:
12
+ ruby: [2.5, 2.6, 2.7, 3.0]
10
13
  runs-on: ubuntu-latest
11
14
 
12
15
  steps:
@@ -14,52 +17,7 @@ jobs:
14
17
  - name: Set up Ruby
15
18
  uses: ruby/setup-ruby@v1
16
19
  with:
17
- ruby-version: 2.5
18
- - name: Install dependencies
19
- run: bundle install
20
- - name: Run linter
21
- run: bundle exec rubocop
22
- - name: Run tests
23
- run: bundle exec rspec
24
- test-2_6:
25
- runs-on: ubuntu-latest
26
-
27
- steps:
28
- - uses: actions/checkout@v2
29
- - name: Set up Ruby
30
- uses: ruby/setup-ruby@v1
31
- with:
32
- ruby-version: 2.6
33
- - name: Install dependencies
34
- run: bundle install
35
- - name: Run linter
36
- run: bundle exec rubocop
37
- - name: Run tests
38
- run: bundle exec rspec
39
- test-2_7:
40
- runs-on: ubuntu-latest
41
-
42
- steps:
43
- - uses: actions/checkout@v2
44
- - name: Set up Ruby
45
- uses: ruby/setup-ruby@v1
46
- with:
47
- ruby-version: 2.7
48
- - name: Install dependencies
49
- run: bundle install
50
- - name: Run linter
51
- run: bundle exec rubocop
52
- - name: Run tests
53
- run: bundle exec rspec
54
- test-3_0:
55
- runs-on: ubuntu-latest
56
-
57
- steps:
58
- - uses: actions/checkout@v2
59
- - name: Set up Ruby
60
- uses: ruby/setup-ruby@v1
61
- with:
62
- ruby-version: 3.0
20
+ ruby-version: ${{ matrix.ruby }}
63
21
  - name: Install dependencies
64
22
  run: bundle install
65
23
  - name: Run linter
@@ -18,6 +18,10 @@ Metrics/BlockLength:
18
18
  - 'vendor/bundle'
19
19
  - '*.gemspec'
20
20
 
21
+ Style/GlobalVars:
22
+ Exclude:
23
+ - spec/rspec_in_context/in_context_spec.rb
24
+
21
25
  Naming/MethodParameterName:
22
26
  Enabled: false
23
27
 
@@ -6,10 +6,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
6
6
 
7
7
  ## [Unreleased]
8
8
 
9
+ ## [1.1.0] - 2020-12-27
10
+ ### Added
11
+ - **BREAKING** Option to silence in_context block. They used to always wrap themself into a context block with their name. This is not the case anymore. All in_context are silent unless explicitely declared as not.
12
+
9
13
  ## [1.0.1.2] - 2020-12-26
10
14
  ### Added
11
15
  - Changelog
12
16
  - Support ruby 3.0
13
17
 
14
- [Unreleased]: https://github.com/zaratan/active_shotgun/compare/v1.0.1.2...HEAD
15
- [1.0.1.2]: https://github.com/zaratan/active_shotgun/releases/tag/v1.0.1.2
18
+ [Unreleased]: https://github.com/zaratan/rspec_in_context/compare/v1.1.0...HEAD
19
+ [1.1.0]: https://github.com/zaratan/rspec_in_context/releases/tag/v1.1.0
20
+ [1.0.1.2]: https://github.com/zaratan/rspec_in_context/releases/tag/v1.0.1.2
data/README.md CHANGED
@@ -219,6 +219,54 @@ in_context "namespaced context", namespace: "namespace name"
219
219
  in_context "namespaced context", ns: "namespace name"
220
220
  ```
221
221
 
222
+ #### Making `in_context` adverstise itself
223
+
224
+ The fact that a `in_context` block is used inside the test is silent and invisible by default.
225
+
226
+ But, there's some case where it helps to make the `in_context` to wrap its execution in a `context` block.
227
+ For example:
228
+ ```ruby
229
+ define_context "with my_var defined" do
230
+ before do
231
+ described_class.set_my_var(true)
232
+ end
233
+
234
+ it "works"
235
+ end
236
+
237
+ define_context "without my_var defined" do
238
+ it "doesn't work"
239
+ end
240
+
241
+ RSpec.describe MyNiceClass do
242
+ in_context "with my_var defined"
243
+ in_context "without my_var defined"
244
+ end
245
+ ```
246
+ Using a `rspec -f doc` will only print "MyNiceClass works" and "MyNiceClass doesn't work" which is not really a good documentation.
247
+
248
+ So, you can define a context specifying it not to be `silent` or to `print_context`.
249
+ For example :
250
+ ```ruby
251
+ define_context "with my_var defined", silent: false do
252
+ before do
253
+ described_class.set_my_var(true)
254
+ end
255
+
256
+ it "works"
257
+ end
258
+
259
+ define_context "without my_var defined", print_context: true do
260
+ it "doesn't work"
261
+ end
262
+
263
+ RSpec.describe MyNiceClass do
264
+ in_context "with my_var defined"
265
+ in_context "without my_var defined"
266
+ end
267
+ ```
268
+ Will print "MyNiceClass with my_var defined works" and "MyNiceClass without my_var defined doesn't work". Which is valid and readable documentation.
269
+
222
270
  ## Development
223
271
 
224
272
 
@@ -21,9 +21,12 @@ module RSpec
21
21
  # @param name [String, Symbol] Name of the defined context
22
22
  # @param namespace [String, Symbol] Namespace where to store your context
23
23
  # @param ns Alias of namespace
24
+ # @param silent [Boolean] Does the in_context should wrap itself into a context block with its name
25
+ # @param print_context [Boolean] Reverse alias of silent
24
26
  # @param block [Proc] code that will be injected later
25
- def self.define_context(name, namespace: nil, ns: nil, &block)
27
+ def self.define_context(name, namespace: nil, ns: nil, silent: true, print_context: nil, &block)
26
28
  namespace ||= ns
27
- RspecInContext::InContext.outside_define_context(name, namespace, &block)
29
+ silent = print_context.nil? ? silent : !print_context
30
+ RspecInContext::InContext.outside_define_context(name, namespace, silent, &block)
28
31
  end
29
32
  end
@@ -6,7 +6,12 @@ module RspecInContext
6
6
  class NoContextFound < StandardError; end
7
7
 
8
8
  # Context struct
9
- Context = Struct.new(:block, :owner, :name, :namespace)
9
+ # @attr [Proc] block what will be executed in the test context
10
+ # @attr [Class] owner current rspec context class. This will be used to know where a define_context has been defined
11
+ # @attr [String | Symbol] name represent the name by which the context can be find.
12
+ # @attr [String | Symbol] namespace namespace for context names to avoid collisions
13
+ # @attr [Boolean] silent does the in_context should wrap itself into a context with its name upon execution
14
+ Context = Struct.new(:block, :owner, :name, :namespace, :silent)
10
15
 
11
16
  # Main module containing almost every methods
12
17
  module InContext
@@ -29,10 +34,10 @@ module RspecInContext
29
34
  # @api private
30
35
  #
31
36
  # @note Will warn if a context is overriden
32
- def add_context(context_name, owner = nil, namespace = nil, &block)
37
+ def add_context(context_name, owner = nil, namespace = nil, silent = true, &block) # rubocop:disable Style/OptionalBooleanParameter
33
38
  namespace ||= GLOBAL_CONTEXT
34
39
  warn("Overriding an existing context: #{context_name}@#{namespace}") if contexts[namespace][context_name]
35
- contexts[namespace][context_name] = Context.new(block, owner, context_name, namespace)
40
+ contexts[namespace][context_name] = Context.new(block, owner, context_name, namespace, silent)
36
41
  end
37
42
 
38
43
  # Find a context.
@@ -63,8 +68,8 @@ module RspecInContext
63
68
 
64
69
  # @api private
65
70
  # Define a context from outside a RSpec.describe block
66
- def outside_define_context(context_name, namespace, &block)
67
- InContext.add_context(context_name, nil, namespace, &block)
71
+ def outside_define_context(context_name, namespace, silent, &block)
72
+ InContext.add_context(context_name, nil, namespace, silent, &block)
68
73
  end
69
74
  end
70
75
 
@@ -80,8 +85,13 @@ module RspecInContext
80
85
  def in_context(context_name, *args, namespace: nil, ns: nil, &block)
81
86
  namespace ||= ns
82
87
  Thread.current[:test_block] = block
88
+ context_to_exec = InContext.find_context(context_name, namespace)
89
+ if context_to_exec.silent
90
+ return instance_exec(*args, &context_to_exec.block)
91
+ end
92
+
83
93
  context(context_name.to_s) do
84
- instance_exec(*args, &InContext.find_context(context_name, namespace).block)
94
+ instance_exec(*args, &context_to_exec.block)
85
95
  end
86
96
  end
87
97
 
@@ -102,12 +112,15 @@ module RspecInContext
102
112
  # It helps reducing colisions when you define "global" contexts
103
113
  # @param ns [String, Symbol] Alias of namespace
104
114
  # @param block [Proc] Contain the code that will be injected with #in_context later
115
+ # @param silent [Boolean] Does the in_context should wrap itself into a context block with its name
116
+ # @param print_context [Boolean] Reverse alias of silent
105
117
  #
106
118
  # @note contexts are scoped to the block they are defined in.
107
- def define_context(context_name, namespace: nil, ns: nil, &block)
119
+ def define_context(context_name, namespace: nil, ns: nil, silent: true, print_context: nil, &block)
108
120
  namespace ||= ns
121
+ silent = print_context.nil? ? silent : !print_context
109
122
  instance_exec do
110
- InContext.add_context(context_name, hooks.instance_variable_get(:@owner), namespace, &block)
123
+ InContext.add_context(context_name, hooks.instance_variable_get(:@owner), namespace, silent, &block)
111
124
  end
112
125
  end
113
126
  end
@@ -2,5 +2,5 @@
2
2
 
3
3
  module RspecInContext
4
4
  # Version of the gem
5
- VERSION = "1.0.1.2"
5
+ VERSION = "1.1.0"
6
6
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rspec_in_context
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1.2
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Denis <Zaratan> Pasin
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-12-25 00:00:00.000000000 Z
11
+ date: 2020-12-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport