Skip to content

Fix cp overwriting identical files#679

Merged
nfischer merged 5 commits intomasterfrom
cp-fix-identical
Mar 8, 2017
Merged

Fix cp overwriting identical files#679
nfischer merged 5 commits intomasterfrom
cp-fix-identical

Conversation

@freitagbr
Copy link
Copy Markdown
Contributor

If cp is passed a destination that is location of a source file, in effect, copying a file to itself, the source file will become empty. This prevents this situation form happening by verifying that the destination is a different location than the source.

Fixes #678

@freitagbr freitagbr added fix Bug/defect, or a fix for such a problem high priority labels Mar 4, 2017
@freitagbr freitagbr added this to the v0.7.x milestone Mar 4, 2017
@freitagbr freitagbr requested a review from nfischer March 4, 2017 09:28
@freitagbr
Copy link
Copy Markdown
Contributor Author

Same bug from #673 is affecting the windows tests here. Let's see if we can implement a fix.

src/cp.js Outdated
if (path.relative(src, thisDest) === '') {
// a file cannot be copied to itself, but we want to continue copying other files,
// so save the error for later
identicalFiles.push("'" + thisDest + "' and '" + src + "' are the same file");
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Use common.error() here, with the continue: true option.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Will this still set the result.stderr? And shell.error() to true?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It sets result.stderr to the error output (it's a += operation, so it all collects up). shell.error() is some truthy value (it's currently the same string as result.stderr, but this is no longer guaranteed as part of the API and it may later change to an error code).

src/cp.js Outdated
common.error(err, { continue: cont });
});
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Remove this hunk, it shouldn't be necessary

});

test('copy file to same location', t => {
const result = shell.cp('resources/file1', 'resources');
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Does it make sense to also write a test case for shell.cp('resources/file1', 'resources/file1') (copy a file to its same name)?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes, so I added a test for this.

t.is(result.code, 1);
t.is(
result.stderr,
"cp: 'resources/file1' and 'resources/file1' are the same file\n" +
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think we should just check t.truthy(result.stderr) for now, with a TODO to update it later. Please tag issue #6 in the TODO.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Issue #6? Or issue #681?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This test was fixed by rebasing off of master.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I meant 681. But this comment can be ignored now 😄

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I figured. Once this is approved it should be good to merge.

@codecov-io
Copy link
Copy Markdown

codecov-io commented Mar 7, 2017

Codecov Report

Merging #679 into master will increase coverage by 0.01%.
The diff coverage is 100%.

@@            Coverage Diff             @@
##           master     #679      +/-   ##
==========================================
+ Coverage   94.32%   94.33%   +0.01%     
==========================================
  Files          33       33              
  Lines        1180     1183       +3     
==========================================
+ Hits         1113     1116       +3     
  Misses         67       67
Impacted Files Coverage Δ
src/cp.js 90.35% <100%> (+0.26%)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update b6b2cd8...311ed4d. Read the comment docs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

fix Bug/defect, or a fix for such a problem high priority

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants