Skip to content
This repository was archived by the owner on Nov 6, 2018. It is now read-only.

Remove support for relative path starting with / #148

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,7 @@ public IFileInfo GetFileInfo(string subpath)

var builder = new StringBuilder(_baseNamespace.Length + subpath.Length);
builder.Append(_baseNamespace);

// Relative paths starting with a leading slash okay
if (subpath.StartsWith("/", StringComparison.Ordinal))
{
builder.Append(subpath, 1, subpath.Length - 1);
}
else
{
builder.Append(subpath);
}
builder.Append(subpath);

for (var i = _baseNamespace.Length; i < builder.Length; i++)
{
Expand Down Expand Up @@ -105,12 +96,6 @@ public IDirectoryContents GetDirectoryContents(string subpath)
return new NotFoundDirectoryContents();
}

// Relative paths starting with a leading slash okay
if (subpath.StartsWith("/", StringComparison.Ordinal))
{
subpath = subpath.Substring(1);
}

// Non-hierarchal.
if (!subpath.Equals(string.Empty))
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you combine this with the condition at Line 94?

{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,6 @@ public IFileInfo GetFileInfo(string subpath)
return new NotFoundFileInfo(subpath);
}

// Relative paths starting with a leading slash okay
if (subpath.StartsWith("/", StringComparison.Ordinal))
{
subpath = subpath.Substring(1);
}

// Absolute paths not permitted.
if (Path.IsPathRooted(subpath))
{
Expand Down Expand Up @@ -122,12 +116,6 @@ public IDirectoryContents GetDirectoryContents(string subpath)
return new NotFoundDirectoryContents();
}

// Relative paths starting with a leading slash okay
if (subpath.StartsWith("/", StringComparison.Ordinal))
{
subpath = subpath.Substring(1);
}

// Absolute paths not permitted.
if (Path.IsPathRooted(subpath))
{
Expand Down Expand Up @@ -179,12 +167,6 @@ public IChangeToken Watch(string filter)
return NoopChangeToken.Singleton;
}

// Relative paths starting with a leading slash okay
if (filter.StartsWith("/", StringComparison.Ordinal))
{
filter = filter.Substring(1);
}

// Absolute paths not permitted.
if (Path.IsPathRooted(filter))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,15 @@ public void GetFileInfo_ReturnsNotFoundFileInfo_IfFileDoesNotExist()
Assert.False(fileInfo.Exists);
}

[Theory]
[InlineData("File.txt")]
[InlineData("/File.txt")]
public void GetFileInfo_ReturnsFilesAtRoot(string filePath)
[Fact]
public void GetFileInfo_ReturnsFilesAtRoot()
{
// Arrange
var provider = new EmbeddedFileProvider(GetType().GetTypeInfo().Assembly, Namespace);
var expectedFileLength = new FileInfo("File.txt").Length;

// Act
var fileInfo = provider.GetFileInfo(filePath);
var fileInfo = provider.GetFileInfo("File.txt");

// Assert
Assert.NotNull(fileInfo);
Expand Down Expand Up @@ -70,7 +68,6 @@ public void GetFileInfo_ReturnsNotFoundIfPathStartsWithBackSlash()
{
// Arrange
var provider = new EmbeddedFileProvider(GetType().GetTypeInfo().Assembly, Namespace);
var expectedFileLength = new FileInfo("File.txt").Length;

// Act
var fileInfo = provider.GetFileInfo("\\File.txt");
Expand All @@ -80,6 +77,20 @@ public void GetFileInfo_ReturnsNotFoundIfPathStartsWithBackSlash()
Assert.False(fileInfo.Exists);
}

[Fact]
public void GetFileInfo_ReturnsNotFoundIfPathStartsWithSlash()
{
// Arrange
var provider = new EmbeddedFileProvider(GetType().GetTypeInfo().Assembly, Namespace);

// Act
var fileInfo = provider.GetFileInfo("/File.txt");

// Assert
Assert.NotNull(fileInfo);
Assert.False(fileInfo.Exists);
}

public static TheoryData GetFileInfo_LocatesFilesUnderSpecifiedNamespaceData
{
get
Expand Down Expand Up @@ -156,16 +167,14 @@ public void GetFileInfo_LocatesFilesUnderSubDirectories(string path)
Assert.Equal("File.txt", fileInfo.Name);
}

[Theory]
[InlineData("")]
[InlineData("/")]
public void GetDirectoryContents_ReturnsAllFilesInFileSystem(string path)
[Fact]
public void GetDirectoryContents_ReturnsAllFilesInFileSystem()
{
// Arrange
var provider = new EmbeddedFileProvider(GetType().GetTypeInfo().Assembly, Namespace + ".Resources");

// Act
var files = provider.GetDirectoryContents(path);
var files = provider.GetDirectoryContents("");

// Assert
Assert.Collection(files.OrderBy(f => f.Name, StringComparer.Ordinal),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,15 @@ public void ExistingFilesReturnTrue()
var info = provider.GetFileInfo("File.txt");
Assert.NotNull(info);
Assert.True(info.Exists);
}

Copy link
Member

Choose a reason for hiding this comment

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

Need a negative test making sure that passing /file does the appropriate thing on each OS.

info = provider.GetFileInfo("/File.txt");
[Fact]
public void GetFileInfo_FiltersSlashPath()
{
var provider = new PhysicalFileProvider(Directory.GetCurrentDirectory());
var info = provider.GetFileInfo("/File.txt");
Assert.NotNull(info);
Assert.True(info.Exists);
Assert.False(info.Exists);
}

[Fact]
Expand Down Expand Up @@ -452,21 +457,33 @@ public void Token_For_Whitespace_Filters()
}
}

