This repository was archived by the owner on Apr 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 133
Rework search path resolution #1289
Merged
jakebailey
merged 10 commits into
microsoft:master
from
jakebailey:rework-search-path-resolution
Jul 10, 2019
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
ead5c7b
first working path classfier, breaks existing path code for now
jakebailey 10f42be
inherit Comparer instead of implementing both
jakebailey 5567c37
don't do path depth comparison, preserve user ordering as before
jakebailey 98cc56f
switch to using new path classification code in main module resolution
jakebailey dc9259d
fix typo
jakebailey 01339ce
clean up comment about paths in Server.cs
jakebailey b7f010a
use String.Split instead of regex when parsing script output
jakebailey 78010cc
test ordering of user provided paths
jakebailey 524ae5b
add new normalize and trim helper, check preconditions as debug asser…
jakebailey 9ed05ee
use Split extension, don't MaybeEnumerate paths
jakebailey File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,218 @@ | ||
// Copyright(c) Microsoft Corporation | ||
// All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the License); you may not use | ||
// this file except in compliance with the License. You may obtain a copy of the | ||
// License at http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS | ||
// OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY | ||
// IMPLIED WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, | ||
// MERCHANTABILITY OR NON-INFRINGEMENT. | ||
// | ||
// See the Apache Version 2.0 License for specific language governing | ||
// permissions and limitations under the License. | ||
|
||
using System; | ||
using System.IO; | ||
using FluentAssertions; | ||
using Microsoft.Python.Analysis.Core.Interpreter; | ||
using Microsoft.Python.Core.IO; | ||
using Microsoft.Python.Core.OS; | ||
using Microsoft.Python.Tests.Utilities.FluentAssertions; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using TestUtilities; | ||
|
||
namespace Microsoft.Python.Analysis.Tests { | ||
[TestClass] | ||
public class PathClassificationTests { | ||
private readonly FileSystem _fs = new FileSystem(new OSPlatform()); | ||
|
||
public TestContext TestContext { get; set; } | ||
|
||
[TestInitialize] | ||
public void TestInitialize() | ||
=> TestEnvironmentImpl.TestInitialize($"{TestContext.FullyQualifiedTestClassName}.{TestContext.TestName}"); | ||
|
||
[TestCleanup] | ||
public void Cleanup() => TestEnvironmentImpl.TestCleanup(); | ||
|
||
[TestMethod] | ||
public void Plain() { | ||
var appPath = TestData.GetTestSpecificPath("app.py"); | ||
var root = Path.GetDirectoryName(appPath); | ||
|
||
var venv = Path.Combine(root, "venv"); | ||
var venvLib = Path.Combine(venv, "Lib"); | ||
var venvSitePackages = Path.Combine(venvLib, "site-packages"); | ||
|
||
var fromInterpreter = new[] { | ||
new PythonLibraryPath(venvLib, PythonLibraryPathType.StdLib), | ||
new PythonLibraryPath(venv, PythonLibraryPathType.StdLib), | ||
new PythonLibraryPath(venvSitePackages, PythonLibraryPathType.Site), | ||
}; | ||
|
||
var (interpreterPaths, userPaths) = PythonLibraryPath.ClassifyPaths(root, _fs, fromInterpreter, Array.Empty<string>()); | ||
|
||
interpreterPaths.Should().BeEquivalentToWithStrictOrdering(new[] { | ||
new PythonLibraryPath(venvLib, PythonLibraryPathType.StdLib), | ||
new PythonLibraryPath(venv, PythonLibraryPathType.StdLib), | ||
new PythonLibraryPath(venvSitePackages, PythonLibraryPathType.Site), | ||
}); | ||
|
||
userPaths.Should().BeEmpty(); | ||
} | ||
|
||
[TestMethod] | ||
public void WithSrcDir() { | ||
var appPath = TestData.GetTestSpecificPath("app.py"); | ||
var root = Path.GetDirectoryName(appPath); | ||
|
||
var venv = Path.Combine(root, "venv"); | ||
var venvLib = Path.Combine(venv, "Lib"); | ||
var venvSitePackages = Path.Combine(venvLib, "site-packages"); | ||
|
||
var src = Path.Combine(root, "src"); | ||
|
||
var fromInterpreter = new[] { | ||
new PythonLibraryPath(venvLib, PythonLibraryPathType.StdLib), | ||
new PythonLibraryPath(venv, PythonLibraryPathType.StdLib), | ||
new PythonLibraryPath(venvSitePackages, PythonLibraryPathType.Site), | ||
}; | ||
|
||
var fromUser = new[] { | ||
"./src", | ||
}; | ||
|
||
var (interpreterPaths, userPaths) = PythonLibraryPath.ClassifyPaths(root, _fs, fromInterpreter, fromUser); | ||
|
||
interpreterPaths.Should().BeEquivalentToWithStrictOrdering(new[] { | ||
new PythonLibraryPath(venvLib, PythonLibraryPathType.StdLib), | ||
new PythonLibraryPath(venv, PythonLibraryPathType.StdLib), | ||
new PythonLibraryPath(venvSitePackages, PythonLibraryPathType.Site), | ||
}); | ||
|
||
userPaths.Should().BeEquivalentToWithStrictOrdering(new[] { | ||
new PythonLibraryPath(src, PythonLibraryPathType.Unspecified), | ||
}); | ||
} | ||
|
||
[TestMethod] | ||
public void NormalizeUser() { | ||
var appPath = TestData.GetTestSpecificPath("app.py"); | ||
var root = Path.GetDirectoryName(appPath); | ||
|
||
var src = Path.Combine(root, "src"); | ||
|
||
var fromUser = new[] { | ||
"./src/", | ||
}; | ||
|
||
var (interpreterPaths, userPaths) = PythonLibraryPath.ClassifyPaths(root, _fs, Array.Empty<PythonLibraryPath>(), fromUser); | ||
|
||
interpreterPaths.Should().BeEmpty(); | ||
|
||
userPaths.Should().BeEquivalentToWithStrictOrdering(new[] { | ||
new PythonLibraryPath(src, PythonLibraryPathType.Unspecified), | ||
}); | ||
} | ||
|
||
[TestMethod] | ||
public void NestedUser() { | ||
var appPath = TestData.GetTestSpecificPath("app.py"); | ||
var root = Path.GetDirectoryName(appPath); | ||
|
||
var src = Path.Combine(root, "src"); | ||
var srcSomething = Path.Combine(src, "something"); | ||
var srcFoo = Path.Combine(src, "foo"); | ||
var srcFooBar = Path.Combine(srcFoo, "bar"); | ||
|
||
var fromUser = new[] { | ||
"./src", | ||
"./src/something", | ||
"./src/foo/bar", | ||
"./src/foo", | ||
}; | ||
|
||
var (interpreterPaths, userPaths) = PythonLibraryPath.ClassifyPaths(root, _fs, Array.Empty<PythonLibraryPath>(), fromUser); | ||
|
||
interpreterPaths.Should().BeEmpty(); | ||
|
||
userPaths.Should().BeEquivalentToWithStrictOrdering(new[] { | ||
new PythonLibraryPath(src, PythonLibraryPathType.Unspecified), | ||
new PythonLibraryPath(srcSomething, PythonLibraryPathType.Unspecified), | ||
new PythonLibraryPath(srcFooBar, PythonLibraryPathType.Unspecified), | ||
new PythonLibraryPath(srcFoo, PythonLibraryPathType.Unspecified), | ||
}); | ||
} | ||
|
||
[TestMethod] | ||
public void NestedUserOrdering() { | ||
var appPath = TestData.GetTestSpecificPath("app.py"); | ||
var root = Path.GetDirectoryName(appPath); | ||
|
||
var src = Path.Combine(root, "src"); | ||
var srcSomething = Path.Combine(src, "something"); | ||
var srcFoo = Path.Combine(src, "foo"); | ||
var srcFooBar = Path.Combine(srcFoo, "bar"); | ||
|
||
var fromUser = new[] { | ||
"./src/foo", | ||
"./src/foo/bar", | ||
"./src", | ||
"./src/something", | ||
}; | ||
|
||
var (interpreterPaths, userPaths) = PythonLibraryPath.ClassifyPaths(root, _fs, Array.Empty<PythonLibraryPath>(), fromUser); | ||
|
||
interpreterPaths.Should().BeEmpty(); | ||
|
||
userPaths.Should().BeEquivalentToWithStrictOrdering(new[] { | ||
new PythonLibraryPath(srcFoo, PythonLibraryPathType.Unspecified), | ||
new PythonLibraryPath(srcFooBar, PythonLibraryPathType.Unspecified), | ||
new PythonLibraryPath(src, PythonLibraryPathType.Unspecified), | ||
new PythonLibraryPath(srcSomething, PythonLibraryPathType.Unspecified), | ||
}); | ||
} | ||
|
||
[TestMethod] | ||
public void InsideStdLib() { | ||
var appPath = TestData.GetTestSpecificPath("app.py"); | ||
var root = Path.GetDirectoryName(appPath); | ||
AlexanderSher marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
var venv = Path.Combine(root, "venv"); | ||
var venvLib = Path.Combine(venv, "Lib"); | ||
var venvSitePackages = Path.Combine(venvLib, "site-packages"); | ||
var inside = Path.Combine(venvSitePackages, "inside"); | ||
var what = Path.Combine(venvSitePackages, "what"); | ||
|
||
var src = Path.Combine(root, "src"); | ||
|
||
var fromInterpreter = new[] { | ||
new PythonLibraryPath(venvLib, PythonLibraryPathType.StdLib), | ||
new PythonLibraryPath(venv, PythonLibraryPathType.StdLib), | ||
new PythonLibraryPath(venvSitePackages, PythonLibraryPathType.Site), | ||
new PythonLibraryPath(inside, PythonLibraryPathType.Pth), | ||
}; | ||
|
||
var fromUser = new[] { | ||
"./src", | ||
"./venv/Lib/site-packages/what", | ||
}; | ||
|
||
var (interpreterPaths, userPaths) = PythonLibraryPath.ClassifyPaths(root, _fs, fromInterpreter, fromUser); | ||
|
||
interpreterPaths.Should().BeEquivalentToWithStrictOrdering(new[] { | ||
new PythonLibraryPath(venvLib, PythonLibraryPathType.StdLib), | ||
new PythonLibraryPath(venv, PythonLibraryPathType.StdLib), | ||
new PythonLibraryPath(what, PythonLibraryPathType.Unspecified), | ||
new PythonLibraryPath(venvSitePackages, PythonLibraryPathType.Site), | ||
new PythonLibraryPath(inside, PythonLibraryPathType.Pth), | ||
}); | ||
|
||
userPaths.Should().BeEquivalentToWithStrictOrdering(new[] { | ||
new PythonLibraryPath(src, PythonLibraryPathType.Unspecified), | ||
}); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.