|
| 1 | +require 'test_helper' |
| 2 | +require 'tempfile' |
| 3 | +require 'fileutils' |
| 4 | + |
| 5 | +describe MetaRequest::Config do |
| 6 | + CONFIG_DIRECTORY = "#{MetaRequest.rails_root}/config" |
| 7 | + CONFIG_FILE_PATH = "#{CONFIG_DIRECTORY}/meta_request.yml" |
| 8 | + |
| 9 | + def write_config_file(config) |
| 10 | + file = File.new(CONFIG_FILE_PATH, "w") |
| 11 | + file.write(config.to_yaml) |
| 12 | + file.close |
| 13 | + end |
| 14 | + |
| 15 | + before(:each) do |
| 16 | + FileUtils.mkdir_p(CONFIG_DIRECTORY) |
| 17 | + end |
| 18 | + |
| 19 | + after(:each) do |
| 20 | + FileUtils.remove_dir(CONFIG_DIRECTORY, force: true) |
| 21 | + MetaRequest::Config.class_variable_set(:@@config_file, nil) |
| 22 | + end |
| 23 | + |
| 24 | + describe 'config_file' do |
| 25 | + it 'provides default config when config file is not present' do |
| 26 | + assert_equal [], MetaRequest::Config.config_file[:ignore_paths] |
| 27 | + end |
| 28 | + |
| 29 | + it 'provides default config when config file is empty' do |
| 30 | + write_config_file(nil) |
| 31 | + assert_equal [], MetaRequest::Config.config_file[:ignore_paths] |
| 32 | + end |
| 33 | + |
| 34 | + it 'converts ignore paths array to regexs' do |
| 35 | + config = { |
| 36 | + ignore_paths: ['.*files', 'notaregex'] |
| 37 | + } |
| 38 | + expected_ignore_paths = config[:ignore_paths].map { |p| Regexp.new(p) } |
| 39 | + write_config_file(config) |
| 40 | + assert_equal expected_ignore_paths, MetaRequest::Config.config_file[:ignore_paths] |
| 41 | + end |
| 42 | + end |
| 43 | + |
| 44 | + describe 'ignored_path?' do |
| 45 | + it 'does not ignore files if the ignore_paths option is empty' do |
| 46 | + assert_equal [], MetaRequest::Config.config_file[:ignore_paths] |
| 47 | + assert_equal false, MetaRequest::Config.ignored_path?('this/file/is/not/ignored.txt') |
| 48 | + end |
| 49 | + |
| 50 | + it 'ignores files that match the ignore_paths' do |
| 51 | + config = { |
| 52 | + ignore_paths: ['.*ignored.*', 'notaregex'] |
| 53 | + } |
| 54 | + write_config_file(config) |
| 55 | + assert_equal true, MetaRequest::Config.ignored_path?('this/file/is/ignored.txt') |
| 56 | + assert_equal true, MetaRequest::Config.ignored_path?('notaregex') |
| 57 | + assert_equal false, MetaRequest::Config.ignored_path?('this/file/is/included.txt') |
| 58 | + end |
| 59 | + |
| 60 | + end |
| 61 | +end |
| 62 | + |
0 commit comments