Skip to content

Commit

Permalink
Improve repeated file match handling (#37)
Browse files Browse the repository at this point in the history
Closes #36
  • Loading branch information
tjwp authored Mar 22, 2017
1 parent 3727e64 commit 03a75ca
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 4 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# avro-builder changelog

## v0.14.1
- File handling fixes.

## v0.14.0
- Allow `filetype` to be specified in rake task to generate Avro JSON schema
files.
Expand Down
9 changes: 7 additions & 2 deletions lib/avro/builder/file_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,22 @@ def find_file(name)
# a namespace then ensure that periods (.) are replaced by forward
# slashes. E.g. for 'test.example' search for '/test/example.rb'.
file_name = "/#{name.to_s.tr('.', '/').sub(/^\//, '').sub(/\.rb$/, '')}.rb"
matches = self.class.load_paths.flat_map do |load_path|
matches = real_load_paths.flat_map do |load_path|
Dir["#{load_path}/**/*.rb"].select do |file_path|
file_path.end_with?(file_name)
end
end
end.uniq
raise "Multiple matches: #{matches}" if matches.size > 1
raise "File not found #{file_name}" if matches.empty?

matches.first
end

private

def real_load_paths
self.class.load_paths.map { |path| File.realpath(path) }.uniq
end
end
end
end
2 changes: 1 addition & 1 deletion lib/avro/builder/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Avro
module Builder
VERSION = '0.14.0'.freeze
VERSION = '0.14.1'.freeze
end
end
43 changes: 43 additions & 0 deletions spec/avro/builder/file_handler_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,49 @@
end
end

context "with duplicated paths" do
subject(:schema_json) do
Avro::Builder.build do
record :with_date do
required :dt, :date
end
end
end
let(:expected) do
{
type: :record,
name: :with_date,
fields: [
{
name: :dt,
type: {
type: :int,
logicalType: :date
}
}
]
}
end

context "subpath" do
before do
Avro::Builder.add_load_path('spec/avro/dsl')
Avro::Builder.add_load_path('spec/avro/dsl/test')
end

it { is_expected.to be_json_eql(expected.to_json) }
end

context "normalization required" do
before do
Avro::Builder.add_load_path('spec/avro/dsl')
Avro::Builder.add_load_path(File.join(__dir__, '../dsl'))
end

it { is_expected.to be_json_eql(expected.to_json) }
end
end

context "a file with a name that ends with a builtin type" do
let(:file_path) { 'spec/avro/dsl/test/with_array.rb' }
subject(:schema_json) { Avro::Builder.build(File.read(file_path)) }
Expand Down
2 changes: 1 addition & 1 deletion spec/avro/builder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1847,7 +1847,7 @@
schema_json
rescue => ex
end
expect(ex.backtrace[1]).to start_with('spec/avro/dsl/test/invalid.rb:4:')
expect(ex.backtrace[1]).to match(/^.*spec\/avro\/dsl\/test\/invalid.rb:4:/)
end
# rubocop:enable Lint/HandleExceptions
end
Expand Down
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

RSpec.configure do |config|
config.before do
Avro::Builder::DSL.load_paths.clear
Avro::Builder.add_load_path('spec/avro/dsl')
end
end

0 comments on commit 03a75ca

Please sign in to comment.