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

Commit fc594f8

Browse files
authored
Ensure FileInfo is not relative in DirectoryInfoProxy (microsoft#1519)
* Ensure FileInfo is not relative in DirectoryInfoProxy * Add tests
1 parent ce574a6 commit fc594f8

File tree

3 files changed

+86
-1
lines changed

3 files changed

+86
-1
lines changed

src/Core/Impl/IO/DirectoryInfoProxy.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public IEnumerable<IFileSystemInfo> EnumerateFileSystemInfos(string[] includePat
5555
var matcher = GetMatcher(includePatterns, excludePatterns);
5656
PatternMatchingResult matchResult = SafeExecuteMatcher(matcher);
5757
return matchResult.Files.Select((filePatternMatch) => {
58-
var path = PathUtils.NormalizePath(filePatternMatch.Path);
58+
var path = PathUtils.NormalizePath(Path.Combine(_directoryInfo.FullName, filePatternMatch.Path));
5959
return CreateFileSystemInfoProxy(new FileInfo(path));
6060
});
6161
}

src/Core/Test/AssemblySetup.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright(c) Microsoft Corporation
2+
// All rights reserved.
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the License); you may not use
5+
// this file except in compliance with the License. You may obtain a copy of the
6+
// License at http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS
9+
// OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY
10+
// IMPLIED WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
11+
// MERCHANTABILITY OR NON-INFRINGEMENT.
12+
//
13+
// See the Apache Version 2.0 License for specific language governing
14+
// permissions and limitations under the License.
15+
16+
using Microsoft.Python.Core.Testing;
17+
using Microsoft.VisualStudio.TestTools.UnitTesting;
18+
using TestUtilities;
19+
20+
namespace Microsoft.Python.Core.Tests {
21+
[TestClass]
22+
public sealed class AssemblySetup {
23+
[AssemblyInitialize]
24+
public static void Initialize(TestContext testContext) => AnalysisTestEnvironment.Initialize();
25+
26+
private class AnalysisTestEnvironment : TestEnvironmentImpl, ITestEnvironment {
27+
public static void Initialize() {
28+
var instance = new AnalysisTestEnvironment();
29+
Instance = instance;
30+
TestEnvironment.Current = instance;
31+
}
32+
}
33+
}
34+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Copyright(c) Microsoft Corporation
2+
// All rights reserved.
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the License); you may not use
5+
// this file except in compliance with the License. You may obtain a copy of the
6+
// License at http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS
9+
// OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY
10+
// IMPLIED WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
11+
// MERCHANTABILITY OR NON-INFRINGEMENT.
12+
//
13+
// See the Apache Version 2.0 License for specific language governing
14+
// permissions and limitations under the License.
15+
16+
using System.IO;
17+
using System.Linq;
18+
using System.Threading.Tasks;
19+
using FluentAssertions;
20+
using Microsoft.Python.Core.IO;
21+
using Microsoft.VisualStudio.TestTools.UnitTesting;
22+
using TestUtilities;
23+
24+
namespace Microsoft.Python.Core.Tests {
25+
[TestClass]
26+
public class DirectoryInfoProxyTests {
27+
public TestContext TestContext { get; set; }
28+
29+
[TestInitialize]
30+
public void TestInitialize()
31+
=> TestEnvironmentImpl.TestInitialize($"{TestContext.FullyQualifiedTestClassName}.{TestContext.TestName}");
32+
33+
[TestCleanup]
34+
public void Cleanup() => TestEnvironmentImpl.TestCleanup();
35+
36+
[TestMethod, Priority(0)]
37+
public async Task EnumerateFileSystemInfos() {
38+
var root = TestData.GetTestSpecificPath();
39+
await TestData.CreateTestSpecificFileAsync("a_y.py", "not important");
40+
await TestData.CreateTestSpecificFileAsync("b_y.py", "not important");
41+
await TestData.CreateTestSpecificFileAsync("c_z.py", "not important");
42+
43+
var proxy = new DirectoryInfoProxy(root);
44+
var files = proxy.EnumerateFileSystemInfos(new[] { "*.py" }, new[] { "*z.py" }).OrderBy(x => x.FullName).ToArray();
45+
files.Should().HaveCount(2);
46+
47+
files[0].FullName.Should().Be(Path.Combine(root, "a_y.py"));
48+
files[1].FullName.Should().Be(Path.Combine(root, "b_y.py"));
49+
}
50+
}
51+
}

0 commit comments

Comments
 (0)