[ConditionalFact]
[OSSkipCondition(OperatingSystems.Linux, SkipReason = "Skipping until #104 is resolved.")]
[OSSkipCondition(OperatingSystems.MacOSX, SkipReason = "Skipping until #104 is resolved.")]
[Fact]
public void Token_For_AbsolutePath_Filters()
{
using (var root = new DisposableFileSystem())
{
var provider = new PhysicalFileProvider(root.RootPath);
var path = Path.Combine(root.RootPath, "filename");
var path = Path.GetFullPath(Path.Combine(root.RootPath, "filename"));
var token = provider.Watch(path);

Assert.Same(NoopChangeToken.Singleton, token);
}
}


[Fact]
public void Token_For_PathStartigWithSlash_Filters()
{
using (var root = new DisposableFileSystem())
{
var provider = new PhysicalFileProvider(root.RootPath);
var path = Path.GetFullPath(Path.Combine(root.RootPath, ""));
var token = provider.Watch("/filename");

Assert.Same(NoopChangeToken.Singleton, token);
}
}

[Fact]
public async Task Token_Fired_For_File_Or_Directory_Create_And_Delete()
{
Expand Down Expand Up @@ -519,7 +536,7 @@ public async Task Tokens_With_Path_Ending_With_Slash()
string folderName = Guid.NewGuid().ToString();

int tokenCount = 0;
var filetoken = provider.Watch("/" + folderName + "/");
var filetoken = provider.Watch(folderName + "/");
filetoken.RegisterChangeCallback(_ => { tokenCount++; }, null);

var folderPath = Path.Combine(root.RootPath, folderName);
Expand All @@ -529,14 +546,14 @@ public async Task Tokens_With_Path_Ending_With_Slash()
await Task.Delay(WaitTimeForTokenToFire);
Assert.Equal(1, tokenCount);

filetoken = provider.Watch("/" + folderName + "/");
filetoken = provider.Watch(folderName + "/");
filetoken.RegisterChangeCallback(_ => { tokenCount++; }, null);

File.AppendAllText(Path.Combine(folderPath, fileName), "UpdatedContent");
await Task.Delay(WaitTimeForTokenToFire);
Assert.Equal(2, tokenCount);

filetoken = provider.Watch("/" + folderName + "/");
filetoken = provider.Watch(folderName + "/");
filetoken.RegisterChangeCallback(_ => { tokenCount++; }, null);

File.Delete(Path.Combine(folderPath, fileName));
Expand All @@ -556,15 +573,15 @@ public async Task Tokens_With_Path_Not_Ending_With_Slash()

int tokenCount = 0;
// Matches file/directory with this name.
var filetoken = provider.Watch("/" + directoryName);
var filetoken = provider.Watch(directoryName);
filetoken.RegisterChangeCallback(_ => { tokenCount++; }, null);

Directory.CreateDirectory(Path.Combine(root.RootPath, directoryName));
await Task.Delay(WaitTimeForTokenToFire);
Assert.Equal(1, tokenCount);

// Matches file/directory with this name.
filetoken = provider.Watch("/" + fileName);
filetoken = provider.Watch(fileName);
filetoken.RegisterChangeCallback(_ => { tokenCount++; }, null);

File.WriteAllText(Path.Combine(root.RootPath, fileName), "Content");
Expand Down Expand Up @@ -705,20 +722,16 @@ public void Tokens_With_Forward_And_Backward_Slash()
using (var root = new DisposableFileSystem())
{
var provider = new PhysicalFileProvider(root.RootPath);
var token1 = provider.Watch("/a/b");
var token2 = provider.Watch("a/b");
var token3 = provider.Watch(@"a\b");
var token1 = provider.Watch("a/b");
var token2 = provider.Watch(@"a\b");

Assert.Equal(token2, token1);
Assert.Equal(token3, token2);

Assert.True(token1.ActiveChangeCallbacks);
Assert.True(token2.ActiveChangeCallbacks);
Assert.True(token3.ActiveChangeCallbacks);

Assert.False(token1.HasChanged);
Assert.False(token2.HasChanged);
Assert.False(token3.HasChanged);
}
}

Expand Down