Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set default encoding of minter state files and allow overrides. #16

Merged
merged 1 commit into from
Aug 10, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions lib/active_fedora/noid/synchronized_minter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,20 @@ def state_for(io_object)
{ template: template }
end

def file_opts
{ encoding: Encoding::ASCII_8BIT }
end

def next_id
id = ''
::File.open(statefile, ::File::RDWR|::File::CREAT, 0644) do |f|
::File.open(statefile, ::File::RDWR|::File::CREAT, 0644, file_opts) do |f|
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Setting this to ::File.open(statefile, 'a+b', 0644) do |f| ... is how I'd do this. You should be able to avoid lines 54 & the file_opts method then.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added |::File::BINARY to the above modes and that didn't fix this issue in the Rails environment, so I'm not sure binary is the fix here.

f.flock(::File::LOCK_EX)
state = state_for(f)
minter = ::Noid::Minter.new(state)
id = minter.mint
f.rewind
new_state = Marshal.dump(minter.dump)
f.write(new_state)
f.write(new_state.force_encoding(f.external_encoding))
f.flush
f.truncate(f.pos)
end
Expand Down
10 changes: 10 additions & 0 deletions spec/unit/synchronized_minter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,16 @@
it 'is valid' do
expect(ActiveFedora::Noid::Service.new.valid?(subject)).to be true
end

context 'with a non-UTF8 encoding (mimicking the Rails environment)' do
subject { ActiveFedora::Noid::SynchronizedMinter.new }
before do
allow(subject).to receive(:file_opts) { { encoding: Encoding::UTF_8 } }
end
it 'mints an ID' do
expect { subject.mint }.not_to raise_error
end
end
end

context "when the pid already exists in Fedora" do
Expand Down