|
| 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; |
| 17 | +using System.IO; |
| 18 | +using FluentAssertions; |
| 19 | +using Microsoft.Python.Analysis.Core.Interpreter; |
| 20 | +using Microsoft.Python.Core.IO; |
| 21 | +using Microsoft.Python.Core.OS; |
| 22 | +using Microsoft.Python.Tests.Utilities.FluentAssertions; |
| 23 | +using Microsoft.VisualStudio.TestTools.UnitTesting; |
| 24 | +using TestUtilities; |
| 25 | + |
| 26 | +namespace Microsoft.Python.Analysis.Tests { |
| 27 | + [TestClass] |
| 28 | + public class PathClassificationTests { |
| 29 | + private readonly FileSystem _fs = new FileSystem(new OSPlatform()); |
| 30 | + |
| 31 | + public TestContext TestContext { get; set; } |
| 32 | + |
| 33 | + [TestInitialize] |
| 34 | + public void TestInitialize() |
| 35 | + => TestEnvironmentImpl.TestInitialize($"{TestContext.FullyQualifiedTestClassName}.{TestContext.TestName}"); |
| 36 | + |
| 37 | + [TestCleanup] |
| 38 | + public void Cleanup() => TestEnvironmentImpl.TestCleanup(); |
| 39 | + |
| 40 | + [TestMethod] |
| 41 | + public void Plain() { |
| 42 | + var appPath = TestData.GetTestSpecificPath("app.py"); |
| 43 | + var root = Path.GetDirectoryName(appPath); |
| 44 | + |
| 45 | + var venv = Path.Combine(root, "venv"); |
| 46 | + var venvLib = Path.Combine(venv, "Lib"); |
| 47 | + var venvSitePackages = Path.Combine(venvLib, "site-packages"); |
| 48 | + |
| 49 | + var fromInterpreter = new[] { |
| 50 | + new PythonLibraryPath(venvLib, PythonLibraryPathType.StdLib), |
| 51 | + new PythonLibraryPath(venv, PythonLibraryPathType.StdLib), |
| 52 | + new PythonLibraryPath(venvSitePackages, PythonLibraryPathType.Site), |
| 53 | + }; |
| 54 | + |
| 55 | + var (interpreterPaths, userPaths) = PythonLibraryPath.ClassifyPaths(root, _fs, fromInterpreter, Array.Empty<string>()); |
| 56 | + |
| 57 | + interpreterPaths.Should().BeEquivalentToWithStrictOrdering(new[] { |
| 58 | + new PythonLibraryPath(venvLib, PythonLibraryPathType.StdLib), |
| 59 | + new PythonLibraryPath(venv, PythonLibraryPathType.StdLib), |
| 60 | + new PythonLibraryPath(venvSitePackages, PythonLibraryPathType.Site), |
| 61 | + }); |
| 62 | + |
| 63 | + userPaths.Should().BeEmpty(); |
| 64 | + } |
| 65 | + |
| 66 | + [TestMethod] |
| 67 | + public void WithSrcDir() { |
| 68 | + var appPath = TestData.GetTestSpecificPath("app.py"); |
| 69 | + var root = Path.GetDirectoryName(appPath); |
| 70 | + |
| 71 | + var venv = Path.Combine(root, "venv"); |
| 72 | + var venvLib = Path.Combine(venv, "Lib"); |
| 73 | + var venvSitePackages = Path.Combine(venvLib, "site-packages"); |
| 74 | + |
| 75 | + var src = Path.Combine(root, "src"); |
| 76 | + |
| 77 | + var fromInterpreter = new[] { |
| 78 | + new PythonLibraryPath(venvLib, PythonLibraryPathType.StdLib), |
| 79 | + new PythonLibraryPath(venv, PythonLibraryPathType.StdLib), |
| 80 | + new PythonLibraryPath(venvSitePackages, PythonLibraryPathType.Site), |
| 81 | + }; |
| 82 | + |
| 83 | + var fromUser = new[] { |
| 84 | + "./src", |
| 85 | + }; |
| 86 | + |
| 87 | + var (interpreterPaths, userPaths) = PythonLibraryPath.ClassifyPaths(root, _fs, fromInterpreter, fromUser); |
| 88 | + |
| 89 | + interpreterPaths.Should().BeEquivalentToWithStrictOrdering(new[] { |
| 90 | + new PythonLibraryPath(venvLib, PythonLibraryPathType.StdLib), |
| 91 | + new PythonLibraryPath(venv, PythonLibraryPathType.StdLib), |
| 92 | + new PythonLibraryPath(venvSitePackages, PythonLibraryPathType.Site), |
| 93 | + }); |
| 94 | + |
| 95 | + userPaths.Should().BeEquivalentToWithStrictOrdering(new[] { |
| 96 | + new PythonLibraryPath(src, PythonLibraryPathType.Unspecified), |
| 97 | + }); |
| 98 | + } |
| 99 | + |
| 100 | + [TestMethod] |
| 101 | + public void NormalizeUser() { |
| 102 | + var appPath = TestData.GetTestSpecificPath("app.py"); |
| 103 | + var root = Path.GetDirectoryName(appPath); |
| 104 | + |
| 105 | + var src = Path.Combine(root, "src"); |
| 106 | + |
| 107 | + var fromUser = new[] { |
| 108 | + "./src/", |
| 109 | + }; |
| 110 | + |
| 111 | + var (interpreterPaths, userPaths) = PythonLibraryPath.ClassifyPaths(root, _fs, Array.Empty<PythonLibraryPath>(), fromUser); |
| 112 | + |
| 113 | + interpreterPaths.Should().BeEmpty(); |
| 114 | + |
| 115 | + userPaths.Should().BeEquivalentToWithStrictOrdering(new[] { |
| 116 | + new PythonLibraryPath(src, PythonLibraryPathType.Unspecified), |
| 117 | + }); |
| 118 | + } |
| 119 | + |
| 120 | + [TestMethod] |
| 121 | + public void NestedUser() { |
| 122 | + var appPath = TestData.GetTestSpecificPath("app.py"); |
| 123 | + var root = Path.GetDirectoryName(appPath); |
| 124 | + |
| 125 | + var src = Path.Combine(root, "src"); |
| 126 | + var srcSomething = Path.Combine(src, "something"); |
| 127 | + var srcFoo = Path.Combine(src, "foo"); |
| 128 | + var srcFooBar = Path.Combine(srcFoo, "bar"); |
| 129 | + |
| 130 | + var fromUser = new[] { |
| 131 | + "./src", |
| 132 | + "./src/something", |
| 133 | + "./src/foo/bar", |
| 134 | + "./src/foo", |
| 135 | + }; |
| 136 | + |
| 137 | + var (interpreterPaths, userPaths) = PythonLibraryPath.ClassifyPaths(root, _fs, Array.Empty<PythonLibraryPath>(), fromUser); |
| 138 | + |
| 139 | + interpreterPaths.Should().BeEmpty(); |
| 140 | + |
| 141 | + userPaths.Should().BeEquivalentToWithStrictOrdering(new[] { |
| 142 | + new PythonLibraryPath(src, PythonLibraryPathType.Unspecified), |
| 143 | + new PythonLibraryPath(srcSomething, PythonLibraryPathType.Unspecified), |
| 144 | + new PythonLibraryPath(srcFooBar, PythonLibraryPathType.Unspecified), |
| 145 | + new PythonLibraryPath(srcFoo, PythonLibraryPathType.Unspecified), |
| 146 | + }); |
| 147 | + } |
| 148 | + |
| 149 | + [TestMethod] |
| 150 | + public void NestedUserOrdering() { |
| 151 | + var appPath = TestData.GetTestSpecificPath("app.py"); |
| 152 | + var root = Path.GetDirectoryName(appPath); |
| 153 | + |
| 154 | + var src = Path.Combine(root, "src"); |
| 155 | + var srcSomething = Path.Combine(src, "something"); |
| 156 | + var srcFoo = Path.Combine(src, "foo"); |
| 157 | + var srcFooBar = Path.Combine(srcFoo, "bar"); |
| 158 | + |
| 159 | + var fromUser = new[] { |
| 160 | + "./src/foo", |
| 161 | + "./src/foo/bar", |
| 162 | + "./src", |
| 163 | + "./src/something", |
| 164 | + }; |
| 165 | + |
| 166 | + var (interpreterPaths, userPaths) = PythonLibraryPath.ClassifyPaths(root, _fs, Array.Empty<PythonLibraryPath>(), fromUser); |
| 167 | + |
| 168 | + interpreterPaths.Should().BeEmpty(); |
| 169 | + |
| 170 | + userPaths.Should().BeEquivalentToWithStrictOrdering(new[] { |
| 171 | + new PythonLibraryPath(srcFoo, PythonLibraryPathType.Unspecified), |
| 172 | + new PythonLibraryPath(srcFooBar, PythonLibraryPathType.Unspecified), |
| 173 | + new PythonLibraryPath(src, PythonLibraryPathType.Unspecified), |
| 174 | + new PythonLibraryPath(srcSomething, PythonLibraryPathType.Unspecified), |
| 175 | + }); |
| 176 | + } |
| 177 | + |
| 178 | + [TestMethod] |
| 179 | + public void InsideStdLib() { |
| 180 | + var appPath = TestData.GetTestSpecificPath("app.py"); |
| 181 | + var root = Path.GetDirectoryName(appPath); |
| 182 | + |
| 183 | + var venv = Path.Combine(root, "venv"); |
| 184 | + var venvLib = Path.Combine(venv, "Lib"); |
| 185 | + var venvSitePackages = Path.Combine(venvLib, "site-packages"); |
| 186 | + var inside = Path.Combine(venvSitePackages, "inside"); |
| 187 | + var what = Path.Combine(venvSitePackages, "what"); |
| 188 | + |
| 189 | + var src = Path.Combine(root, "src"); |
| 190 | + |
| 191 | + var fromInterpreter = new[] { |
| 192 | + new PythonLibraryPath(venvLib, PythonLibraryPathType.StdLib), |
| 193 | + new PythonLibraryPath(venv, PythonLibraryPathType.StdLib), |
| 194 | + new PythonLibraryPath(venvSitePackages, PythonLibraryPathType.Site), |
| 195 | + new PythonLibraryPath(inside, PythonLibraryPathType.Pth), |
| 196 | + }; |
| 197 | + |
| 198 | + var fromUser = new[] { |
| 199 | + "./src", |
| 200 | + "./venv/Lib/site-packages/what", |
| 201 | + }; |
| 202 | + |
| 203 | + var (interpreterPaths, userPaths) = PythonLibraryPath.ClassifyPaths(root, _fs, fromInterpreter, fromUser); |
| 204 | + |
| 205 | + interpreterPaths.Should().BeEquivalentToWithStrictOrdering(new[] { |
| 206 | + new PythonLibraryPath(venvLib, PythonLibraryPathType.StdLib), |
| 207 | + new PythonLibraryPath(venv, PythonLibraryPathType.StdLib), |
| 208 | + new PythonLibraryPath(what, PythonLibraryPathType.Unspecified), |
| 209 | + new PythonLibraryPath(venvSitePackages, PythonLibraryPathType.Site), |
| 210 | + new PythonLibraryPath(inside, PythonLibraryPathType.Pth), |
| 211 | + }); |
| 212 | + |
| 213 | + userPaths.Should().BeEquivalentToWithStrictOrdering(new[] { |
| 214 | + new PythonLibraryPath(src, PythonLibraryPathType.Unspecified), |
| 215 | + }); |
| 216 | + } |
| 217 | + } |
| 218 | +} |
0 commit comments