Skip to content

Commit

Permalink
Merge pull request #101 from getgauge/file_cache_create
Browse files Browse the repository at this point in the history
adding check to see if file is already cached before processing create file cache request
  • Loading branch information
NivedhaSenthil authored Jan 23, 2019
2 parents 947dac6 + 527ca63 commit 1bf0186
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 2 deletions.
9 changes: 9 additions & 0 deletions lib/method_cache.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,15 @@ def self.remove_steps(file)
end
end

def self.is_file_cached(file)
@@steps_map.each_pair do |step, info|
if info[:locations].any? { |loc| relative_filepath(loc[:file]).eql? relative_filepath(file) }
return true
end
end
return false
end

def self.multiple_implementation?(step_value)
@@steps_map[step_value][:locations].length > 1
end
Expand Down
6 changes: 4 additions & 2 deletions lib/processors/cache_file_processor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,10 @@ def cache_file_response(request)
ast = CodeParser.code_to_ast(request.content)
StaticLoader.reload_steps(f, ast)
elsif status == Messages::CacheFileRequest::FileStatus::CREATED
ast = CodeParser.code_to_ast File.read(f)
StaticLoader.reload_steps(f, ast)
if !Gauge::MethodCache.is_file_cached f
ast = CodeParser.code_to_ast File.read(f)
StaticLoader.reload_steps(f, ast)
end
elsif (status == Messages::CacheFileRequest::FileStatus::CLOSED) && File.file?(f)
ast = CodeParser.code_to_ast File.read(f)
StaticLoader.reload_steps(f, ast)
Expand Down
15 changes: 15 additions & 0 deletions spec/cache_file_processor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,21 @@
expect(Gauge::MethodCache.valid_step? "foo").to eq true
end
end
context 'on Cache file request with CREATED status when file is already cached' do
before {
ast = Gauge::CodeParser.code_to_ast "step 'foo' do\n\tputs 'hello'\nend\n"
Gauge::StaticLoader.load_steps '/temp/foo.rb', ast
allow(File).to receive(:file?).with('/temp/foo.rb').and_return(true)
allow(File).to receive(:read).with('/temp/foo.rb').and_return("")
allow(message).to receive_message_chain(:cacheFileRequest, :filePath => '/temp/foo.rb')
allow(message).to receive_message_chain(:cacheFileRequest, :status => :CREATED)
allow(message).to receive(:messageId) {1}
}
it 'should reload step cache from file system' do
subject.process_cache_file_request(message)
expect(Gauge::MethodCache.valid_step? "foo").to eq true
end
end
context 'on Cache file request with CLOSED status' do
before {
ast = Gauge::CodeParser.code_to_ast "step 'foo' do\n\tputs 'hello'\nend\n"
Expand Down
14 changes: 14 additions & 0 deletions spec/method_cache_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -242,4 +242,18 @@
expect(step_info[0][:file]).to eq "/temp/foo1.bar"
end
end
context 'is_file_cached' do
before { subject.clear }
it 'should check if file is already cached' do
subject.add_step("step {}", {:step_text => "step <param>",:location => {:file => "/temp/foo.bar"}})
expect(subject.is_file_cached("/temp/foo.bar")).to eq true
expect(subject.is_file_cached("/tmp/foo1.bar")).to eq false
end
it 'should check if file is already cached in windows', :if => OS.windows? do
ENV['GAUGE_PROJECT_ROOT'] = "c:/temp"
subject.add_step("step {}", {:step_text => "step <param>",:location => {:file => "c:/temp/foo.bar"}})
expect(subject.is_file_cached("c:/temp/foo.bar")).to eq true
expect(subject.is_file_cached("c:/temp/foo1.bar")).to eq false
end
end
end

0 comments on commit 1bf0186

Please sign in to comment.