-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added methods for working with files (#241)
* Added methods for working with files copy file move file get file size * Added tests for new methods * update CHANGELOG.md
- Loading branch information
Showing
7 changed files
with
114 additions
and
0 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
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,31 @@ | ||
# frozen_string_literal: true | ||
|
||
module OnlyofficeFileHelper | ||
# Methods used to work with files | ||
module FileMethods | ||
# Copies a file | ||
# @param [String] file_path path to file to be copied | ||
# @param [String] destination path to where to copy file | ||
def copy_file(file_path, destination) | ||
FileUtils.mkdir_p(destination) unless File.directory?(destination) | ||
FileUtils.copy(file_path, destination) | ||
end | ||
|
||
# Moves a file | ||
# @param [String] file_path path to file to be moved | ||
# @param [String] destination path to where to move file | ||
def move_file(file_path, destination) | ||
FileUtils.mkdir_p(destination) unless File.directory?(destination) | ||
FileUtils.move(file_path, destination) | ||
end | ||
|
||
# Get file size in bytes | ||
# @param [String] file_path path to file | ||
# @return [Integer] size of file in bytes | ||
def file_size(file_path) | ||
size = File.size?(file_path) | ||
size = 0 if size.nil? | ||
size | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'spec_helper' | ||
|
||
RSpec.describe OnlyofficeFileHelper::FileHelper, '.copy_file' do | ||
let(:file_path) { Tempfile.new('copy').path.to_s } | ||
let(:destination) { "/tmp/#{Random.hex(12)}/" } | ||
|
||
it 'copied directory created' do | ||
described_class.copy_file(file_path, destination) | ||
expect(File).to be_directory(destination) | ||
end | ||
|
||
it 'if copied folder already exists' do | ||
described_class.create_folder(destination) | ||
described_class.copy_file(file_path, destination) | ||
expect(File).to be_directory(destination) | ||
end | ||
|
||
it 'file exists in the new directory' do | ||
described_class.copy_file(file_path, destination) | ||
expect(File).to exist(destination) | ||
end | ||
end |
20 changes: 20 additions & 0 deletions
20
spec/onlyoffice_file_helper/file_helper_get_file_size_spec.rb
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,20 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'spec_helper' | ||
|
||
RSpec.describe OnlyofficeFileHelper::FileHelper, '.get_file_size' do | ||
let(:file_path) { "/tmp/file_size#{Random.hex(12)}" } | ||
|
||
it 'get file size' do | ||
described_class.create_file_with_size(file_path: file_path, size: '15K') | ||
expect(described_class.file_size(file_path)).to be(15_360) | ||
end | ||
|
||
it 'get file size if file size 0' do | ||
expect(described_class.file_size(Tempfile.new('size').path.to_s)).to be(0) | ||
end | ||
|
||
it 'get file size if nonexistent file' do | ||
expect(described_class.file_size("/tmp/nonexistent_file#{Random.hex(12)}")).to be(0) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'spec_helper' | ||
|
||
RSpec.describe OnlyofficeFileHelper::FileHelper, '.move_file' do | ||
let(:file_path) { Tempfile.new('move').path.to_s } | ||
let(:destination) { "/tmp/#{Random.hex(12)}/" } | ||
|
||
it 'if moved folder already exists' do | ||
described_class.create_folder(destination) | ||
described_class.move_file(file_path, destination) | ||
expect(File).to be_directory(destination) | ||
end | ||
|
||
it 'moved directory created' do | ||
described_class.move_file(file_path, destination) | ||
expect(File).to be_directory(destination) | ||
end | ||
|
||
it 'file is moved from the source directory' do | ||
described_class.move_file(file_path, destination) | ||
expect(File).not_to exist(file_path) | ||
end | ||
|
||
it 'file exists in the new directory' do | ||
described_class.move_file(file_path, destination) | ||
expect(File).to exist(destination) | ||
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