@@ -412,6 +412,14 @@ def expect_parsing_to_fail_mentioning_source(source, options=[])
412
412
end
413
413
end
414
414
415
+ context "defined in $XDG_CONFIG_HOME/rspec/options" do
416
+ it "mentions the file name in the error so users know where to look for it" do
417
+ file_name = File . expand_path ( "~/.config/rspec/options" )
418
+ create_fixture_file ( file_name , "--foo_bar" )
419
+ expect_parsing_to_fail_mentioning_source ( file_name )
420
+ end
421
+ end
422
+
415
423
context "defined in SPEC_OPTS" do
416
424
it "mentions ENV['SPEC_OPTS'] as the source in the error so users know where to look for it" do
417
425
with_env_vars 'SPEC_OPTS' => "--foo_bar" do
@@ -440,25 +448,64 @@ def expect_parsing_to_fail_mentioning_source(source, options=[])
440
448
end
441
449
end
442
450
443
- describe "sources: ~/.rspec, ./.rspec, ./.rspec-local, custom, CLI, and SPEC_OPTS" do
444
- it "merges global, local, SPEC_OPTS, and CLI" do
445
- File . open ( "./.rspec" , "w" ) { |f | f << "--require some_file" }
446
- File . open ( "./.rspec-local" , "w" ) { |f | f << "--format global" }
447
- File . open ( File . expand_path ( "~/.rspec" ) , "w" ) { |f | f << "--force-color" }
451
+ describe "sources: $XDG_CONFIG_HOME/rspec/options, ~/.rspec, ./.rspec, ./.rspec-local, custom, CLI, and SPEC_OPTS" do
452
+ it "merges both global, local, SPEC_OPTS, and CLI" do
453
+ create_fixture_file ( "./.rspec" , "--require some_file" )
454
+ create_fixture_file ( "./.rspec-local" , "--format global" )
455
+ create_fixture_file ( "~/.rspec" , "--force-color" )
456
+ create_fixture_file ( "~/.config/rspec/options" , "--order defined" )
448
457
with_env_vars 'SPEC_OPTS' => "--example 'foo bar'" do
449
458
options = parse_options ( "--drb" )
450
- expect ( options [ :color_mode ] ) . to eq ( :on )
459
+ # $XDG_CONFIG_HOME/rspec/options file ("order") is read, but ~/.rspec
460
+ # file ("color") is not read because ~/.rspec has lower priority over
461
+ # the file in the XDG config directory.
462
+ expect ( options [ :order ] ) . to eq ( "defined" )
463
+ expect ( options [ :color_mode ] ) . to be_nil
464
+
451
465
expect ( options [ :requires ] ) . to eq ( [ "some_file" ] )
452
466
expect ( options [ :full_description ] ) . to eq ( [ /foo\ bar/ ] )
453
467
expect ( options [ :drb ] ) . to be_truthy
454
468
expect ( options [ :formatters ] ) . to eq ( [ [ 'global' ] ] )
455
469
end
456
470
end
457
471
472
+ it "reads ~/.rspec if $XDG_CONFIG_HOME/rspec/options is not found" do
473
+ create_fixture_file ( "~/.rspec" , "--force-color" )
474
+
475
+ options = parse_options ( )
476
+ expect ( options [ :color_mode ] ) . to eq ( :on )
477
+ expect ( options [ :order ] ) . to be_nil
478
+ end
479
+
480
+ it "does not read ~/.rspec if $XDG_CONFIG_HOME/rspec/options is present" do
481
+ create_fixture_file ( "~/.rspec" , "--force-color" )
482
+ create_fixture_file ( "~/.config/rspec/options" , "--order defined" )
483
+
484
+ options = parse_options ( )
485
+ expect ( options [ :color_mode ] ) . to be_nil
486
+ expect ( options [ :order ] ) . to eq ( "defined" )
487
+ end
488
+
489
+ it "uses $XDG_CONFIG_HOME environment variable when set to find XDG global options" do
490
+ create_fixture_file ( "~/.config/rspec/options" , "--format default_xdg" )
491
+ create_fixture_file ( "~/.custom-config/rspec/options" , "--format overridden_xdg" )
492
+
493
+ with_env_vars 'XDG_CONFIG_HOME' => "~/.custom-config" do
494
+ options = parse_options ( )
495
+ expect ( options [ :formatters ] ) . to eq ( [ [ 'overridden_xdg' ] ] )
496
+ end
497
+
498
+ without_env_vars 'XDG_CONFIG_HOME' do
499
+ options = parse_options ( )
500
+ expect ( options [ :formatters ] ) . to eq ( [ [ 'default_xdg' ] ] )
501
+ end
502
+ end
503
+
458
504
it 'ignores file or dir names put in one of the option files or in SPEC_OPTS, since those are for persistent options' do
459
- File . open ( "./.rspec" , "w" ) { |f | f << "path/to/spec_1.rb" }
460
- File . open ( "./.rspec-local" , "w" ) { |f | f << "path/to/spec_2.rb" }
461
- File . open ( File . expand_path ( "~/.rspec" ) , "w" ) { |f | f << "path/to/spec_3.rb" }
505
+ create_fixture_file ( "./.rspec" , "path/to/spec_1.rb" )
506
+ create_fixture_file ( "./.rspec-local" , "path/to/spec_2.rb" )
507
+ create_fixture_file ( "~/.rspec" , "path/to/spec_3.rb" )
508
+ create_fixture_file ( "~/.config/rspec/options" , "path/to/spec_4.rb" )
462
509
with_env_vars 'SPEC_OPTS' => "path/to/spec_4.rb" do
463
510
options = parse_options ( )
464
511
expect ( options [ :files_or_directories_to_run ] ) . to eq ( [ ] )
@@ -472,13 +519,14 @@ def expect_parsing_to_fail_mentioning_source(source, options=[])
472
519
end
473
520
474
521
it "prefers CLI over file options" do
475
- File . open ( "./.rspec" , "w" ) { |f | f << "--format project" }
476
- File . open ( File . expand_path ( "~/.rspec" ) , "w" ) { |f | f << "--format global" }
522
+ create_fixture_file ( "./.rspec" , "--format project" )
523
+ create_fixture_file ( "~/.rspec" , "--format global" )
524
+ create_fixture_file ( "~/.config/rspec/options" , "--format xdg" )
477
525
expect ( parse_options ( "--format" , "cli" ) [ :formatters ] ) . to eq ( [ [ 'cli' ] ] )
478
526
end
479
527
480
528
it "prefers CLI over file options for filter inclusion" do
481
- File . open ( "./.rspec" , "w" ) { | f | f << " --tag ~slow"}
529
+ create_fixture_file ( "./.rspec" , "--tag ~slow" )
482
530
opts = config_options_object ( "--tag" , "slow" )
483
531
config = RSpec ::Core ::Configuration . new
484
532
opts . configure ( config )
@@ -487,14 +535,15 @@ def expect_parsing_to_fail_mentioning_source(source, options=[])
487
535
end
488
536
489
537
it "prefers project file options over global file options" do
490
- File . open ( "./.rspec" , "w" ) { |f | f << "--format project" }
491
- File . open ( File . expand_path ( "~/.rspec" ) , "w" ) { |f | f << "--format global" }
538
+ create_fixture_file ( "./.rspec" , "--format project" )
539
+ create_fixture_file ( "~/.rspec" , "--format global" )
540
+ create_fixture_file ( "~/.config/rspec/options" , "--format xdg" )
492
541
expect ( parse_options [ :formatters ] ) . to eq ( [ [ 'project' ] ] )
493
542
end
494
543
495
544
it "prefers local file options over project file options" do
496
- File . open ( "./.rspec-local" , "w" ) { | f | f << " --format local"}
497
- File . open ( "./.rspec" , "w" ) { | f | f << " --format global"}
545
+ create_fixture_file ( "./.rspec-local" , "--format local" )
546
+ create_fixture_file ( "./.rspec" , "--format global" )
498
547
expect ( parse_options [ :formatters ] ) . to eq ( [ [ 'local' ] ] )
499
548
end
500
549
@@ -510,16 +559,17 @@ def expect_parsing_to_fail_mentioning_source(source, options=[])
510
559
511
560
context "with custom options file" do
512
561
it "ignores project and global options files" do
513
- File . open ( "./.rspec" , "w" ) { |f | f << "--format project" }
514
- File . open ( File . expand_path ( "~/.rspec" ) , "w" ) { |f | f << "--format global" }
515
- File . open ( "./custom.opts" , "w" ) { |f | f << "--force-color" }
562
+ create_fixture_file ( "./.rspec" , "--format project" )
563
+ create_fixture_file ( "~/.rspec" , "--format global" )
564
+ create_fixture_file ( "~/.config/rspec/options" , "--format xdg" )
565
+ create_fixture_file ( "./custom.opts" , "--force-color" )
516
566
options = parse_options ( "-O" , "./custom.opts" )
517
567
expect ( options [ :format ] ) . to be_nil
518
568
expect ( options [ :color_mode ] ) . to eq ( :on )
519
569
end
520
570
521
571
it "parses -e 'full spec description'" do
522
- File . open ( "./custom.opts" , "w" ) { | f | f << " -e 'The quick brown fox jumps over the lazy dog'"}
572
+ create_fixture_file ( "./custom.opts" , "-e 'The quick brown fox jumps over the lazy dog'" )
523
573
options = parse_options ( "-O" , "./custom.opts" )
524
574
expect ( options [ :full_description ] ) . to eq ( [ /The\ quick\ brown\ fox\ jumps\ over\ the\ lazy\ dog/ ] )
525
575
end
0 commit comments