Skip to content

Commit 5a511b2

Browse files
johvetJohannes Vetter
andauthored
Allow the specify-groups option to receive its value from STDIN (grosser#1004)
* The --specify-groups option supports to read from STDIN * Add entry to Changelog * Improve language * Use read instead of gets and ensure to chomp the trailling newlines * Also update the CLI parser's help * Don't use backticks --------- Co-authored-by: Johannes Vetter <[email protected]>
1 parent 7af3e6c commit 5a511b2

File tree

5 files changed

+116
-39
lines changed

5 files changed

+116
-39
lines changed

CHANGELOG.md

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

33
## Unreleased
44

5+
- The `specify-groups` option supports reading from STDIN when set to `-`
6+
57
### Breaking Changes
68

79
### Added

Readme.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,8 @@ Options are:
258258
--failure-exit-code INT Specify the exit code to use when tests fail
259259
--specify-groups SPECS Use 'specify-groups' if you want to specify multiple specs running in multiple
260260
processes in a specific formation. Commas indicate specs in the same process,
261-
pipes indicate specs in a new process. Cannot use with --single, --isolate, or
261+
pipes indicate specs in a new process. If SPECS is a `-` the value for this
262+
option is read from STDIN instead. Cannot use with --single, --isolate, or
262263
--isolate-n. Ex.
263264
$ parallel_tests -n 3 . --specify-groups '1_spec.rb,2_spec.rb|3_spec.rb'
264265
Process 1 will contain 1_spec.rb and 2_spec.rb

lib/parallel_tests/cli.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,8 @@ def parse_options!(argv)
254254
<<~TEXT.rstrip.split("\n").join("\n#{newline_padding}")
255255
Use 'specify-groups' if you want to specify multiple specs running in multiple
256256
processes in a specific formation. Commas indicate specs in the same process,
257-
pipes indicate specs in a new process. Cannot use with --single, --isolate, or
257+
pipes indicate specs in a new process. If SPECS is a '-' the value for this
258+
option is read from STDIN instead. Cannot use with --single, --isolate, or
258259
--isolate-n. Ex.
259260
$ parallel_tests -n 3 . --specify-groups '1_spec.rb,2_spec.rb|3_spec.rb'
260261
Process 1 will contain 1_spec.rb and 2_spec.rb

lib/parallel_tests/grouper.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,15 @@ def in_even_groups_by_size(items, num_groups, options = {})
4848

4949
private
5050

51+
def specified_groups(options)
52+
groups = options[:specify_groups]
53+
return groups if groups != '-'
54+
55+
$stdin.read.chomp
56+
end
57+
5158
def specify_groups(items, num_groups, options, groups)
52-
specify_test_process_groups = options[:specify_groups].split('|')
59+
specify_test_process_groups = specified_groups(options).split('|')
5360
if specify_test_process_groups.count > num_groups
5461
raise 'Number of processes separated by pipe must be less than or equal to the total number of processes'
5562
end

spec/parallel_tests/grouper_spec.rb

Lines changed: 102 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -76,50 +76,116 @@ def call(num_groups, options = {})
7676
)
7777
end
7878

79-
it "groups specify_groups as specified when specify_groups is just one spec" do
80-
expect(call(3, specify_groups: '1')).to eq([["1"], ["2", "5"], ["3", "4"]])
81-
end
79+
context 'with specific groups provided directly' do
80+
it "groups specify_groups as specified when specify_groups is just one spec" do
81+
expect(call(3, specify_groups: '1')).to eq([["1"], ["2", "5"], ["3", "4"]])
82+
end
8283

83-
it "groups specify_groups as specified when specify_groups is just multiple specs in one process" do
84-
expect(call(3, specify_groups: '3,1')).to eq([["3", "1"], ["5"], ["2", "4"]])
85-
end
84+
it "groups specify_groups as specified when specify_groups is just multiple specs in one process" do
85+
expect(call(3, specify_groups: '3,1')).to eq([["3", "1"], ["5"], ["2", "4"]])
86+
end
8687

87-
it "groups specify_groups as specified when specify_groups is multiple specs" do
88-
expect(call(3, specify_groups: '1,2|4')).to eq([["1", "2"], ["4"], ["3", "5"]])
89-
end
88+
it "groups specify_groups as specified when specify_groups is multiple specs" do
89+
expect(call(3, specify_groups: '1,2|4')).to eq([["1", "2"], ["4"], ["3", "5"]])
90+
end
9091

91-
it "specify_groups aborts when number of specs separated by pipe is out of bounds" do
92-
expect do
93-
call(3, specify_groups: '1|2|3|4')
94-
end.to raise_error(
95-
"Number of processes separated by pipe must be less than or equal to the total number of processes"
96-
)
97-
end
92+
it "specify_groups aborts when number of specs separated by pipe is out of bounds" do
93+
expect do
94+
call(3, specify_groups: '1|2|3|4')
95+
end.to raise_error(
96+
"Number of processes separated by pipe must be less than or equal to the total number of processes"
97+
)
98+
end
9899

