test/dirmon_entry_test.rb in rocketjob-1.2.0 vs test/dirmon_entry_test.rb in rocketjob-1.2.1

- old
+ new

@@ -61,11 +61,11 @@ end end describe '#fail_with_exception!' do before do - @dirmon_entry = RocketJob::DirmonEntry.new(job_class_name: 'Jobs::TestJob', pattern: '/abc/**', arguments: [1]) + @dirmon_entry = RocketJob::DirmonEntry.new(job_class_name: 'Jobs::TestJob', pattern: 'test/files/**', arguments: [1]) @dirmon_entry.enable! end after do @dirmon_entry.destroy if @dirmon_entry && @dirmon_entry.new_record? end @@ -109,121 +109,124 @@ end end describe 'job_class_name' do it 'ensure presence' do - assert entry = RocketJob::DirmonEntry.new(pattern: '/abc/**') + assert entry = RocketJob::DirmonEntry.new(pattern: 'test/files/**') assert_equal false, entry.valid? assert_equal ["can't be blank", 'job_class_name must be defined and must be derived from RocketJob::Job'], entry.errors[:job_class_name], entry.errors.inspect end end describe 'arguments' do it 'allow no arguments' do assert entry = RocketJob::DirmonEntry.new( job_class_name: 'Jobs::TestJob', - pattern: '/abc/**', + pattern: 'test/files/**', perform_method: :result ) assert_equal true, entry.valid?, entry.errors.inspect assert_equal [], entry.errors[:arguments], entry.errors.inspect end it 'ensure correct number of arguments' do assert entry = RocketJob::DirmonEntry.new( job_class_name: 'Jobs::TestJob', - pattern: '/abc/**' + pattern: 'test/files/**' ) assert_equal false, entry.valid? assert_equal ['There must be 1 argument(s)'], entry.errors[:arguments], entry.errors.inspect end it 'return false if the job name is bad' do assert entry = RocketJob::DirmonEntry.new( job_class_name: 'Jobs::Tests::Names::Things', - pattern: '/abc/**' + pattern: 'test/files/**' ) assert_equal false, entry.valid? assert_equal [], entry.errors[:arguments], entry.errors.inspect end end it 'arguments with perform_method' do assert entry = RocketJob::DirmonEntry.new( job_class_name: 'Jobs::TestJob', - pattern: '/abc/**', + pattern: 'test/files/**', perform_method: :sum ) assert_equal false, entry.valid? assert_equal ['There must be 2 argument(s)'], entry.errors[:arguments], entry.errors.inspect end it 'valid' do assert entry = RocketJob::DirmonEntry.new( job_class_name: 'Jobs::TestJob', - pattern: '/abc/**', + pattern: 'test/files/**', arguments: [1] ) assert entry.valid?, entry.errors.inspect end it 'valid with perform_method' do assert entry = RocketJob::DirmonEntry.new( job_class_name: 'Jobs::TestJob', - pattern: '/abc/**', + pattern: 'test/files/**', perform_method: :sum, arguments: [1, 2] ) assert entry.valid?, entry.errors.inspect end end describe 'with valid entry' do before do @archive_directory = '/tmp/archive_directory' - @entry = RocketJob::DirmonEntry.new( - pattern: 'abc/*', + @archive_path = Pathname.new(@archive_directory) + @archive_path.mkpath + @archive_path = @archive_path.realdirpath + @entry = RocketJob::DirmonEntry.new( + pattern: 'test/files/**/*', job_class_name: 'Jobs::TestJob', arguments: [{input: 'yes'}], properties: {priority: 23, perform_method: :event}, archive_directory: @archive_directory ) - @job = Jobs::TestJob.new - @file = Tempfile.new('archive') - @file_name = @file.path - @pathname = Pathname.new(@file_name) + @job = Jobs::TestJob.new + @file = Tempfile.new('archive') + @file_name = @file.path + @pathname = Pathname.new(@file_name) File.open(@file_name, 'w') { |file| file.write('Hello World') } assert File.exists?(@file_name) - @archive_file_name = File.join(@archive_directory, "#{@job.id}_#{File.basename(@file_name)}") + @archive_real_name = @archive_path.join("#{@job.id}_#{File.basename(@file_name)}").to_s end after do @file.delete if @file end describe '#archive_pathname' do it 'with archive directory' do - assert_equal @archive_directory.to_s, @entry.archive_pathname.to_s + assert_equal File.dirname(@archive_real_name), @entry.archive_pathname(@pathname).to_s end it 'without archive directory' do @entry.archive_directory = nil - assert_equal '_archive', @entry.archive_pathname.to_s + assert @entry.archive_pathname(@pathname).to_s.end_with?('_archive') end end describe '#archive_file' do it 'archive file' do - assert_equal @archive_file_name, @entry.send(:archive_file, @job, Pathname.new(@file_name)) - assert File.exists?(@archive_file_name), @archive_file_name + assert_equal @archive_real_name, @entry.send(:archive_file, @job, Pathname.new(@file_name)) + assert File.exists?(@archive_real_name) end end describe '#upload_default' do it 'upload' do @entry.send(:upload_default, @job, @pathname) - assert_equal File.absolute_path(@archive_file_name), @job.arguments.first[:full_file_name], @job.arguments + assert_equal @archive_real_name, @job.arguments.first[:full_file_name], @job.arguments end end describe '#upload_file' do it 'upload using #file_store_upload' do @@ -246,11 +249,68 @@ describe '#later' do it 'enqueue job' do @entry.arguments = [{}] @entry.perform_method = :event job = @entry.later(@pathname) - assert_equal File.join(@archive_directory, "#{job.id}_#{File.basename(@file_name)}"), job.arguments.first[:full_file_name] + assert_equal Pathname.new(@archive_directory).join("#{job.id}_#{File.basename(@file_name)}").realdirpath.to_s, job.arguments.first[:full_file_name] assert job.queued? + end + end + + describe '#each' do + it 'without archive path' do + @entry.archive_directory = nil + files = [] + @entry.each do |file_name| + files << file_name + end + assert_equal nil, @entry.archive_directory + assert_equal 1, files.count + assert_equal Pathname.new('test/files/text.txt').realpath, files.first + end + + it 'with archive path' do + files = [] + @entry.each do |file_name| + files << file_name + end + assert_equal 1, files.count + assert_equal Pathname.new('test/files/text.txt').realpath, files.first + end + + it 'with case-insensitive pattern' do + @entry.pattern = 'test/files/**/*.TxT' + files = [] + @entry.each do |file_name| + files << file_name + end + assert_equal 1, files.count + assert_equal Pathname.new('test/files/text.txt').realpath, files.first + end + + it 'reads paths inside of the whitelist' do + @entry.archive_directory = nil + files = [] + @entry.stub(:whitelist_paths, [Pathname.new('test/files').realpath.to_s]) do + @entry.each do |file_name| + files << file_name + end + end + assert_equal nil, @entry.archive_directory + assert_equal 1, files.count + assert_equal Pathname.new('test/files/text.txt').realpath, files.first + end + + it 'skips paths outside of the whitelist' do + @entry.archive_directory = nil + files = [] + @entry.stub(:whitelist_paths, [Pathname.new('test/config').realpath.to_s]) do + @entry.each do |file_name| + files << file_name + end + end + assert_equal nil, @entry.archive_directory + assert_equal 0, files.count end end end end