Added TestingHelper for directory SetAttributes#138
Added TestingHelper for directory SetAttributes#138fgreinacher merged 1 commit intoTestableIO:masterfrom
Conversation
|
@kmcginnes the implementation looks good. Can you add the parameter checks (et al) based on the documentation and the actual implementation. |
f57bfab to
a55cbc1
Compare
|
I updated the code to the latest and fixed conflicts. I also added some tests that prove out some of the parameter preconditions. Let me know what else you want. |
There was a problem hiding this comment.
The name of the method does not match the thrown exception.
see other comment below
There was a problem hiding this comment.
Good catch. Copy pasta...
|
there is a useful vs extension to recognize the trailing whitespaces it really helps a lot. I deactivated removing the trailing whitespaces on saving. |
|
I've made the changes requested. I would also like to move the SetAttributes tests into their own file. Although, that might make more sense as a separate pull request. What do you think? |
|
Thanks for this PR @kmcginnes. We're making progress on getting through the open PRs and issues that have been sitting around for awhile.. (ok a LONG while). This one looks like it's in pretty good shape, and you still seem pretty active. If you have interest in resolving the merge conflicts, we can re-run the checks and get this merged in for you. |
|
I'll take a look. I'll have to reacquaint myself with these changes. @jpreese I think it would be a good idea if I squash these commits down to one. I don't see a reason to keep the multiple commits around. |
|
I agree. When it's time to merge into master, I'll be squashing them. |
|
So, I take it you want me to merge |
|
For the PR I'm indifferent. I was just going to squash before approving. |
5df28a5 to
c01c1a5
Compare
|
A couple of questions:
[Test]
public void MockFile_SetAttributeOfExistingDirectoryWithPathTooLong_ShouldThrowPathTooLongException()
{
var path = XFS.Path("C:\\8e95d05bc8d747d194e59516f8d1493b\\aa3a4391679642f9bf83f9501f7fb0e1\\8c51cab5b63543909fb7da59a3932b47\\49910ca19d0a4b59aadd4276829cc476\\3b0540cbd57b4770a52166c0ffd72e10\\70ab25de331e473a9cffbc5e297c79c1\\607c63a6f57645aa98073e9df7c7edd8\\dc473e93228c44c08d127c36d9cd9b01");
var attributes = FileAttributes.Normal;
var fileSystem = new MockFileSystem();
TestDelegate action = () => fileSystem.File.SetAttributes(path, attributes);
Assert.Throws<PathTooLongException>(action);
} |
| [Test] | ||
| public void MockFile_SetAttributeOfExistingDirectoryWithPathTooLong_ShouldThrowPathTooLongException() | ||
| { | ||
| var path = XFS.Path("C:\\8e95d05bc8d747d194e59516f8d1493b\\aa3a4391679642f9bf83f9501f7fb0e1\\8c51cab5b63543909fb7da59a3932b47\\49910ca19d0a4b59aadd4276829cc476\\3b0540cbd57b4770a52166c0ffd72e10\\70ab25de331e473a9cffbc5e297c79c1\\607c63a6f57645aa98073e9df7c7edd8\\dc473e93228c44c08d127c36d9cd9b01"); |
There was a problem hiding this comment.
Instead of this approach, I think calling something like string.PadRight() would be a little nicer.
| } | ||
| else | ||
| { | ||
| throw new FileNotFoundException($"Could not find file '{path}'.", path); |
There was a problem hiding this comment.
I would take this opportunity to move this string into the Resources.resx file. Just make sure it's the same verbiage that System.IO uses, which it already looks like it is.
There was a problem hiding this comment.
As a followup, I'm about to deploy this resource verbiage as part of another PR.
There was a problem hiding this comment.
Ok, you should be able to use StringResources.Manager.GetString("COULD_NOT_FIND_FILE_EXCEPTION")
There was a problem hiding this comment.
I see what I'm supposed to be able to do, but the compiler keeps telling me:
'StringResources' is inaccessible due to its protection level
And I do see that you have this defined in the AssemblyInfo.cs:
using System.Runtime.CompilerServices;
#if DEBUG
[assembly: InternalsVisibleTo("System.IO.Abstractions.TestingHelpers.Tests")]
#else
[assembly: InternalsVisibleTo("System.IO.Abstractions.TestingHelpers.Tests, PublicKey=002400000480000094000000060200000024000052534131000400000100010051bf2aa00ba30d507d4cebcab1751dfa13768a6f5235ce52da572260e33a11f52b87707f858fe4bbe32cd51830a8dd73245f688902707fa797c07205ff9b5212f93760d52f6d13022a286ff7daa13a0cd9eb958e888fcd7d9ed1f7cf76b19a5391835a7b633418a5f584d10925d76810f782f6b814cc34a2326b438abdc3b5bd")]
#endifSo, in theory, it should work. I even tried shortening the assembly name to be TestingHelpers.Tests, which I believe is actually what it should be, but that didn't seem to help.
It looks like I'm the first consumer of StringResources in the test project. That's why I'm the first to see this.
😭
There was a problem hiding this comment.
I'm not sure I follow. You're able to use it in the TestingHelpers library right? For MockInfo.cs? I wouldn't bother using it as an Assertion within the test. Just checking that it throws a FileNotFoundException is more than enough.
There was a problem hiding this comment.
Oh, I totally misread your first comment, and what file it was on.
I'll make that change now.
| [Test] | ||
| public void MockFile_SetAttributeOfMissingDirectory_ShouldThrowFileNotFoundException() | ||
| { | ||
| var path = XFS.Path("C:\\Some\\Path\\That\\Does\\Not\\Exist"); |
There was a problem hiding this comment.
While I like the intent that it shows, we can probably dramatically reduce the length of this test data to simply C:\ or something along those lines. Since we never add anything to the MockFileSystem, any path is going to fail since no paths exist.
It also reduces the chance of the test failing because it depends on two artfully crafted strings (one in the arrange, and one in the assert)
|
|
What do you think I should do about the
|
|
My gut says 4. You're not introducing the logic that checks the length. That's probably going to be the job of GetFullPath (or maybe even elsewhere, definitely not SetAttributes) sometime in the future. I think the check for a long path should be tied with the class/method that's going to do the check. Yeah? |
501e30a to
727ac11
Compare
|
Ok, so I've updated with the following:
|
727ac11 to
c13ff38
Compare
| [TestFixture] | ||
| public class MockFileSetAttributesTests | ||
| { | ||
|
|
|
|
||
| namespace System.IO.Abstractions.TestingHelpers.Tests | ||
| { | ||
| using XFS = MockUnixSupport; |
There was a problem hiding this comment.
I'd prefer if this was moved up with the other usings, for consistency.
There was a problem hiding this comment.
I prefer the same. I only put it here for consistency with other test files:
| fileSystem.AddDirectory(path); | ||
| var existingAttributes = fileSystem.File.GetAttributes(path); | ||
|
|
||
| fileSystem.File.SetAttributes(path, existingAttributes | FileAttributes.Hidden); |
There was a problem hiding this comment.
The file case makes perfect sense to me. It starts out Normal, and we set the attribute to Hidden. Thus the assertion should be to make sure that the net result is that the file is Hidden (replaced the attribute).
I'm not quite certain why the directory test is pulling the existing attributes, and the File test did not?
There was a problem hiding this comment.
The explanation is that I wrote the directory ones, and the file one was simply moved from the MockFileTests.cs file and into MockFileSetAttributesTests.cs to live along side my new tests.
But I think I agree with you. The file and directory tests should be consistent. And the MSDN article on SetAttributes() shows you should always add or remove from the existing set of attributes:
https://msdn.microsoft.com/en-us/library/system.io.file.setattributes(v=vs.110).aspx
So my next commit will update the file test with these changes.
There was a problem hiding this comment.
Ok. Let me grab your branch real quick and mess around with it in my IDE. I still have a couple concerns about how these tests are laid out, but I could be taking crazy pills.
There was a problem hiding this comment.
Ok, just went through the debugger and confirmed. So, I think how you had it first was correct, given functionality, test name, etc.
Right now you're saying "ShouldREPLACEExistingFlags...", but really what we're doing is ADDing more flags. So the end result is a file that is both "Normal" and "Hidden".
I think that SetAttributes() can be used sort of like a pseudo-RemoveAttribute. i.e. if you have a file thats Hidden -- to remove the hidden and make it normal, I'd think SetAttributes(FileAttributes.Normal) would work. And that would be "replacing".
If you want to preserve all/some of your attributes, then it probably makes sense to selectively remove and then add onto. But if you want to remove all but "Normal" or whatever, I don't think you need to -always- get the current attributes.
Hopefully that wasnt too much of a ramble. The TLDR is that I think it's possible we have two test case scenarios here "replace" and "add additional".
There was a problem hiding this comment.
I see your point on the file test.
But for directories, they always need to have the FileAttributes.Directory flag. So a full replacement isn't really possible.
How about initializing the directory's attributes to something in addition to the FileAttributes.Directory flag. Then we can replace it with something else.
Like this:
[Test]
public void MockFile_SetAttributes_ShouldReplaceExistingFlagsIfExistingDirectory()
{
var fileSystem = new MockFileSystem();
var path = XFS.Path(@"c:\something");
fileSystem.AddDirectory(path);
var directoryInfo = fileSystem.DirectoryInfo.FromDirectoryName(path);
directoryInfo.Attributes = FileAttributes.Directory | FileAttributes.Normal;
fileSystem.File.SetAttributes(path, FileAttributes.Directory | FileAttributes.Hidden);
var attributes = fileSystem.File.GetAttributes(path);
Assert.That(attributes, Is.EqualTo(FileAttributes.Directory | FileAttributes.Hidden));
}There was a problem hiding this comment.
Honestly, the more I think about it and familiarize myself with everything. I don't really see anything wrong with what you're tests are testing.
I just think the naming is less than ideal and may be a source of confusion (at least for me). Maybe we could just change them to something along the lines of MockFile_SetAttributes_ShouldSetAttributesOnDirectory and MockFile_SetAttributes_ShouldSetAttributesOnFile. Simple and to the point. No worry about what does "replace" mean or are we "adding" more attributes.
I would also drop the..
Attributes = FileAttributes.Normal
initialization on the File test since that should be set by System.IO.Abstractions for you.
What do you think about that?
There was a problem hiding this comment.
Sorry, I'll hopefully have a chance to take a look at this again tomorrow.
Life, amiright 😞
There was a problem hiding this comment.
No worries, we've made you wait years, I'm sure we can wait a little bit. No sweat! 👍
There was a problem hiding this comment.
I've made the two changes you suggested, but also made the attributes more explicit. I'm not sure if I like it better or not.
There was a problem hiding this comment.
@jpreese I'll hold off on cleaning up the commits and rebasing on master until you've had a chance to check out these changes. If you're not a fan of making the attributes more explicit in the tests, it's an easy commit to just delete.
|
Couple small nits and just a general question, otherwise looks great! |
|
I’ll merge as soon as the conflicts are resolved! |
|
@fgreinacher Don't merge until I have a chance to fixup some of these commits. I'm keeping all the commits separate during review, but will cleanup once I get a 👍 |
fgreinacher
left a comment
There was a problem hiding this comment.
One minor thing from my side - otherwise LGTM (I like the shorter test names 👍)
| { | ||
| if (path == null) | ||
| { | ||
| throw new ArgumentNullException("path", StringResources.Manager.GetString("VALUE_CANNOT_BE_NULL")); |
|
@kmcginnes Could you update this branch one last time? I’d then squash-merge, so no worries about cleaning up the history :) |
This was supported for files before, but directories behave a little differenlty. This should better match .Net System.IO behavior.
9b2e621 to
86c8d2c
Compare
|
The branch has been updated from upstream, and I've squashed all my commits. We should be good to go 🚀 |
|
Thanks again, merging! |
I've implemented the logic for SetAttributes for directories. The code for files was already in place. And someone else had already provided the code to get attributes from directory paths. I just borrowed and adapted that code.