99-
it "specify_groups aborts when spec passed in doesn't match existing specs" do
100-
expect do
101-
call(3, specify_groups: '1|2|6')
102-
end.to raise_error(
103-
"Could not find [\"6\"] from --specify-groups in the selected files & folders"
104-
)
105-
end
100+
it "specify_groups aborts when spec passed in doesn't match existing specs" do
101+
expect do
102+
call(3, specify_groups: '1|2|6')
103+
end.to raise_error(
104+
"Could not find [\"6\"] from --specify-groups in the selected files & folders"
105+
)
106+
end
106107

107-
it "specify_groups aborts when spec passed in doesn't match existing specs again" do
108-
expect do
109-
call(3, specify_groups: '1,6|2')
110-
end.to raise_error(
111-
"Could not find [\"6\"] from --specify-groups in the selected files & folders"
112-
)
113-
end
108+
it "specify_groups aborts when spec passed in doesn't match existing specs again" do
109+
expect do
110+
call(3, specify_groups: '1,6|2')
111+
end.to raise_error(
112+
"Could not find [\"6\"] from --specify-groups in the selected files & folders"
113+
)
114+
end
114115

115-
it "specify_groups aborts when number of specs is equal to number passed in" do
116-
expect do
117-
call(3, specify_groups: '1|2|3')
118-
end.to raise_error(/The specs that aren't run:\n\["4", "5"\]/)
116+
it "specify_groups aborts when number of specs is equal to number passed in" do
117+
expect do
118+
call(3, specify_groups: '1|2|3')
119+
end.to raise_error(/The specs that aren't run:\n\["4", "5"\]/)
120+
end
121+
122+
it "specify_groups does not abort when the every single spec is specified in it" do
123+
expect(call(3, specify_groups: '1,2|3,4|5')).to eq([["1", "2"], ["3", "4"], ["5"]])
124+
end
119125
end
120126

121-
it "specify_groups does not abort when the every single spec is specified in it" do
122-
expect(call(3, specify_groups: '1,2|3,4|5')).to eq([["1", "2"], ["3", "4"], ["5"]])
127+
context 'with specific groups provided through STDIN' do
128+
it "groups specify_groups as specified when specify_groups is just one spec" do
129+
allow($stdin).to receive(:read).and_return("1\n")
130+
131+
expect(call(3, specify_groups: '-')).to eq([["1"], ["2", "5"], ["3", "4"]])
132+
end
133+
134+
it "groups specify_groups as specified when specify_groups is just multiple specs in one process" do
135+
allow($stdin).to receive(:read).and_return("3,1\n")
136+
137+
expect(call(3, specify_groups: '-')).to eq([["3", "1"], ["5"], ["2", "4"]])
138+
end
139+
140+
it "groups specify_groups as specified when specify_groups is multiple specs" do
141+
allow($stdin).to receive(:read).and_return("1,2|4\n")
142+
143+
expect(call(3, specify_groups: '-')).to eq([["1", "2"], ["4"], ["3", "5"]])
144+
end
145+
146+
it "specify_groups aborts when number of specs separated by pipe is out of bounds" do
147+
allow($stdin).to receive(:read).and_return("1|2|3|4\n")
148+
149+
expect do
150+
call(3, specify_groups: '-')
151+
end.to raise_error(
152+
"Number of processes separated by pipe must be less than or equal to the total number of processes"
153+
)
154+
end
155+
156+
it "specify_groups aborts when spec passed in doesn't match existing specs" do
157+
allow($stdin).to receive(:read).and_return("1|2|6\n")
158+
159+
expect do
160+
call(3, specify_groups: '-')
161+
end.to raise_error(
162+
"Could not find [\"6\"] from --specify-groups in the selected files & folders"
163+
)
164+
end
165+
166+
it "specify_groups aborts when spec passed in doesn't match existing specs again" do
167+
allow($stdin).to receive(:read).and_return("1,6|2\n")
168+
169+
expect do
170+
call(3, specify_groups: '-')
171+
end.to raise_error(
172+
"Could not find [\"6\"] from --specify-groups in the selected files & folders"
173+
)
174+
end
175+
176+
it "specify_groups aborts when number of specs is equal to number passed in" do
177+
allow($stdin).to receive(:read).and_return("1|2|3\n")
178+
179+
expect do
180+
call(3, specify_groups: '-')
181+
end.to raise_error(/The specs that aren't run:\n\["4", "5"\]/)
182+
end
183+
184+
it "specify_groups does not abort when the every single spec is specified in it" do
185+
allow($stdin).to receive(:read).and_return("1,2|3,4|5\n")
186+
187+
expect(call(3, specify_groups: '-')).to eq([["1", "2"], ["3", "4"], ["5"]])
188+
end
123189
end
124190
end
125191

0 commit comments

Comments
 (0)