Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
143 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# frozen_string_literal: true | ||
|
||
# Creates and removes the druid path from the filesystem | ||
class DruidPath | ||
class SameContentExistsError < RuntimeError; end | ||
class DifferentContentExistsError < RuntimeError; end | ||
|
||
# @param [DruidTools::Druid] druid | ||
def initialize(druid:) | ||
@druid = druid | ||
end | ||
|
||
def mkdir(extra = nil) | ||
new_path = druid.path(extra) | ||
raise DifferentContentExistsError, "Unable to create directory, link already exists: #{new_path}" if File.symlink? new_path | ||
raise SameContentExistsError, "The directory already exists: #{new_path}" if File.directory? new_path | ||
|
||
FileUtils.mkdir_p(new_path) | ||
end | ||
|
||
def mkdir_with_final_link(source, extra = nil) | ||
new_path = druid.path(extra) | ||
raise DifferentContentExistsError, "Unable to create link, directory already exists: #{new_path}" if File.directory?(new_path) && !File.symlink?(new_path) | ||
|
||
real_path = File.expand_path('..', new_path) | ||
FileUtils.mkdir_p(real_path) | ||
FileUtils.ln_s(source, new_path, force: true) | ||
end | ||
|
||
def rmdir(extra = nil) | ||
parts = druid.tree | ||
parts << extra unless extra.nil? | ||
until parts.empty? | ||
dir = File.join(druid.base, *parts) | ||
begin | ||
FileUtils.rm(File.join(dir, '.DS_Store'), force: true) | ||
FileUtils.rmdir(dir) | ||
rescue Errno::ENOTEMPTY | ||
break | ||
end | ||
parts.pop | ||
end | ||
end | ||
|
||
private | ||
|
||
attr_reader :druid | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'rails_helper' | ||
|
||
RSpec.describe DruidPath do | ||
let(:strictly_valid_druid_str) { 'druid:cd456gh1234' } | ||
let(:tree2) { File.join(fixture_dir, 'cd/456/gh/1234/cd456gh1234') } | ||
|
||
after do | ||
FileUtils.rm_rf(File.join(fixture_dir, 'cd')) | ||
end | ||
|
||
describe '#mkdir error handling' do | ||
it 'raises SameContentExistsError if the directory already exists' do | ||
druid_obj = DruidTools::Druid.new(strictly_valid_druid_str, fixture_dir) | ||
service = described_class.new(druid: druid_obj) | ||
service.mkdir | ||
expect { service.mkdir }.to raise_error(described_class::SameContentExistsError) | ||
end | ||
|
||
it 'raises DifferentContentExistsError if a link already exists in the workspace for this druid' do | ||
source_dir = '/tmp/content_dir' | ||
FileUtils.mkdir_p(source_dir) | ||
dr = DruidTools::Druid.new(strictly_valid_druid_str, fixture_dir) | ||
service = described_class.new(druid: dr) | ||
service.mkdir_with_final_link(source_dir) | ||
|
||
expect { service.mkdir }.to raise_error(described_class::DifferentContentExistsError) | ||
end | ||
end | ||
|
||
describe '#mkdir_with_final_link' do | ||
let(:source_dir) { '/tmp/content_dir' } | ||
let(:druid_obj) { DruidTools::Druid.new(strictly_valid_druid_str, fixture_dir) } | ||
let(:service) { described_class.new(druid: druid_obj) } | ||
|
||
before do | ||
FileUtils.mkdir_p(source_dir) | ||
end | ||
|
||
it 'creates a druid tree in the workspace with the final directory being a link to the passed in source' do | ||
service.mkdir_with_final_link(source_dir) | ||
expect(File).to be_symlink(druid_obj.path) | ||
expect(File.readlink(tree2)).to eq(source_dir) | ||
end | ||
|
||
it 'raises DifferentContentExistsError if a directory already exists in the workspace for this druid' do | ||
service.mkdir(fixture_dir) | ||
expect { service.mkdir_with_final_link(source_dir) }.to raise_error(described_class::DifferentContentExistsError) | ||
end | ||
end | ||
|
||
describe '#rmdir' do | ||
let(:druid_str) { 'druid:cd456ef7890' } | ||
let(:tree1) { File.join(fixture_dir, 'cd/456/ef/7890/cd456ef7890') } | ||
let(:druid1) { DruidTools::Druid.new(druid_str, fixture_dir) } | ||
let(:druid2) { DruidTools::Druid.new(strictly_valid_druid_str, fixture_dir) } | ||
let(:service1) { described_class.new(druid: druid1) } | ||
let(:service2) { described_class.new(druid: druid2) } | ||
|
||
it 'destroys druid directories' do | ||
expect(File.exist?(tree1)).to eq false | ||
expect(File.exist?(tree2)).to eq false | ||
|
||
service1.mkdir | ||
expect(File.exist?(tree1)).to eq true | ||
expect(File.exist?(tree2)).to eq false | ||
|
||
service2.mkdir | ||
expect(File.exist?(tree1)).to eq true | ||
expect(File.exist?(tree2)).to eq true | ||
|
||
service2.rmdir | ||
expect(File.exist?(tree1)).to eq true | ||
expect(File.exist?(tree2)).to eq false | ||
|
||
service1.rmdir | ||
expect(File.exist?(tree1)).to eq false | ||
expect(File.exist?(tree2)).to eq false | ||
expect(File.exist?(File.join(fixture_dir, 'cd'))).to eq false | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters