diff --git a/.rspec b/.rspec index 847008d9a4..34c5164d9b 100644 --- a/.rspec +++ b/.rspec @@ -1,3 +1,3 @@ --format documentation --color -#--require spec_helper +--require spec_helper diff --git a/spec/features/catalog_spec.rb b/spec/features/catalog_spec.rb index 74aeaf774f..4dfe22cad6 100644 --- a/spec/features/catalog_spec.rb +++ b/spec/features/catalog_spec.rb @@ -164,7 +164,7 @@ def expect_poster(id) it 'works' do visit '/catalog/1234.pbcore' expect(page.status_code).to eq(200) - expect(page.source).to eq(File.read(File.dirname(File.dirname(__FILE__)) + '/fixtures/pbcore/clean-MOCK.xml')) + expect(page.source).to eq(File.read(Rails.root + 'spec/fixtures/pbcore/clean-MOCK.xml')) expect(page.response_headers['Content-Type']).to eq('text/xml; charset=utf-8') end end diff --git a/spec/features/media_spec.rb b/spec/features/media_spec.rb index 987bd0f408..03c62d1755 100644 --- a/spec/features/media_spec.rb +++ b/spec/features/media_spec.rb @@ -6,7 +6,7 @@ TARGET = 'lorem ipsum' def safe_ci - credentials_path = File.dirname(File.dirname(File.dirname(__FILE__))) + '/config/ci.yml' + credentials_path = Rails.root + 'config/ci.yml' ci = Ci.new(credentials_path: credentials_path) fail 'Workspace must be empty' unless ci.list_names.empty? ci diff --git a/spec/models/vocab_map_spec.rb b/spec/models/vocab_map_spec.rb index e37e25f475..9992147e7d 100644 --- a/spec/models/vocab_map_spec.rb +++ b/spec/models/vocab_map_spec.rb @@ -2,13 +2,13 @@ require_relative '../../app/models/vocab_map' describe VocabMap do - fixtures = File.dirname(File.dirname(__FILE__)) + '/fixtures/vocab-maps' + fixtures = Rails.root + 'spec/fixtures/vocab-maps/' # TODO: test xml processing, particularly attribute values. describe 'when the map is good' do describe 'acceptance' do - [File.dirname(File.dirname(File.dirname(__FILE__))) + '/config/vocab-maps', + [Rails.root + 'config/vocab-maps', fixtures].each do |dir| Dir["#{dir}/*"].reject { |file| file =~ /bad-/ }.each do |yaml| it "accepts #{yaml}" do @@ -19,17 +19,17 @@ end it '#authorized_names' do - map = VocabMap.new(fixtures + '/good-ordinal-map.yml') + map = VocabMap.new(fixtures + 'good-ordinal-map.yml') expect(map.authorized_names).to eq ['erste', 'zweite', 'dritte', 'undefined'] end it 'implicitly case-normalizes' do - map = VocabMap.new(fixtures + '/good-map.yml') + map = VocabMap.new(fixtures + 'good-map.yml') expect(map.map_string('YesThisIsRight')).to eq 'yesTHISisRIGHT' end it 'maps text nodes' do - map = VocabMap.new(fixtures + '/good-map.yml') + map = VocabMap.new(fixtures + 'good-map.yml') doc = REXML::Document.new('foo') nodes = REXML::XPath.match(doc, '/doc/element') @@ -38,7 +38,7 @@ end it 'maps attribute values' do - map = VocabMap.new(fixtures + '/good-map.yml') + map = VocabMap.new(fixtures + 'good-map.yml') doc = REXML::Document.new('') nodes = REXML::XPath.match(doc, '/doc/@attr') @@ -47,8 +47,8 @@ end it 'reorders by mapped attribute value' do - card_map = VocabMap.new(fixtures + '/good-cardinal-map.yml') - ord_map = VocabMap.new(fixtures + '/good-ordinal-map.yml') + card_map = VocabMap.new(fixtures + 'good-cardinal-map.yml') + ord_map = VocabMap.new(fixtures + 'good-ordinal-map.yml') doc = REXML::Document.new( 'dreitwoI') @@ -73,27 +73,27 @@ end it 'catches case discrepancies on RHS' do - expect { VocabMap.new(fixtures + '/bad-mixed-case.yml') }.to( + expect { VocabMap.new(fixtures + 'bad-mixed-case.yml') }.to( raise_error(/Case discrepancy on RHS/)) end it 'catches bad yaml types' do - expect { VocabMap.new(fixtures + '/bad-not-omap.yml') }.to( + expect { VocabMap.new(fixtures + 'bad-not-omap.yml') }.to( raise_error(/Unexpected datatype/)) end it 'catches hidden keys' do - expect { VocabMap.new(fixtures + '/bad-hidden-keys.yml') }.to( + expect { VocabMap.new(fixtures + 'bad-hidden-keys.yml') }.to( raise_error(/Hidden keys \["ShouldNotBeRemapped"\]/)) end it 'catches hidden substring' do - expect { VocabMap.new(fixtures + '/bad-hidden-substring.yml') }.to( + expect { VocabMap.new(fixtures + 'bad-hidden-substring.yml') }.to( raise_error(/Hidden keys \["this-prefix-hides", "hidden-by-this-suffix"\]/)) end it 'catches missing defaults' do - expect { VocabMap.new(fixtures + '/bad-no-default.yml') }.to( + expect { VocabMap.new(fixtures + 'bad-no-default.yml') }.to( raise_error(/No default mapping/)) end end diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb index c4dfcae97b..e4cc4fcafb 100644 --- a/spec/rails_helper.rb +++ b/spec/rails_helper.rb @@ -1,6 +1,5 @@ # This file is copied to spec/ when you run 'rails generate rspec:install' ENV['RAILS_ENV'] ||= 'test' -require 'spec_helper' require File.expand_path('../../config/environment', __FILE__) require 'rspec/rails' # Add additional requires below this line. Rails is not loaded until this point! diff --git a/spec/scripts/ci_spec.rb b/spec/scripts/ci_spec.rb index 83455f1b93..1e5ef82bf4 100644 --- a/spec/scripts/ci_spec.rb +++ b/spec/scripts/ci_spec.rb @@ -2,7 +2,7 @@ require 'tmpdir' describe Ci, not_on_travis: true do - let(:credentials_path) { File.dirname(File.dirname(File.dirname(__FILE__))) + '/config/ci.yml' } + let(:credentials_path) { Rails.root + 'config/ci.yml' } let(:aapb_workspace_id) { '051303c1c1d24da7988128e6d2f56aa9' } # we make sure NOT to use this. it 'requires credentials' do diff --git a/spec/scripts/download_clean_ingest_spec.rb b/spec/scripts/download_clean_ingest_spec.rb index 07fad501fc..e2a879289e 100644 --- a/spec/scripts/download_clean_ingest_spec.rb +++ b/spec/scripts/download_clean_ingest_spec.rb @@ -24,7 +24,7 @@ def dci_output(*args) end default_flags = '--stdout-log --same-mount --skip-sitemap' - default_mode = "--files #{File.dirname(__FILE__)}/../fixtures/dci/pbcore-dir/pbcore.xml" + default_mode = "--files #{Rails.root + 'spec/fixtures/dci/pbcore-dir/pbcore.xml'}" { # Expected to fail: '' => [/USAGE:/], @@ -50,17 +50,17 @@ def dci_output(*args) /Successfully added .*37-010p2nvv.pbcore/, /1 succeeded/ ], - "#{default_flags} --id-files #{File.dirname(__FILE__)}/../fixtures/dci/id-file.txt" => [ + "#{default_flags} --id-files #{Rails.root + 'spec/fixtures/dci/id-file.txt'}" => [ /Downloading .*guid\/37-010p2nvv/, /Updated solr record cpb-aacip_37-010p2nvv/, /Successfully added .*37-010p2nvv.pbcore/, /1 succeeded/ ], - "#{default_flags} --dirs #{File.dirname(__FILE__)}/../fixtures/dci/pbcore-dir" => [ + "#{default_flags} --dirs #{Rails.root + 'spec/fixtures/dci/pbcore-dir'}" => [ /Updated solr record 1234/, /1 succeeded/ ], - "#{default_flags} --files #{File.dirname(__FILE__)}/../fixtures/dci/pbcore-dir/pbcore.xml" => [ + "#{default_flags} --files #{Rails.root + 'spec/fixtures/dci/pbcore-dir/pbcore.xml'}" => [ /Updated solr record 1234/, /1 succeeded/ ], diff --git a/spec/scripts/pb_core_ingester_spec.rb b/spec/scripts/pb_core_ingester_spec.rb index a0499ac0f2..e8a985e8a5 100644 --- a/spec/scripts/pb_core_ingester_spec.rb +++ b/spec/scripts/pb_core_ingester_spec.rb @@ -2,7 +2,7 @@ require 'tmpdir' describe PBCoreIngester do - let(:path) { File.dirname(File.dirname(__FILE__)) + '/fixtures/pbcore/clean-MOCK.xml' } + let(:path) { Rails.root + 'spec/fixtures/pbcore/clean-MOCK.xml' } before(:each) do @ingester = PBCoreIngester.new(is_same_mount: true) diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 27d238b829..8b183fad46 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -81,3 +81,10 @@ # # as the one that triggered the failure. # Kernel.srand config.seed end + +module Rails + # Allow more idiomatic tests everywhere, without loading in all of rails. + def self.root + File.dirname(File.dirname(__FILE__)) + '/' + end +end diff --git a/spec/support/code_style_spec.rb b/spec/support/code_style_spec.rb index fea905ec89..da7c3cd9e3 100644 --- a/spec/support/code_style_spec.rb +++ b/spec/support/code_style_spec.rb @@ -4,14 +4,13 @@ @merge = [] @error = [] - root = File.dirname(File.dirname(File.dirname(__FILE__))) - all_paths = Dir[root + '/{app,config,scripts,spec}/**/*'] + all_paths = Dir[Rails.root + '{app,config,scripts,spec}/**/*'] paths_to_check = all_paths.reject do |path| path=~/\.(jar|ico|png|gif|jpg)$/ || File.directory?(path) || path == __FILE__ end - puts "Checking #{paths_to_check.count} files under #{root} for cruft..." + puts "Checking #{paths_to_check.count} files under #{Rails.root} for cruft..." paths_to_check.each do |path| File.readlines(path).each_with_index do |line, i| combo = "#{path}:#{i}: #{line}" diff --git a/spec/support/link_checker.rb b/spec/support/link_checker.rb index 5431d7f185..f956239b97 100644 --- a/spec/support/link_checker.rb +++ b/spec/support/link_checker.rb @@ -5,7 +5,7 @@ class LinkChecker include Singleton - FILENAME = File.join(File.dirname(__FILE__), '.link-check.txt') + FILENAME = File.dirname(__FILE__) + '/.link-check.txt' RE_IGNORES = [ /^\/catalog\?/, # too many combos /^\/catalog\//, # redundant: other tests load each fixture