|
19 | 19 | using System.Linq;
|
20 | 20 | using System.Threading;
|
21 | 21 | using System.Threading.Tasks;
|
| 22 | +using FluentAssertions; |
| 23 | +using Microsoft.Python.LanguageServer; |
22 | 24 | using Microsoft.Python.LanguageServer.Implementation;
|
23 | 25 | using Microsoft.PythonTools.Analysis;
|
| 26 | +using Microsoft.PythonTools.Analysis.Analyzer; |
24 | 27 | using Microsoft.PythonTools.Analysis.FluentAssertions;
|
25 | 28 | using Microsoft.VisualStudio.TestTools.UnitTesting;
|
26 | 29 | using TestUtilities;
|
| 30 | +using Resources = Microsoft.PythonTools.Analysis.Resources; |
27 | 31 |
|
28 | 32 | namespace AnalysisTests {
|
29 | 33 | [TestClass]
|
@@ -251,6 +255,145 @@ import projectB.foo.baz
|
251 | 255 | completion.Should().HaveLabels("foo");
|
252 | 256 | }
|
253 | 257 |
|
| 258 | + [ServerTestMethod(LatestAvailable3X = true, TestSpecificRootUri = true), Priority(0)] |
| 259 | + [AddTestSpecificSearchPath("src")] |
| 260 | + public async Task Diagnostics_InvalidRelativeImportInUserSearchPath(Server server) { |
| 261 | + var modulePath = "src/module.py"; |
| 262 | + var moduleCode = "TEST = 0"; |
| 263 | + var appPath = "src/app.py"; |
| 264 | + var appCode = "from .module import TEST"; |
| 265 | + var args = new PublishDiagnosticsEventArgs(); |
| 266 | + server.OnPublishDiagnostics += (o, e) => args = e; |
| 267 | + |
| 268 | + await server.OpenDocumentAndGetUriAsync(modulePath, moduleCode); |
| 269 | + await server.OpenDocumentAndGetUriAsync(appPath, appCode); |
| 270 | + await server.WaitForCompleteAnalysisAsync(CancellationToken.None); |
| 271 | + |
| 272 | + args.diagnostics.Should().ContainSingle() |
| 273 | + .Which.message.Should().Be(Resources.ErrorRelativeImportNoPackage); |
| 274 | + } |
| 275 | + |
| 276 | + [ServerTestMethod(LatestAvailable3X = true, TestSpecificRootUri = true), Priority(0)] |
| 277 | + public async Task Diagnostics_RelativeImportInWorkingDirectory(Server server) { |
| 278 | + var modulePath = "module.py"; |
| 279 | + var moduleCode = "TEST = 0"; |
| 280 | + var appPath = "app.py"; |
| 281 | + var appCode = "from .module import *"; |
| 282 | + var args = new PublishDiagnosticsEventArgs(); |
| 283 | + server.OnPublishDiagnostics += (o, e) => args = e; |
| 284 | + |
| 285 | + await server.OpenDocumentAndGetUriAsync(modulePath, moduleCode); |
| 286 | + await server.OpenDocumentAndGetUriAsync(appPath, appCode); |
| 287 | + await server.WaitForCompleteAnalysisAsync(CancellationToken.None); |
| 288 | + |
| 289 | + args.diagnostics.Should().BeEmpty(); |
| 290 | + } |
| 291 | + |
| 292 | + [ServerTestMethod(LatestAvailable3X = true, TestSpecificRootUri = true), Priority(0)] |
| 293 | + [AddTestSpecificSearchPath("src")] |
| 294 | + public async Task Diagnostics_InvalidRelativeImportInWorkingDirectory(Server server) { |
| 295 | + var modulePath = "src/module.py"; |
| 296 | + var moduleCode = "TEST = 0"; |
| 297 | + var appPath = "app.py"; |
| 298 | + var appCode = "from .module import TEST"; |
| 299 | + var args = new PublishDiagnosticsEventArgs(); |
| 300 | + server.OnPublishDiagnostics += (o, e) => args = e; |
| 301 | + |
| 302 | + await server.OpenDocumentAndGetUriAsync(modulePath, moduleCode); |
| 303 | + await server.OpenDocumentAndGetUriAsync(appPath, appCode); |
| 304 | + await server.WaitForCompleteAnalysisAsync(CancellationToken.None); |
| 305 | + |
| 306 | + args.diagnostics.Should().ContainSingle() |
| 307 | + .Which.message.Should().Be(ErrorMessages.UnresolvedImport("module")); |
| 308 | + } |
| 309 | + |
| 310 | + [ServerTestMethod(LatestAvailable3X = true), Priority(0)] |
| 311 | + public async Task Diagnostics_RelativeImportInNonRooted(Server server) { |
| 312 | + var modulePath = "module.py"; |
| 313 | + var moduleCode = "TEST = 0"; |
| 314 | + var appPath = "app.py"; |
| 315 | + var appCode = "from .module import TEST"; |
| 316 | + var args = new PublishDiagnosticsEventArgs(); |
| 317 | + server.OnPublishDiagnostics += (o, e) => args = e; |
| 318 | + |
| 319 | + await server.OpenDocumentAndGetUriAsync(modulePath, moduleCode); |
| 320 | + await server.OpenDocumentAndGetUriAsync(appPath, appCode); |
| 321 | + await server.WaitForCompleteAnalysisAsync(CancellationToken.None); |
| 322 | + |
| 323 | + args.diagnostics.Should().BeEmpty(); |
| 324 | + } |
| 325 | + |
| 326 | + [ServerTestMethod(LatestAvailable3X = true, TestSpecificRootUri = true), Priority(0)] |
| 327 | + public async Task Diagnostics_RelativeImportInPackage(Server server) { |
| 328 | + var module2Path = "package/module2.py"; |
| 329 | + var module2Code = "TEST = 0"; |
| 330 | + var module1Path = "package/module1.py"; |
| 331 | + var module1Code = "from .module2 import TEST"; |
| 332 | + var initPath = "package/__init__.py"; |
| 333 | + var args = new PublishDiagnosticsEventArgs(); |
| 334 | + server.OnPublishDiagnostics += (o, e) => args = e; |
| 335 | + |
| 336 | + await server.OpenDocumentAndGetUriAsync(initPath, string.Empty); |
| 337 | + await server.OpenDocumentAndGetUriAsync(module1Path, module1Code); |
| 338 | + await server.OpenDocumentAndGetUriAsync(module2Path, module2Code); |
| 339 | + await server.WaitForCompleteAnalysisAsync(CancellationToken.None); |
| 340 | + |
| 341 | + args.diagnostics.Should().BeEmpty(); |
| 342 | + } |
| 343 | + |
| 344 | + [ServerTestMethod(LatestAvailable3X = true, TestSpecificRootUri = true), Priority(0)] |
| 345 | + public async Task Diagnostics_RelativeImportInPackage_ModuleWithNameOfFolderWithInitPy(Server server) { |
| 346 | + var initPyPath = "package/module/__init__.py"; |
| 347 | + var module1Path = "package/module.py"; |
| 348 | + var module1Code = "TEST = 1"; |
| 349 | + var module2Path = "package/module/sub/module.py"; |
| 350 | + var module2Code = "TEST = 2"; |
| 351 | + var testPath = "package/test.py"; |
| 352 | + var testCode = "from .module.sub.module import TEST"; |
| 353 | + var args = new PublishDiagnosticsEventArgs(); |
| 354 | + |
| 355 | + var testUri = await server.OpenDocumentAndGetUriAsync(testPath, testCode); |
| 356 | + server.OnPublishDiagnostics += (o, e) => { |
| 357 | + if (e.uri == testUri) { |
| 358 | + args = e; |
| 359 | + } |
| 360 | + }; |
| 361 | + |
| 362 | + await server.OpenDocumentAndGetUriAsync(initPyPath, string.Empty); |
| 363 | + await server.OpenDocumentAndGetUriAsync(module1Path, module1Code); |
| 364 | + await server.OpenDocumentAndGetUriAsync(module2Path, module2Code); |
| 365 | + |
| 366 | + await server.WaitForCompleteAnalysisAsync(CancellationToken.None); |
| 367 | + |
| 368 | + args.diagnostics.Should().BeEmpty(); |
| 369 | + } |
| 370 | + |
| 371 | + [ServerTestMethod(LatestAvailable3X = true, TestSpecificRootUri = true), Priority(0)] |
| 372 | + public async Task Diagnostics_InvalidRelativeImportInPackage_ModuleWithNameOfFolder(Server server) { |
| 373 | + var module2Path = "package/module/submodule.py"; |
| 374 | + var module2Code = "TEST = 2"; |
| 375 | + var module1Path = "package/module.py"; |
| 376 | + var module1Code = "TEST = 1"; |
| 377 | + var testPath = "package/test.py"; |
| 378 | + var testCode = "from .module.submodule import TEST"; |
| 379 | + var args = new PublishDiagnosticsEventArgs(); |
| 380 | + |
| 381 | + var testUri = await server.OpenDocumentAndGetUriAsync(testPath, testCode); |
| 382 | + server.OnPublishDiagnostics += (o, e) => { |
| 383 | + if (e.uri == testUri) { |
| 384 | + args = e; |
| 385 | + } |
| 386 | + }; |
| 387 | + |
| 388 | + await server.OpenDocumentAndGetUriAsync(module1Path, module1Code); |
| 389 | + await server.OpenDocumentAndGetUriAsync(module2Path, module2Code); |
| 390 | + |
| 391 | + await server.WaitForCompleteAnalysisAsync(CancellationToken.None); |
| 392 | + |
| 393 | + args.diagnostics.Should().ContainSingle() |
| 394 | + .Which.message.Should().Be(ErrorMessages.UnresolvedImport("package.module.submodule")); |
| 395 | + } |
| 396 | + |
254 | 397 | [ServerTestMethod(LatestAvailable3X = true), Priority(0)]
|
255 | 398 | public async Task Completions_SysModuleChain(Server server) {
|
256 | 399 | var content1 = @"import module2.mod as mod
|
|
0 commit comments