site_file: catch for moving directory into itself

This commit is contained in:
Kyle Drake
2025-09-23 16:06:23 -05:00
parent 81d97432cd
commit c281b1a992
2 changed files with 18 additions and 0 deletions
+4
View File
@@ -89,6 +89,10 @@ class SiteFile < Sequel::Model
return false, 'directory name cannot end with .htm or .html'
end
if new_path.start_with?(current_path + '/')
return false, 'cannot move directory into itself'
end
else # a file
begin
mime_type = Magic.guess_file_mime_type site.files_path(self.path)
+14
View File
@@ -107,6 +107,20 @@ describe 'site_files' do
_(res).must_equal [false, 'directory name cannot end with .htm or .html']
end
it 'fails when trying to move directory into itself' do
@site.create_directory 'dir'
dir = @site.site_files_dataset.where(path: 'dir').first
res = dir.rename('dir/newdir')
_(res).must_equal [false, 'cannot move directory into itself']
_(@site.site_files_dataset.where(path: 'dir').first).wont_equal nil
_(@site.site_files_dataset.where(path: 'dir/newdir').first).must_equal nil
res = dir.rename('dir/sub/dir')
_(res).must_equal [false, 'cannot move directory into itself']
_(@site.site_files_dataset.where(path: 'dir').first).wont_equal nil
_(@site.site_files_dataset.where(path: 'dir/sub/dir').first).must_equal nil
end
it 'wont set an empty directory' do
@site.create_directory 'dirone'
_(@site.site_files.select {|sf| sf.path == 'dirone'}.length).must_equal 1