-
-
Notifications
You must be signed in to change notification settings - Fork 11
Update #13 , Correct corner cases #14
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -288,16 +288,26 @@ def create_file(relative_path, *args, context: nil, force: false, skip: false, | |
# @api public | ||
def copy_file(source_path, *args, context: nil, force: false, skip: false, | ||
verbose: true, color: :green, noop: false, preserve: nil, &block) | ||
|
||
source_path = source_path.to_s | ||
unless ::File.file? source_path | ||
log_status(:error, source_path, verbose: verbose, color: :red) | ||
return | ||
end | ||
|
||
dest_path = (args.first || source_path).to_s.sub(/\.erb$/, "") | ||
|
||
if ::File.directory? dest_path | ||
dest_path = ::File.join(dest_path, ::File.basename(source_path)) | ||
end | ||
Comment on lines
+300
to
+302
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch! However, I think we shouldn't change the destination path. If someone wants to copy a file to an existing directory, we should allow for it and the file collision will handle the situation when the file already exists. For example, TTY::File.copy_file("foo/bar.rb", "baz/")
# => baz/bar.rb Also, any changes like this require a test case. |
||
|
||
ctx = if context | ||
context.instance_eval("binding") | ||
else | ||
instance_eval("binding") | ||
end | ||
|
||
create_file(dest_path, context: context, force: force, skip: skip, | ||
file_dest = create_file(dest_path, context: context, force: force, skip: skip, | ||
verbose: verbose, color: color, noop: noop) do | ||
version = ERB.version.scan(/\d+\.\d+\.\d+/)[0] | ||
template = if version.to_f >= 2.2 | ||
|
@@ -309,10 +319,11 @@ def copy_file(source_path, *args, context: nil, force: false, skip: false, | |
content = block[content] if block | ||
content | ||
end | ||
return unless preserve | ||
return file_dest unless preserve | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We need a test case that confirms this behaviour so that it stays true in future releases. |
||
|
||
copy_metadata(source_path, dest_path, verbose: verbose, noop: noop, | ||
color: color) | ||
file_dest | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We need a test case that confirms this behaviour so that it stays true in future releases. |
||
end | ||
module_function :copy_file | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,6 +27,10 @@ def exist? | |
::File.exist?(relative_path) | ||
end | ||
|
||
def directory? | ||
::File.directory?(relative_path) | ||
end | ||
|
||
def identical? | ||
::File.binread(relative_path) == content | ||
end | ||
|
@@ -59,6 +63,11 @@ def convert_encoded_path(filename) | |
# | ||
# @api private | ||
def detect_collision | ||
if directory? | ||
notify(:error, :red) | ||
return | ||
end | ||
Comment on lines
+66
to
+69
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't believe this is necessary if we allow copying a file to a directory destination. This should never be the case unless someone tries to copy a directory to directory, I guess we could test for that case. This would require another test case. |
||
|
||
if exist? | ||
if identical? | ||
notify(:identical, :cyan) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe this statement makes the directory check unreachable. Also, I much prefer to allow copying a file to a directory location rather than providing an error. Wouldn't you agree?