Skip to content

Commit c5dfd97

Browse files
Merge pull request #7801 from jeromedalbert/only-allow-valid-values
Only allow valid values for `--test`, `--ci`, and `--linter` options (cherry picked from commit 3da9b1d)
1 parent 3a6293f commit c5dfd97

File tree

2 files changed

+43
-7
lines changed

2 files changed

+43
-7
lines changed

bundler/lib/bundler/cli.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -550,10 +550,13 @@ def viz
550550
method_option :rubocop, type: :boolean, desc: "Add rubocop to the generated Rakefile and gemspec. Set a default with `bundle config set --global gem.rubocop true`."
551551
method_option :changelog, type: :boolean, desc: "Generate changelog file. Set a default with `bundle config set --global gem.changelog true`."
552552
method_option :test, type: :string, lazy_default: Bundler.settings["gem.test"] || "", aliases: "-t", banner: "Use the specified test framework for your library",
553+
enum: %w[rspec minitest test-unit],
553554
desc: "Generate a test directory for your library, either rspec, minitest or test-unit. Set a default with `bundle config set --global gem.test (rspec|minitest|test-unit)`."
554555
method_option :ci, type: :string, lazy_default: Bundler.settings["gem.ci"] || "",
556+
enum: %w[github gitlab circle],
555557
desc: "Generate CI configuration, either GitHub Actions, GitLab CI or CircleCI. Set a default with `bundle config set --global gem.ci (github|gitlab|circle)`"
556558
method_option :linter, type: :string, lazy_default: Bundler.settings["gem.linter"] || "",
559+
enum: %w[rubocop standard],
557560
desc: "Add a linter and code formatter, either RuboCop or Standard. Set a default with `bundle config set --global gem.linter (rubocop|standard)`"
558561
method_option :github_username, type: :string, default: Bundler.settings["gem.github_username"], banner: "Set your username on GitHub", desc: "Fill in GitHub username on README so that you don't have to do it manually. Set a default with `bundle config set --global gem.github_username <your_username>`."
559562

bundler/spec/commands/newgem_spec.rb

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -269,11 +269,11 @@ def bundle_exec_standardrb
269269
end
270270
end
271271

272-
shared_examples_for "--linter=none flag" do
272+
shared_examples_for "--no-linter flag" do
273273
define_negated_matcher :exclude, :include
274274

275275
before do
276-
bundle "gem #{gem_name} --linter=none"
276+
bundle "gem #{gem_name} --no-linter"
277277
end
278278

279279
it "generates a gem skeleton without rubocop" do
@@ -854,6 +854,17 @@ def create_temporary_dir(dir)
854854
end
855855
end
856856

857+
context "--test parameter set to an invalid value" do
858+
before do
859+
bundle "gem #{gem_name} --test=foo", raise_on_error: false
860+
end
861+
862+
it "fails loudly" do
863+
expect(last_command).to be_failure
864+
expect(err).to match(/Expected '--test' to be one of .*; got foo/)
865+
end
866+
end
867+
857868
context "gem.test setting set to test-unit" do
858869
before do
859870
bundle "config set gem.test test-unit"
@@ -998,6 +1009,17 @@ def create_temporary_dir(dir)
9981009
end
9991010
end
10001011

1012+
context "--ci set to an invalid value" do
1013+
before do
1014+
bundle "gem #{gem_name} --ci=foo", raise_on_error: false
1015+
end
1016+
1017+
it "fails loudly" do
1018+
expect(last_command).to be_failure
1019+
expect(err).to match(/Expected '--ci' to be one of .*; got foo/)
1020+
end
1021+
end
1022+
10011023
context "gem.ci setting set to none" do
10021024
it "doesn't generate any CI config" do
10031025
expect(bundled_app("#{gem_name}/.github/workflows/main.yml")).to_not exist
@@ -1124,6 +1146,17 @@ def create_temporary_dir(dir)
11241146
end
11251147
end
11261148

1149+
context "--linter set to an invalid value" do
1150+
before do
1151+
bundle "gem #{gem_name} --linter=foo", raise_on_error: false
1152+
end
1153+
1154+
it "fails loudly" do
1155+
expect(last_command).to be_failure
1156+
expect(err).to match(/Expected '--linter' to be one of .*; got foo/)
1157+
end
1158+
end
1159+
11271160
context "gem.linter setting set to none" do
11281161
it "doesn't generate any linter config" do
11291162
bundle "gem #{gem_name}"
@@ -1288,7 +1321,7 @@ def create_temporary_dir(dir)
12881321
end
12891322
it_behaves_like "--linter=rubocop flag"
12901323
it_behaves_like "--linter=standard flag"
1291-
it_behaves_like "--linter=none flag"
1324+
it_behaves_like "--no-linter flag"
12921325
it_behaves_like "--rubocop flag"
12931326
it_behaves_like "--no-rubocop flag"
12941327
end
@@ -1299,7 +1332,7 @@ def create_temporary_dir(dir)
12991332
end
13001333
it_behaves_like "--linter=rubocop flag"
13011334
it_behaves_like "--linter=standard flag"
1302-
it_behaves_like "--linter=none flag"
1335+
it_behaves_like "--no-linter flag"
13031336
it_behaves_like "--rubocop flag"
13041337
it_behaves_like "--no-rubocop flag"
13051338
end
@@ -1310,7 +1343,7 @@ def create_temporary_dir(dir)
13101343
end
13111344
it_behaves_like "--linter=rubocop flag"
13121345
it_behaves_like "--linter=standard flag"
1313-
it_behaves_like "--linter=none flag"
1346+
it_behaves_like "--no-linter flag"
13141347
end
13151348

13161349
context "with linter option in bundle config settings set to standard" do
@@ -1319,7 +1352,7 @@ def create_temporary_dir(dir)
13191352
end
13201353
it_behaves_like "--linter=rubocop flag"
13211354
it_behaves_like "--linter=standard flag"
1322-
it_behaves_like "--linter=none flag"
1355+
it_behaves_like "--no-linter flag"
13231356
end
13241357

13251358
context "with linter option in bundle config settings set to false" do
@@ -1328,7 +1361,7 @@ def create_temporary_dir(dir)
13281361
end
13291362
it_behaves_like "--linter=rubocop flag"
13301363
it_behaves_like "--linter=standard flag"
1331-
it_behaves_like "--linter=none flag"
1364+
it_behaves_like "--no-linter flag"
13321365
end
13331366

13341367
context "with changelog option in bundle config settings set to true" do

0 commit comments

Comments
 (0)