From bf63313acae0fd7902e84676921646d23d7389e8 Mon Sep 17 00:00:00 2001 From: Eleanor Boyd Date: Fri, 9 Jun 2023 17:48:31 +0000 Subject: [PATCH] Merged PR posit-dev/positron-python#122: Merging upstream vscode-python release 2023.10.0 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Merge pull request #122 from posit-dev/merge/2023.10.0 Merging upstream vscode-python release 2023.10.0 -------------------- Commit message for posit-dev/positron-python@fc0926f1150b810cd79a139c6a099fe335757d5f: Add typings to get updated EnvironmentVariableCollection API -------------------- Commit message for posit-dev/positron-python@8c11f292849e81d19d680eb4961ac613d9272cff: Merge remote-tracking branch 'upstream/release/2023.10' -------------------- Commit message for posit-dev/positron-python@fea895f127920f4ad519de309bff452caeccadcc: Update version and readme for release (posit-dev/positron-python#21388) Co-authored-by: Soojin (Min) Choi -------------------- Commit message for microsoft/vscode-python@ad9c899bea2864ba99d60997413c050f225a2d87: Update version for release candidate (microsoft/vscode-python#21369) -------------------- Commit message for microsoft/vscode-python@a395e2ec6cdc8b4b5f5577d0cc175fde451ec590: fix bug so canceling debug works in rewrite (microsoft/vscode-python#21361) fixes https://github.com/microsoft/vscode-python/issues/21336 -------------------- Commit message for microsoft/vscode-python@be829b308fd2334ca999ac41b4f39d3f7abeb2f1: Unittest for large workspaces (microsoft/vscode-python#21351) follows the same steps as making pytest compatible with large workspaces with many tests. Now test_ids are sent over a port as a json instead of in the exec function which can hit a cap on # of characters. Should fix https://github.com/microsoft/vscode-python/issues/21339. -------------------- Commit message for microsoft/vscode-python@cd76ee19a25426119c168bac3996bcd6ced5a431: add pythonTestAdapter to experiment enum (microsoft/vscode-python#21357) allow people to opt in and out of the pythonTestAdapter rewrite via the settings `python.experiment.optInto` or `python.experiment.optOutfrom` -------------------- Commit message for microsoft/vscode-python@dbd0b73b9605eb96d0da295a8ae95462f88e9915: adding extra log messages for rewrite debugging (microsoft/vscode-python#21352) These logs print errors and other bits of information which will be helpful for debugging workflows of users where we need to get information such as args or which step in the process they got to. -------------------- Commit message for microsoft/vscode-python@d968b8c472208f4635f75af649b58a9be027400d: Dont show command for button trigger in command pallet (microsoft/vscode-python#21350) Fixes https://github.com/microsoft/vscode-python/issues/21322 -------------------- Commit message for microsoft/vscode-python@e9a8dd52341a1a0f2beee7a43d0ccf44793e6a1f: remove duplicates from test_ids array (microsoft/vscode-python#21347) this will partially remediate https://github.com/microsoft/vscode-python/issues/21339 in regards to the duplicate IDs being run. -------------------- Commit message for microsoft/vscode-python@f148139f65e455fcc911cd5a3fdaba15b4bdb776: allow pytest tests to handle multiple payloads (microsoft/vscode-python#21301) As part of the switch to allow for dynamic run- the pytest discovery and execution tests are now switched to be take lists of dicts where the dicts are the payloads. -------------------- Commit message for microsoft/vscode-python@c2134917896b009ac678dac9c712aba813b55e55: Apply environment variables after shell initialization scripts are run in `pythonTerminalEnvVarActivation` experiment (microsoft/vscode-python#21290) For microsoft/vscode-python#11039 https://github.com/microsoft/vscode-python/issues/20822 Closes https://github.com/microsoft/vscode-python/issues/21297 Update proposed APIs to be used in Terminal activation experiment. -------------------- Commit message for microsoft/vscode-python@72f7ef8113536d35fa000c8a75f7aae598575990: Set up testing rewrite experiment (microsoft/vscode-python#21258) is the beginning of this issue: https://github.com/microsoft/vscode-python/issues/21150, in that it will start the process of implementing the setting in the extension -------------------- Commit message for microsoft/vscode-python@4109228af1b3d151b01aeb6f11a7b83491599682: fix debugging with new pytest run script (microsoft/vscode-python#21299) fix debugging for run_pytest_script.py setup -------------------- Commit message for microsoft/vscode-python@b916981ed6a9a6a8267477637843e6fb4b66d7f3: remove unneeded multiroot code (microsoft/vscode-python#21295) removed extra steps to wrap data since this creates duplicate folders in the controller and only keeps the most recent instead of all the roots from different workspaces. -------------------- Commit message for microsoft/vscode-python@e2a9cecf9cec0f3a42d2723927a83d486c9b5bd8: allow large scale testing (microsoft/vscode-python#21269) allows new testing rewrite to handle 500+ tests and load and run these tests. High limit tested was 10,000 tests. -------------------- Commit message for microsoft/vscode-python@f2f5fe26c41d7fff79b56b5a0490cd1c3b49f451: Check config type in the ChildProcessAttachEvents (microsoft/vscode-python#21272) -------------------- Commit message for microsoft/vscode-python@4b4e5b7f5ce637cdc626a5d473f8723cb72dccf7: Update pyright version (microsoft/vscode-python#21296) Fix error in tests, updating pyright version -------------------- Commit message for microsoft/vscode-python@c9a7268a7e6ae6f968ed595d4aaa4e0b187b7407: Revert "Remove hack to check the vscode version" (microsoft/vscode-python#21294) Reverts microsoft/vscode-python#21180 For https://github.com/microsoft/vscode-python/issues/20769 -------------------- Commit message for microsoft/vscode-python@a74f1d15b58e2a835f5564449918452ff30d62d6: Detect installed packages in the selected environment (microsoft/vscode-python#21231) Fixes https://github.com/microsoft/vscode-python/issues/21140 -------------------- Commit message for microsoft/vscode-python@b0ebc9ba50c3f89a8be713acc4fb49190f390d2f: Enable debug pytest (microsoft/vscode-python#21228) fixes https://github.com/microsoft/vscode-python/issues/21147 --------- Co-authored-by: Aidos Kanapyanov <65722512+aidoskanapyanov@users.noreply.github.com> Co-authored-by: Karthik Nadig -------------------- Commit message for microsoft/vscode-python@be9662f0316856b8e119a3441d5502d75cf5cc11: revert testing to using socket (microsoft/vscode-python#21242) switch back to using a socket instead of an output file for use in the plugin communication during testing. This should work now that we resolved the issue with python path for windows. -------------------- Commit message for microsoft/vscode-python@b4a47bbc2d27ae476db14f4f406f166c49214930: Add reload flag on fastApi provider (microsoft/vscode-python#21241) -------------------- Commit message for microsoft/vscode-python@fcfc54c43e01942d8d7bd89214a5e94c0c34d469: Add option for pyenv interpreters when creating environments with venv (microsoft/vscode-python#21219) Resolves microsoft/vscode-python#20881 . Testing: Behaves as expected when testing with Extension Development Host: ![image](https://github.com/microsoft/vscode-python/assets/30149293/d114d9ab-f2d8-4273-877b-d7dd030cfe76) -------------------- Commit message for microsoft/vscode-python@b3d43e5f7ee97d30c578593d52720b805d10e0f7: Do not open "save as" window when running existing Python files (microsoft/vscode-python#21232) Closes https://github.com/microsoft/vscode-python/issues/21209 -------------------- Commit message for microsoft/vscode-python@b0da28cd3d595728944034773a8dc68ea425e3c1: Remove IS_WINDOWS constant in favor of PlatformService (microsoft/vscode-python#21157) Solves partially microsoft/vscode-python#8542 -------------------- Commit message for microsoft/vscode-python@0c4fa40d28bcdd2848c36499be4be61a1a90a4e5: Change name of command to run Python files in separate terminals (microsoft/vscode-python#21229) Closes https://github.com/microsoft/vscode-python/issues/14094 -------------------- Commit message for microsoft/vscode-python@15338189257cdf264a9a00966c8909d6a06123e8: Added option to run multiple Python files in separate terminals (microsoft/vscode-python#21223) Closes https://github.com/microsoft/vscode-python/issues/21215 https://github.com/microsoft/vscode-python/issues/14094 Added the option to assign a dedicated terminal for each Python file: ![image](https://github.com/microsoft/vscode-python/assets/13199757/b01248e4-c826-4de0-b15f-cde959965e68) -------------------- Commit message for microsoft/vscode-python@eb9fde3100e9c78363d803b083370315bc202d58: Add `createEnvironment.contentButton` setting (microsoft/vscode-python#21212) Closes https://github.com/microsoft/vscode-python/issues/20982 --------- Co-authored-by: Luciana Abud <45497113+luabud@users.noreply.github.com> -------------------- Commit message for microsoft/vscode-python@5eef5252d660d7552f01e9233499b31c541d1358: Add logging when interpreter path changes (microsoft/vscode-python#21210) For https://github.com/microsoft/vscode-python/issues/21208 -------------------- Commit message for microsoft/vscode-python@8d291f7a5d74d8015d27f762e7c18525f853c8aa: Disable "snippets" expansion in Jedi LSP (microsoft/vscode-python#21194) This brings the Jedi based completion experience in line with that provided by Pylance. Completions now insert only the current symbol rather than assuming that the user wants to e.g: call that symbol. This means for example that completing `max` will insert just `max` rather `max(arg1, arg2)`. While for this case this may be seen as less useful, it means that insertions in places where a call is not desired (such as imports and typing contexts) will not be forced to manually remove the parentheses and template arguments which might otherwise be inserted. Users can still use the signature support UI to explore signatures and of course insertion of an opening parenthesis will still insert a closing one. Hopefully this new configuration will be preferable to a majority of users. I've done some light testing to check that this disables the described additional completion, however I'm not massively familiar with JediLSP so I'm not sure what other behaviours this will disable. Fixes https://github.com/microsoft/vscode-python/issues/15858 -------------------- Commit message for microsoft/vscode-python@a85eb3b132a752ddd9a6474f1cf62d29cd13d2da: Fix startup telemetry issue (microsoft/vscode-python#21203) Could fix https://github.com/microsoft/vscode-python/issues/20874 based on error trace. -------------------- Commit message for microsoft/vscode-python@17daae4208001c894229bbe794822e5f3d2fcb5e: Open separate Python terminals when running different Python files (microsoft/vscode-python#21202) Closes https://github.com/microsoft/vscode-python/issues/21097 Closes https://github.com/microsoft/vscode-python/issues/14094 -------------------- Commit message for microsoft/vscode-python@f0253e5662aca4d7e8265df0ccc0598f8f3787c4: Use actions from `vscode-github-triage-actions` in all Python automations (microsoft/vscode-python#21178) -------------------- Commit message for microsoft/vscode-python@678f70dbf5d57bde3fee9c6c6e88adef899a752b: Remove hack to check the vscode version (microsoft/vscode-python#21180) Closed: microsoft/vscode-python#20769 -------------------- Commit message for microsoft/vscode-python@6bdada01fe838efad8a9be6c5b43394bf10c2f15: Use `saveEditor` proposed API for running untitled Python files (microsoft/vscode-python#21183) Closes https://github.com/microsoft/vscode-python/issues/21182 Lead-authored-by: Eleanor Boyd Co-authored-by: Peter Law Co-authored-by: Carlos Piña Martinez Co-authored-by: Jonathan Rayner Co-authored-by: Pete Farland Co-authored-by: paulacamargo25 Co-authored-by: Karthik Nadig Co-authored-by: Kartik Raj Signed-off-by: GitHub --- .../.github/workflows/build.yml | 1 + .../community-feedback-auto-comment.yml | 21 +- .../.github/workflows/issue-labels.yml | 62 +--- .../.github/workflows/lock-issues.yml | 18 +- .../.github/workflows/pr-check.yml | 1 + .../.github/workflows/pr-file-check.yml | 32 +- .../.github/workflows/pr-labels.yml | 16 +- .../workflows/test-plan-item-validator.yml | 2 + .../.github/workflows/triage-info-needed.yml | 101 ++---- extensions/positron-python/README.md | 2 +- extensions/positron-python/package.json | 68 +++- extensions/positron-python/package.nls.json | 3 + .../pythonFiles/installed_check.py | 129 +++++++ .../testing_tools/process_json_util.py | 31 ++ .../tests/pytestadapter/helpers.py | 110 +++--- .../tests/pytestadapter/test_discovery.py | 44 ++- .../tests/pytestadapter/test_execution.py | 52 +-- .../tests/test_data/missing-deps.data | 121 +++++++ .../tests/test_data/no-missing-deps.data | 13 + .../test_data/pyproject-missing-deps.data | 9 + .../test_data/pyproject-no-missing-deps.data | 9 + .../pythonFiles/tests/test_installed_check.py | 90 +++++ .../tests/unittestadapter/test_execution.py | 12 +- .../pythonFiles/unittestadapter/discovery.py | 8 +- .../pythonFiles/unittestadapter/execution.py | 71 +++- .../pythonFiles/unittestadapter/utils.py | 9 +- .../pythonFiles/vscode_pytest/__init__.py | 47 ++- .../vscode_pytest/run_pytest_script.py | 68 ++++ extensions/positron-python/requirements.in | 5 + extensions/positron-python/requirements.txt | 20 +- .../client/activation/jedi/analysisOptions.ts | 2 +- .../src/client/common/application/types.ts | 10 + .../client/common/application/workspace.ts | 10 + .../src/client/common/configSettings.ts | 4 +- .../src/client/common/constants.ts | 1 + .../src/client/common/experiments/groups.ts | 4 + .../client/common/interpreterPathService.ts | 4 +- .../src/client/common/platform/constants.ts | 7 - .../client/common/platform/platformService.ts | 7 +- .../common/process/internal/scripts/index.ts | 12 + .../src/client/common/process/types.ts | 1 + .../src/client/common/serviceRegistry.ts | 4 +- .../src/client/common/terminal/factory.ts | 21 +- .../src/client/common/terminal/types.ts | 2 +- .../client/common/vscodeApis/languageApis.ts | 12 + .../client/common/vscodeApis/windowApis.ts | 4 + .../client/common/vscodeApis/workspaceApis.ts | 20 +- .../dynamicdebugConfigurationService.ts | 2 +- .../configuration/providers/fastapiLaunch.ts | 6 +- .../hooks/childProcessAttachHandler.ts | 3 +- .../src/client/extensionActivation.ts | 6 +- .../terminalEnvVarCollectionService.ts | 38 +- .../src/client/linters/pydocstyle.ts | 4 +- .../creation/common/installCheckUtils.ts | 60 ++++ .../creation/createEnvButtonContext.ts | 22 ++ .../creation/installedPackagesDiagnostic.ts | 88 +++++ .../creation/provider/venvCreationProvider.ts | 3 +- ...mlCreateEnv.ts => pyProjectTomlContext.ts} | 25 +- .../creation/registrations.ts | 21 ++ .../client/pythonEnvironments/info/index.ts | 4 +- .../client/pythonEnvironments/legacyIOC.ts | 1 + .../src/client/startupTelemetry.ts | 4 +- .../src/client/telemetry/index.ts | 6 + .../codeExecution/codeExecutionManager.ts | 56 +-- .../client/terminals/codeExecution/helper.ts | 12 +- .../codeExecution/terminalCodeExecution.ts | 34 +- .../src/client/terminals/types.ts | 2 +- .../client/testing/common/debugLauncher.ts | 60 +++- .../src/client/testing/common/types.ts | 5 +- .../testing/testController/common/server.ts | 75 ++-- .../testing/testController/common/types.ts | 5 +- .../testing/testController/common/utils.ts | 47 +++ .../testing/testController/controller.ts | 45 ++- .../testController/pytest/pytestController.ts | 31 +- .../pytest/pytestDiscoveryAdapter.ts | 13 +- .../pytest/pytestExecutionAdapter.ts | 92 ++++- .../unittest/testDiscoveryAdapter.ts | 2 + .../unittest/testExecutionAdapter.ts | 48 ++- .../unittest/unittestController.ts | 33 +- .../testController/workspaceTestAdapter.ts | 60 +--- .../jedi/jediAnalysisOptions.unit.test.ts | 2 +- .../src/test/common/configSettings.test.ts | 4 +- .../common/terminals/factory.unit.test.ts | 47 ++- .../providers/fastapiLaunch.unit.test.ts | 4 +- .../childProcessAttachHandler.unit.test.ts | 21 +- ...rminalEnvVarCollectionService.unit.test.ts | 66 ++-- .../common/installCheckUtils.unit.test.ts | 65 ++++ .../createEnvButtonContext.unit.test.ts | 105 ++++++ .../installedPackagesDiagnostics.unit.test.ts | 333 ++++++++++++++++++ ...t.ts => pyProjectTomlContext.unit.test.ts} | 112 ++++-- .../src/test/serviceRegistry.ts | 5 +- .../codeExecutionManager.unit.test.ts | 25 +- .../terminals/codeExecution/helper.test.ts | 23 +- .../testing/common/debugLauncher.unit.test.ts | 29 +- .../testExecutionAdapter.unit.test.ts | 236 ++++++------- extensions/positron-python/tsconfig.json | 1 + .../vscode.proposed.envCollectionOptions.d.ts | 56 +++ ...scode.proposed.envCollectionWorkspace.d.ts | 44 ++- .../vscode.proposed.saveEditor.d.ts | 34 ++ 99 files changed, 2579 insertions(+), 846 deletions(-) create mode 100644 extensions/positron-python/pythonFiles/installed_check.py create mode 100644 extensions/positron-python/pythonFiles/testing_tools/process_json_util.py create mode 100644 extensions/positron-python/pythonFiles/tests/test_data/missing-deps.data create mode 100644 extensions/positron-python/pythonFiles/tests/test_data/no-missing-deps.data create mode 100644 extensions/positron-python/pythonFiles/tests/test_data/pyproject-missing-deps.data create mode 100644 extensions/positron-python/pythonFiles/tests/test_data/pyproject-no-missing-deps.data create mode 100644 extensions/positron-python/pythonFiles/tests/test_installed_check.py create mode 100644 extensions/positron-python/pythonFiles/vscode_pytest/run_pytest_script.py delete mode 100644 extensions/positron-python/src/client/common/platform/constants.ts create mode 100644 extensions/positron-python/src/client/common/vscodeApis/languageApis.ts create mode 100644 extensions/positron-python/src/client/pythonEnvironments/creation/common/installCheckUtils.ts create mode 100644 extensions/positron-python/src/client/pythonEnvironments/creation/createEnvButtonContext.ts create mode 100644 extensions/positron-python/src/client/pythonEnvironments/creation/installedPackagesDiagnostic.ts rename extensions/positron-python/src/client/pythonEnvironments/creation/{pyprojectTomlCreateEnv.ts => pyProjectTomlContext.ts} (58%) create mode 100644 extensions/positron-python/src/client/pythonEnvironments/creation/registrations.ts create mode 100644 extensions/positron-python/src/test/pythonEnvironments/creation/common/installCheckUtils.unit.test.ts create mode 100644 extensions/positron-python/src/test/pythonEnvironments/creation/createEnvButtonContext.unit.test.ts create mode 100644 extensions/positron-python/src/test/pythonEnvironments/creation/installedPackagesDiagnostics.unit.test.ts rename extensions/positron-python/src/test/pythonEnvironments/creation/{pyprojectTomlCreateEnv.unit.test.ts => pyProjectTomlContext.unit.test.ts} (61%) create mode 100644 extensions/positron-python/types/src/vscode-dts/vscode.proposed.envCollectionOptions.d.ts create mode 100644 extensions/positron-python/typings/vscode-proposed/vscode.proposed.saveEditor.d.ts diff --git a/extensions/positron-python/.github/workflows/build.yml b/extensions/positron-python/.github/workflows/build.yml index 805077ffdb46..f418a802ff8e 100644 --- a/extensions/positron-python/.github/workflows/build.yml +++ b/extensions/positron-python/.github/workflows/build.yml @@ -103,6 +103,7 @@ jobs: - name: Run Pyright uses: jakebailey/pyright-action@v1 with: + version: 1.1.308 working-directory: 'pythonFiles' ### Non-smoke tests diff --git a/extensions/positron-python/.github/workflows/community-feedback-auto-comment.yml b/extensions/positron-python/.github/workflows/community-feedback-auto-comment.yml index cbba6db30b50..57bbd97bf430 100644 --- a/extensions/positron-python/.github/workflows/community-feedback-auto-comment.yml +++ b/extensions/positron-python/.github/workflows/community-feedback-auto-comment.yml @@ -13,18 +13,17 @@ jobs: issues: write steps: - - name: Check For Existing Comment - uses: peter-evans/find-comment@v2 - id: finder + - name: Checkout Actions + uses: actions/checkout@v3 with: - issue-number: ${{ github.event.issue.number }} - comment-author: 'github-actions[bot]' - body-includes: Thanks for the feature request! We are going to give the community + repository: 'microsoft/vscode-github-triage-actions' + ref: stable + path: ./actions + + - name: Install Actions + run: npm install --production --prefix ./actions - - name: Add Community Feedback Comment - if: steps.finder.outputs.comment-id == '' - uses: peter-evans/create-or-update-comment@v3 + - name: Add Community Feedback Comment if applicable + uses: ./actions/python-community-feedback-auto-comment with: issue-number: ${{ github.event.issue.number }} - body: | - Thanks for the feature request! We are going to give the community 60 days from when this issue was created to provide 7 👍 upvotes on the opening comment to gauge general interest in this idea. If there's enough upvotes then we will consider this feature request in our future planning. If there's unfortunately not enough upvotes then we will close this issue. diff --git a/extensions/positron-python/.github/workflows/issue-labels.yml b/extensions/positron-python/.github/workflows/issue-labels.yml index 520592be2fce..98ac4eaca81d 100644 --- a/extensions/positron-python/.github/workflows/issue-labels.yml +++ b/extensions/positron-python/.github/workflows/issue-labels.yml @@ -18,55 +18,19 @@ jobs: name: "Add 'triage-needed' and remove unrecognizable labels & assignees" runs-on: ubuntu-latest steps: - - uses: actions/github-script@v6 + - name: Checkout Actions + uses: actions/checkout@v3 with: - github-token: ${{ secrets.GITHUB_TOKEN }} - script: | - const result = await github.rest.issues.listLabelsOnIssue({ - owner: context.repo.owner, - repo: context.repo.repo, - issue_number: context.issue.number, - }) - const labels = result.data.map((label) => label.name) - const hasNeedsOrTPI = labels.some((label) => (label.startsWith('needs') || label === 'testplan-item' || label.startsWith('iteration-plan') || label === 'release-plan')) + repository: 'microsoft/vscode-github-triage-actions' + ref: stable + path: ./actions - if (!hasNeedsOrTPI) { - console.log('This issue is not labeled with a "needs __", "iteration-plan", "release-plan", or the "testplan-item" label; add the "triage-needed" label.') + - name: Install Actions + run: npm install --production --prefix ./actions - github.rest.issues.addLabels({ - owner: context.repo.owner, - repo: context.repo.repo, - issue_number: context.issue.number, - labels: ['triage-needed'] - }) - } else { - console.log('This issue already has a "needs __", "iteration-plan", "release-plan", or the "testplan-item" label, do not add the "triage-needed" label.') - } - const knownTriagers = ${{ env.TRIAGERS }} - const currentAssignees = await github.rest.issues - .get({ - owner: context.repo.owner, - repo: context.repo.repo, - issue_number: context.issue.number, - }) - .then((result) => result.data.assignees.map((a) => a.login)); - console.log('Known triagers:', JSON.stringify(knownTriagers)); - console.log('Current assignees:', JSON.stringify(currentAssignees)); - const assigneesToRemove = currentAssignees.filter(a => !knownTriagers.includes(a)); - console.log('Assignees to remove:', JSON.stringify(assigneesToRemove)); - github.rest.issues.removeAssignees({ - owner: context.repo.owner, - repo: context.repo.repo, - issue_number: context.issue.number, - assignees: assigneesToRemove, - }); - const knownLabels = ${{ env.REPO_LABELS }} - for( const label of labels) { - if (!knownLabels.includes(label)) { - await github.rest.issues.deleteLabel({ - owner: context.repo.owner, - repo: context.repo.repo, - name: label, - }) - } - } + - name: "Add 'triage-needed' and remove unrecognizable labels & assignees" + uses: ./actions/python-issue-labels + with: + triagers: ${{ env.TRIAGERS }} + token: ${{secrets.GITHUB_TOKEN}} + repo_labels: ${{ env.REPO_LABELS }} diff --git a/extensions/positron-python/.github/workflows/lock-issues.yml b/extensions/positron-python/.github/workflows/lock-issues.yml index 6417d415fcfe..8c828ff766cb 100644 --- a/extensions/positron-python/.github/workflows/lock-issues.yml +++ b/extensions/positron-python/.github/workflows/lock-issues.yml @@ -15,9 +15,17 @@ jobs: lock-issues: runs-on: ubuntu-latest steps: - - uses: dessant/lock-threads@v4 + - name: Checkout Actions + uses: actions/checkout@v3 with: - github-token: ${{ github.token }} - issue-inactive-days: '30' - process-only: 'issues' - log-output: true + repository: 'microsoft/vscode-github-triage-actions' + ref: stable + path: ./actions + + - name: Install Actions + run: npm install --production --prefix ./actions + + - name: 'Lock Issues' + uses: ./actions/python-lock-issues + with: + token: ${{ github.token }} diff --git a/extensions/positron-python/.github/workflows/pr-check.yml b/extensions/positron-python/.github/workflows/pr-check.yml index 2ac560af995d..84903463e204 100644 --- a/extensions/positron-python/.github/workflows/pr-check.yml +++ b/extensions/positron-python/.github/workflows/pr-check.yml @@ -77,6 +77,7 @@ jobs: - name: Run Pyright uses: jakebailey/pyright-action@v1 with: + version: 1.1.308 working-directory: 'pythonFiles' ### Non-smoke tests diff --git a/extensions/positron-python/.github/workflows/pr-file-check.yml b/extensions/positron-python/.github/workflows/pr-file-check.yml index 3eb7ad7b6338..258e07daace7 100644 --- a/extensions/positron-python/.github/workflows/pr-file-check.yml +++ b/extensions/positron-python/.github/workflows/pr-file-check.yml @@ -15,29 +15,15 @@ jobs: name: 'Check for changed files' runs-on: ubuntu-latest steps: - - name: 'package-lock.json matches package.json' - uses: brettcannon/check-for-changed-files@v1.1.0 + - name: Checkout Actions + uses: actions/checkout@v3 with: - prereq-pattern: 'package.json' - file-pattern: 'package-lock.json' - skip-label: 'skip package*.json' - failure-message: '${prereq-pattern} was edited but ${file-pattern} was not (the ${skip-label} label can be used to pass this check)' + repository: 'microsoft/vscode-github-triage-actions' + ref: stable + path: ./actions - - name: 'package.json matches package-lock.json' - uses: brettcannon/check-for-changed-files@v1.1.0 - with: - prereq-pattern: 'package-lock.json' - file-pattern: 'package.json' - skip-label: 'skip package*.json' - failure-message: '${prereq-pattern} was edited but ${file-pattern} was not (the ${skip-label} label can be used to pass this check)' + - name: Install Actions + run: npm install --production --prefix ./actions - - name: 'Tests' - uses: brettcannon/check-for-changed-files@v1.1.0 - with: - prereq-pattern: src/**/*.ts - file-pattern: | - src/**/*.test.ts - src/**/*.testvirtualenvs.ts - .github/test_plan.md - skip-label: 'skip tests' - failure-message: 'TypeScript code was edited without also editing a ${file-pattern} file; see the Testing page in our wiki on testing guidelines (the ${skip-label} label can be used to pass this check)' + - name: Check for changed files + uses: ./actions/python-pr-file-check diff --git a/extensions/positron-python/.github/workflows/pr-labels.yml b/extensions/positron-python/.github/workflows/pr-labels.yml index e953f62d2011..7ddb781e2a85 100644 --- a/extensions/positron-python/.github/workflows/pr-labels.yml +++ b/extensions/positron-python/.github/workflows/pr-labels.yml @@ -13,9 +13,15 @@ jobs: name: 'Classify PR' runs-on: ubuntu-latest steps: - - name: 'PR impact specified' - uses: mheap/github-action-required-labels@v4 + - name: Checkout Actions + uses: actions/checkout@v3 with: - mode: exactly - count: 1 - labels: 'bug, debt, feature-request, no-changelog' + repository: 'microsoft/vscode-github-triage-actions' + ref: stable + path: ./actions + + - name: Install Actions + run: npm install --production --prefix ./actions + + - name: Classify PR + uses: ./actions/python-pr-labels diff --git a/extensions/positron-python/.github/workflows/test-plan-item-validator.yml b/extensions/positron-python/.github/workflows/test-plan-item-validator.yml index 43713049f5e8..9d0805a9db9b 100644 --- a/extensions/positron-python/.github/workflows/test-plan-item-validator.yml +++ b/extensions/positron-python/.github/workflows/test-plan-item-validator.yml @@ -17,8 +17,10 @@ jobs: repository: 'microsoft/vscode-github-triage-actions' path: ./actions ref: stable + - name: Install Actions run: npm install --production --prefix ./actions + - name: Run Test Plan Item Validator uses: ./actions/test-plan-item-validator with: diff --git a/extensions/positron-python/.github/workflows/triage-info-needed.yml b/extensions/positron-python/.github/workflows/triage-info-needed.yml index 51c5b610f03f..1c384d824da5 100644 --- a/extensions/positron-python/.github/workflows/triage-info-needed.yml +++ b/extensions/positron-python/.github/workflows/triage-info-needed.yml @@ -11,86 +11,41 @@ jobs: add_label: runs-on: ubuntu-latest if: contains(github.event.issue.labels.*.name, 'triage-needed') && !contains(github.event.issue.labels.*.name, 'info-needed') - env: - KEYWORDS: '["\\?", "please", "kindly", "let me know", "try", "can you", "could you", "would you", "may I", "provide", "let us know", "tell me", "give me", "send me", "what", "when", "where", "why", "how"]' steps: - - name: Check for author - uses: actions/github-script@v6 + - name: Checkout Actions + uses: actions/checkout@v3 with: - github-token: ${{ secrets.GITHUB_TOKEN }} - script: | - const issue = await github.rest.issues.get({ - owner: context.repo.owner, - repo: context.repo.repo, - issue_number: context.issue.number - }); - const commentAuthor = context.payload.comment.user.login; - const commentBody = context.payload.comment.body; - const isTeamMember = ${{ env.TRIAGERS }}.includes(commentAuthor); + repository: 'microsoft/vscode-github-triage-actions' + ref: stable + path: ./actions - const keywords = ${{ env.KEYWORDS }}; - const isRequestForInfo = new RegExp(keywords.join('|'), 'i').test(commentBody); + - name: Install Actions + run: npm install --production --prefix ./actions - const shouldAddLabel = isTeamMember && commentAuthor !== issue.data.user.login && isRequestForInfo; - - if (shouldAddLabel) { - await github.rest.issues.addLabels({ - owner: context.repo.owner, - repo: context.repo.repo, - issue_number: context.issue.number, - labels: ['info-needed'] - }); - } + - name: Add "info-needed" label + uses: ./actions/python-triage-info-needed + with: + triagers: ${{ env.TRIAGERS }} + action: 'add' + token: ${{secrets.GITHUB_TOKEN}} remove_label: if: contains(github.event.issue.labels.*.name, 'info-needed') && contains(github.event.issue.labels.*.name, 'triage-needed') runs-on: ubuntu-latest steps: - - name: Check for author - uses: actions/github-script@v6 + - name: Checkout Actions + uses: actions/checkout@v3 + with: + repository: 'microsoft/vscode-github-triage-actions' + ref: stable + path: ./actions + + - name: Install Actions + run: npm install --production --prefix ./actions + + - name: Remove "info-needed" label + uses: ./actions/python-triage-info-needed with: - github-token: ${{ secrets.GITHUB_TOKEN }} - script: | - const issue = await github.rest.issues.get({ - owner: context.repo.owner, - repo: context.repo.repo, - issue_number: context.issue.number - }); - const commentAuthor = context.payload.comment.user.login; - const issueAuthor = issue.data.user.login; - if (commentAuthor === issueAuthor) { - await github.rest.issues.removeLabel({ - owner: context.repo.owner, - repo: context.repo.repo, - issue_number: context.issue.number, - name: 'info-needed' - }); - return; - } - if (${{ env.TRIAGERS }}.includes(commentAuthor)) { - // If one of triagers made a comment, ignore it - return; - } - // Loop through all the comments on the issue in reverse order and find the last username that a TRIAGER mentioned - // If the comment author is the last mentioned username, remove the "info-needed" label - const comments = await github.rest.issues.listComments({ - owner: context.repo.owner, - repo: context.repo.repo, - issue_number: context.issue.number - }); - for (const comment of comments.data.slice().reverse()) { - if (!${{ env.TRIAGERS }}.includes(comment.user.login)) { - continue; - } - const matches = comment.body.match(/@\w+/g) || []; - const mentionedUsernames = matches.map(match => match.replace('@', '')); - if (mentionedUsernames.includes(commentAuthor)) { - await github.rest.issues.removeLabel({ - owner: context.repo.owner, - repo: context.repo.repo, - issue_number: context.issue.number, - name: 'info-needed' - }); - break; - } - } + triagers: ${{ env.TRIAGERS }} + action: 'remove' + token: ${{secrets.GITHUB_TOKEN}} diff --git a/extensions/positron-python/README.md b/extensions/positron-python/README.md index d27ec8e762f5..8a5df6720717 100644 --- a/extensions/positron-python/README.md +++ b/extensions/positron-python/README.md @@ -9,7 +9,7 @@ The Python extension does offer [some support](https://github.com/microsoft/vsco ## Installed extensions -The Python extension will automatically install the [Pylance](https://marketplace.visualstudio.com/items?itemName=ms-python.vscode-pylance) and [Jupyter](https://marketplace.visualstudio.com/items?itemName=ms-toolsai.jupyter) extensions to give you the best experience when working with Python files and Jupyter notebooks. However, Pylance is an optional dependency, meaning the Python extension will remain fully functional if it fails to be installed. You can also [uninstall](https://code.visualstudio.com/docs/editor/extension-marketplace#_uninstall-an-extension) it at the expense of some features if you’re using a different language server. +The Python extension will automatically install the [Pylance](https://marketplace.visualstudio.com/items?itemName=ms-python.vscode-pylance) extension to give you the best experience when working with Python files. However, Pylance is an optional dependency, meaning the Python extension will remain fully functional if it fails to be installed. You can also [uninstall](https://code.visualstudio.com/docs/editor/extension-marketplace#_uninstall-an-extension) it at the expense of some features if you’re using a different language server. Extensions installed through the marketplace are subject to the [Marketplace Terms of Use](https://cdn.vsassets.io/v/M146_20190123.39/_content/Microsoft-Visual-Studio-Marketplace-Terms-of-Use.pdf). diff --git a/extensions/positron-python/package.json b/extensions/positron-python/package.json index 6a0d68094e23..89c3e579a21a 100644 --- a/extensions/positron-python/package.json +++ b/extensions/positron-python/package.json @@ -23,7 +23,9 @@ "envShellEvent", "testObserver", "quickPickItemTooltip", - "envCollectionWorkspace" + "envCollectionWorkspace", + "saveEditor", + "envCollectionOptions" ], "author": { "name": "Microsoft Corporation" @@ -44,7 +46,7 @@ "theme": "dark" }, "engines": { - "vscode": "^1.78.0-20230421" + "vscode": "^1.79.0-20230526" }, "keywords": [ "python", @@ -304,6 +306,12 @@ "icon": "$(play)", "title": "%python.command.python.execInTerminalIcon.title%" }, + { + "category": "Python", + "command": "python.execInDedicatedTerminal", + "icon": "$(play)", + "title": "%python.command.python.execInDedicatedTerminal.title%" + }, { "category": "Python", "command": "python.debugInTerminal", @@ -404,6 +412,19 @@ "type": "array", "uniqueItems": true }, + "python.createEnvironment.contentButton": { + "default": "show", + "markdownDescription": "%python.createEnvironment.contentButton.description%", + "scope": "machine-overridable", + "type": "string", + "enum": [ + "show", + "hide" + ], + "tags": [ + "experimental" + ] + }, "python.condaPath": { "default": "", "description": "%python.condaPath.description%", @@ -442,13 +463,15 @@ "All", "pythonSurveyNotification", "pythonPromptNewToolsExt", - "pythonTerminalEnvVarActivation" + "pythonTerminalEnvVarActivation", + "pythonTestAdapter" ], "enumDescriptions": [ "%python.experiments.All.description%", "%python.experiments.pythonSurveyNotification.description%", "%python.experiments.pythonPromptNewToolsExt.description%", - "%python.experiments.pythonTerminalEnvVarActivation.description%" + "%python.experiments.pythonTerminalEnvVarActivation.description%", + "%python.experiments.pythonTestAdapter.description%" ] }, "scope": "machine", @@ -463,13 +486,15 @@ "All", "pythonSurveyNotification", "pythonPromptNewToolsExt", - "pythonTerminalEnvVarActivation" + "pythonTerminalEnvVarActivation", + "pythonTestAdapter" ], "enumDescriptions": [ "%python.experiments.All.description%", "%python.experiments.pythonSurveyNotification.description%", "%python.experiments.pythonPromptNewToolsExt.description%", - "%python.experiments.pythonTerminalEnvVarActivation.description%" + "%python.experiments.pythonTerminalEnvVarActivation.description%", + "%python.experiments.pythonTestAdapter.description%" ] }, "scope": "machine", @@ -1527,10 +1552,8 @@ ], "configuration": "./languages/pip-requirements.json", "filenamePatterns": [ - "**/*-requirements.{txt, in}", - "**/*-constraints.txt", - "**/requirements-*.{txt, in}", - "**/constraints-*.txt", + "**/*requirements*.{txt, in}", + "**/*constraints*.txt", "**/requirements/*.{txt,in}", "**/constraints/*.txt" ], @@ -1602,6 +1625,12 @@ "title": "%python.command.python.createEnvironment.title%", "when": "!virtualWorkspace && shellExecutionSupported" }, + { + "category": "Python", + "command": "python.createEnvironment-button", + "title": "%python.command.python.createEnvironment.title%", + "when": "false" + }, { "category": "Python", "command": "python.createTerminal", @@ -1631,7 +1660,14 @@ "command": "python.execInTerminal-icon", "icon": "$(play)", "title": "%python.command.python.execInTerminalIcon.title%", - "when": "false && editorLangId == python" + "when": "false" + }, + { + "category": "Python", + "command": "python.execInDedicatedTerminal", + "icon": "$(play)", + "title": "%python.command.python.execInDedicatedTerminal.title%", + "when": "false" }, { "category": "Python", @@ -1727,12 +1763,12 @@ { "group": "Python", "command": "python.createEnvironment-button", - "when": "resourceLangId == pip-requirements && !virtualWorkspace && shellExecutionSupported && !inDiffEditor" + "when": "showCreateEnvButton && resourceLangId == pip-requirements && !virtualWorkspace && shellExecutionSupported && !inDiffEditor && pythonDepsNotInstalled" }, { "group": "Python", "command": "python.createEnvironment-button", - "when": "resourceFilename == pyproject.toml && pipInstallableToml && !virtualWorkspace && shellExecutionSupported && !inDiffEditor" + "when": "showCreateEnvButton && resourceFilename == pyproject.toml && pipInstallableToml && !virtualWorkspace && shellExecutionSupported && !inDiffEditor && pythonDepsNotInstalled" } ], "editor/context": [ @@ -1791,6 +1827,12 @@ "title": "%python.command.python.execInTerminalIcon.title%", "when": "resourceLangId == python && !isInDiffEditor && !virtualWorkspace && shellExecutionSupported" }, + { + "command": "python.execInDedicatedTerminal", + "group": "navigation@0", + "title": "%python.command.python.execInDedicatedTerminal.title%", + "when": "resourceLangId == python && !isInDiffEditor && !virtualWorkspace && shellExecutionSupported" + }, { "command": "python.debugInTerminal", "group": "navigation@1", diff --git a/extensions/positron-python/package.nls.json b/extensions/positron-python/package.nls.json index df7506feba19..525fdd36e279 100644 --- a/extensions/positron-python/package.nls.json +++ b/extensions/positron-python/package.nls.json @@ -9,6 +9,7 @@ "python.command.python.execInTerminal.title": "Run Python File in Terminal", "python.command.python.debugInTerminal.title": "Debug Python File", "python.command.python.execInTerminalIcon.title": "Run Python File", + "python.command.python.execInDedicatedTerminal.title": "Run Python File in Dedicated Terminal", "python.command.python.setInterpreter.title": "Select Interpreter", "python.command.python.clearWorkspaceInterpreter.title": "Clear Workspace Interpreter Setting", "python.command.python.viewOutput.title": "Show Output", @@ -27,6 +28,7 @@ "python.command.python.analysis.restartLanguageServer.title": "Restart Language Server", "python.command.python.launchTensorBoard.title": "Launch TensorBoard", "python.command.python.refreshTensorBoard.title": "Refresh TensorBoard", + "python.createEnvironment.contentButton.description": "Show or hide Create Environment button in the editor for `requirements.txt` or other dependency files.", "python.menu.createNewFile.title": "Python File", "python.editor.context.submenu.runPython": "Run Python", "python.editor.context.submenu.runPythonInteractive": "Run in Interactive window", @@ -43,6 +45,7 @@ "python.experiments.pythonSurveyNotification.description": "Denotes the Python Survey Notification experiment.", "python.experiments.pythonPromptNewToolsExt.description": "Denotes the Python Prompt New Tools Extension experiment.", "python.experiments.pythonTerminalEnvVarActivation.description": "Enables use of environment variables to activate terminals instead of sending activation commands.", + "python.experiments.pythonTestAdapter.description": "Denotes the Python Test Adapter experiment.", "python.formatting.autopep8Args.description": "Arguments passed in. Each argument is a separate item in the array.", "python.formatting.autopep8Path.description": "Path to autopep8, you can use a custom version of autopep8 by modifying this setting to include the full path.", "python.formatting.blackArgs.description": "Arguments passed in. Each argument is a separate item in the array.", diff --git a/extensions/positron-python/pythonFiles/installed_check.py b/extensions/positron-python/pythonFiles/installed_check.py new file mode 100644 index 000000000000..f0e1c268d270 --- /dev/null +++ b/extensions/positron-python/pythonFiles/installed_check.py @@ -0,0 +1,129 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +import argparse +import json +import os +import pathlib +import sys +from typing import Dict, List, Optional, Sequence, Tuple, Union + +LIB_ROOT = pathlib.Path(__file__).parent / "lib" / "python" +sys.path.insert(0, os.fspath(LIB_ROOT)) + +import tomli +from importlib_metadata import metadata +from packaging.requirements import Requirement + +DEFAULT_SEVERITY = 3 + + +def parse_args(argv: Optional[Sequence[str]] = None): + if argv is None: + argv = sys.argv[1:] + parser = argparse.ArgumentParser( + description="Check for installed packages against requirements" + ) + parser.add_argument("FILEPATH", type=str, help="Path to requirements.[txt, in]") + + return parser.parse_args(argv) + + +def parse_requirements(line: str) -> Optional[Requirement]: + try: + req = Requirement(line.strip("\\")) + if req.marker is None: + return req + elif req.marker.evaluate(): + return req + except: + return None + + +def process_requirements(req_file: pathlib.Path) -> List[Dict[str, Union[str, int]]]: + diagnostics = [] + for n, line in enumerate(req_file.read_text(encoding="utf-8").splitlines()): + if line.startswith(("#", "-", " ")) or line == "": + continue + + req = parse_requirements(line) + if req: + try: + # Check if package is installed + metadata(req.name) + except: + diagnostics.append( + { + "line": n, + "character": 0, + "endLine": n, + "endCharacter": len(req.name), + "package": req.name, + "code": "not-installed", + "severity": DEFAULT_SEVERITY, + } + ) + return diagnostics + + +def get_pos(lines: List[str], text: str) -> Tuple[int, int, int, int]: + for n, line in enumerate(lines): + index = line.find(text) + if index >= 0: + return n, index, n, index + len(text) + return (0, 0, 0, 0) + + +def process_pyproject(req_file: pathlib.Path) -> List[Dict[str, Union[str, int]]]: + diagnostics = [] + try: + raw_text = req_file.read_text(encoding="utf-8") + pyproject = tomli.loads(raw_text) + except: + return diagnostics + + lines = raw_text.splitlines() + reqs = pyproject.get("project", {}).get("dependencies", []) + for raw_req in reqs: + req = parse_requirements(raw_req) + n, start, _, end = get_pos(lines, raw_req) + if req: + try: + # Check if package is installed + metadata(req.name) + except: + diagnostics.append( + { + "line": n, + "character": start, + "endLine": n, + "endCharacter": end, + "package": req.name, + "code": "not-installed", + "severity": DEFAULT_SEVERITY, + } + ) + return diagnostics + + +def get_diagnostics(req_file: pathlib.Path) -> List[Dict[str, Union[str, int]]]: + diagnostics = [] + if not req_file.exists(): + return diagnostics + + if req_file.name == "pyproject.toml": + diagnostics = process_pyproject(req_file) + else: + diagnostics = process_requirements(req_file) + + return diagnostics + + +def main(): + args = parse_args() + diagnostics = get_diagnostics(pathlib.Path(args.FILEPATH)) + print(json.dumps(diagnostics, ensure_ascii=False)) + + +if __name__ == "__main__": + main() diff --git a/extensions/positron-python/pythonFiles/testing_tools/process_json_util.py b/extensions/positron-python/pythonFiles/testing_tools/process_json_util.py new file mode 100644 index 000000000000..f116b0d9a8f3 --- /dev/null +++ b/extensions/positron-python/pythonFiles/testing_tools/process_json_util.py @@ -0,0 +1,31 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. +import io +import json +from typing import List + +CONTENT_LENGTH: str = "Content-Length:" + + +def process_rpc_json(data: str) -> List[str]: + """Process the JSON data which comes from the server.""" + str_stream: io.StringIO = io.StringIO(data) + + length: int = 0 + + while True: + line: str = str_stream.readline() + if CONTENT_LENGTH.lower() in line.lower(): + length = int(line[len(CONTENT_LENGTH) :]) + break + + if not line or line.isspace(): + raise ValueError("Header does not contain Content-Length") + + while True: + line: str = str_stream.readline() + if not line or line.isspace(): + break + + raw_json: str = str_stream.read(length) + return json.loads(raw_json) diff --git a/extensions/positron-python/pythonFiles/tests/pytestadapter/helpers.py b/extensions/positron-python/pythonFiles/tests/pytestadapter/helpers.py index b078439f6eac..013e4bb31fca 100644 --- a/extensions/positron-python/pythonFiles/tests/pytestadapter/helpers.py +++ b/extensions/positron-python/pythonFiles/tests/pytestadapter/helpers.py @@ -1,36 +1,20 @@ # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. - -import contextlib import io import json import os import pathlib -import random import socket import subprocess import sys +import threading import uuid -from typing import Any, Dict, List, Optional, Union +from typing import Any, Dict, List, Optional, Tuple TEST_DATA_PATH = pathlib.Path(__file__).parent / ".data" from typing_extensions import TypedDict -@contextlib.contextmanager -def test_output_file(root: pathlib.Path, ext: str = ".txt"): - """Creates a temporary python file with a random name.""" - basename = ( - "".join(random.choice("abcdefghijklmnopqrstuvwxyz") for _ in range(9)) + ext - ) - fullpath = root / basename - try: - fullpath.write_text("", encoding="utf-8") - yield fullpath - finally: - os.unlink(str(fullpath)) - - def create_server( host: str = "127.0.0.1", port: int = 0, @@ -83,31 +67,29 @@ def _new_sock() -> socket.socket: ) -def process_rpc_json(data: str) -> Dict[str, Any]: +def process_rpc_message(data: str) -> Tuple[Dict[str, Any], str]: """Process the JSON data which comes from the server which runs the pytest discovery.""" str_stream: io.StringIO = io.StringIO(data) length: int = 0 - while True: line: str = str_stream.readline() if CONTENT_LENGTH.lower() in line.lower(): length = int(line[len(CONTENT_LENGTH) :]) break - if not line or line.isspace(): raise ValueError("Header does not contain Content-Length") - while True: line: str = str_stream.readline() if not line or line.isspace(): break raw_json: str = str_stream.read(length) - return json.loads(raw_json) + dict_json: Dict[str, Any] = json.loads(raw_json) + return dict_json, str_stream.read() -def runner(args: List[str]) -> Optional[Dict[str, Any]]: +def runner(args: List[str]) -> Optional[List[Dict[str, Any]]]: """Run the pytest discovery and return the JSON data from the server.""" process_args: List[str] = [ sys.executable, @@ -116,31 +98,63 @@ def runner(args: List[str]) -> Optional[Dict[str, Any]]: "-p", "vscode_pytest", ] + args + listener: socket.socket = create_server() + _, port = listener.getsockname() + listener.listen() + + env = os.environ.copy() + env.update( + { + "TEST_UUID": str(uuid.uuid4()), + "TEST_PORT": str(port), + "PYTHONPATH": os.fspath(pathlib.Path(__file__).parent.parent.parent), + } + ) + + result: list = [] + t1: threading.Thread = threading.Thread( + target=_listen_on_socket, args=(listener, result) + ) + t1.start() + + t2 = threading.Thread( + target=lambda proc_args, proc_env, proc_cwd: subprocess.run( + proc_args, env=proc_env, cwd=proc_cwd + ), + args=(process_args, env, TEST_DATA_PATH), + ) + t2.start() + + t1.join() + t2.join() + + a = process_rpc_json(result[0]) + return a if result else None + + +def process_rpc_json(data: str) -> List[Dict[str, Any]]: + """Process the JSON data which comes from the server which runs the pytest discovery.""" + json_messages = [] + remaining = data + while remaining: + json_data, remaining = process_rpc_message(remaining) + json_messages.append(json_data) - with test_output_file(TEST_DATA_PATH) as output_path: - env = os.environ.copy() - env.update( - { - "TEST_UUID": str(uuid.uuid4()), - "TEST_PORT": str(12345), # port is not used for tests - "PYTHONPATH": os.fspath(pathlib.Path(__file__).parent.parent.parent), - "TEST_OUTPUT_FILE": os.fspath(output_path), - } - ) - - result = subprocess.run( - process_args, - env=env, - cwd=os.fspath(TEST_DATA_PATH), - stdout=subprocess.PIPE, - stderr=subprocess.PIPE, - ) - if result.returncode != 0: - print("Subprocess Run failed with:") - print(result.stdout.decode(encoding="utf-8")) - print(result.stderr.decode(encoding="utf-8")) - - return process_rpc_json(output_path.read_text(encoding="utf-8")) + return json_messages + + +def _listen_on_socket(listener: socket.socket, result: List[str]): + """Listen on the socket for the JSON data from the server. + Created as a seperate function for clarity in threading. + """ + sock, (other_host, other_port) = listener.accept() + all_data: list = [] + while True: + data: bytes = sock.recv(1024 * 1024) + if not data: + break + all_data.append(data.decode("utf-8")) + result.append("".join(all_data)) def find_test_line_number(test_name: str, test_file_path) -> str: diff --git a/extensions/positron-python/pythonFiles/tests/pytestadapter/test_discovery.py b/extensions/positron-python/pythonFiles/tests/pytestadapter/test_discovery.py index bb6e7255704e..ab7abb508153 100644 --- a/extensions/positron-python/pythonFiles/tests/pytestadapter/test_discovery.py +++ b/extensions/positron-python/pythonFiles/tests/pytestadapter/test_discovery.py @@ -2,6 +2,7 @@ # Licensed under the MIT License. import os import shutil +from typing import Any, Dict, List, Optional import pytest @@ -28,12 +29,15 @@ def test_syntax_error(tmp_path): temp_dir.mkdir() p = temp_dir / "error_syntax_discovery.py" shutil.copyfile(file_path, p) - actual = runner(["--collect-only", os.fspath(p)]) - assert actual - assert all(item in actual for item in ("status", "cwd", "error")) - assert actual["status"] == "error" - assert actual["cwd"] == os.fspath(TEST_DATA_PATH) - assert len(actual["error"]) == 2 + actual_list: Optional[List[Dict[str, Any]]] = runner( + ["--collect-only", os.fspath(p)] + ) + assert actual_list + for actual in actual_list: + assert all(item in actual for item in ("status", "cwd", "error")) + assert actual["status"] == "error" + assert actual["cwd"] == os.fspath(TEST_DATA_PATH) + assert len(actual["error"]) == 2 def test_parameterized_error_collect(): @@ -42,12 +46,15 @@ def test_parameterized_error_collect(): The json should still be returned but the errors list should be present. """ file_path_str = "error_parametrize_discovery.py" - actual = runner(["--collect-only", file_path_str]) - assert actual - assert all(item in actual for item in ("status", "cwd", "error")) - assert actual["status"] == "error" - assert actual["cwd"] == os.fspath(TEST_DATA_PATH) - assert len(actual["error"]) == 2 + actual_list: Optional[List[Dict[str, Any]]] = runner( + ["--collect-only", file_path_str] + ) + assert actual_list + for actual in actual_list: + assert all(item in actual for item in ("status", "cwd", "error")) + assert actual["status"] == "error" + assert actual["cwd"] == os.fspath(TEST_DATA_PATH) + assert len(actual["error"]) == 2 @pytest.mark.parametrize( @@ -98,14 +105,15 @@ def test_pytest_collect(file, expected_const): file -- a string with the file or folder to run pytest discovery on. expected_const -- the expected output from running pytest discovery on the file. """ - actual = runner( + actual_list: Optional[List[Dict[str, Any]]] = runner( [ "--collect-only", os.fspath(TEST_DATA_PATH / file), ] ) - assert actual - assert all(item in actual for item in ("status", "cwd", "tests")) - assert actual["status"] == "success" - assert actual["cwd"] == os.fspath(TEST_DATA_PATH) - assert actual["tests"] == expected_const + assert actual_list + for actual in actual_list: + assert all(item in actual for item in ("status", "cwd", "tests")) + assert actual["status"] == "success" + assert actual["cwd"] == os.fspath(TEST_DATA_PATH) + assert actual["tests"] == expected_const diff --git a/extensions/positron-python/pythonFiles/tests/pytestadapter/test_execution.py b/extensions/positron-python/pythonFiles/tests/pytestadapter/test_execution.py index 8613deb96098..d54e4e758d35 100644 --- a/extensions/positron-python/pythonFiles/tests/pytestadapter/test_execution.py +++ b/extensions/positron-python/pythonFiles/tests/pytestadapter/test_execution.py @@ -2,6 +2,7 @@ # Licensed under the MIT License. import os import shutil +from typing import Any, Dict, List, Optional import pytest from tests.pytestadapter import expected_execution_test_output @@ -13,7 +14,7 @@ def test_syntax_error_execution(tmp_path): """Test pytest execution on a file that has a syntax error. Copies the contents of a .txt file to a .py file in the temporary directory - to then run pytest exeuction on. + to then run pytest execution on. The json should still be returned but the errors list should be present. @@ -28,12 +29,15 @@ def test_syntax_error_execution(tmp_path): temp_dir.mkdir() p = temp_dir / "error_syntax_discovery.py" shutil.copyfile(file_path, p) - actual = runner(["error_syntax_discover.py::test_function"]) - assert actual - assert all(item in actual for item in ("status", "cwd", "error")) - assert actual["status"] == "error" - assert actual["cwd"] == os.fspath(TEST_DATA_PATH) - assert len(actual["error"]) == 1 + actual_list: Optional[List[Dict[str, Any]]] = runner( + ["error_syntax_discover.py::test_function"] + ) + assert actual_list + for actual in actual_list: + assert all(item in actual for item in ("status", "cwd", "error")) + assert actual["status"] == "error" + assert actual["cwd"] == os.fspath(TEST_DATA_PATH) + assert len(actual["error"]) == 1 def test_bad_id_error_execution(): @@ -41,12 +45,13 @@ def test_bad_id_error_execution(): The json should still be returned but the errors list should be present. """ - actual = runner(["not/a/real::test_id"]) - assert actual - assert all(item in actual for item in ("status", "cwd", "error")) - assert actual["status"] == "error" - assert actual["cwd"] == os.fspath(TEST_DATA_PATH) - assert len(actual["error"]) == 1 + actual_list: Optional[List[Dict[str, Any]]] = runner(["not/a/real::test_id"]) + assert actual_list + for actual in actual_list: + assert all(item in actual for item in ("status", "cwd", "error")) + assert actual["status"] == "error" + assert actual["cwd"] == os.fspath(TEST_DATA_PATH) + assert len(actual["error"]) == 1 @pytest.mark.parametrize( @@ -153,13 +158,14 @@ def test_pytest_execution(test_ids, expected_const): expected_const -- a dictionary of the expected output from running pytest discovery on the files. """ args = test_ids - actual = runner(args) - assert actual - assert all(item in actual for item in ("status", "cwd", "result")) - assert actual["status"] == "success" - assert actual["cwd"] == os.fspath(TEST_DATA_PATH) - result_data = actual["result"] - for key in result_data: - if result_data[key]["outcome"] == "failure": - result_data[key]["message"] = "ERROR MESSAGE" - assert result_data == expected_const + actual_list: Optional[List[Dict[str, Any]]] = runner(args) + assert actual_list + for actual in actual_list: + assert all(item in actual for item in ("status", "cwd", "result")) + assert actual["status"] == "success" + assert actual["cwd"] == os.fspath(TEST_DATA_PATH) + result_data = actual["result"] + for key in result_data: + if result_data[key]["outcome"] == "failure": + result_data[key]["message"] = "ERROR MESSAGE" + assert result_data == expected_const diff --git a/extensions/positron-python/pythonFiles/tests/test_data/missing-deps.data b/extensions/positron-python/pythonFiles/tests/test_data/missing-deps.data new file mode 100644 index 000000000000..c42d23c7dd67 --- /dev/null +++ b/extensions/positron-python/pythonFiles/tests/test_data/missing-deps.data @@ -0,0 +1,121 @@ +# +# This file is autogenerated by pip-compile with Python 3.7 +# by the following command: +# +# pip-compile --generate-hashes --resolver=backtracking requirements-test.in +# +flake8-csv==0.2.0 \ + --hash=sha256:246e07207fefbf8f80a59ff7e878f153635f562ebaf20cf796a2b00b1528ea9a \ + --hash=sha256:bf3ac6aecbaebe36a2c7d5d275f310996fcc33b7370cdd81feec04b79af2e07c + # via -r requirements-test.in +levenshtein==0.21.0 \ + --hash=sha256:01dd427cf72b4978b09558e3d36e3f92c8eef467e3eb4653c3fdccd8d70aaa08 \ + --hash=sha256:0236c8ff4648c50ebd81ac3692430d2241b134936ac9d86d7ca32ba6ab4a4e63 \ + --hash=sha256:023ca95c833ca548280e444e9a4c34fdecb3be3851e96af95bad290ae0c708b9 \ + --hash=sha256:024302c82d49fc1f1d044794997ef7aa9d01b509a9040e222480b64a01cd4b80 \ + --hash=sha256:04046878a57129da4e2352c032df7c1fceaa54870916d12772cad505ef998290 \ + --hash=sha256:04850a0719e503014acb3fee6d4ec7d7f170a2c7375ffbc5833c7256b7cd10ee \ + --hash=sha256:0cc3679978cd0250bf002963cf2e08855b93f70fa0fc9f74956115c343983fbb \ + --hash=sha256:0f42b8dba2cce257cd34efd1ce9678d06f3248cb0bb2a92a5db8402e1e4a6f30 \ + --hash=sha256:13e8a5b1b58de49befea555bb913dc394614f2d3553bc5b86bc672c69ef1a85a \ + --hash=sha256:1f19fe25ea0dd845d0f48505e8947f6080728e10b7642ba0dad34e9b48c81130 \ + --hash=sha256:1fde464f937878e6f5c30c234b95ce2cb969331a175b3089367e077113428062 \ + --hash=sha256:2290732763e3b75979888364b26acce79d72b8677441b5762a4e97b3630cc3d9 \ + --hash=sha256:24843f28cbbdcbcfc18b08e7d3409dbaad7896fb7113442592fa978590a7bbf0 \ + --hash=sha256:25576ad9c337ecb342306fe87166b54b2f49e713d4ff592c752cc98e0046296e \ + --hash=sha256:26c6fb012538a245d78adea786d2cfe3c1506b835762c1c523a4ed6b9e08dc0b \ + --hash=sha256:31cb59d86a5f99147cd4a67ebced8d6df574b5d763dcb63c033a642e29568746 \ + --hash=sha256:32dfda2e64d0c50553e47d0ab2956413970f940253351c196827ad46f17916d5 \ + --hash=sha256:3305262cb85ff78ace9e2d8d2dfc029b34dc5f93aa2d24fd20b6ed723e2ad501 \ + --hash=sha256:37a99d858fa1d88b1a917b4059a186becd728534e5e889d583086482356b7ca1 \ + --hash=sha256:3c6858cfd84568bc1df3ad545553b5c27af6ed3346973e8f4b57d23c318cf8f4 \ + --hash=sha256:3e1723d515ab287b9b2c2e4a111894dc6b474f5d28826fff379647486cae98d2 \ + --hash=sha256:3e22d31375d5fea5797c9b7aa0f8cc36579c31dcf5754e9931ca86c27d9011f8 \ + --hash=sha256:426883be613d912495cf6ee2a776d2ab84aa6b3de5a8d82c43a994267ea6e0e3 \ + --hash=sha256:4357bf8146cbadb10016ad3a950bba16e042f79015362a575f966181d95b4bc7 \ + --hash=sha256:4515f9511cb91c66d254ee30154206aad76b57d8b25f64ba1402aad43efdb251 \ + --hash=sha256:457442911df185e28a32fd8b788b14ca22ab3a552256b556e7687173d5f18bc4 \ + --hash=sha256:46dab8c6e8fae563ca77acfaeb3824c4dd4b599996328b8a081b06f16befa6a0 \ + --hash=sha256:4b2156f32e46d16b74a055ccb4f64ee3c64399372a6aaf1ee98f6dccfadecee1 \ + --hash=sha256:4bbceef2caba4b2ae613b0e853a7aaab990c1a13bddb9054ba1328a84bccdbf7 \ + --hash=sha256:4c8eaaa6f0df2838437d1d8739629486b145f7a3405d3ef0874301a9f5bc7dcd \ + --hash=sha256:4dc79033140f82acaca40712a6d26ed190cc2dd403e104020a87c24f2771aa72 \ + --hash=sha256:4ec2ef9836a34a3bb009a81e5efe4d9d43515455fb5f182c5d2cf8ae61c79496 \ + --hash=sha256:5369827ace536c6df04e0e670d782999bc17bf9eb111e77435fdcdaecb10c2a3 \ + --hash=sha256:5378a8139ba61d7271c0f9350201259c11eb90bfed0ac45539c4aeaed3907230 \ + --hash=sha256:545635d9e857711d049dcdb0b8609fb707b34b032517376c531ca159fcd46265 \ + --hash=sha256:587ad51770de41eb491bea1bfb676abc7ff9a94dbec0e2bc51fc6a25abef99c4 \ + --hash=sha256:5cfbc4ed7ee2965e305bf81388fea377b795dabc82ee07f04f31d1fb8677a885 \ + --hash=sha256:5e748c2349719cb1bc90f802d9d7f07310633dcf166d468a5bd821f78ed17698 \ + --hash=sha256:608beb1683508c3cdbfff669c1c872ea02b47965e1bbb8a630de548e2490f96a \ + --hash=sha256:6338a47b6f8c7f1ee8b5636cc8b245ad2d1d0ee47f7bb6f33f38a522ef0219cc \ + --hash=sha256:668ea30b311944c643f866ce5e45edf346f05e920075c0056f2ba7f74dde6071 \ + --hash=sha256:66d303cd485710fe6d62108209219b7a695bdd10a722f4e86abdaf26f4bf2202 \ + --hash=sha256:6ebabcf982ae161534f8729d13fe05eebc977b497ac34936551f97cf8b07dd9e \ + --hash=sha256:6ede583155f24c8b2456a7720fbbfa5d9c1154ae04b4da3cf63368e2406ea099 \ + --hash=sha256:709a727f58d31a5ee1e5e83b247972fe55ef0014f6222256c9692c5efa471785 \ + --hash=sha256:742b785c93d16c63289902607219c200bd2b6077dafc788073c74337cae382fb \ + --hash=sha256:76d5d34a8e21de8073c66ae801f053520f946d499fa533fbba654712775f8132 \ + --hash=sha256:7bc550d0986ace95bde003b8a60e622449baf2bdf24d8412f7a50f401a289ec3 \ + --hash=sha256:7c2d67220867d640e36931b3d63b8349369b485d52cf6f4a2635bec8da92d678 \ + --hash=sha256:7ce3f14a8e006fb7e3fc7bab965ab7da5817f48fc48d25cf735fcec8f1d2e39a \ + --hash=sha256:7e40a4bac848c9a8883225f926cfa7b2bc9f651e989a8b7006cdb596edc7ac9b \ + --hash=sha256:80e67bd73a05592ecd52aede4afa8ea49575de70f9d5bfbe2c52ebd3541b20be \ + --hash=sha256:8446f8da38857482ec0cfd616fe5e7dcd3695fd323cc65f37366a9ff6a31c9cb \ + --hash=sha256:8476862a5c3150b8d63a7475563a4bff6dc50bbc0447894eb6b6a116ced0809d \ + --hash=sha256:84b55b732e311629a8308ad2778a0f9824e29e3c35987eb35610fc52eb6d4634 \ + --hash=sha256:88ccdc8dc20c16e8059ace00fb58d353346a04fd24c0733b009678b2554801d2 \ + --hash=sha256:8aa92b05156dfa2e248c3743670d5deb41a45b5789416d5fa31be009f4f043ab \ + --hash=sha256:8ac4ed77d3263eac7f9b6ed89d451644332aecd55cda921201e348803a1e5c57 \ + --hash=sha256:8bdbcd1570340b07549f71e8a5ba3f0a6d84408bf86c4051dc7b70a29ae342bb \ + --hash=sha256:8c031cbe3685b0343f5cc2dcf2172fd21b82f8ccc5c487179a895009bf0e4ea8 \ + --hash=sha256:8c27a5178ce322b56527a451185b4224217aa81955d9b0dad6f5a8de81ffe80f \ + --hash=sha256:8cf87a5e2962431d7260dd81dc1ca0697f61aad81036145d3666f4c0d514ce3a \ + --hash=sha256:8d4ba0df46bb41d660d77e7cc6b4d38c8d5b6f977d51c48ed1217db6a8474cde \ + --hash=sha256:8dd8ef4239b24fb1c9f0b536e48e55194d5966d351d349af23e67c9eb3875c68 \ + --hash=sha256:92bf2370b01d7a4862abf411f8f60f39f064cebebce176e3e9ee14e744db8288 \ + --hash=sha256:9485f2a5c88113410153256657072bc93b81bf5c8690d47e4cc3df58135dbadb \ + --hash=sha256:9ff1255c499fcb41ba37a578ad8c1b8dab5c44f78941b8e1c1d7fab5b5e831bc \ + --hash=sha256:a18c8e4d1aae3f9950797d049020c64a8a63cc8b4e43afcca91ec400bf6304c5 \ + --hash=sha256:a68b05614d25cc2a5fbcc4d2fd124be7668d075fd5ac3d82f292eec573157361 \ + --hash=sha256:a7adaabe07c5ceb6228332b9184f06eb9cda89c227d198a1b8a6f78c05b3c672 \ + --hash=sha256:aa39bb773915e4df330d311bb6c100a8613e265cc50d5b25b015c8db824e1c47 \ + --hash=sha256:ac8b6266799645827980ab1af4e0bfae209c1f747a10bdf6e5da96a6ebe511a2 \ + --hash=sha256:b0ba9723c7d67a61e160b3457259552f7d679d74aaa144b892eb68b7e2a5ebb6 \ + --hash=sha256:b167b32b3e336c5ec5e0212f025587f9248344ae6e73ed668270eba5c6a506e5 \ + --hash=sha256:b646ace5085a60d4f89b28c81301c9d9e8cd6a9bdda908181b2fa3dfac7fc10d \ + --hash=sha256:bd0bfa71b1441be359e99e77709885b79c22857bf9bb7f4e84c09e501f6c5fad \ + --hash=sha256:be038321695267a8faa5ae1b1a83deb3748827f0b6f72471e0beed36afcbd72a \ + --hash=sha256:be87998ffcbb5fb0c37a76d100f63b4811f48527192677da0ec3624b49ab8a64 \ + --hash=sha256:c270487d60b33102efea73be6dcd5835f3ddc3dc06e77499f0963df6cba2ec71 \ + --hash=sha256:c290a7211f1b4f87c300df4424cc46b7379cead3b6f37fa8d3e7e6c6212ccd39 \ + --hash=sha256:cc36ba40027b4f8821155c9e3e0afadffccdccbe955556039d1d1169dfc659c9 \ + --hash=sha256:ce7e76c6341abb498368d42b8081f2f45c245ac2a221af6a0394349d41302c08 \ + --hash=sha256:cefd5a668f6d7af1279aca10104b43882fdd83f9bdc68933ba5429257a628abe \ + --hash=sha256:cf2dee0f8c71598f8be51e3feceb9142ac01576277b9e691e25740987761c86e \ + --hash=sha256:d23c647b03acbb5783f9bdfd51cfa5365d51f7df9f4029717a35eff5cc32bbcc \ + --hash=sha256:d647f1e0c30c7a73f70f4de7376ed7dafc2b856b67fe480d32a81af133edbaeb \ + --hash=sha256:d932cb21e40beb93cfc8973de7f25fbf25ba4a07d1dccac3b9ba977164cf9887 \ + --hash=sha256:db7567997ffbc2feb999e30002a92461a76f17a596a142bdb463b5f7037f160c \ + --hash=sha256:de2dfd6498454c7d89036d56a53c0a01fd9bcf1c2970253e469b5e8bb938b69f \ + --hash=sha256:df9b0f8f511270ad259c7bfba22ab6d5a0c33d81cd594461668e67cd80dd9052 \ + --hash=sha256:e043b79e39f165026bc941c95582bfc4bfdd297a1de6f13ace0d0a7abf486288 \ + --hash=sha256:e2686c37d22faf27d02a19e83b55812d248b32b7ba3aa638e768d0ea032e1f3c \ + --hash=sha256:e9a6251818b9eb6d519bffd7a0b745f3a99b3e99563a4c9d3cad26e34f6ac880 \ + --hash=sha256:eab6c253983a6659e749f4c44fcc2215194c2e00bf7b1c5e90fe683ea3b7b00f \ + --hash=sha256:ec64b7b3fb95bc9c20c72548277794b81281a6ba9da85eda2c87324c218441ff \ + --hash=sha256:ee62ec5882a857b252faffeb7867679f7e418052ca6bf7d6b56099f6498a2b0e \ + --hash=sha256:ee757fd36bad66ad8b961958840894021ecaad22194f65219a666432739393ff \ + --hash=sha256:f55623094b665d79a3b82ba77386ac34fa85049163edfe65387063e5127d4184 \ + --hash=sha256:f622f542bd065ffec7d26b26d44d0c9a25c9c1295fd8ba6e4d77778e2293a12c \ + --hash=sha256:f873af54014cac12082c7f5ccec6bbbeb5b57f63466e7f9c61a34588621313fb \ + --hash=sha256:fae24c875c4ecc8c5f34a9715eb2a459743b4ca21d35c51819b640ee2f71cb51 \ + --hash=sha256:fb26e69fc6c12534fbaa1657efed3b6482f1a166ba8e31227fa6f6f062a59070 + # via -r requirements-test.in +pytest==7.3.1 \ + --hash=sha256:3799fa815351fea3a5e96ac7e503a96fa51cc9942c3753cda7651b93c1cfa362 \ + --hash=sha256:434afafd78b1d78ed0addf160ad2b77a30d35d4bdf8af234fe621919d9ed15e3 + +tomli==2.0.1 \ + --hash=sha256:939de3e7a6161af0c887ef91b7d41a53e7c5a1ca976325f429cb46ea9bc30ecc \ + --hash=sha256:de526c12914f0c550d15924c62d72abc48d6fe7364aa87328337a31007fe8a4f diff --git a/extensions/positron-python/pythonFiles/tests/test_data/no-missing-deps.data b/extensions/positron-python/pythonFiles/tests/test_data/no-missing-deps.data new file mode 100644 index 000000000000..5c2f1178bbdf --- /dev/null +++ b/extensions/positron-python/pythonFiles/tests/test_data/no-missing-deps.data @@ -0,0 +1,13 @@ +# +# This file is autogenerated by pip-compile with Python 3.7 +# by the following command: +# +# pip-compile --generate-hashes --resolver=backtracking requirements-test.in +# +pytest==7.3.1 \ + --hash=sha256:3799fa815351fea3a5e96ac7e503a96fa51cc9942c3753cda7651b93c1cfa362 \ + --hash=sha256:434afafd78b1d78ed0addf160ad2b77a30d35d4bdf8af234fe621919d9ed15e3 + +tomli==2.0.1 \ + --hash=sha256:939de3e7a6161af0c887ef91b7d41a53e7c5a1ca976325f429cb46ea9bc30ecc \ + --hash=sha256:de526c12914f0c550d15924c62d72abc48d6fe7364aa87328337a31007fe8a4f diff --git a/extensions/positron-python/pythonFiles/tests/test_data/pyproject-missing-deps.data b/extensions/positron-python/pythonFiles/tests/test_data/pyproject-missing-deps.data new file mode 100644 index 000000000000..f217a0bdade6 --- /dev/null +++ b/extensions/positron-python/pythonFiles/tests/test_data/pyproject-missing-deps.data @@ -0,0 +1,9 @@ +[build-system] +requires = ["flit_core >=3.2,<4"] +build-backend = "flit_core.buildapi" + +[project] +name = "something" +version = "2023.0.0" +requires-python = ">=3.7" +dependencies = ["pytest==7.3.1", "flake8-csv"] diff --git a/extensions/positron-python/pythonFiles/tests/test_data/pyproject-no-missing-deps.data b/extensions/positron-python/pythonFiles/tests/test_data/pyproject-no-missing-deps.data new file mode 100644 index 000000000000..729bc9169e6f --- /dev/null +++ b/extensions/positron-python/pythonFiles/tests/test_data/pyproject-no-missing-deps.data @@ -0,0 +1,9 @@ +[build-system] +requires = ["flit_core >=3.2,<4"] +build-backend = "flit_core.buildapi" + +[project] +name = "something" +version = "2023.0.0" +requires-python = ">=3.7" +dependencies = [jedi-language-server"] diff --git a/extensions/positron-python/pythonFiles/tests/test_installed_check.py b/extensions/positron-python/pythonFiles/tests/test_installed_check.py new file mode 100644 index 000000000000..f76070d197be --- /dev/null +++ b/extensions/positron-python/pythonFiles/tests/test_installed_check.py @@ -0,0 +1,90 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +import contextlib +import json +import os +import pathlib +import subprocess +import sys + +import pytest +from typing import Dict, List, Union + +SCRIPT_PATH = pathlib.Path(__file__).parent.parent / "installed_check.py" +TEST_DATA = pathlib.Path(__file__).parent / "test_data" +DEFAULT_SEVERITY = 3 + + +@contextlib.contextmanager +def generate_file(base_file: pathlib.Path): + basename = "pyproject.toml" if "pyproject" in base_file.name else "requirements.txt" + fullpath = base_file.parent / basename + if fullpath.exists(): + os.unlink(os.fspath(fullpath)) + fullpath.write_text(base_file.read_text(encoding="utf-8")) + try: + yield fullpath + finally: + os.unlink(str(fullpath)) + + +def run_on_file(file_path: pathlib.Path) -> List[Dict[str, Union[str, int]]]: + result = subprocess.run( + [ + sys.executable, + os.fspath(SCRIPT_PATH), + os.fspath(file_path), + ], + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + check=True, + ) + assert result.returncode == 0 + assert result.stderr == b"" + return json.loads(result.stdout) + + +EXPECTED_DATA = { + "missing-deps": [ + { + "line": 6, + "character": 0, + "endLine": 6, + "endCharacter": 10, + "package": "flake8-csv", + "code": "not-installed", + "severity": 3, + }, + { + "line": 10, + "character": 0, + "endLine": 10, + "endCharacter": 11, + "package": "levenshtein", + "code": "not-installed", + "severity": 3, + }, + ], + "no-missing-deps": [], + "pyproject-missing-deps": [ + { + "line": 8, + "character": 34, + "endLine": 8, + "endCharacter": 44, + "package": "flake8-csv", + "code": "not-installed", + "severity": 3, + } + ], + "pyproject-no-missing-deps": [], +} + + +@pytest.mark.parametrize("test_name", EXPECTED_DATA.keys()) +def test_installed_check(test_name: str): + base_file = TEST_DATA / f"{test_name}.data" + with generate_file(base_file) as file_path: + result = run_on_file(file_path) + assert result == EXPECTED_DATA[test_name] diff --git a/extensions/positron-python/pythonFiles/tests/unittestadapter/test_execution.py b/extensions/positron-python/pythonFiles/tests/unittestadapter/test_execution.py index 7f58049a56b7..d461ead9ad94 100644 --- a/extensions/positron-python/pythonFiles/tests/unittestadapter/test_execution.py +++ b/extensions/positron-python/pythonFiles/tests/unittestadapter/test_execution.py @@ -20,14 +20,12 @@ "111", "--uuid", "fake-uuid", - "--testids", - "test_file.test_class.test_method", ], - (111, "fake-uuid", ["test_file.test_class.test_method"]), + (111, "fake-uuid"), ), ( - ["--port", "111", "--uuid", "fake-uuid", "--testids", ""], - (111, "fake-uuid", [""]), + ["--port", "111", "--uuid", "fake-uuid"], + (111, "fake-uuid"), ), ( [ @@ -35,12 +33,10 @@ "111", "--uuid", "fake-uuid", - "--testids", - "test_file.test_class.test_method", "-v", "-s", ], - (111, "fake-uuid", ["test_file.test_class.test_method"]), + (111, "fake-uuid"), ), ], ) diff --git a/extensions/positron-python/pythonFiles/unittestadapter/discovery.py b/extensions/positron-python/pythonFiles/unittestadapter/discovery.py index dc0a139ed5a2..bcc2fd967f78 100644 --- a/extensions/positron-python/pythonFiles/unittestadapter/discovery.py +++ b/extensions/positron-python/pythonFiles/unittestadapter/discovery.py @@ -8,7 +8,13 @@ import sys import traceback import unittest -from typing import List, Literal, Optional, Tuple, Union +from typing import List, Optional, Tuple, Union + +script_dir = pathlib.Path(__file__).parent.parent +sys.path.append(os.fspath(script_dir)) +sys.path.append(os.fspath(script_dir / "lib" / "python")) + +from typing_extensions import Literal # Add the path to pythonFiles to sys.path to find testing_tools.socket_manager. PYTHON_FILES = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) diff --git a/extensions/positron-python/pythonFiles/unittestadapter/execution.py b/extensions/positron-python/pythonFiles/unittestadapter/execution.py index 37288651f531..4695064396cc 100644 --- a/extensions/positron-python/pythonFiles/unittestadapter/execution.py +++ b/extensions/positron-python/pythonFiles/unittestadapter/execution.py @@ -5,12 +5,19 @@ import enum import json import os +import pathlib +import socket import sys import traceback import unittest from types import TracebackType from typing import Dict, List, Optional, Tuple, Type, Union +script_dir = pathlib.Path(__file__).parent.parent +sys.path.append(os.fspath(script_dir)) +sys.path.append(os.fspath(script_dir / "lib" / "python")) +from testing_tools import process_json_util + # Add the path to pythonFiles to sys.path to find testing_tools.socket_manager. PYTHON_FILES = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) sys.path.insert(0, PYTHON_FILES) @@ -25,7 +32,7 @@ def parse_execution_cli_args( args: List[str], -) -> Tuple[int, Union[str, None], List[str]]: +) -> Tuple[int, Union[str, None]]: """Parse command-line arguments that should be processed by the script. So far this includes the port number that it needs to connect to, the uuid passed by the TS side, @@ -39,10 +46,9 @@ def parse_execution_cli_args( arg_parser = argparse.ArgumentParser() arg_parser.add_argument("--port", default=DEFAULT_PORT) arg_parser.add_argument("--uuid") - arg_parser.add_argument("--testids", nargs="+") parsed_args, _ = arg_parser.parse_known_args(args) - return (int(parsed_args.port), parsed_args.uuid, parsed_args.testids) + return (int(parsed_args.port), parsed_args.uuid) ErrorType = Union[ @@ -226,11 +232,62 @@ def run_tests( start_dir, pattern, top_level_dir = parse_unittest_args(argv[index + 1 :]) - # Perform test execution. - port, uuid, testids = parse_execution_cli_args(argv[:index]) - payload = run_tests(start_dir, testids, pattern, top_level_dir, uuid) + run_test_ids_port = os.environ.get("RUN_TEST_IDS_PORT") + run_test_ids_port_int = ( + int(run_test_ids_port) if run_test_ids_port is not None else 0 + ) + + # get data from socket + test_ids_from_buffer = [] + try: + client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + client_socket.connect(("localhost", run_test_ids_port_int)) + print(f"CLIENT: Server listening on port {run_test_ids_port_int}...") + buffer = b"" + + while True: + # Receive the data from the client + data = client_socket.recv(1024 * 1024) + if not data: + break + + # Append the received data to the buffer + buffer += data + + try: + # Try to parse the buffer as JSON + test_ids_from_buffer = process_json_util.process_rpc_json( + buffer.decode("utf-8") + ) + # Clear the buffer as complete JSON object is received + buffer = b"" + + # Process the JSON data + print(f"Received JSON data: {test_ids_from_buffer}") + break + except json.JSONDecodeError: + # JSON decoding error, the complete JSON object is not yet received + continue + except socket.error as e: + print(f"Error: Could not connect to runTestIdsPort: {e}") + print("Error: Could not connect to runTestIdsPort") + + port, uuid = parse_execution_cli_args(argv[:index]) + if test_ids_from_buffer: + # Perform test execution. + payload = run_tests( + start_dir, test_ids_from_buffer, pattern, top_level_dir, uuid + ) + else: + cwd = os.path.abspath(start_dir) + status = TestExecutionStatus.error + payload: PayloadDict = { + "cwd": cwd, + "status": status, + "error": "No test ids received from buffer", + } - # Build the request data (it has to be a POST request or the Node side will not process it), and send it. + # Build the request data and send it. addr = ("localhost", port) data = json.dumps(payload) request = f"""Content-Length: {len(data)} diff --git a/extensions/positron-python/pythonFiles/unittestadapter/utils.py b/extensions/positron-python/pythonFiles/unittestadapter/utils.py index 568ff30ee92d..9c8b896a8d6e 100644 --- a/extensions/positron-python/pythonFiles/unittestadapter/utils.py +++ b/extensions/positron-python/pythonFiles/unittestadapter/utils.py @@ -6,8 +6,15 @@ import inspect import os import pathlib +import sys import unittest -from typing import List, Tuple, TypedDict, Union +from typing import List, Tuple, Union + +script_dir = pathlib.Path(__file__).parent.parent +sys.path.append(os.fspath(script_dir)) +sys.path.append(os.fspath(script_dir / "lib" / "python")) + +from typing_extensions import TypedDict # Types diff --git a/extensions/positron-python/pythonFiles/vscode_pytest/__init__.py b/extensions/positron-python/pythonFiles/vscode_pytest/__init__.py index 6063e4113d55..0f0bcbd1d323 100644 --- a/extensions/positron-python/pythonFiles/vscode_pytest/__init__.py +++ b/extensions/positron-python/pythonFiles/vscode_pytest/__init__.py @@ -179,6 +179,12 @@ def pytest_sessionfinish(session, exitstatus): 4: Pytest encountered an internal error or exception during test execution. 5: Pytest was unable to find any tests to run. """ + print( + "pytest session has finished, exit status: ", + exitstatus, + "in discovery? ", + IS_DISCOVERY, + ) cwd = pathlib.Path.cwd() if IS_DISCOVERY: try: @@ -209,7 +215,6 @@ def pytest_sessionfinish(session, exitstatus): f"Pytest exited with error status: {exitstatus}, {ERROR_MESSAGE_CONST[exitstatus]}" ) exitstatus_bool = "error" - execution_post( os.fsdecode(cwd), exitstatus_bool, @@ -437,19 +442,13 @@ def execution_post( Request-uuid: {testuuid} {data}""" - test_output_file: Optional[str] = os.getenv("TEST_OUTPUT_FILE", None) - if test_output_file == "stdout": - print(request) - elif test_output_file: - pathlib.Path(test_output_file).write_text(request, encoding="utf-8") - else: - try: - with socket_manager.SocketManager(addr) as s: - if s.socket is not None: - s.socket.sendall(request.encode("utf-8")) - except Exception as e: - print(f"Plugin error connection error[vscode-pytest]: {e}") - print(f"[vscode-pytest] data: {request}") + try: + with socket_manager.SocketManager(addr) as s: + if s.socket is not None: + s.socket.sendall(request.encode("utf-8")) + except Exception as e: + print(f"Plugin error connection error[vscode-pytest]: {e}") + print(f"[vscode-pytest] data: {request}") def post_response(cwd: str, session_node: TestNode) -> None: @@ -477,16 +476,10 @@ def post_response(cwd: str, session_node: TestNode) -> None: Request-uuid: {testuuid} {data}""" - test_output_file: Optional[str] = os.getenv("TEST_OUTPUT_FILE", None) - if test_output_file == "stdout": - print(request) - elif test_output_file: - pathlib.Path(test_output_file).write_text(request, encoding="utf-8") - else: - try: - with socket_manager.SocketManager(addr) as s: - if s.socket is not None: - s.socket.sendall(request.encode("utf-8")) - except Exception as e: - print(f"Plugin error connection error[vscode-pytest]: {e}") - print(f"[vscode-pytest] data: {request}") + try: + with socket_manager.SocketManager(addr) as s: + if s.socket is not None: + s.socket.sendall(request.encode("utf-8")) + except Exception as e: + print(f"Plugin error connection error[vscode-pytest]: {e}") + print(f"[vscode-pytest] data: {request}") diff --git a/extensions/positron-python/pythonFiles/vscode_pytest/run_pytest_script.py b/extensions/positron-python/pythonFiles/vscode_pytest/run_pytest_script.py new file mode 100644 index 000000000000..57bb41a3a7dd --- /dev/null +++ b/extensions/positron-python/pythonFiles/vscode_pytest/run_pytest_script.py @@ -0,0 +1,68 @@ +# Copyright (c) Microsoft Corporation. +# Licensed under the MIT License. +import json +import os +import pathlib +import socket +import sys + +import pytest + +script_dir = pathlib.Path(__file__).parent.parent +sys.path.append(os.fspath(script_dir)) +sys.path.append(os.fspath(script_dir / "lib" / "python")) +from testing_tools import process_json_util + +# This script handles running pytest via pytest.main(). It is called via run in the +# pytest execution adapter and gets the test_ids to run via stdin and the rest of the +# args through sys.argv. It then runs pytest.main() with the args and test_ids. + +if __name__ == "__main__": + # Add the root directory to the path so that we can import the plugin. + directory_path = pathlib.Path(__file__).parent.parent + sys.path.append(os.fspath(directory_path)) + # Get the rest of the args to run with pytest. + args = sys.argv[1:] + run_test_ids_port = os.environ.get("RUN_TEST_IDS_PORT") + run_test_ids_port_int = ( + int(run_test_ids_port) if run_test_ids_port is not None else 0 + ) + test_ids_from_buffer = [] + try: + client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + client_socket.connect(("localhost", run_test_ids_port_int)) + print(f"CLIENT: Server listening on port {run_test_ids_port_int}...") + buffer = b"" + + while True: + # Receive the data from the client + data = client_socket.recv(1024 * 1024) + if not data: + break + + # Append the received data to the buffer + buffer += data + + try: + # Try to parse the buffer as JSON + test_ids_from_buffer = process_json_util.process_rpc_json( + buffer.decode("utf-8") + ) + # Clear the buffer as complete JSON object is received + buffer = b"" + + # Process the JSON data + print(f"Received JSON data: {test_ids_from_buffer}") + break + except json.JSONDecodeError: + # JSON decoding error, the complete JSON object is not yet received + continue + except socket.error as e: + print(f"Error: Could not connect to runTestIdsPort: {e}") + print("Error: Could not connect to runTestIdsPort") + try: + if test_ids_from_buffer: + arg_array = ["-p", "vscode_pytest"] + args + test_ids_from_buffer + pytest.main(arg_array) + except json.JSONDecodeError: + print("Error: Could not parse test ids from stdin") diff --git a/extensions/positron-python/requirements.in b/extensions/positron-python/requirements.in index 8b76e392917e..bdd5e10dfc47 100644 --- a/extensions/positron-python/requirements.in +++ b/extensions/positron-python/requirements.in @@ -8,3 +8,8 @@ typing-extensions==4.5.0 # Fallback env creator for debian microvenv + +# Checker for installed packages +importlib_metadata +packaging +tomli diff --git a/extensions/positron-python/requirements.txt b/extensions/positron-python/requirements.txt index 07145e1832d5..1419e0528ccc 100644 --- a/extensions/positron-python/requirements.txt +++ b/extensions/positron-python/requirements.txt @@ -4,11 +4,29 @@ # # pip-compile --generate-hashes requirements.in # +importlib-metadata==6.6.0 \ + --hash=sha256:43dd286a2cd8995d5eaef7fee2066340423b818ed3fd70adf0bad5f1fac53fed \ + --hash=sha256:92501cdf9cc66ebd3e612f1b4f0c0765dfa42f0fa38ffb319b6bd84dd675d705 + # via -r requirements.in microvenv==2023.2.0 \ --hash=sha256:5b46296d6a65992946da504bd9e724a5becf5c256091f2f9383e5b4e9f567f23 \ --hash=sha256:a07e88a8fb5ee90219b86dd90095cb5646462d45d30285ea3b1a3c7cf33616d3 # via -r requirements.in +packaging==23.1 \ + --hash=sha256:994793af429502c4ea2ebf6bf664629d07c1a9fe974af92966e4b8d2df7edc61 \ + --hash=sha256:a392980d2b6cffa644431898be54b0045151319d1e7ec34f0cfed48767dd334f + # via -r requirements.in +tomli==2.0.1 \ + --hash=sha256:939de3e7a6161af0c887ef91b7d41a53e7c5a1ca976325f429cb46ea9bc30ecc \ + --hash=sha256:de526c12914f0c550d15924c62d72abc48d6fe7364aa87328337a31007fe8a4f + # via -r requirements.in typing-extensions==4.5.0 \ --hash=sha256:5cb5f4a79139d699607b3ef622a1dedafa84e115ab0024e0d9c044a9479ca7cb \ --hash=sha256:fb33085c39dd998ac16d1431ebc293a8b3eedd00fd4a32de0ff79002c19511b4 - # via -r requirements.in + # via + # -r requirements.in + # importlib-metadata +zipp==3.15.0 \ + --hash=sha256:112929ad649da941c23de50f356a2b5570c954b65150642bccdd66bf194d224b \ + --hash=sha256:48904fc76a60e542af151aded95726c1a5c34ed43ab4134b597665c86d7ad556 + # via importlib-metadata diff --git a/extensions/positron-python/src/client/activation/jedi/analysisOptions.ts b/extensions/positron-python/src/client/activation/jedi/analysisOptions.ts index 67c9af75937c..4778c4e1523f 100644 --- a/extensions/positron-python/src/client/activation/jedi/analysisOptions.ts +++ b/extensions/positron-python/src/client/activation/jedi/analysisOptions.ts @@ -59,7 +59,7 @@ export class JediLanguageServerAnalysisOptions extends LanguageServerAnalysisOpt markupKindPreferred: 'markdown', completion: { resolveEagerly: false, - disableSnippets: false, + disableSnippets: true, }, diagnostics: { enable: true, diff --git a/extensions/positron-python/src/client/common/application/types.ts b/extensions/positron-python/src/client/common/application/types.ts index 77d7b5af3279..72cd357d62d8 100644 --- a/extensions/positron-python/src/client/common/application/types.ts +++ b/extensions/positron-python/src/client/common/application/types.ts @@ -851,6 +851,16 @@ export interface IWorkspaceService { * @return A promise that resolves to a {@link TextDocument document}. */ openTextDocument(options?: { language?: string; content?: string }): Thenable; + /** + * Saves the editor identified by the given resource and returns the resulting resource or `undefined` + * if save was not successful. + * + * **Note** that an editor with the provided resource must be opened in order to be saved. + * + * @param uri the associated uri for the opened editor to save. + * @return A thenable that resolves when the save operation has finished. + */ + save(uri: Uri): Thenable; } export const ITerminalManager = Symbol('ITerminalManager'); diff --git a/extensions/positron-python/src/client/common/application/workspace.ts b/extensions/positron-python/src/client/common/application/workspace.ts index 0a5fd8d81816..a76a78777bef 100644 --- a/extensions/positron-python/src/client/common/application/workspace.ts +++ b/extensions/positron-python/src/client/common/application/workspace.ts @@ -112,4 +112,14 @@ export class WorkspaceService implements IWorkspaceService { const enabledSearchExcludes = Object.keys(searchExcludes).filter((key) => searchExcludes.get(key) === true); return `{${enabledSearchExcludes.join(',')}}`; } + + public async save(uri: Uri): Promise { + try { + // This is a proposed API hence putting it inside try...catch. + const result = await workspace.save(uri); + return result; + } catch (ex) { + return undefined; + } + } } diff --git a/extensions/positron-python/src/client/common/configSettings.ts b/extensions/positron-python/src/client/common/configSettings.ts index b4af055791f0..ba0c520e7a14 100644 --- a/extensions/positron-python/src/client/common/configSettings.ts +++ b/extensions/positron-python/src/client/common/configSettings.ts @@ -23,7 +23,6 @@ import { ITestingSettings } from '../testing/configuration/types'; import { IWorkspaceService } from './application/types'; import { WorkspaceService } from './application/workspace'; import { DEFAULT_INTERPRETER_SETTING, isTestExecution } from './constants'; -import { IS_WINDOWS } from './platform/constants'; import { IAutoCompleteSettings, IDefaultLanguageServer, @@ -41,6 +40,7 @@ import { import { debounceSync } from './utils/decorators'; import { SystemVariables } from './variables/systemVariables'; import { getOSType, OSType } from './utils/platform'; +import { isWindows } from './platform/platformService'; const untildify = require('untildify'); @@ -668,7 +668,7 @@ function getPythonExecutable(pythonPath: string): string { for (let executableName of KnownPythonExecutables) { // Suffix with 'python' for linux and 'osx', and 'python.exe' for 'windows'. - if (IS_WINDOWS) { + if (isWindows()) { executableName = `${executableName}.exe`; if (isValidPythonPath(path.join(pythonPath, executableName))) { return path.join(pythonPath, executableName); diff --git a/extensions/positron-python/src/client/common/constants.ts b/extensions/positron-python/src/client/common/constants.ts index b285667aaa6a..bea0ef9e235c 100644 --- a/extensions/positron-python/src/client/common/constants.ts +++ b/extensions/positron-python/src/client/common/constants.ts @@ -44,6 +44,7 @@ export namespace Commands { export const Enable_SourceMap_Support = 'python.enableSourceMapSupport'; export const Exec_In_Terminal = 'python.execInTerminal'; export const Exec_In_Terminal_Icon = 'python.execInTerminal-icon'; + export const Exec_In_Separate_Terminal = 'python.execInDedicatedTerminal'; export const Exec_Selection_In_Django_Shell = 'python.execSelectionInDjangoShell'; export const Exec_Selection_In_Terminal = 'python.execSelectionInTerminal'; export const GetSelectedInterpreterPath = 'python.interpreterPath'; diff --git a/extensions/positron-python/src/client/common/experiments/groups.ts b/extensions/positron-python/src/client/common/experiments/groups.ts index 5884aafd122d..1ee06469095c 100644 --- a/extensions/positron-python/src/client/common/experiments/groups.ts +++ b/extensions/positron-python/src/client/common/experiments/groups.ts @@ -14,3 +14,7 @@ export enum TerminalEnvVarActivation { export enum ShowFormatterExtensionPrompt { experiment = 'pythonPromptNewFormatterExt', } +// Experiment to enable the new testing rewrite. +export enum EnableTestAdapterRewrite { + experiment = 'pythonTestAdapter', +} diff --git a/extensions/positron-python/src/client/common/interpreterPathService.ts b/extensions/positron-python/src/client/common/interpreterPathService.ts index 5b27fd5a9d72..9eea1548977c 100644 --- a/extensions/positron-python/src/client/common/interpreterPathService.ts +++ b/extensions/positron-python/src/client/common/interpreterPathService.ts @@ -6,7 +6,7 @@ import * as fs from 'fs-extra'; import { inject, injectable } from 'inversify'; import { ConfigurationChangeEvent, ConfigurationTarget, Event, EventEmitter, Uri } from 'vscode'; -import { traceError } from '../logging'; +import { traceError, traceVerbose } from '../logging'; import { IApplicationEnvironment, IWorkspaceService } from './application/types'; import { PythonSettings } from './configSettings'; import { isTestExecution } from './constants'; @@ -56,6 +56,7 @@ export class InterpreterPathService implements IInterpreterPathService { public async onDidChangeConfiguration(event: ConfigurationChangeEvent) { if (event.affectsConfiguration(`python.${defaultInterpreterPathSetting}`)) { this._didChangeInterpreterEmitter.fire({ uri: undefined, configTarget: ConfigurationTarget.Global }); + traceVerbose('Interpreter Path updated', `python.${defaultInterpreterPathSetting}`); } } @@ -129,6 +130,7 @@ export class InterpreterPathService implements IInterpreterPathService { if (persistentSetting.value !== pythonPath) { await persistentSetting.updateValue(pythonPath); this._didChangeInterpreterEmitter.fire({ uri: resource, configTarget }); + traceVerbose('Interpreter Path updated', settingKey, pythonPath); } } diff --git a/extensions/positron-python/src/client/common/platform/constants.ts b/extensions/positron-python/src/client/common/platform/constants.ts deleted file mode 100644 index 808a63188c1d..000000000000 --- a/extensions/positron-python/src/client/common/platform/constants.ts +++ /dev/null @@ -1,7 +0,0 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. -// Licensed under the MIT License. - -// TODO : Drop all these in favor of IPlatformService. -// See https://github.com/microsoft/vscode-python/issues/8542. - -export const IS_WINDOWS = /^win/.test(process.platform); diff --git a/extensions/positron-python/src/client/common/platform/platformService.ts b/extensions/positron-python/src/client/common/platform/platformService.ts index 0277c1bcd2a2..aa139eeeebc0 100644 --- a/extensions/positron-python/src/client/common/platform/platformService.ts +++ b/extensions/positron-python/src/client/common/platform/platformService.ts @@ -50,8 +50,9 @@ export class PlatformService implements IPlatformService { } } + // eslint-disable-next-line class-methods-use-this public get isWindows(): boolean { - return this.osType === OSType.Windows; + return isWindows(); } public get isMac(): boolean { @@ -72,3 +73,7 @@ export class PlatformService implements IPlatformService { return getArchitecture() === Architecture.x64; } } + +export function isWindows(): boolean { + return getOSType() === OSType.Windows; +} diff --git a/extensions/positron-python/src/client/common/process/internal/scripts/index.ts b/extensions/positron-python/src/client/common/process/internal/scripts/index.ts index c52983d9910b..f719844ef80b 100644 --- a/extensions/positron-python/src/client/common/process/internal/scripts/index.ts +++ b/extensions/positron-python/src/client/common/process/internal/scripts/index.ts @@ -110,6 +110,13 @@ export function testlauncher(testArgs: string[]): string[] { return [script, ...testArgs]; } +// run_pytest_script.py +export function pytestlauncher(testArgs: string[]): string[] { + const script = path.join(SCRIPTS_DIR, 'vscode_pytest', 'run_pytest_script.py'); + // There is no output to parse, so we do not return a function. + return [script, ...testArgs]; +} + // visualstudio_py_testlauncher.py // eslint-disable-next-line camelcase @@ -149,3 +156,8 @@ export function createCondaScript(): string { const script = path.join(SCRIPTS_DIR, 'create_conda.py'); return script; } + +export function installedCheckScript(): string { + const script = path.join(SCRIPTS_DIR, 'installed_check.py'); + return script; +} diff --git a/extensions/positron-python/src/client/common/process/types.ts b/extensions/positron-python/src/client/common/process/types.ts index 8298957285e8..62e787b694b5 100644 --- a/extensions/positron-python/src/client/common/process/types.ts +++ b/extensions/positron-python/src/client/common/process/types.ts @@ -25,6 +25,7 @@ export type SpawnOptions = ChildProcessSpawnOptions & { throwOnStdErr?: boolean; extraVariables?: NodeJS.ProcessEnv; outputChannel?: OutputChannel; + stdinStr?: string; }; export type ShellOptions = ExecOptions & { throwOnStdErr?: boolean }; diff --git a/extensions/positron-python/src/client/common/serviceRegistry.ts b/extensions/positron-python/src/client/common/serviceRegistry.ts index 5b527499460a..be0559496ace 100644 --- a/extensions/positron-python/src/client/common/serviceRegistry.ts +++ b/extensions/positron-python/src/client/common/serviceRegistry.ts @@ -57,7 +57,6 @@ import { ProductInstaller } from './installer/productInstaller'; import { InterpreterPathService } from './interpreterPathService'; import { BrowserService } from './net/browser'; import { PersistentStateFactory } from './persistentState'; -import { IS_WINDOWS } from './platform/constants'; import { PathUtils } from './platform/pathUtils'; import { CurrentProcess } from './process/currentProcess'; import { ProcessLogger } from './process/logger'; @@ -91,9 +90,10 @@ import { Random } from './utils/random'; import { ContextKeyManager } from './application/contextKeyManager'; import { CreatePythonFileCommandHandler } from './application/commands/createPythonFile'; import { RequireJupyterPrompt } from '../jupyter/requireJupyterPrompt'; +import { isWindows } from './platform/platformService'; export function registerTypes(serviceManager: IServiceManager): void { - serviceManager.addSingletonInstance(IsWindows, IS_WINDOWS); + serviceManager.addSingletonInstance(IsWindows, isWindows()); serviceManager.addSingleton(IActiveResourceService, ActiveResourceService); serviceManager.addSingleton(IInterpreterPathService, InterpreterPathService); diff --git a/extensions/positron-python/src/client/common/terminal/factory.ts b/extensions/positron-python/src/client/common/terminal/factory.ts index 3855cb6cee3c..39cc88c4b024 100644 --- a/extensions/positron-python/src/client/common/terminal/factory.ts +++ b/extensions/positron-python/src/client/common/terminal/factory.ts @@ -3,6 +3,7 @@ import { inject, injectable } from 'inversify'; import { Uri } from 'vscode'; +import * as path from 'path'; import { IInterpreterService } from '../../interpreter/contracts'; import { IServiceContainer } from '../../ioc/types'; import { PythonEnvironment } from '../../pythonEnvironments/info'; @@ -23,13 +24,17 @@ export class TerminalServiceFactory implements ITerminalServiceFactory { ) { this.terminalServices = new Map(); } - public getTerminalService(options: TerminalCreationOptions): ITerminalService { + public getTerminalService(options: TerminalCreationOptions & { newTerminalPerFile?: boolean }): ITerminalService { const resource = options?.resource; const title = options?.title; - const terminalTitle = typeof title === 'string' && title.trim().length > 0 ? title.trim() : 'Python'; + let terminalTitle = typeof title === 'string' && title.trim().length > 0 ? title.trim() : 'Python'; const interpreter = options?.interpreter; - const id = this.getTerminalId(terminalTitle, resource, interpreter); + const id = this.getTerminalId(terminalTitle, resource, interpreter, options.newTerminalPerFile); if (!this.terminalServices.has(id)) { + if (resource && options.newTerminalPerFile) { + terminalTitle = `${terminalTitle}: ${path.basename(resource.fsPath).replace('.py', '')}`; + } + options.title = terminalTitle; const terminalService = new TerminalService(this.serviceContainer, options); this.terminalServices.set(id, terminalService); } @@ -46,13 +51,19 @@ export class TerminalServiceFactory implements ITerminalServiceFactory { title = typeof title === 'string' && title.trim().length > 0 ? title.trim() : 'Python'; return new TerminalService(this.serviceContainer, { resource, title }); } - private getTerminalId(title: string, resource?: Uri, interpreter?: PythonEnvironment): string { + private getTerminalId( + title: string, + resource?: Uri, + interpreter?: PythonEnvironment, + newTerminalPerFile?: boolean, + ): string { if (!resource && !interpreter) { return title; } const workspaceFolder = this.serviceContainer .get(IWorkspaceService) .getWorkspaceFolder(resource || undefined); - return `${title}:${workspaceFolder?.uri.fsPath || ''}:${interpreter?.path}`; + const fileId = resource && newTerminalPerFile ? resource.fsPath : ''; + return `${title}:${workspaceFolder?.uri.fsPath || ''}:${interpreter?.path}:${fileId}`; } } diff --git a/extensions/positron-python/src/client/common/terminal/types.ts b/extensions/positron-python/src/client/common/terminal/types.ts index 880bf0dd72fb..303188682378 100644 --- a/extensions/positron-python/src/client/common/terminal/types.ts +++ b/extensions/positron-python/src/client/common/terminal/types.ts @@ -97,7 +97,7 @@ export interface ITerminalServiceFactory { * @returns {ITerminalService} * @memberof ITerminalServiceFactory */ - getTerminalService(options: TerminalCreationOptions): ITerminalService; + getTerminalService(options: TerminalCreationOptions & { newTerminalPerFile?: boolean }): ITerminalService; createTerminalService(resource?: Uri, title?: string): ITerminalService; } diff --git a/extensions/positron-python/src/client/common/vscodeApis/languageApis.ts b/extensions/positron-python/src/client/common/vscodeApis/languageApis.ts new file mode 100644 index 000000000000..87681507693d --- /dev/null +++ b/extensions/positron-python/src/client/common/vscodeApis/languageApis.ts @@ -0,0 +1,12 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License + +import { DiagnosticChangeEvent, DiagnosticCollection, Disposable, languages } from 'vscode'; + +export function createDiagnosticCollection(name: string): DiagnosticCollection { + return languages.createDiagnosticCollection(name); +} + +export function onDidChangeDiagnostics(handler: (e: DiagnosticChangeEvent) => void): Disposable { + return languages.onDidChangeDiagnostics(handler); +} diff --git a/extensions/positron-python/src/client/common/vscodeApis/windowApis.ts b/extensions/positron-python/src/client/common/vscodeApis/windowApis.ts index 5c279b890a9f..1c242314cb87 100644 --- a/extensions/positron-python/src/client/common/vscodeApis/windowApis.ts +++ b/extensions/positron-python/src/client/common/vscodeApis/windowApis.ts @@ -77,6 +77,10 @@ export function getActiveTextEditor(): TextEditor | undefined { return activeTextEditor; } +export function onDidChangeActiveTextEditor(handler: (e: TextEditor | undefined) => void): Disposable { + return window.onDidChangeActiveTextEditor(handler); +} + export enum MultiStepAction { Back = 'Back', Cancel = 'Cancel', diff --git a/extensions/positron-python/src/client/common/vscodeApis/workspaceApis.ts b/extensions/positron-python/src/client/common/vscodeApis/workspaceApis.ts index 74200ba46924..0e860743a32d 100644 --- a/extensions/positron-python/src/client/common/vscodeApis/workspaceApis.ts +++ b/extensions/positron-python/src/client/common/vscodeApis/workspaceApis.ts @@ -36,22 +36,26 @@ export function findFiles( return vscode.workspace.findFiles(include, exclude, maxResults, token); } -export function onDidSaveTextDocument( - listener: (e: vscode.TextDocument) => unknown, - thisArgs?: unknown, - disposables?: vscode.Disposable[], -): vscode.Disposable { - return vscode.workspace.onDidSaveTextDocument(listener, thisArgs, disposables); +export function onDidCloseTextDocument(handler: (e: vscode.TextDocument) => unknown): vscode.Disposable { + return vscode.workspace.onDidCloseTextDocument(handler); +} + +export function onDidSaveTextDocument(handler: (e: vscode.TextDocument) => unknown): vscode.Disposable { + return vscode.workspace.onDidSaveTextDocument(handler); } export function getOpenTextDocuments(): readonly vscode.TextDocument[] { return vscode.workspace.textDocuments; } -export function onDidOpenTextDocument(handler: (doc: vscode.TextDocument) => void): vscode.Disposable { +export function onDidOpenTextDocument(handler: (doc: vscode.TextDocument) => unknown): vscode.Disposable { return vscode.workspace.onDidOpenTextDocument(handler); } -export function onDidChangeTextDocument(handler: (e: vscode.TextDocumentChangeEvent) => void): vscode.Disposable { +export function onDidChangeTextDocument(handler: (e: vscode.TextDocumentChangeEvent) => unknown): vscode.Disposable { return vscode.workspace.onDidChangeTextDocument(handler); } + +export function onDidChangeConfiguration(handler: (e: vscode.ConfigurationChangeEvent) => unknown): vscode.Disposable { + return vscode.workspace.onDidChangeConfiguration(handler); +} diff --git a/extensions/positron-python/src/client/debugger/extension/configuration/dynamicdebugConfigurationService.ts b/extensions/positron-python/src/client/debugger/extension/configuration/dynamicdebugConfigurationService.ts index 2d80f0e3d6e8..e79f201d9367 100644 --- a/extensions/positron-python/src/client/debugger/extension/configuration/dynamicdebugConfigurationService.ts +++ b/extensions/positron-python/src/client/debugger/extension/configuration/dynamicdebugConfigurationService.ts @@ -69,7 +69,7 @@ export class DynamicPythonDebugConfigurationService implements IDynamicDebugConf type: DebuggerTypeName, request: 'launch', module: 'uvicorn', - args: [`${fastApiPath}:app`], + args: [`${fastApiPath}:app`, '--reload'], jinja: true, justMyCode: true, }); diff --git a/extensions/positron-python/src/client/debugger/extension/configuration/providers/fastapiLaunch.ts b/extensions/positron-python/src/client/debugger/extension/configuration/providers/fastapiLaunch.ts index 25aaf3d25c08..38a9b7ccf1a2 100644 --- a/extensions/positron-python/src/client/debugger/extension/configuration/providers/fastapiLaunch.ts +++ b/extensions/positron-python/src/client/debugger/extension/configuration/providers/fastapiLaunch.ts @@ -25,12 +25,12 @@ export async function buildFastAPILaunchDebugConfiguration( type: DebuggerTypeName, request: 'launch', module: 'uvicorn', - args: ['main:app'], + args: ['main:app', '--reload'], jinja: true, justMyCode: true, }; - if (!application) { + if (!application && config.args) { const selectedPath = await input.showInputBox({ title: DebugConfigStrings.fastapi.enterAppPathOrNamePath.title, value: 'main.py', @@ -44,7 +44,7 @@ export async function buildFastAPILaunchDebugConfiguration( }); if (selectedPath) { manuallyEnteredAValue = true; - config.args = [`${path.basename(selectedPath, '.py').replace('/', '.')}:app`]; + config.args[0] = `${path.basename(selectedPath, '.py').replace('/', '.')}:app`; } } diff --git a/extensions/positron-python/src/client/debugger/extension/hooks/childProcessAttachHandler.ts b/extensions/positron-python/src/client/debugger/extension/hooks/childProcessAttachHandler.ts index 6851e54a8723..23602ffce086 100644 --- a/extensions/positron-python/src/client/debugger/extension/hooks/childProcessAttachHandler.ts +++ b/extensions/positron-python/src/client/debugger/extension/hooks/childProcessAttachHandler.ts @@ -9,6 +9,7 @@ import { swallowExceptions } from '../../../common/utils/decorators'; import { AttachRequestArguments } from '../../types'; import { DebuggerEvents } from './constants'; import { IChildProcessAttachService, IDebugSessionEventHandlers } from './types'; +import { DebuggerTypeName } from '../../constants'; /** * This class is responsible for automatically attaching the debugger to any @@ -25,7 +26,7 @@ export class ChildProcessAttachEventHandler implements IDebugSessionEventHandler @swallowExceptions('Handle child process launch') public async handleCustomEvent(event: DebugSessionCustomEvent): Promise { - if (!event) { + if (!event || event.session.configuration.type !== DebuggerTypeName) { return; } diff --git a/extensions/positron-python/src/client/extensionActivation.ts b/extensions/positron-python/src/client/extensionActivation.ts index ba7bdf61c8f2..8dcea063676a 100644 --- a/extensions/positron-python/src/client/extensionActivation.ts +++ b/extensions/positron-python/src/client/extensionActivation.ts @@ -51,10 +51,9 @@ import { DebugSessionEventDispatcher } from './debugger/extension/hooks/eventHan import { IDebugSessionEventHandlers } from './debugger/extension/hooks/types'; import { WorkspaceService } from './common/application/workspace'; import { DynamicPythonDebugConfigurationService } from './debugger/extension/configuration/dynamicdebugConfigurationService'; -import { registerCreateEnvironmentFeatures } from './pythonEnvironments/creation/createEnvApi'; import { IInterpreterQuickPick } from './interpreter/configuration/types'; import { registerInstallFormatterPrompt } from './providers/prompts/installFormatterPrompt'; -import { registerPyProjectTomlCreateEnvFeatures } from './pythonEnvironments/creation/pyprojectTomlCreateEnv'; +import { registerAllCreateEnvironmentFeatures } from './pythonEnvironments/creation/registrations'; export async function activateComponents( // `ext` is passed to any extra activation funcs. @@ -97,8 +96,7 @@ export function activateFeatures(ext: ExtensionState, _components: Components): IInterpreterPathService, ); const pathUtils = ext.legacyIOC.serviceContainer.get(IPathUtils); - registerCreateEnvironmentFeatures(ext.disposables, interpreterQuickPick, interpreterPathService, pathUtils); - registerPyProjectTomlCreateEnvFeatures(ext.disposables); + registerAllCreateEnvironmentFeatures(ext.disposables, interpreterQuickPick, interpreterPathService, pathUtils); } /// ////////////////////////// diff --git a/extensions/positron-python/src/client/interpreter/activation/terminalEnvVarCollectionService.ts b/extensions/positron-python/src/client/interpreter/activation/terminalEnvVarCollectionService.ts index 26852303d099..dbf77e72379a 100644 --- a/extensions/positron-python/src/client/interpreter/activation/terminalEnvVarCollectionService.ts +++ b/extensions/positron-python/src/client/interpreter/activation/terminalEnvVarCollectionService.ts @@ -3,7 +3,14 @@ import * as path from 'path'; import { inject, injectable } from 'inversify'; -import { ProgressOptions, ProgressLocation, MarkdownString, WorkspaceFolder } from 'vscode'; +import { + ProgressOptions, + ProgressLocation, + MarkdownString, + WorkspaceFolder, + EnvironmentVariableCollection, + EnvironmentVariableScope, +} from 'vscode'; import { pathExists } from 'fs-extra'; import { IExtensionActivationService } from '../../activation/types'; import { IApplicationShell, IApplicationEnvironment, IWorkspaceService } from '../../common/application/types'; @@ -108,6 +115,7 @@ export class TerminalEnvVarCollectionService implements IExtensionActivationServ undefined, shell, ); + const envVarCollection = this.getEnvironmentVariableCollection(workspaceFolder); if (!env) { const shellType = identifyShellFromShellPath(shell); const defaultShell = defaultShells[this.platform.osType]; @@ -117,7 +125,7 @@ export class TerminalEnvVarCollectionService implements IExtensionActivationServ await this._applyCollection(resource, defaultShell?.shell); return; } - this.context.environmentVariableCollection.clear({ workspaceFolder }); + envVarCollection.clear(); this.previousEnvVars = _normCaseKeys(process.env); return; } @@ -129,10 +137,10 @@ export class TerminalEnvVarCollectionService implements IExtensionActivationServ if (prevValue !== value) { if (value !== undefined) { traceVerbose(`Setting environment variable ${key} in collection to ${value}`); - this.context.environmentVariableCollection.replace(key, value, { workspaceFolder }); + envVarCollection.replace(key, value, { applyAtShellIntegration: true }); } else { traceVerbose(`Clearing environment variable ${key} from collection`); - this.context.environmentVariableCollection.delete(key, { workspaceFolder }); + envVarCollection.delete(key); } } }); @@ -140,14 +148,21 @@ export class TerminalEnvVarCollectionService implements IExtensionActivationServ // If the previous env var is not in the current env, clear it from collection. if (!(key in env)) { traceVerbose(`Clearing environment variable ${key} from collection`); - this.context.environmentVariableCollection.delete(key, { workspaceFolder }); + envVarCollection.delete(key); } }); const displayPath = this.pathUtils.getDisplayName(settings.pythonPath, workspaceFolder?.uri.fsPath); const description = new MarkdownString(`${Interpreters.activateTerminalDescription} \`${displayPath}\``); - this.context.environmentVariableCollection.setDescription(description, { - workspaceFolder, - }); + envVarCollection.description = description; + } + + private getEnvironmentVariableCollection(workspaceFolder?: WorkspaceFolder) { + const envVarCollection = this.context.environmentVariableCollection as EnvironmentVariableCollection & { + getScopedEnvironmentVariableCollection(scope: EnvironmentVariableScope): EnvironmentVariableCollection; + }; + return workspaceFolder + ? envVarCollection.getScopedEnvironmentVariableCollection({ workspaceFolder }) + : envVarCollection; } private async handleMicroVenv(resource: Resource) { @@ -156,12 +171,11 @@ export class TerminalEnvVarCollectionService implements IExtensionActivationServ if (interpreter?.envType === EnvironmentType.Venv) { const activatePath = path.join(path.dirname(interpreter.path), 'activate'); if (!(await pathExists(activatePath))) { - this.context.environmentVariableCollection.replace( + const envVarCollection = this.getEnvironmentVariableCollection(workspaceFolder); + envVarCollection.replace( 'PATH', `${path.dirname(interpreter.path)}${path.delimiter}${process.env.Path}`, - { - workspaceFolder, - }, + { applyAtShellIntegration: true }, ); return; } diff --git a/extensions/positron-python/src/client/linters/pydocstyle.ts b/extensions/positron-python/src/client/linters/pydocstyle.ts index 93c059440fe7..4851190a92ac 100644 --- a/extensions/positron-python/src/client/linters/pydocstyle.ts +++ b/extensions/positron-python/src/client/linters/pydocstyle.ts @@ -4,9 +4,9 @@ import '../common/extensions'; import { Product } from '../common/types'; import { IServiceContainer } from '../ioc/types'; import { traceError } from '../logging'; -import { IS_WINDOWS } from '../common/platform/constants'; import { BaseLinter } from './baseLinter'; import { ILintMessage, LintMessageSeverity } from './types'; +import { isWindows } from '../common/platform/platformService'; export class PyDocStyle extends BaseLinter { constructor(serviceContainer: IServiceContainer) { @@ -47,7 +47,7 @@ export class PyDocStyle extends BaseLinter { .filter((value, index) => index < maxLines && value.indexOf(':') >= 0) .map((line) => { // Windows will have a : after the drive letter (e.g. c:\). - if (IS_WINDOWS) { + if (isWindows()) { return line.substring(line.indexOf(`${baseFileName}:`) + baseFileName.length + 1).trim(); } return line.substring(line.indexOf(':') + 1).trim(); diff --git a/extensions/positron-python/src/client/pythonEnvironments/creation/common/installCheckUtils.ts b/extensions/positron-python/src/client/pythonEnvironments/creation/common/installCheckUtils.ts new file mode 100644 index 000000000000..8c9817f83b7a --- /dev/null +++ b/extensions/positron-python/src/client/pythonEnvironments/creation/common/installCheckUtils.ts @@ -0,0 +1,60 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License + +import { Diagnostic, DiagnosticSeverity, l10n, Range, TextDocument, Uri } from 'vscode'; +import { installedCheckScript } from '../../../common/process/internal/scripts'; +import { plainExec } from '../../../common/process/rawProcessApis'; +import { IInterpreterPathService } from '../../../common/types'; +import { traceInfo, traceVerbose, traceError } from '../../../logging'; + +interface PackageDiagnostic { + package: string; + line: number; + character: number; + endLine: number; + endCharacter: number; + code: string; + severity: DiagnosticSeverity; +} + +export const INSTALL_CHECKER_SOURCE = 'Python-InstalledPackagesChecker'; + +function parseDiagnostics(data: string): Diagnostic[] { + let diagnostics: Diagnostic[] = []; + try { + const raw = JSON.parse(data) as PackageDiagnostic[]; + diagnostics = raw.map((item) => { + const d = new Diagnostic( + new Range(item.line, item.character, item.endLine, item.endCharacter), + l10n.t(`Package \`${item.package}\` is not installed in the selected environment.`), + item.severity, + ); + d.code = { value: item.code, target: Uri.parse(`https://pypi.org/p/${item.package}`) }; + d.source = INSTALL_CHECKER_SOURCE; + return d; + }); + } catch { + diagnostics = []; + } + return diagnostics; +} + +export async function getInstalledPackagesDiagnostics( + interpreterPathService: IInterpreterPathService, + doc: TextDocument, +): Promise { + const interpreter = interpreterPathService.get(doc.uri); + const scriptPath = installedCheckScript(); + try { + traceInfo('Running installed packages checker: ', interpreter, scriptPath, doc.uri.fsPath); + const result = await plainExec(interpreter, [scriptPath, doc.uri.fsPath]); + traceVerbose('Installed packages check result:\n', result.stdout); + if (result.stderr) { + traceError('Installed packages check error:\n', result.stderr); + } + return parseDiagnostics(result.stdout); + } catch (ex) { + traceError('Error while getting installed packages check result:\n', ex); + } + return []; +} diff --git a/extensions/positron-python/src/client/pythonEnvironments/creation/createEnvButtonContext.ts b/extensions/positron-python/src/client/pythonEnvironments/creation/createEnvButtonContext.ts new file mode 100644 index 000000000000..4ce7d07ad69d --- /dev/null +++ b/extensions/positron-python/src/client/pythonEnvironments/creation/createEnvButtonContext.ts @@ -0,0 +1,22 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +import { IDisposableRegistry } from '../../common/types'; +import { executeCommand } from '../../common/vscodeApis/commandApis'; +import { getConfiguration, onDidChangeConfiguration } from '../../common/vscodeApis/workspaceApis'; + +async function setShowCreateEnvButtonContextKey(): Promise { + const config = getConfiguration('python'); + const showCreateEnvButton = config.get('createEnvironment.contentButton', 'show') === 'show'; + await executeCommand('setContext', 'showCreateEnvButton', showCreateEnvButton); +} + +export function registerCreateEnvironmentButtonFeatures(disposables: IDisposableRegistry): void { + disposables.push( + onDidChangeConfiguration(async () => { + await setShowCreateEnvButtonContextKey(); + }), + ); + + setShowCreateEnvButtonContextKey(); +} diff --git a/extensions/positron-python/src/client/pythonEnvironments/creation/installedPackagesDiagnostic.ts b/extensions/positron-python/src/client/pythonEnvironments/creation/installedPackagesDiagnostic.ts new file mode 100644 index 000000000000..a46a32ce8276 --- /dev/null +++ b/extensions/positron-python/src/client/pythonEnvironments/creation/installedPackagesDiagnostic.ts @@ -0,0 +1,88 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License + +import { Diagnostic, DiagnosticCollection, TextDocument, Uri } from 'vscode'; +import { IDisposableRegistry, IInterpreterPathService } from '../../common/types'; +import { executeCommand } from '../../common/vscodeApis/commandApis'; +import { createDiagnosticCollection, onDidChangeDiagnostics } from '../../common/vscodeApis/languageApis'; +import { getActiveTextEditor, onDidChangeActiveTextEditor } from '../../common/vscodeApis/windowApis'; +import { + getOpenTextDocuments, + onDidCloseTextDocument, + onDidOpenTextDocument, + onDidSaveTextDocument, +} from '../../common/vscodeApis/workspaceApis'; +import { traceVerbose } from '../../logging'; +import { getInstalledPackagesDiagnostics, INSTALL_CHECKER_SOURCE } from './common/installCheckUtils'; + +export const DEPS_NOT_INSTALLED_KEY = 'pythonDepsNotInstalled'; + +async function setContextForActiveEditor(diagnosticCollection: DiagnosticCollection): Promise { + const doc = getActiveTextEditor()?.document; + if (doc && (doc.languageId === 'pip-requirements' || doc.fileName.endsWith('pyproject.toml'))) { + const diagnostics = diagnosticCollection.get(doc.uri); + if (diagnostics && diagnostics.length > 0) { + traceVerbose(`Setting context for python dependencies not installed: ${doc.uri.fsPath}`); + await executeCommand('setContext', DEPS_NOT_INSTALLED_KEY, true); + return; + } + } + + // undefined here in the logs means no file was selected + traceVerbose(`Clearing context for python dependencies not installed: ${doc?.uri.fsPath}`); + await executeCommand('setContext', DEPS_NOT_INSTALLED_KEY, false); +} + +export function registerInstalledPackagesDiagnosticsProvider( + disposables: IDisposableRegistry, + interpreterPathService: IInterpreterPathService, +): void { + const diagnosticCollection = createDiagnosticCollection(INSTALL_CHECKER_SOURCE); + const updateDiagnostics = (uri: Uri, diagnostics: Diagnostic[]) => { + if (diagnostics.length > 0) { + diagnosticCollection.set(uri, diagnostics); + } else if (diagnosticCollection.has(uri)) { + diagnosticCollection.delete(uri); + } + }; + + disposables.push(diagnosticCollection); + disposables.push( + onDidOpenTextDocument(async (doc: TextDocument) => { + if (doc.languageId === 'pip-requirements' || doc.fileName.endsWith('pyproject.toml')) { + const diagnostics = await getInstalledPackagesDiagnostics(interpreterPathService, doc); + updateDiagnostics(doc.uri, diagnostics); + } + }), + onDidSaveTextDocument(async (doc: TextDocument) => { + if (doc.languageId === 'pip-requirements' || doc.fileName.endsWith('pyproject.toml')) { + const diagnostics = await getInstalledPackagesDiagnostics(interpreterPathService, doc); + updateDiagnostics(doc.uri, diagnostics); + } + }), + onDidCloseTextDocument((e: TextDocument) => { + updateDiagnostics(e.uri, []); + }), + onDidChangeDiagnostics(async () => { + await setContextForActiveEditor(diagnosticCollection); + }), + onDidChangeActiveTextEditor(async () => { + await setContextForActiveEditor(diagnosticCollection); + }), + interpreterPathService.onDidChange(() => { + getOpenTextDocuments().forEach(async (doc: TextDocument) => { + if (doc.languageId === 'pip-requirements' || doc.fileName.endsWith('pyproject.toml')) { + const diagnostics = await getInstalledPackagesDiagnostics(interpreterPathService, doc); + updateDiagnostics(doc.uri, diagnostics); + } + }); + }), + ); + + getOpenTextDocuments().forEach(async (doc: TextDocument) => { + if (doc.languageId === 'pip-requirements' || doc.fileName.endsWith('pyproject.toml')) { + const diagnostics = await getInstalledPackagesDiagnostics(interpreterPathService, doc); + updateDiagnostics(doc.uri, diagnostics); + } + }); +} diff --git a/extensions/positron-python/src/client/pythonEnvironments/creation/provider/venvCreationProvider.ts b/extensions/positron-python/src/client/pythonEnvironments/creation/provider/venvCreationProvider.ts index e18f2fa79fde..b00682c3cb5d 100644 --- a/extensions/positron-python/src/client/pythonEnvironments/creation/provider/venvCreationProvider.ts +++ b/extensions/positron-python/src/client/pythonEnvironments/creation/provider/venvCreationProvider.ts @@ -162,7 +162,8 @@ export class VenvCreationProvider implements CreateEnvironmentProvider { EnvironmentType.System, EnvironmentType.MicrosoftStore, EnvironmentType.Global, - ].includes(i.envType), + EnvironmentType.Pyenv, + ].includes(i.envType) && i.type === undefined, // only global intepreters { skipRecommended: true, showBackButton: true, diff --git a/extensions/positron-python/src/client/pythonEnvironments/creation/pyprojectTomlCreateEnv.ts b/extensions/positron-python/src/client/pythonEnvironments/creation/pyProjectTomlContext.ts similarity index 58% rename from extensions/positron-python/src/client/pythonEnvironments/creation/pyprojectTomlCreateEnv.ts rename to extensions/positron-python/src/client/pythonEnvironments/creation/pyProjectTomlContext.ts index 5ead37b80dc9..5925b7641f45 100644 --- a/extensions/positron-python/src/client/pythonEnvironments/creation/pyprojectTomlCreateEnv.ts +++ b/extensions/positron-python/src/client/pythonEnvironments/creation/pyProjectTomlContext.ts @@ -1,12 +1,12 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. -import { TextDocument, TextDocumentChangeEvent } from 'vscode'; +import { TextDocument } from 'vscode'; import { IDisposableRegistry } from '../../common/types'; import { executeCommand } from '../../common/vscodeApis/commandApis'; import { onDidOpenTextDocument, - onDidChangeTextDocument, + onDidSaveTextDocument, getOpenTextDocuments, } from '../../common/vscodeApis/workspaceApis'; import { isPipInstallableToml } from './provider/venvUtils'; @@ -19,23 +19,26 @@ async function setPyProjectTomlContextKey(doc: TextDocument): Promise { } } -export function registerPyProjectTomlCreateEnvFeatures(disposables: IDisposableRegistry): void { +export function registerPyProjectTomlFeatures(disposables: IDisposableRegistry): void { disposables.push( onDidOpenTextDocument(async (doc: TextDocument) => { if (doc.fileName.endsWith('pyproject.toml')) { await setPyProjectTomlContextKey(doc); } }), - onDidChangeTextDocument(async (e: TextDocumentChangeEvent) => { - if (e.document.fileName.endsWith('pyproject.toml')) { - await setPyProjectTomlContextKey(e.document); + onDidSaveTextDocument(async (doc: TextDocument) => { + if (doc.fileName.endsWith('pyproject.toml')) { + await setPyProjectTomlContextKey(doc); } }), ); - getOpenTextDocuments().forEach(async (doc: TextDocument) => { - if (doc.fileName.endsWith('pyproject.toml')) { - await setPyProjectTomlContextKey(doc); - } - }); + const docs = getOpenTextDocuments().filter( + (doc) => doc.fileName.endsWith('pyproject.toml') && isPipInstallableToml(doc.getText()), + ); + if (docs.length > 0) { + executeCommand('setContext', 'pipInstallableToml', true); + } else { + executeCommand('setContext', 'pipInstallableToml', false); + } } diff --git a/extensions/positron-python/src/client/pythonEnvironments/creation/registrations.ts b/extensions/positron-python/src/client/pythonEnvironments/creation/registrations.ts new file mode 100644 index 000000000000..eeb04036bc1b --- /dev/null +++ b/extensions/positron-python/src/client/pythonEnvironments/creation/registrations.ts @@ -0,0 +1,21 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +import { IDisposableRegistry, IInterpreterPathService, IPathUtils } from '../../common/types'; +import { IInterpreterQuickPick } from '../../interpreter/configuration/types'; +import { registerCreateEnvironmentFeatures } from './createEnvApi'; +import { registerCreateEnvironmentButtonFeatures } from './createEnvButtonContext'; +import { registerInstalledPackagesDiagnosticsProvider } from './installedPackagesDiagnostic'; +import { registerPyProjectTomlFeatures } from './pyProjectTomlContext'; + +export function registerAllCreateEnvironmentFeatures( + disposables: IDisposableRegistry, + interpreterQuickPick: IInterpreterQuickPick, + interpreterPathService: IInterpreterPathService, + pathUtils: IPathUtils, +): void { + registerCreateEnvironmentFeatures(disposables, interpreterQuickPick, interpreterPathService, pathUtils); + registerCreateEnvironmentButtonFeatures(disposables); + registerPyProjectTomlFeatures(disposables); + registerInstalledPackagesDiagnosticsProvider(disposables, interpreterPathService); +} diff --git a/extensions/positron-python/src/client/pythonEnvironments/info/index.ts b/extensions/positron-python/src/client/pythonEnvironments/info/index.ts index 70abbb0fad76..60628f61314e 100644 --- a/extensions/positron-python/src/client/pythonEnvironments/info/index.ts +++ b/extensions/positron-python/src/client/pythonEnvironments/info/index.ts @@ -68,10 +68,11 @@ export type InterpreterInformation = { * * @prop companyDisplayName - the user-facing name of the distro publisher * @prop displayName - the user-facing name for the environment - * @prop type - the kind of Python environment + * @prop envType - the kind of Python environment * @prop envName - the environment's name, if applicable (else `envPath` is set) * @prop envPath - the environment's root dir, if applicable (else `envName`) * @prop cachedEntry - whether or not the info came from a cache + * @prop type - the type of Python environment, if applicable */ // Note that "cachedEntry" is specific to the caching machinery // and doesn't really belong here. @@ -84,6 +85,7 @@ export type PythonEnvironment = InterpreterInformation & { envName?: string; envPath?: string; cachedEntry?: boolean; + type?: string; }; /** diff --git a/extensions/positron-python/src/client/pythonEnvironments/legacyIOC.ts b/extensions/positron-python/src/client/pythonEnvironments/legacyIOC.ts index 0c80c3414728..f116bdb63a89 100644 --- a/extensions/positron-python/src/client/pythonEnvironments/legacyIOC.ts +++ b/extensions/positron-python/src/client/pythonEnvironments/legacyIOC.ts @@ -75,6 +75,7 @@ function convertEnvInfo(info: PythonEnvInfo): PythonEnvironment { } env.displayName = info.display; env.detailedDisplayName = info.detailedDisplayName; + env.type = info.type; // We do not worry about using distro.defaultDisplayName. return env; diff --git a/extensions/positron-python/src/client/startupTelemetry.ts b/extensions/positron-python/src/client/startupTelemetry.ts index 8a123d6bee36..f4d3fc254b67 100644 --- a/extensions/positron-python/src/client/startupTelemetry.ts +++ b/extensions/positron-python/src/client/startupTelemetry.ts @@ -89,7 +89,9 @@ async function getActivationTelemetryProps(serviceContainer: IServiceContainer): return { workspaceFolderCount, terminal: terminalShellType }; } const interpreterService = serviceContainer.get(IInterpreterService); - const mainWorkspaceUri = workspaceService.workspaceFolders ? workspaceService.workspaceFolders[0].uri : undefined; + const mainWorkspaceUri = workspaceService.workspaceFolders?.length + ? workspaceService.workspaceFolders[0].uri + : undefined; const hasPythonThree = await interpreterService.hasInterpreters(async (item) => item.version?.major === 3); // If an unknown type environment can be found from windows registry or path env var, // consider them as global type instead of unknown. Such types can only be known after diff --git a/extensions/positron-python/src/client/telemetry/index.ts b/extensions/positron-python/src/client/telemetry/index.ts index 5dff35067196..d0b9d463c070 100644 --- a/extensions/positron-python/src/client/telemetry/index.ts +++ b/extensions/positron-python/src/client/telemetry/index.ts @@ -825,6 +825,12 @@ export interface IEventNamePropertyMapping { * @type {('command' | 'icon')} */ trigger?: 'command' | 'icon'; + /** + * Whether user chose to execute this Python file in a separate terminal or not. + * + * @type {boolean} + */ + newTerminalPerFile?: boolean; }; /** * Telemetry Event sent when user executes code against Django Shell. diff --git a/extensions/positron-python/src/client/terminals/codeExecution/codeExecutionManager.ts b/extensions/positron-python/src/client/terminals/codeExecution/codeExecutionManager.ts index ed671f2846a2..9f1ba6e90d90 100644 --- a/extensions/positron-python/src/client/terminals/codeExecution/codeExecutionManager.ts +++ b/extensions/positron-python/src/client/terminals/codeExecution/codeExecutionManager.ts @@ -36,25 +36,31 @@ export class CodeExecutionManager implements ICodeExecutionManager { } public registerCommands() { - [Commands.Exec_In_Terminal, Commands.Exec_In_Terminal_Icon].forEach((cmd) => { - this.disposableRegistry.push( - this.commandManager.registerCommand(cmd as any, async (file: Resource) => { - const interpreterService = this.serviceContainer.get(IInterpreterService); - const interpreter = await interpreterService.getActiveInterpreter(file); - if (!interpreter) { - this.commandManager.executeCommand(Commands.TriggerEnvironmentSelection, file).then(noop, noop); - return; - } - const trigger = cmd === Commands.Exec_In_Terminal ? 'command' : 'icon'; - await this.executeFileInTerminal(file, trigger) - .then(() => { - if (this.shouldTerminalFocusOnStart(file)) - this.commandManager.executeCommand('workbench.action.terminal.focus'); + [Commands.Exec_In_Terminal, Commands.Exec_In_Terminal_Icon, Commands.Exec_In_Separate_Terminal].forEach( + (cmd) => { + this.disposableRegistry.push( + this.commandManager.registerCommand(cmd as any, async (file: Resource) => { + const interpreterService = this.serviceContainer.get(IInterpreterService); + const interpreter = await interpreterService.getActiveInterpreter(file); + if (!interpreter) { + this.commandManager + .executeCommand(Commands.TriggerEnvironmentSelection, file) + .then(noop, noop); + return; + } + const trigger = cmd === Commands.Exec_In_Terminal ? 'command' : 'icon'; + await this.executeFileInTerminal(file, trigger, { + newTerminalPerFile: cmd === Commands.Exec_In_Separate_Terminal, }) - .catch((ex) => traceError('Failed to execute file in terminal', ex)); - }), - ); - }); + .then(() => { + if (this.shouldTerminalFocusOnStart(file)) + this.commandManager.executeCommand('workbench.action.terminal.focus'); + }) + .catch((ex) => traceError('Failed to execute file in terminal', ex)); + }), + ); + }, + ); this.disposableRegistry.push( this.commandManager.registerCommand(Commands.Exec_Selection_In_Terminal as any, async (file: Resource) => { const interpreterService = this.serviceContainer.get(IInterpreterService); @@ -87,8 +93,16 @@ export class CodeExecutionManager implements ICodeExecutionManager { ), ); } - private async executeFileInTerminal(file: Resource, trigger: 'command' | 'icon') { - sendTelemetryEvent(EventName.EXECUTION_CODE, undefined, { scope: 'file', trigger }); + private async executeFileInTerminal( + file: Resource, + trigger: 'command' | 'icon', + options?: { newTerminalPerFile: boolean }, + ): Promise { + sendTelemetryEvent(EventName.EXECUTION_CODE, undefined, { + scope: 'file', + trigger, + newTerminalPerFile: options?.newTerminalPerFile, + }); const codeExecutionHelper = this.serviceContainer.get(ICodeExecutionHelper); file = file instanceof Uri ? file : undefined; let fileToExecute = file ? file : await codeExecutionHelper.getFileToExecute(); @@ -110,7 +124,7 @@ export class CodeExecutionManager implements ICodeExecutionManager { } const executionService = this.serviceContainer.get(ICodeExecutionService, 'standard'); - await executionService.executeFile(fileToExecute); + await executionService.executeFile(fileToExecute, options); } @captureTelemetry(EventName.EXECUTION_CODE, { scope: 'selection' }, false) diff --git a/extensions/positron-python/src/client/terminals/codeExecution/helper.ts b/extensions/positron-python/src/client/terminals/codeExecution/helper.ts index eee7d91a0db2..0d5694b4a28d 100644 --- a/extensions/positron-python/src/client/terminals/codeExecution/helper.ts +++ b/extensions/positron-python/src/client/terminals/codeExecution/helper.ts @@ -5,7 +5,7 @@ import '../../common/extensions'; import { inject, injectable } from 'inversify'; import { l10n, Position, Range, TextEditor, Uri } from 'vscode'; -import { IApplicationShell, ICommandManager, IDocumentManager } from '../../common/application/types'; +import { IApplicationShell, IDocumentManager, IWorkspaceService } from '../../common/application/types'; import { PYTHON_LANGUAGE } from '../../common/constants'; import * as internalScripts from '../../common/process/internal/scripts'; import { IProcessServiceFactory } from '../../common/process/types'; @@ -122,13 +122,9 @@ export class CodeExecutionHelper implements ICodeExecutionHelper { public async saveFileIfDirty(file: Uri): Promise { const docs = this.documentManager.textDocuments.filter((d) => d.uri.path === file.path); - if (docs.length === 1 && docs[0].isDirty) { - const deferred = createDeferred(); - this.documentManager.onDidSaveTextDocument((e) => deferred.resolve(e.uri)); - const commandManager = this.serviceContainer.get(ICommandManager); - await commandManager.executeCommand('workbench.action.files.save', file); - const savedFileUri = await deferred.promise; - return savedFileUri; + if (docs.length === 1 && (docs[0].isDirty || docs[0].isUntitled)) { + const workspaceService = this.serviceContainer.get(IWorkspaceService); + return workspaceService.save(docs[0].uri); } return undefined; } diff --git a/extensions/positron-python/src/client/terminals/codeExecution/terminalCodeExecution.ts b/extensions/positron-python/src/client/terminals/codeExecution/terminalCodeExecution.ts index 9261483b45e1..ca7488530f75 100644 --- a/extensions/positron-python/src/client/terminals/codeExecution/terminalCodeExecution.ts +++ b/extensions/positron-python/src/client/terminals/codeExecution/terminalCodeExecution.ts @@ -19,7 +19,6 @@ import { ICodeExecutionService } from '../../terminals/types'; export class TerminalCodeExecutionProvider implements ICodeExecutionService { private hasRanOutsideCurrentDrive = false; protected terminalTitle!: string; - private _terminalService!: ITerminalService; private replActive?: Promise; constructor( @inject(ITerminalServiceFactory) protected readonly terminalServiceFactory: ITerminalServiceFactory, @@ -30,13 +29,13 @@ export class TerminalCodeExecutionProvider implements ICodeExecutionService { @inject(IInterpreterService) protected readonly interpreterService: IInterpreterService, ) {} - public async executeFile(file: Uri) { + public async executeFile(file: Uri, options?: { newTerminalPerFile: boolean }) { await this.setCwdForFileExecution(file); const { command, args } = await this.getExecuteFileArgs(file, [ file.fsPath.fileToCommandArgumentForPythonExt(), ]); - await this.getTerminalService(file).sendCommand(command, args); + await this.getTerminalService(file, options).sendCommand(command, args); } public async execute(code: string, resource?: Uri): Promise { @@ -48,17 +47,23 @@ export class TerminalCodeExecutionProvider implements ICodeExecutionService { await this.getTerminalService(resource).sendText(code); } public async initializeRepl(resource?: Uri) { + const terminalService = this.getTerminalService(resource); if (this.replActive && (await this.replActive)) { - await this._terminalService.show(); + await terminalService.show(); return; } this.replActive = new Promise(async (resolve) => { const replCommandArgs = await this.getExecutableInfo(resource); - await this.getTerminalService(resource).sendCommand(replCommandArgs.command, replCommandArgs.args); + terminalService.sendCommand(replCommandArgs.command, replCommandArgs.args); // Give python repl time to start before we start sending text. setTimeout(() => resolve(true), 1000); }); + this.disposables.push( + terminalService.onDidCloseTerminal(() => { + this.replActive = undefined; + }), + ); await this.replActive; } @@ -76,19 +81,12 @@ export class TerminalCodeExecutionProvider implements ICodeExecutionService { public async getExecuteFileArgs(resource?: Uri, executeArgs: string[] = []): Promise { return this.getExecutableInfo(resource, executeArgs); } - private getTerminalService(resource?: Uri): ITerminalService { - if (!this._terminalService) { - this._terminalService = this.terminalServiceFactory.getTerminalService({ - resource, - title: this.terminalTitle, - }); - this.disposables.push( - this._terminalService.onDidCloseTerminal(() => { - this.replActive = undefined; - }), - ); - } - return this._terminalService; + private getTerminalService(resource?: Uri, options?: { newTerminalPerFile: boolean }): ITerminalService { + return this.terminalServiceFactory.getTerminalService({ + resource, + title: this.terminalTitle, + newTerminalPerFile: options?.newTerminalPerFile, + }); } private async setCwdForFileExecution(file: Uri) { const pythonSettings = this.configurationService.getSettings(file); diff --git a/extensions/positron-python/src/client/terminals/types.ts b/extensions/positron-python/src/client/terminals/types.ts index cf31f4ef1dd0..47ac16d9e08b 100644 --- a/extensions/positron-python/src/client/terminals/types.ts +++ b/extensions/positron-python/src/client/terminals/types.ts @@ -8,7 +8,7 @@ export const ICodeExecutionService = Symbol('ICodeExecutionService'); export interface ICodeExecutionService { execute(code: string, resource?: Uri): Promise; - executeFile(file: Uri): Promise; + executeFile(file: Uri, options?: { newTerminalPerFile: boolean }): Promise; initializeRepl(resource?: Uri): Promise; } diff --git a/extensions/positron-python/src/client/testing/common/debugLauncher.ts b/extensions/positron-python/src/client/testing/common/debugLauncher.ts index 36432c0bd831..f7a027ad1304 100644 --- a/extensions/positron-python/src/client/testing/common/debugLauncher.ts +++ b/extensions/positron-python/src/client/testing/common/debugLauncher.ts @@ -15,6 +15,8 @@ import { ITestDebugLauncher, LaunchOptions } from './types'; import { getConfigurationsForWorkspace } from '../../debugger/extension/configuration/launch.json/launchJsonReader'; import { getWorkspaceFolder, getWorkspaceFolders } from '../../common/vscodeApis/workspaceApis'; import { showErrorMessage } from '../../common/vscodeApis/windowApis'; +import { createDeferred } from '../../common/utils/async'; +import { pythonTestAdapterRewriteEnabled } from '../testController/common/utils'; @injectable() export class DebugLauncher implements ITestDebugLauncher { @@ -29,7 +31,7 @@ export class DebugLauncher implements ITestDebugLauncher { this.configService = this.serviceContainer.get(IConfigurationService); } - public async launchDebugger(options: LaunchOptions): Promise { + public async launchDebugger(options: LaunchOptions, callback?: () => void): Promise { if (options.token && options.token.isCancellationRequested) { return undefined; } @@ -42,16 +44,13 @@ export class DebugLauncher implements ITestDebugLauncher { ); const debugManager = this.serviceContainer.get(IDebugService); - return debugManager.startDebugging(workspaceFolder, launchArgs).then( - // Wait for debug session to be complete. - () => - new Promise((resolve) => { - debugManager.onDidTerminateDebugSession(() => { - resolve(); - }); - }), - (ex) => traceError('Failed to start debugging tests', ex), - ); + const deferred = createDeferred(); + debugManager.onDidTerminateDebugSession(() => { + deferred.resolve(); + callback?.(); + }); + debugManager.startDebugging(workspaceFolder, launchArgs); + return deferred.promise; } private static resolveWorkspaceFolder(cwd: string): WorkspaceFolder { @@ -90,6 +89,7 @@ export class DebugLauncher implements ITestDebugLauncher { path: path.join(EXTENSION_ROOT_DIR, 'pythonFiles'), include: false, }); + DebugLauncher.applyDefaults(debugConfig!, workspaceFolder, configSettings); return this.convertConfigToArgs(debugConfig!, workspaceFolder, options); @@ -174,13 +174,15 @@ export class DebugLauncher implements ITestDebugLauncher { workspaceFolder: WorkspaceFolder, options: LaunchOptions, ): Promise { + const pythonTestAdapterRewriteExperiment = pythonTestAdapterRewriteEnabled(this.serviceContainer); const configArgs = debugConfig as LaunchRequestArguments; const testArgs = options.testProvider === 'unittest' ? options.args.filter((item) => item !== '--debug') : options.args; - const script = DebugLauncher.getTestLauncherScript(options.testProvider); + const script = DebugLauncher.getTestLauncherScript(options.testProvider, pythonTestAdapterRewriteExperiment); const args = script(testArgs); const [program] = args; configArgs.program = program; + configArgs.args = args.slice(1); // We leave configArgs.request as "test" so it will be sent in telemetry. @@ -202,6 +204,29 @@ export class DebugLauncher implements ITestDebugLauncher { } launchArgs.request = 'launch'; + // Both types of tests need to have the port for the test result server. + if (options.runTestIdsPort) { + launchArgs.env = { + ...launchArgs.env, + RUN_TEST_IDS_PORT: options.runTestIdsPort, + }; + } + if (options.testProvider === 'pytest' && pythonTestAdapterRewriteExperiment) { + if (options.pytestPort && options.pytestUUID) { + launchArgs.env = { + ...launchArgs.env, + TEST_PORT: options.pytestPort, + TEST_UUID: options.pytestUUID, + }; + } else { + throw Error( + `Missing value for debug setup, both port and uuid need to be defined. port: "${options.pytestPort}" uuid: "${options.pytestUUID}"`, + ); + } + const pluginPath = path.join(EXTENSION_ROOT_DIR, 'pythonFiles'); + launchArgs.env.PYTHONPATH = pluginPath; + } + // Clear out purpose so we can detect if the configuration was used to // run via F5 style debugging. launchArgs.purpose = []; @@ -209,14 +234,19 @@ export class DebugLauncher implements ITestDebugLauncher { return launchArgs; } - private static getTestLauncherScript(testProvider: TestProvider) { + private static getTestLauncherScript(testProvider: TestProvider, pythonTestAdapterRewriteExperiment?: boolean) { switch (testProvider) { case 'unittest': { + if (pythonTestAdapterRewriteExperiment) { + return internalScripts.execution_py_testlauncher; // this is the new way to run unittest execution, debugger + } return internalScripts.visualstudio_py_testlauncher; // old way unittest execution, debugger - // return internalScripts.execution_py_testlauncher; // this is the new way to run unittest execution, debugger } case 'pytest': { - return internalScripts.testlauncher; + if (pythonTestAdapterRewriteExperiment) { + return internalScripts.pytestlauncher; // this is the new way to run pytest execution, debugger + } + return internalScripts.testlauncher; // old way pytest execution, debugger } default: { throw new Error(`Unknown test provider '${testProvider}'`); diff --git a/extensions/positron-python/src/client/testing/common/types.ts b/extensions/positron-python/src/client/testing/common/types.ts index b1476e74435f..29a6de7768cb 100644 --- a/extensions/positron-python/src/client/testing/common/types.ts +++ b/extensions/positron-python/src/client/testing/common/types.ts @@ -25,6 +25,9 @@ export type LaunchOptions = { testProvider: TestProvider; token?: CancellationToken; outChannel?: OutputChannel; + pytestPort?: string; + pytestUUID?: string; + runTestIdsPort?: string; }; export type ParserOptions = TestDiscoveryOptions; @@ -85,7 +88,7 @@ export interface ITestConfigurationManagerFactory { } export const ITestDebugLauncher = Symbol('ITestDebugLauncher'); export interface ITestDebugLauncher { - launchDebugger(options: LaunchOptions): Promise; + launchDebugger(options: LaunchOptions, callback?: () => void): Promise; } export const IUnitTestSocketServer = Symbol('IUnitTestSocketServer'); diff --git a/extensions/positron-python/src/client/testing/testController/common/server.ts b/extensions/positron-python/src/client/testing/testController/common/server.ts index 6849f0f8969a..6bd9bf348e20 100644 --- a/extensions/positron-python/src/client/testing/testController/common/server.ts +++ b/extensions/positron-python/src/client/testing/testController/common/server.ts @@ -9,7 +9,7 @@ import { IPythonExecutionFactory, SpawnOptions, } from '../../../common/process/types'; -import { traceLog } from '../../../logging'; +import { traceError, traceInfo, traceLog, traceVerbose } from '../../../logging'; import { DataReceivedEvent, ITestServer, TestCommandOptions } from './types'; import { ITestDebugLauncher, LaunchOptions } from '../../common/types'; import { UNITTEST_PROVIDER } from '../../common/constants'; @@ -26,27 +26,40 @@ export class PythonTestServer implements ITestServer, Disposable { constructor(private executionFactory: IPythonExecutionFactory, private debugLauncher: ITestDebugLauncher) { this.server = net.createServer((socket: net.Socket) => { + let buffer: Buffer = Buffer.alloc(0); // Buffer to accumulate received data socket.on('data', (data: Buffer) => { try { let rawData: string = data.toString(); - - while (rawData.length > 0) { - const rpcHeaders = jsonRPCHeaders(rawData); + buffer = Buffer.concat([buffer, data]); + while (buffer.length > 0) { + const rpcHeaders = jsonRPCHeaders(buffer.toString()); const uuid = rpcHeaders.headers.get(JSONRPC_UUID_HEADER); + const totalContentLength = rpcHeaders.headers.get('Content-Length'); + if (!uuid) { + traceError('On data received: Error occurred because payload UUID is undefined'); + this._onDataReceived.fire({ uuid: '', data: '' }); + return; + } + if (!this.uuids.includes(uuid)) { + traceError('On data received: Error occurred because the payload UUID is not recognized'); + this._onDataReceived.fire({ uuid: '', data: '' }); + return; + } rawData = rpcHeaders.remainingRawData; - if (uuid && this.uuids.includes(uuid)) { - const rpcContent = jsonRPCContent(rpcHeaders.headers, rawData); - rawData = rpcContent.remainingRawData; - this._onDataReceived.fire({ uuid, data: rpcContent.extractedJSON }); + const rpcContent = jsonRPCContent(rpcHeaders.headers, rawData); + const extractedData = rpcContent.extractedJSON; + if (extractedData.length === Number(totalContentLength)) { + // do not send until we have the full content + traceVerbose(`Received data from test server: ${extractedData}`); + this._onDataReceived.fire({ uuid, data: extractedData }); this.uuids = this.uuids.filter((u) => u !== uuid); + buffer = Buffer.alloc(0); } else { - traceLog(`Error processing test server request: uuid not found`); - this._onDataReceived.fire({ uuid: '', data: '' }); - return; + break; } } } catch (ex) { - traceLog(`Error processing test server request: ${ex} observe`); + traceError(`Error processing test server request: ${ex} observe`); this._onDataReceived.fire({ uuid: '', data: '' }); } }); @@ -93,15 +106,18 @@ export class PythonTestServer implements ITestServer, Disposable { return this._onDataReceived.event; } - async sendCommand(options: TestCommandOptions): Promise { + async sendCommand(options: TestCommandOptions, runTestIdPort?: string, callback?: () => void): Promise { const { uuid } = options; const spawnOptions: SpawnOptions = { token: options.token, cwd: options.cwd, throwOnStdErr: true, outputChannel: options.outChannel, + extraVariables: {}, }; + if (spawnOptions.extraVariables) spawnOptions.extraVariables.RUN_TEST_IDS_PORT = runTestIdPort; + const isRun = !options.testIds; // Create the Python environment in which to execute the command. const creationOptions: ExecutionFactoryCreateWithEnvironmentOptions = { allowEnvironmentFetchExceptions: false, @@ -112,23 +128,9 @@ export class PythonTestServer implements ITestServer, Disposable { // Add the generated UUID to the data to be sent (expecting to receive it back). // first check if we have testIds passed in (in case of execution) and // insert appropriate flag and test id array - let args = []; - if (options.testIds) { - args = [ - options.command.script, - '--port', - this.getPort().toString(), - '--uuid', - uuid, - '--testids', - ...options.testIds, - ].concat(options.command.args); - } else { - // if not case of execution, go with the normal args - args = [options.command.script, '--port', this.getPort().toString(), '--uuid', uuid].concat( - options.command.args, - ); - } + const args = [options.command.script, '--port', this.getPort().toString(), '--uuid', uuid].concat( + options.command.args, + ); if (options.outChannel) { options.outChannel.appendLine(`python ${args.join(' ')}`); @@ -141,10 +143,21 @@ export class PythonTestServer implements ITestServer, Disposable { args, token: options.token, testProvider: UNITTEST_PROVIDER, + runTestIdsPort: runTestIdPort, }; + traceInfo(`Running DEBUG unittest with arguments: ${args}\r\n`); - await this.debugLauncher!.launchDebugger(launchOptions); + await this.debugLauncher!.launchDebugger(launchOptions, () => { + callback?.(); + }); } else { + if (isRun) { + // This means it is running the test + traceInfo(`Running unittests with arguments: ${args}\r\n`); + } else { + // This means it is running discovery + traceLog(`Discovering unittest tests with arguments: ${args}\r\n`); + } await execService.exec(args, spawnOptions); } } catch (ex) { diff --git a/extensions/positron-python/src/client/testing/testController/common/types.ts b/extensions/positron-python/src/client/testing/testController/common/types.ts index 52c6c787040c..4307d7a3913f 100644 --- a/extensions/positron-python/src/client/testing/testController/common/types.ts +++ b/extensions/positron-python/src/client/testing/testController/common/types.ts @@ -12,7 +12,7 @@ import { Uri, WorkspaceFolder, } from 'vscode'; -import { TestDiscoveryOptions } from '../../common/types'; +import { ITestDebugLauncher, TestDiscoveryOptions } from '../../common/types'; import { IPythonExecutionFactory } from '../../../common/process/types'; export type TestRunInstanceOptions = TestRunOptions & { @@ -172,7 +172,7 @@ export type TestCommandOptionsPytest = { */ export interface ITestServer { readonly onDataReceived: Event; - sendCommand(options: TestCommandOptions): Promise; + sendCommand(options: TestCommandOptions, runTestIdsPort?: string, callback?: () => void): Promise; serverReady(): Promise; getPort(): number; createUUID(cwd: string): string; @@ -193,6 +193,7 @@ export interface ITestExecutionAdapter { testIds: string[], debugBool?: boolean, executionFactory?: IPythonExecutionFactory, + debugLauncher?: ITestDebugLauncher, ): Promise; } diff --git a/extensions/positron-python/src/client/testing/testController/common/utils.ts b/extensions/positron-python/src/client/testing/testController/common/utils.ts index e0bad383d695..1bf31e80e11a 100644 --- a/extensions/positron-python/src/client/testing/testController/common/utils.ts +++ b/extensions/positron-python/src/client/testing/testController/common/utils.ts @@ -1,5 +1,11 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. +import * as net from 'net'; +import { traceLog } from '../../../logging'; + +import { EnableTestAdapterRewrite } from '../../../common/experiments/groups'; +import { IExperimentService } from '../../../common/types'; +import { IServiceContainer } from '../../../ioc/types'; export function fixLogLines(content: string): string { const lines = content.split(/\r?\n/g); @@ -50,3 +56,44 @@ export function jsonRPCContent(headers: Map, rawData: string): I remainingRawData, }; } + +export function pythonTestAdapterRewriteEnabled(serviceContainer: IServiceContainer): boolean { + const experiment = serviceContainer.get(IExperimentService); + return experiment.inExperimentSync(EnableTestAdapterRewrite.experiment); +} + +export const startServer = (testIds: string): Promise => + new Promise((resolve, reject) => { + const server = net.createServer((socket: net.Socket) => { + // Convert the test_ids array to JSON + const testData = JSON.stringify(testIds); + + // Create the headers + const headers = [`Content-Length: ${Buffer.byteLength(testData)}`, 'Content-Type: application/json']; + + // Create the payload by concatenating the headers and the test data + const payload = `${headers.join('\r\n')}\r\n\r\n${testData}`; + + // Send the payload to the socket + socket.write(payload); + + // Handle socket events + socket.on('data', (data) => { + traceLog('Received data:', data.toString()); + }); + + socket.on('end', () => { + traceLog('Client disconnected'); + }); + }); + + server.listen(0, () => { + const { port } = server.address() as net.AddressInfo; + traceLog(`Server listening on port ${port}`); + resolve(port); + }); + + server.on('error', (error: Error) => { + reject(error); + }); + }); diff --git a/extensions/positron-python/src/client/testing/testController/controller.ts b/extensions/positron-python/src/client/testing/testController/controller.ts index fb176a30af88..0d3487855380 100644 --- a/extensions/positron-python/src/client/testing/testController/controller.ts +++ b/extensions/positron-python/src/client/testing/testController/controller.ts @@ -24,7 +24,7 @@ import { IConfigurationService, IDisposableRegistry, ITestOutputChannel, Resourc import { DelayedTrigger, IDelayedTrigger } from '../../common/utils/delayTrigger'; import { noop } from '../../common/utils/misc'; import { IInterpreterService } from '../../interpreter/contracts'; -import { traceError, traceVerbose } from '../../logging'; +import { traceError, traceInfo, traceVerbose } from '../../logging'; import { IEventNamePropertyMapping, sendTelemetryEvent } from '../../telemetry'; import { EventName } from '../../telemetry/constants'; import { PYTEST_PROVIDER, UNITTEST_PROVIDER } from '../common/constants'; @@ -44,6 +44,8 @@ import { PytestTestDiscoveryAdapter } from './pytest/pytestDiscoveryAdapter'; import { PytestTestExecutionAdapter } from './pytest/pytestExecutionAdapter'; import { WorkspaceTestAdapter } from './workspaceTestAdapter'; import { ITestDebugLauncher } from '../common/types'; +import { pythonTestAdapterRewriteEnabled } from './common/utils'; +import { IServiceContainer } from '../../ioc/types'; // Types gymnastics to make sure that sendTriggerTelemetry only accepts the correct types. type EventPropertyType = IEventNamePropertyMapping[EventName.UNITTEST_DISCOVERY_TRIGGER]; @@ -93,6 +95,7 @@ export class PythonTestController implements ITestController, IExtensionSingleAc @inject(IPythonExecutionFactory) private readonly pythonExecFactory: IPythonExecutionFactory, @inject(ITestDebugLauncher) private readonly debugLauncher: ITestDebugLauncher, @inject(ITestOutputChannel) private readonly testOutputChannel: ITestOutputChannel, + @inject(IServiceContainer) private readonly serviceContainer: IServiceContainer, ) { this.refreshCancellation = new CancellationTokenSource(); @@ -240,22 +243,19 @@ export class PythonTestController implements ITestController, IExtensionSingleAc this.refreshingStartedEvent.fire(); if (uri) { const settings = this.configSettings.getSettings(uri); - traceVerbose(`Testing: Refreshing test data for ${uri.fsPath}`); - const rewriteTestingEnabled = process.env.ENABLE_PYTHON_TESTING_REWRITE; + const workspace = this.workspaceService.getWorkspaceFolder(uri); + traceInfo(`Discover tests for workspace name: ${workspace?.name} - uri: ${uri.fsPath}`); + // Ensure we send test telemetry if it gets disabled again + this.sendTestDisabledTelemetry = true; + // ** experiment to roll out NEW test discovery mechanism if (settings.testing.pytestEnabled) { - // Ensure we send test telemetry if it gets disabled again - this.sendTestDisabledTelemetry = true; - if (rewriteTestingEnabled) { - // ** rewriteTestingEnabled set to true to use NEW test discovery mechanism - const workspace = this.workspaceService.getWorkspaceFolder(uri); - traceVerbose(`Discover tests for workspace name: ${workspace?.name} - uri: ${uri.fsPath}`); + if (pythonTestAdapterRewriteEnabled(this.serviceContainer)) { + traceInfo(`Running discovery for pytest using the new test adapter.`); const testAdapter = this.testAdapters.get(uri) || (this.testAdapters.values().next().value as WorkspaceTestAdapter); testAdapter.discoverTests( this.testController, this.refreshCancellation.token, - this.testAdapters.size > 1, - this.workspaceService.workspaceFile?.fsPath, this.pythonExecFactory, ); } else { @@ -263,19 +263,14 @@ export class PythonTestController implements ITestController, IExtensionSingleAc await this.pytest.refreshTestData(this.testController, uri, this.refreshCancellation.token); } } else if (settings.testing.unittestEnabled) { - // ** Ensure we send test telemetry if it gets disabled again - this.sendTestDisabledTelemetry = true; - if (rewriteTestingEnabled) { - // ** rewriteTestingEnabled set to true to use NEW test discovery mechanism - const workspace = this.workspaceService.getWorkspaceFolder(uri); - traceVerbose(`Discover tests for workspace name: ${workspace?.name} - uri: ${uri.fsPath}`); + if (pythonTestAdapterRewriteEnabled(this.serviceContainer)) { + traceInfo(`Running discovery for unittest using the new test adapter.`); const testAdapter = this.testAdapters.get(uri) || (this.testAdapters.values().next().value as WorkspaceTestAdapter); testAdapter.discoverTests( this.testController, this.refreshCancellation.token, - this.testAdapters.size > 1, - this.workspaceService.workspaceFile?.fsPath, + this.pythonExecFactory, ); } else { // else use OLD test discovery mechanism @@ -289,7 +284,6 @@ export class PythonTestController implements ITestController, IExtensionSingleAc // If we are here we may have to remove an existing node from the tree // This handles the case where user removes test settings. Which should remove the // tests for that particular case from the tree view - const workspace = this.workspaceService.getWorkspaceFolder(uri); if (workspace) { const toDelete: string[] = []; this.testController.items.forEach((i: TestItem) => { @@ -391,14 +385,13 @@ export class PythonTestController implements ITestController, IExtensionSingleAc const settings = this.configSettings.getSettings(workspace.uri); if (testItems.length > 0) { - const rewriteTestingEnabled = process.env.ENABLE_PYTHON_TESTING_REWRITE; if (settings.testing.pytestEnabled) { sendTelemetryEvent(EventName.UNITTEST_RUN, undefined, { tool: 'pytest', debugging: request.profile?.kind === TestRunProfileKind.Debug, }); - // ** rewriteTestingEnabled set to true to use NEW test discovery mechanism - if (rewriteTestingEnabled) { + // ** experiment to roll out NEW test discovery mechanism + if (pythonTestAdapterRewriteEnabled(this.serviceContainer)) { const testAdapter = this.testAdapters.get(workspace.uri) || (this.testAdapters.values().next().value as WorkspaceTestAdapter); @@ -409,6 +402,7 @@ export class PythonTestController implements ITestController, IExtensionSingleAc token, request.profile?.kind === TestRunProfileKind.Debug, this.pythonExecFactory, + this.debugLauncher, ); } return this.pytest.runTests( @@ -427,8 +421,8 @@ export class PythonTestController implements ITestController, IExtensionSingleAc tool: 'unittest', debugging: request.profile?.kind === TestRunProfileKind.Debug, }); - // ** rewriteTestingEnabled set to true to use NEW test discovery mechanism - if (rewriteTestingEnabled) { + // ** experiment to roll out NEW test discovery mechanism + if (pythonTestAdapterRewriteEnabled(this.serviceContainer)) { const testAdapter = this.testAdapters.get(workspace.uri) || (this.testAdapters.values().next().value as WorkspaceTestAdapter); @@ -438,6 +432,7 @@ export class PythonTestController implements ITestController, IExtensionSingleAc testItems, token, request.profile?.kind === TestRunProfileKind.Debug, + this.pythonExecFactory, ); } // below is old way of running unittest execution diff --git a/extensions/positron-python/src/client/testing/testController/pytest/pytestController.ts b/extensions/positron-python/src/client/testing/testController/pytest/pytestController.ts index 793170231210..997e3e29b7ec 100644 --- a/extensions/positron-python/src/client/testing/testController/pytest/pytestController.ts +++ b/extensions/positron-python/src/client/testing/testController/pytest/pytestController.ts @@ -285,19 +285,24 @@ export class PytestController implements ITestFrameworkController { public runTests(testRun: ITestRun, workspace: WorkspaceFolder, token: CancellationToken): Promise { const settings = this.configService.getSettings(workspace.uri); - return this.runner.runTests( - testRun, - { - workspaceFolder: workspace.uri, - cwd: - settings.testing.cwd && settings.testing.cwd.length > 0 - ? settings.testing.cwd - : workspace.uri.fsPath, - token, - args: settings.testing.pytestArgs, - }, - this.idToRawData, - ); + try { + return this.runner.runTests( + testRun, + { + workspaceFolder: workspace.uri, + cwd: + settings.testing.cwd && settings.testing.cwd.length > 0 + ? settings.testing.cwd + : workspace.uri.fsPath, + token, + args: settings.testing.pytestArgs, + }, + this.idToRawData, + ); + } catch (ex) { + sendTelemetryEvent(EventName.UNITTEST_RUN_ALL_FAILED, undefined); + throw new Error(`Failed to run tests: ${ex}`); + } } } diff --git a/extensions/positron-python/src/client/testing/testController/pytest/pytestDiscoveryAdapter.ts b/extensions/positron-python/src/client/testing/testController/pytest/pytestDiscoveryAdapter.ts index 792826f4c3a5..aeb920407cd2 100644 --- a/extensions/positron-python/src/client/testing/testController/pytest/pytestDiscoveryAdapter.ts +++ b/extensions/positron-python/src/client/testing/testController/pytest/pytestDiscoveryAdapter.ts @@ -10,7 +10,7 @@ import { import { IConfigurationService, ITestOutputChannel } from '../../../common/types'; import { createDeferred, Deferred } from '../../../common/utils/async'; import { EXTENSION_ROOT_DIR } from '../../../constants'; -import { traceVerbose } from '../../../logging'; +import { traceError, traceLog, traceVerbose } from '../../../logging'; import { DataReceivedEvent, DiscoveredTestPayload, ITestDiscoveryAdapter, ITestServer } from '../common/types'; /** @@ -80,11 +80,12 @@ export class PytestTestDiscoveryAdapter implements ITestDiscoveryAdapter { resource: uri, }; const execService = await executionFactory.createActivatedEnvironment(creationOptions); - execService - .exec(['-m', 'pytest', '-p', 'vscode_pytest', '--collect-only'].concat(pytestArgs), spawnOptions) - .catch((ex) => { - deferred.reject(ex as Error); - }); + const discoveryArgs = ['-m', 'pytest', '-p', 'vscode_pytest', '--collect-only'].concat(pytestArgs); + traceLog(`Discovering pytest tests with arguments: ${discoveryArgs.join(' ')}`); + execService.exec(discoveryArgs, spawnOptions).catch((ex) => { + traceError(`Error occurred while discovering tests: ${ex}`); + deferred.reject(ex as Error); + }); return deferred.promise; } } diff --git a/extensions/positron-python/src/client/testing/testController/pytest/pytestExecutionAdapter.ts b/extensions/positron-python/src/client/testing/testController/pytest/pytestExecutionAdapter.ts index d2cbd3151e6f..90704b5d67f4 100644 --- a/extensions/positron-python/src/client/testing/testController/pytest/pytestExecutionAdapter.ts +++ b/extensions/positron-python/src/client/testing/testController/pytest/pytestExecutionAdapter.ts @@ -3,22 +3,25 @@ import { Uri } from 'vscode'; import * as path from 'path'; +import * as net from 'net'; import { IConfigurationService, ITestOutputChannel } from '../../../common/types'; import { createDeferred, Deferred } from '../../../common/utils/async'; -import { traceVerbose } from '../../../logging'; +import { traceError, traceInfo, traceLog, traceVerbose } from '../../../logging'; import { DataReceivedEvent, ExecutionTestPayload, ITestExecutionAdapter, ITestServer } from '../common/types'; import { ExecutionFactoryCreateWithEnvironmentOptions, IPythonExecutionFactory, SpawnOptions, } from '../../../common/process/types'; -import { EXTENSION_ROOT_DIR } from '../../../constants'; import { removePositionalFoldersAndFiles } from './arguments'; +import { ITestDebugLauncher, LaunchOptions } from '../../common/types'; +import { PYTEST_PROVIDER } from '../../common/constants'; +import { EXTENSION_ROOT_DIR } from '../../../common/constants'; // eslint-disable-next-line @typescript-eslint/no-explicit-any -(global as any).EXTENSION_ROOT_DIR = EXTENSION_ROOT_DIR; +// (global as any).EXTENSION_ROOT_DIR = EXTENSION_ROOT_DIR; /** - * Wrapper Class for pytest test execution. This is where we call `runTestCommand`? + * Wrapper Class for pytest test execution.. */ export class PytestTestExecutionAdapter implements ITestExecutionAdapter { @@ -47,11 +50,11 @@ export class PytestTestExecutionAdapter implements ITestExecutionAdapter { testIds: string[], debugBool?: boolean, executionFactory?: IPythonExecutionFactory, + debugLauncher?: ITestDebugLauncher, ): Promise { - traceVerbose(uri, testIds, debugBool); if (executionFactory !== undefined) { // ** new version of run tests. - return this.runTestsNew(uri, testIds, debugBool, executionFactory); + return this.runTestsNew(uri, testIds, debugBool, executionFactory, debugLauncher); } // if executionFactory is undefined, we are using the old method signature of run tests. this.outputChannel.appendLine('Running tests.'); @@ -64,6 +67,7 @@ export class PytestTestExecutionAdapter implements ITestExecutionAdapter { testIds: string[], debugBool?: boolean, executionFactory?: IPythonExecutionFactory, + debugLauncher?: ITestDebugLauncher, ): Promise { const deferred = createDeferred(); const relativePathToPytest = 'pythonFiles'; @@ -86,6 +90,7 @@ export class PytestTestExecutionAdapter implements ITestExecutionAdapter { TEST_PORT: this.testServer.getPort().toString(), }, outputChannel: this.outputChannel, + stdinStr: testIds.toString(), }; // Create the Python environment in which to execute the command. @@ -110,14 +115,75 @@ export class PytestTestExecutionAdapter implements ITestExecutionAdapter { testArgs.push('--capture', 'no'); } - console.debug(`Running test with arguments: ${testArgs.join(' ')}\r\n`); - console.debug(`Current working directory: ${uri.fsPath}\r\n`); - - const argArray = ['-m', 'pytest', '-p', 'vscode_pytest'].concat(testArgs).concat(testIds); - console.debug('argArray', argArray); - execService?.exec(argArray, spawnOptions); + // create payload with testIds to send to run pytest script + const testData = JSON.stringify(testIds); + const headers = [`Content-Length: ${Buffer.byteLength(testData)}`, 'Content-Type: application/json']; + const payload = `${headers.join('\r\n')}\r\n\r\n${testData}`; + traceLog(`Running pytest execution for the following test ids: ${testIds}`); + + let pytestRunTestIdsPort: string | undefined; + const startServer = (): Promise => + new Promise((resolve, reject) => { + const server = net.createServer((socket: net.Socket) => { + socket.on('end', () => { + traceVerbose('Client disconnected for pytest test ids server'); + }); + }); + + server.listen(0, () => { + const { port } = server.address() as net.AddressInfo; + traceVerbose(`Server listening on port ${port} for pytest test ids server`); + resolve(port); + }); + + server.on('error', (error: Error) => { + traceError('Error starting server for pytest test ids server:', error); + reject(error); + }); + server.on('connection', (socket: net.Socket) => { + socket.write(payload); + traceVerbose('payload sent for pytest execution', payload); + }); + }); + + // Start the server and wait until it is listening + await startServer() + .then((assignedPort) => { + traceVerbose(`Server started for pytest test ids server and listening on port ${assignedPort}`); + pytestRunTestIdsPort = assignedPort.toString(); + if (spawnOptions.extraVariables) + spawnOptions.extraVariables.RUN_TEST_IDS_PORT = pytestRunTestIdsPort; + }) + .catch((error) => { + traceError('Error starting server for pytest test ids server:', error); + }); + + if (debugBool) { + const pytestPort = this.testServer.getPort().toString(); + const pytestUUID = uuid.toString(); + const launchOptions: LaunchOptions = { + cwd: uri.fsPath, + args: testArgs, + token: spawnOptions.token, + testProvider: PYTEST_PROVIDER, + pytestPort, + pytestUUID, + runTestIdsPort: pytestRunTestIdsPort, + }; + traceInfo(`Running DEBUG pytest with arguments: ${testArgs.join(' ')}\r\n`); + await debugLauncher!.launchDebugger(launchOptions, () => { + deferred.resolve(); + }); + } else { + // combine path to run script with run args + const scriptPath = path.join(fullPluginPath, 'vscode_pytest', 'run_pytest_script.py'); + const runArgs = [scriptPath, ...testArgs]; + traceInfo(`Running pytests with arguments: ${runArgs.join(' ')}\r\n`); + + await execService?.exec(runArgs, spawnOptions); + } } catch (ex) { - console.debug(`Error while running tests: ${testIds}\r\n${ex}\r\n\r\n`); + traceError(`Error while running tests: ${testIds}\r\n${ex}\r\n\r\n`); return Promise.reject(ex); } diff --git a/extensions/positron-python/src/client/testing/testController/unittest/testDiscoveryAdapter.ts b/extensions/positron-python/src/client/testing/testController/unittest/testDiscoveryAdapter.ts index 3f8ecb5797d3..9c565af78c08 100644 --- a/extensions/positron-python/src/client/testing/testController/unittest/testDiscoveryAdapter.ts +++ b/extensions/positron-python/src/client/testing/testController/unittest/testDiscoveryAdapter.ts @@ -14,6 +14,7 @@ import { TestCommandOptions, TestDiscoveryCommand, } from '../common/types'; +import { traceInfo } from '../../../logging'; /** * Wrapper class for unittest test discovery. This is where we call `runTestCommand`. @@ -61,6 +62,7 @@ export class UnittestTestDiscoveryAdapter implements ITestDiscoveryAdapter { // Send the test command to the server. // The server will fire an onDataReceived event once it gets a response. + traceInfo(`Sending discover unittest script to server.`); this.testServer.sendCommand(options); return deferred.promise; diff --git a/extensions/positron-python/src/client/testing/testController/unittest/testExecutionAdapter.ts b/extensions/positron-python/src/client/testing/testController/unittest/testExecutionAdapter.ts index b39e0cd29560..bf83c3c0feb1 100644 --- a/extensions/positron-python/src/client/testing/testController/unittest/testExecutionAdapter.ts +++ b/extensions/positron-python/src/client/testing/testController/unittest/testExecutionAdapter.ts @@ -3,6 +3,7 @@ import * as path from 'path'; import { Uri } from 'vscode'; +import * as net from 'net'; import { IConfigurationService, ITestOutputChannel } from '../../../common/types'; import { createDeferred, Deferred } from '../../../common/utils/async'; import { EXTENSION_ROOT_DIR } from '../../../constants'; @@ -14,6 +15,7 @@ import { TestCommandOptions, TestExecutionCommand, } from '../common/types'; +import { traceLog, traceError } from '../../../logging'; /** * Wrapper Class for unittest test execution. This is where we call `runTestCommand`? @@ -61,9 +63,49 @@ export class UnittestTestExecutionAdapter implements ITestExecutionAdapter { const deferred = createDeferred(); this.promiseMap.set(uuid, deferred); - // Send test command to server. - // Server fire onDataReceived event once it gets response. - this.testServer.sendCommand(options); + // create payload with testIds to send to run pytest script + const testData = JSON.stringify(testIds); + const headers = [`Content-Length: ${Buffer.byteLength(testData)}`, 'Content-Type: application/json']; + const payload = `${headers.join('\r\n')}\r\n\r\n${testData}`; + + let runTestIdsPort: string | undefined; + const startServer = (): Promise => + new Promise((resolve, reject) => { + const server = net.createServer((socket: net.Socket) => { + socket.on('end', () => { + traceLog('Client disconnected'); + }); + }); + + server.listen(0, () => { + const { port } = server.address() as net.AddressInfo; + traceLog(`Server listening on port ${port}`); + resolve(port); + }); + + server.on('error', (error: Error) => { + reject(error); + }); + server.on('connection', (socket: net.Socket) => { + socket.write(payload); + traceLog('payload sent', payload); + }); + }); + + // Start the server and wait until it is listening + await startServer() + .then((assignedPort) => { + traceLog(`Server started and listening on port ${assignedPort}`); + runTestIdsPort = assignedPort.toString(); + // Send test command to server. + // Server fire onDataReceived event once it gets response. + this.testServer.sendCommand(options, runTestIdsPort, () => { + deferred.resolve(); + }); + }) + .catch((error) => { + traceError('Error starting server:', error); + }); return deferred.promise; } diff --git a/extensions/positron-python/src/client/testing/testController/unittest/unittestController.ts b/extensions/positron-python/src/client/testing/testController/unittest/unittestController.ts index ee79103c4e3e..a795620f3ca0 100644 --- a/extensions/positron-python/src/client/testing/testController/unittest/unittestController.ts +++ b/extensions/positron-python/src/client/testing/testController/unittest/unittestController.ts @@ -251,20 +251,25 @@ export class UnittestController implements ITestFrameworkController { testController?: TestController, ): Promise { const settings = this.configService.getSettings(workspace.uri); - return this.runner.runTests( - testRun, - { - workspaceFolder: workspace.uri, - cwd: - settings.testing.cwd && settings.testing.cwd.length > 0 - ? settings.testing.cwd - : workspace.uri.fsPath, - token, - args: settings.testing.unittestArgs, - }, - this.idToRawData, - testController, - ); + try { + return this.runner.runTests( + testRun, + { + workspaceFolder: workspace.uri, + cwd: + settings.testing.cwd && settings.testing.cwd.length > 0 + ? settings.testing.cwd + : workspace.uri.fsPath, + token, + args: settings.testing.unittestArgs, + }, + this.idToRawData, + testController, + ); + } catch (ex) { + sendTelemetryEvent(EventName.UNITTEST_RUN_ALL_FAILED, undefined); + throw new Error(`Failed to run tests: ${ex}`); + } } } diff --git a/extensions/positron-python/src/client/testing/testController/workspaceTestAdapter.ts b/extensions/positron-python/src/client/testing/testController/workspaceTestAdapter.ts index 39efc67f7c7e..5cba6c193d3c 100644 --- a/extensions/positron-python/src/client/testing/testController/workspaceTestAdapter.ts +++ b/extensions/positron-python/src/client/testing/testController/workspaceTestAdapter.ts @@ -29,15 +29,10 @@ import { getTestCaseNodes, RunTestTag, } from './common/testItemUtilities'; -import { - DiscoveredTestItem, - DiscoveredTestNode, - DiscoveredTestType, - ITestDiscoveryAdapter, - ITestExecutionAdapter, -} from './common/types'; +import { DiscoveredTestItem, DiscoveredTestNode, ITestDiscoveryAdapter, ITestExecutionAdapter } from './common/types'; import { fixLogLines } from './common/utils'; import { IPythonExecutionFactory } from '../../common/process/types'; +import { ITestDebugLauncher } from '../common/types'; /** * This class exposes a test-provider-agnostic way of discovering tests. @@ -77,6 +72,7 @@ export class WorkspaceTestAdapter { token?: CancellationToken, debugBool?: boolean, executionFactory?: IPythonExecutionFactory, + debugLauncher?: ITestDebugLauncher, ): Promise { if (this.executing) { return this.executing.promise; @@ -87,7 +83,7 @@ export class WorkspaceTestAdapter { let rawTestExecData; const testCaseNodes: TestItem[] = []; - const testCaseIds: string[] = []; + const testCaseIdsSet = new Set(); try { // first fetch all the individual test Items that we necessarily want includes.forEach((t) => { @@ -99,19 +95,20 @@ export class WorkspaceTestAdapter { runInstance.started(node); // do the vscode ui test item start here before runtest const runId = this.vsIdToRunId.get(node.id); if (runId) { - testCaseIds.push(runId); + testCaseIdsSet.add(runId); } }); - + const testCaseIds = Array.from(testCaseIdsSet); // ** execution factory only defined for new rewrite way if (executionFactory !== undefined) { + traceVerbose('executionFactory defined'); rawTestExecData = await this.executionAdapter.runTests( this.workspaceUri, testCaseIds, debugBool, executionFactory, + debugLauncher, ); - traceVerbose('executionFactory defined'); } else { rawTestExecData = await this.executionAdapter.runTests(this.workspaceUri, testCaseIds, debugBool); } @@ -285,8 +282,6 @@ export class WorkspaceTestAdapter { public async discoverTests( testController: TestController, token?: CancellationToken, - isMultiroot?: boolean, - workspaceFilePath?: string, executionFactory?: IPythonExecutionFactory, ): Promise { sendTelemetryEvent(EventName.UNITTEST_DISCOVERING, undefined, { tool: this.testProvider }); @@ -305,6 +300,7 @@ export class WorkspaceTestAdapter { try { // ** execution factory only defined for new rewrite way if (executionFactory !== undefined) { + traceVerbose('executionFactory defined'); rawTestData = await this.discoveryAdapter.discoverTests(this.workspaceUri, executionFactory); } else { rawTestData = await this.discoveryAdapter.discoverTests(this.workspaceUri); @@ -345,12 +341,11 @@ export class WorkspaceTestAdapter { const testingErrorConst = this.testProvider === 'pytest' ? Testing.errorPytestDiscovery : Testing.errorUnittestDiscovery; const { errors } = rawTestData; - traceError(testingErrorConst, '\r\n', errors!.join('\r\n\r\n')); - + traceError(testingErrorConst, '\r\n', errors?.join('\r\n\r\n')); let errorNode = testController.items.get(`DiscoveryError:${workspacePath}`); const message = util.format( `${testingErrorConst} ${Testing.seePythonOutput}\r\n`, - errors!.join('\r\n\r\n'), + errors?.join('\r\n\r\n'), ); if (errorNode === undefined) { @@ -364,39 +359,6 @@ export class WorkspaceTestAdapter { // then parse and insert test data. testController.items.delete(`DiscoveryError:${workspacePath}`); - // Wrap the data under a root node named after the test provider. - const wrappedTests = rawTestData.tests; - - // If we are in a multiroot workspace scenario, wrap the current folder's test result in a tree under the overall root + the current folder name. - let rootPath = workspacePath; - let childrenRootPath = rootPath; - let childrenRootName = path.basename(rootPath); - - if (isMultiroot) { - rootPath = workspaceFilePath!; - childrenRootPath = workspacePath; - childrenRootName = path.basename(workspacePath); - } - - const children = [ - { - path: childrenRootPath, - name: childrenRootName, - type_: 'folder' as DiscoveredTestType, - id_: childrenRootPath, - children: wrappedTests ? [wrappedTests] : [], - }, - ]; - - // Update the raw test data with the wrapped data. - rawTestData.tests = { - path: rootPath, - name: this.testProvider, - type_: 'folder', - id_: rootPath, - children, - }; - if (rawTestData.tests) { // If the test root for this folder exists: Workspace refresh, update its children. // Otherwise, it is a freshly discovered workspace, and we need to create a new test root and populate the test tree. diff --git a/extensions/positron-python/src/test/activation/jedi/jediAnalysisOptions.unit.test.ts b/extensions/positron-python/src/test/activation/jedi/jediAnalysisOptions.unit.test.ts index 296d31ba2ddb..8104ed2730b0 100644 --- a/extensions/positron-python/src/test/activation/jedi/jediAnalysisOptions.unit.test.ts +++ b/extensions/positron-python/src/test/activation/jedi/jediAnalysisOptions.unit.test.ts @@ -64,7 +64,7 @@ suite('Jedi LSP - analysis Options', () => { expect(result.initializationOptions.markupKindPreferred).to.deep.equal('markdown'); expect(result.initializationOptions.completion.resolveEagerly).to.deep.equal(false); - expect(result.initializationOptions.completion.disableSnippets).to.deep.equal(false); + expect(result.initializationOptions.completion.disableSnippets).to.deep.equal(true); expect(result.initializationOptions.diagnostics.enable).to.deep.equal(true); expect(result.initializationOptions.diagnostics.didOpen).to.deep.equal(true); expect(result.initializationOptions.diagnostics.didSave).to.deep.equal(true); diff --git a/extensions/positron-python/src/test/common/configSettings.test.ts b/extensions/positron-python/src/test/common/configSettings.test.ts index 75c20f512bbe..8630835081e2 100644 --- a/extensions/positron-python/src/test/common/configSettings.test.ts +++ b/extensions/positron-python/src/test/common/configSettings.test.ts @@ -1,10 +1,10 @@ import * as assert from 'assert'; import * as path from 'path'; import * as vscode from 'vscode'; -import { IS_WINDOWS } from '../../client/common/platform/constants'; import { SystemVariables } from '../../client/common/variables/systemVariables'; import { getExtensionSettings } from '../extensionSettings'; import { initialize } from './../initialize'; +import { isWindows } from '../../client/common/platform/platformService'; const workspaceRoot = path.join(__dirname, '..', '..', '..', 'src', 'test'); @@ -27,7 +27,7 @@ suite('Configuration Settings', () => { } const pythonSettingValue = (pythonSettings as any)[key] as string; - if (key.endsWith('Path') && IS_WINDOWS) { + if (key.endsWith('Path') && isWindows()) { assert.strictEqual( settingValue.toUpperCase(), pythonSettingValue.toUpperCase(), diff --git a/extensions/positron-python/src/test/common/terminals/factory.unit.test.ts b/extensions/positron-python/src/test/common/terminals/factory.unit.test.ts index ef6b7d8f5b0f..5ad2da8e793a 100644 --- a/extensions/positron-python/src/test/common/terminals/factory.unit.test.ts +++ b/extensions/positron-python/src/test/common/terminals/factory.unit.test.ts @@ -105,7 +105,7 @@ suite('Terminal Service Factory', () => { expect(notSameAsThirdInstance).to.not.equal(true, 'Instances are the same'); }); - test('Ensure same terminal is returned when using resources from the same workspace', () => { + test('Ensure same terminal is returned when using different resources from the same workspace', () => { const file1A = Uri.file('1a'); const file2A = Uri.file('2a'); const fileB = Uri.file('b'); @@ -140,4 +140,49 @@ suite('Terminal Service Factory', () => { 'Instances should be different for different workspaces', ); }); + + test('When `newTerminalPerFile` is true, ensure different terminal is returned when using different resources from the same workspace', () => { + const file1A = Uri.file('1a'); + const file2A = Uri.file('2a'); + const fileB = Uri.file('b'); + const workspaceUriA = Uri.file('A'); + const workspaceUriB = Uri.file('B'); + const workspaceFolderA = TypeMoq.Mock.ofType(); + workspaceFolderA.setup((w) => w.uri).returns(() => workspaceUriA); + const workspaceFolderB = TypeMoq.Mock.ofType(); + workspaceFolderB.setup((w) => w.uri).returns(() => workspaceUriB); + + workspaceService + .setup((w) => w.getWorkspaceFolder(TypeMoq.It.isValue(file1A))) + .returns(() => workspaceFolderA.object); + workspaceService + .setup((w) => w.getWorkspaceFolder(TypeMoq.It.isValue(file2A))) + .returns(() => workspaceFolderA.object); + workspaceService + .setup((w) => w.getWorkspaceFolder(TypeMoq.It.isValue(fileB))) + .returns(() => workspaceFolderB.object); + + const terminalForFile1A = factory.getTerminalService({ + resource: file1A, + newTerminalPerFile: true, + }) as SynchronousTerminalService; + const terminalForFile2A = factory.getTerminalService({ + resource: file2A, + newTerminalPerFile: true, + }) as SynchronousTerminalService; + const terminalForFileB = factory.getTerminalService({ + resource: fileB, + newTerminalPerFile: true, + }) as SynchronousTerminalService; + + const terminalsAreSameForWorkspaceA = terminalForFile1A.terminalService === terminalForFile2A.terminalService; + expect(terminalsAreSameForWorkspaceA).to.equal(false, 'Instances are the same for Workspace A'); + + const terminalsForWorkspaceABAreDifferent = + terminalForFile1A.terminalService === terminalForFileB.terminalService; + expect(terminalsForWorkspaceABAreDifferent).to.equal( + false, + 'Instances should be different for different workspaces', + ); + }); }); diff --git a/extensions/positron-python/src/test/debugger/extension/configuration/providers/fastapiLaunch.unit.test.ts b/extensions/positron-python/src/test/debugger/extension/configuration/providers/fastapiLaunch.unit.test.ts index f6c20985e4da..80ce37167024 100644 --- a/extensions/positron-python/src/test/debugger/extension/configuration/providers/fastapiLaunch.unit.test.ts +++ b/extensions/positron-python/src/test/debugger/extension/configuration/providers/fastapiLaunch.unit.test.ts @@ -53,7 +53,7 @@ suite('Debugging - Configuration Provider FastAPI', () => { type: DebuggerTypeName, request: 'launch', module: 'uvicorn', - args: ['main:app'], + args: ['main:app', '--reload'], jinja: true, justMyCode: true, }; @@ -73,7 +73,7 @@ suite('Debugging - Configuration Provider FastAPI', () => { type: DebuggerTypeName, request: 'launch', module: 'uvicorn', - args: ['main:app'], + args: ['main:app', '--reload'], jinja: true, justMyCode: true, }; diff --git a/extensions/positron-python/src/test/debugger/extension/hooks/childProcessAttachHandler.unit.test.ts b/extensions/positron-python/src/test/debugger/extension/hooks/childProcessAttachHandler.unit.test.ts index ee9a59c8e6aa..b1053def2eba 100644 --- a/extensions/positron-python/src/test/debugger/extension/hooks/childProcessAttachHandler.unit.test.ts +++ b/extensions/positron-python/src/test/debugger/extension/hooks/childProcessAttachHandler.unit.test.ts @@ -9,6 +9,7 @@ import { ChildProcessAttachEventHandler } from '../../../../client/debugger/exte import { ChildProcessAttachService } from '../../../../client/debugger/extension/hooks/childProcessAttachService'; import { DebuggerEvents } from '../../../../client/debugger/extension/hooks/constants'; import { AttachRequestArguments } from '../../../../client/debugger/types'; +import { DebuggerTypeName } from '../../../../client/debugger/constants'; suite('Debug - Child Process', () => { test('Do not attach if the event is undefined', async () => { @@ -21,7 +22,15 @@ suite('Debug - Child Process', () => { const attachService = mock(ChildProcessAttachService); const handler = new ChildProcessAttachEventHandler(instance(attachService)); const body: any = {}; - const session: any = {}; + const session: any = { configuration: { type: DebuggerTypeName } }; + await handler.handleCustomEvent({ event: 'abc', body, session }); + verify(attachService.attach(body, session)).never(); + }); + test('Do not attach to child process if debugger type is different', async () => { + const attachService = mock(ChildProcessAttachService); + const handler = new ChildProcessAttachEventHandler(instance(attachService)); + const body: any = {}; + const session: any = { configuration: { type: 'other-type' } }; await handler.handleCustomEvent({ event: 'abc', body, session }); verify(attachService.attach(body, session)).never(); }); @@ -29,7 +38,7 @@ suite('Debug - Child Process', () => { const attachService = mock(ChildProcessAttachService); const handler = new ChildProcessAttachEventHandler(instance(attachService)); const body: any = {}; - const session: any = {}; + const session: any = { configuration: { type: DebuggerTypeName } }; await handler.handleCustomEvent({ event: DebuggerEvents.PtvsdAttachToSubprocess, body, session }); verify(attachService.attach(body, session)).never(); }); @@ -37,7 +46,7 @@ suite('Debug - Child Process', () => { const attachService = mock(ChildProcessAttachService); const handler = new ChildProcessAttachEventHandler(instance(attachService)); const body: any = {}; - const session: any = {}; + const session: any = { configuration: { type: DebuggerTypeName } }; await handler.handleCustomEvent({ event: DebuggerEvents.DebugpyAttachToSubprocess, body, session }); verify(attachService.attach(body, session)).never(); }); @@ -51,9 +60,11 @@ suite('Debug - Child Process', () => { port: 1234, subProcessId: 2, }; - const session: any = {}; + const session: any = { + configuration: { type: DebuggerTypeName }, + }; when(attachService.attach(body, session)).thenThrow(new Error('Kaboom')); - await handler.handleCustomEvent({ event: DebuggerEvents.DebugpyAttachToSubprocess, body, session: {} as any }); + await handler.handleCustomEvent({ event: DebuggerEvents.DebugpyAttachToSubprocess, body, session }); verify(attachService.attach(body, anything())).once(); const [, secondArg] = capture(attachService.attach).last(); expect(secondArg).to.deep.equal(session); diff --git a/extensions/positron-python/src/test/interpreters/activation/terminalEnvVarCollectionService.unit.test.ts b/extensions/positron-python/src/test/interpreters/activation/terminalEnvVarCollectionService.unit.test.ts index feecf63f5577..cb0b6b02f288 100644 --- a/extensions/positron-python/src/test/interpreters/activation/terminalEnvVarCollectionService.unit.test.ts +++ b/extensions/positron-python/src/test/interpreters/activation/terminalEnvVarCollectionService.unit.test.ts @@ -7,7 +7,13 @@ import * as sinon from 'sinon'; import { assert, expect } from 'chai'; import { cloneDeep } from 'lodash'; import { mock, instance, when, anything, verify, reset } from 'ts-mockito'; -import { EnvironmentVariableCollection, ProgressLocation, Uri, WorkspaceFolder } from 'vscode'; +import { + EnvironmentVariableCollection, + EnvironmentVariableScope, + ProgressLocation, + Uri, + WorkspaceFolder, +} from 'vscode'; import { IApplicationShell, IApplicationEnvironment, @@ -39,7 +45,10 @@ suite('Terminal Environment Variable Collection Service', () => { let context: IExtensionContext; let shell: IApplicationShell; let experimentService: IExperimentService; - let collection: EnvironmentVariableCollection; + let collection: EnvironmentVariableCollection & { + getScopedEnvironmentVariableCollection(scope: EnvironmentVariableScope): EnvironmentVariableCollection; + }; + let scopedCollection: EnvironmentVariableCollection; let applicationEnvironment: IApplicationEnvironment; let environmentActivationService: IEnvironmentActivationService; let workspaceService: IWorkspaceService; @@ -62,7 +71,13 @@ suite('Terminal Environment Variable Collection Service', () => { interpreterService = mock(); context = mock(); shell = mock(); - collection = mock(); + collection = mock< + EnvironmentVariableCollection & { + getScopedEnvironmentVariableCollection(scope: EnvironmentVariableScope): EnvironmentVariableCollection; + } + >(); + scopedCollection = mock(); + when(collection.getScopedEnvironmentVariableCollection(anything())).thenReturn(instance(scopedCollection)); when(context.environmentVariableCollection).thenReturn(instance(collection)); experimentService = mock(); when(experimentService.inExperimentSync(TerminalEnvVarActivation.experiment)).thenReturn(true); @@ -166,12 +181,12 @@ suite('Terminal Environment Variable Collection Service', () => { ).thenResolve(envVars); when(collection.replace(anything(), anything(), anything())).thenResolve(); - when(collection.delete(anything(), anything())).thenResolve(); + when(collection.delete(anything())).thenResolve(); await terminalEnvVarCollectionService._applyCollection(undefined, customShell); verify(collection.replace('CONDA_PREFIX', 'prefix/to/conda', anything())).once(); - verify(collection.delete('PATH', anything())).once(); + verify(collection.delete('PATH')).once(); }); test('Verify envs are not applied if env activation is disabled', async () => { @@ -187,7 +202,7 @@ suite('Terminal Environment Variable Collection Service', () => { ).thenResolve(envVars); when(collection.replace(anything(), anything(), anything())).thenResolve(); - when(collection.delete(anything(), anything())).thenResolve(); + when(collection.delete(anything())).thenResolve(); reset(configService); when(configService.getSettings(anything())).thenReturn(({ terminal: { activateEnvironment: false }, @@ -197,10 +212,10 @@ suite('Terminal Environment Variable Collection Service', () => { await terminalEnvVarCollectionService._applyCollection(undefined, customShell); verify(collection.replace('CONDA_PREFIX', 'prefix/to/conda', anything())).never(); - verify(collection.delete('PATH', anything())).never(); + verify(collection.delete('PATH')).never(); }); - test('Verify correct scope is used when applying envs and setting description', async () => { + test('Verify correct options are used when applying envs and setting description', async () => { const envVars: NodeJS.ProcessEnv = { CONDA_PREFIX: 'prefix/to/conda', ..._normCaseKeys(process.env) }; delete envVars.PATH; const resource = Uri.file('a'); @@ -214,25 +229,16 @@ suite('Terminal Environment Variable Collection Service', () => { environmentActivationService.getActivatedEnvironmentVariables(resource, undefined, undefined, customShell), ).thenResolve(envVars); - when(collection.replace(anything(), anything(), anything())).thenCall((_e, _v, scope) => { - assert.deepEqual(scope, { workspaceFolder }); - return Promise.resolve(); - }); - when(collection.delete(anything(), anything())).thenCall((_e, scope) => { - assert.deepEqual(scope, { workspaceFolder }); + when(scopedCollection.replace(anything(), anything(), anything())).thenCall((_e, _v, options) => { + assert.deepEqual(options, { applyAtShellIntegration: true }); return Promise.resolve(); }); - let description = ''; - when(collection.setDescription(anything(), anything())).thenCall((d, scope) => { - assert.deepEqual(scope, { workspaceFolder }); - description = d.value; - }); + when(scopedCollection.delete(anything())).thenResolve(); await terminalEnvVarCollectionService._applyCollection(resource, customShell); - verify(collection.replace('CONDA_PREFIX', 'prefix/to/conda', anything())).once(); - verify(collection.delete('PATH', anything())).once(); - expect(description).to.equal(`${Interpreters.activateTerminalDescription} \`${displayPath}\``); + verify(scopedCollection.replace('CONDA_PREFIX', 'prefix/to/conda', anything())).once(); + verify(scopedCollection.delete('PATH')).once(); }); test('Only relative changes to previously applied variables are applied to the collection', async () => { @@ -251,7 +257,7 @@ suite('Terminal Environment Variable Collection Service', () => { ).thenResolve(envVars); when(collection.replace(anything(), anything(), anything())).thenResolve(); - when(collection.delete(anything(), anything())).thenResolve(); + when(collection.delete(anything())).thenResolve(); await terminalEnvVarCollectionService._applyCollection(undefined, customShell); @@ -270,8 +276,8 @@ suite('Terminal Environment Variable Collection Service', () => { await terminalEnvVarCollectionService._applyCollection(undefined, customShell); - verify(collection.delete('CONDA_PREFIX', anything())).once(); - verify(collection.delete('RANDOM_VAR', anything())).once(); + verify(collection.delete('CONDA_PREFIX')).once(); + verify(collection.delete('RANDOM_VAR')).once(); }); test('If no activated variables are returned for custom shell, fallback to using default shell', async () => { @@ -294,12 +300,12 @@ suite('Terminal Environment Variable Collection Service', () => { ).thenResolve(envVars); when(collection.replace(anything(), anything(), anything())).thenResolve(); - when(collection.delete(anything(), anything())).thenResolve(); + when(collection.delete(anything())).thenResolve(); await terminalEnvVarCollectionService._applyCollection(undefined, customShell); verify(collection.replace('CONDA_PREFIX', 'prefix/to/conda', anything())).once(); - verify(collection.delete(anything(), anything())).never(); + verify(collection.delete(anything())).never(); }); test('If no activated variables are returned for default shell, clear collection', async () => { @@ -313,12 +319,10 @@ suite('Terminal Environment Variable Collection Service', () => { ).thenResolve(undefined); when(collection.replace(anything(), anything(), anything())).thenResolve(); - when(collection.delete(anything(), anything())).thenResolve(); - when(collection.setDescription(anything(), anything())).thenReturn(); + when(collection.delete(anything())).thenResolve(); await terminalEnvVarCollectionService._applyCollection(undefined, defaultShell?.shell); - verify(collection.clear(anything())).once(); - verify(collection.setDescription(anything(), anything())).never(); + verify(collection.clear()).once(); }); }); diff --git a/extensions/positron-python/src/test/pythonEnvironments/creation/common/installCheckUtils.unit.test.ts b/extensions/positron-python/src/test/pythonEnvironments/creation/common/installCheckUtils.unit.test.ts new file mode 100644 index 000000000000..de8e263fc3fe --- /dev/null +++ b/extensions/positron-python/src/test/pythonEnvironments/creation/common/installCheckUtils.unit.test.ts @@ -0,0 +1,65 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License + +import * as chaiAsPromised from 'chai-as-promised'; +import * as sinon from 'sinon'; +import * as typemoq from 'typemoq'; +import { assert, use as chaiUse } from 'chai'; +import { Diagnostic, TextDocument, Range, Uri } from 'vscode'; +import * as rawProcessApis from '../../../../client/common/process/rawProcessApis'; +import { getInstalledPackagesDiagnostics } from '../../../../client/pythonEnvironments/creation/common/installCheckUtils'; +import { IInterpreterPathService } from '../../../../client/common/types'; + +chaiUse(chaiAsPromised); + +function getSomeRequirementFile(): typemoq.IMock { + const someFilePath = 'requirements.txt'; + const someFile = typemoq.Mock.ofType(); + someFile.setup((p) => p.languageId).returns(() => 'pip-requirements'); + someFile.setup((p) => p.fileName).returns(() => someFilePath); + someFile.setup((p) => p.getText(typemoq.It.isAny())).returns(() => 'flake8-csv'); + return someFile; +} + +const MISSING_PACKAGES_STR = + '[{"line": 8, "character": 34, "endLine": 8, "endCharacter": 44, "package": "flake8-csv", "code": "not-installed", "severity": 3}]'; +const MISSING_PACKAGES: Diagnostic[] = [ + { + range: new Range(8, 34, 8, 44), + message: 'Package `flake8-csv` is not installed in the selected environment.', + source: 'Python-InstalledPackagesChecker', + code: { value: 'not-installed', target: Uri.parse(`https://pypi.org/p/flake8-csv`) }, + severity: 3, + relatedInformation: [], + }, +]; + +suite('Install check diagnostics tests', () => { + let plainExecStub: sinon.SinonStub; + let interpreterPathService: typemoq.IMock; + + setup(() => { + plainExecStub = sinon.stub(rawProcessApis, 'plainExec'); + interpreterPathService = typemoq.Mock.ofType(); + }); + + teardown(() => { + sinon.restore(); + }); + + test('Test parse diagnostics', async () => { + plainExecStub.resolves({ stdout: MISSING_PACKAGES_STR, stderr: '' }); + const someFile = getSomeRequirementFile(); + const result = await getInstalledPackagesDiagnostics(interpreterPathService.object, someFile.object); + + assert.deepStrictEqual(result, MISSING_PACKAGES); + }); + + test('Test parse empty diagnostics', async () => { + plainExecStub.resolves({ stdout: '', stderr: '' }); + const someFile = getSomeRequirementFile(); + const result = await getInstalledPackagesDiagnostics(interpreterPathService.object, someFile.object); + + assert.deepStrictEqual(result, []); + }); +}); diff --git a/extensions/positron-python/src/test/pythonEnvironments/creation/createEnvButtonContext.unit.test.ts b/extensions/positron-python/src/test/pythonEnvironments/creation/createEnvButtonContext.unit.test.ts new file mode 100644 index 000000000000..eec2d066aadb --- /dev/null +++ b/extensions/positron-python/src/test/pythonEnvironments/creation/createEnvButtonContext.unit.test.ts @@ -0,0 +1,105 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +import * as chaiAsPromised from 'chai-as-promised'; +import * as sinon from 'sinon'; +import * as typemoq from 'typemoq'; +import { assert, use as chaiUse } from 'chai'; +import { WorkspaceConfiguration } from 'vscode'; +import * as cmdApis from '../../../client/common/vscodeApis/commandApis'; +import * as workspaceApis from '../../../client/common/vscodeApis/workspaceApis'; +import { IDisposableRegistry } from '../../../client/common/types'; +import { registerCreateEnvironmentButtonFeatures } from '../../../client/pythonEnvironments/creation/createEnvButtonContext'; + +chaiUse(chaiAsPromised); + +class FakeDisposable { + public dispose() { + // Do nothing + } +} + +suite('Create Env content button settings tests', () => { + let executeCommandStub: sinon.SinonStub; + const disposables: IDisposableRegistry = []; + let onDidChangeConfigurationStub: sinon.SinonStub; + let getConfigurationStub: sinon.SinonStub; + let configMock: typemoq.IMock; + + setup(() => { + executeCommandStub = sinon.stub(cmdApis, 'executeCommand'); + getConfigurationStub = sinon.stub(workspaceApis, 'getConfiguration'); + onDidChangeConfigurationStub = sinon.stub(workspaceApis, 'onDidChangeConfiguration'); + onDidChangeConfigurationStub.returns(new FakeDisposable()); + + configMock = typemoq.Mock.ofType(); + configMock.setup((c) => c.get(typemoq.It.isAny(), typemoq.It.isAny())).returns(() => 'show'); + getConfigurationStub.returns(configMock.object); + }); + + teardown(() => { + sinon.restore(); + disposables.forEach((d) => d.dispose()); + }); + + test('python.createEnvironment.contentButton setting is set to "show", no files open', async () => { + registerCreateEnvironmentButtonFeatures(disposables); + + assert.ok(executeCommandStub.calledWithExactly('setContext', 'showCreateEnvButton', true)); + }); + + test('python.createEnvironment.contentButton setting is set to "hide", no files open', async () => { + configMock.reset(); + configMock.setup((c) => c.get(typemoq.It.isAny(), typemoq.It.isAny())).returns(() => 'hide'); + + registerCreateEnvironmentButtonFeatures(disposables); + + assert.ok(executeCommandStub.calledWithExactly('setContext', 'showCreateEnvButton', false)); + }); + + test('python.createEnvironment.contentButton setting changed from "hide" to "show"', async () => { + configMock.reset(); + configMock.setup((c) => c.get(typemoq.It.isAny(), typemoq.It.isAny())).returns(() => 'hide'); + + let handler: () => void = () => { + /* do nothing */ + }; + onDidChangeConfigurationStub.callsFake((callback) => { + handler = callback; + return new FakeDisposable(); + }); + + registerCreateEnvironmentButtonFeatures(disposables); + assert.ok(executeCommandStub.calledWithExactly('setContext', 'showCreateEnvButton', false)); + executeCommandStub.reset(); + + configMock.reset(); + configMock.setup((c) => c.get(typemoq.It.isAny(), typemoq.It.isAny())).returns(() => 'show'); + handler(); + + assert.ok(executeCommandStub.calledWithExactly('setContext', 'showCreateEnvButton', true)); + }); + + test('python.createEnvironment.contentButton setting changed from "show" to "hide"', async () => { + configMock.reset(); + configMock.setup((c) => c.get(typemoq.It.isAny(), typemoq.It.isAny())).returns(() => 'show'); + + let handler: () => void = () => { + /* do nothing */ + }; + onDidChangeConfigurationStub.callsFake((callback) => { + handler = callback; + return new FakeDisposable(); + }); + + registerCreateEnvironmentButtonFeatures(disposables); + assert.ok(executeCommandStub.calledWithExactly('setContext', 'showCreateEnvButton', true)); + executeCommandStub.reset(); + + configMock.reset(); + configMock.setup((c) => c.get(typemoq.It.isAny(), typemoq.It.isAny())).returns(() => 'hide'); + handler(); + + assert.ok(executeCommandStub.calledWithExactly('setContext', 'showCreateEnvButton', false)); + }); +}); diff --git a/extensions/positron-python/src/test/pythonEnvironments/creation/installedPackagesDiagnostics.unit.test.ts b/extensions/positron-python/src/test/pythonEnvironments/creation/installedPackagesDiagnostics.unit.test.ts new file mode 100644 index 000000000000..10fe06bba442 --- /dev/null +++ b/extensions/positron-python/src/test/pythonEnvironments/creation/installedPackagesDiagnostics.unit.test.ts @@ -0,0 +1,333 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +import * as chaiAsPromised from 'chai-as-promised'; +import * as sinon from 'sinon'; +import * as typemoq from 'typemoq'; +import { assert, use as chaiUse } from 'chai'; +import { Diagnostic, DiagnosticCollection, TextEditor, Range, Uri, TextDocument } from 'vscode'; +import * as cmdApis from '../../../client/common/vscodeApis/commandApis'; +import * as workspaceApis from '../../../client/common/vscodeApis/workspaceApis'; +import * as languageApis from '../../../client/common/vscodeApis/languageApis'; +import * as windowApis from '../../../client/common/vscodeApis/windowApis'; +import { IDisposableRegistry, IInterpreterPathService } from '../../../client/common/types'; +import * as installUtils from '../../../client/pythonEnvironments/creation/common/installCheckUtils'; +import { + DEPS_NOT_INSTALLED_KEY, + registerInstalledPackagesDiagnosticsProvider, +} from '../../../client/pythonEnvironments/creation/installedPackagesDiagnostic'; + +chaiUse(chaiAsPromised); + +class FakeDisposable { + public dispose() { + // Do nothing + } +} + +const MISSING_PACKAGES: Diagnostic[] = [ + { + range: new Range(8, 34, 8, 44), + message: 'Package `flake8-csv` is not installed in the selected environment.', + source: 'Python-InstalledPackagesChecker', + code: { value: 'not-installed', target: Uri.parse(`https://pypi.org/p/flake8-csv`) }, + severity: 3, + relatedInformation: [], + }, +]; + +function getSomeFile(): typemoq.IMock { + const someFilePath = 'something.py'; + const someFile = typemoq.Mock.ofType(); + someFile.setup((p) => p.fileName).returns(() => someFilePath); + someFile.setup((p) => p.getText(typemoq.It.isAny())).returns(() => 'print("Hello World")'); + return someFile; +} + +function getSomeRequirementFile(): typemoq.IMock { + const someFilePath = 'requirements.txt'; + const someFile = typemoq.Mock.ofType(); + someFile.setup((p) => p.languageId).returns(() => 'pip-requirements'); + someFile.setup((p) => p.fileName).returns(() => someFilePath); + someFile.setup((p) => p.getText(typemoq.It.isAny())).returns(() => 'flake8-csv'); + return someFile; +} + +function getPyProjectTomlFile(): typemoq.IMock { + const someFilePath = 'pyproject.toml'; + const someFile = typemoq.Mock.ofType(); + someFile.setup((p) => p.languageId).returns(() => 'toml'); + someFile.setup((p) => p.fileName).returns(() => someFilePath); + someFile + .setup((p) => p.getText(typemoq.It.isAny())) + .returns( + () => + '[build-system]\nrequires = ["flit_core >=3.2,<4"]\nbuild-backend = "flit_core.buildapi"\n\n[project]\nname = "something"\nversion = "2023.0.0"\nrequires-python = ">=3.7"\ndependencies = ["attrs>=21.3.0", "flake8-csv"]\n ', + ); + return someFile; +} + +function getSomeTomlFile(): typemoq.IMock { + const someFilePath = 'something.toml'; + const someFile = typemoq.Mock.ofType(); + someFile.setup((p) => p.languageId).returns(() => 'toml'); + someFile.setup((p) => p.fileName).returns(() => someFilePath); + someFile + .setup((p) => p.getText(typemoq.It.isAny())) + .returns( + () => + '[build-system]\nrequires = ["flit_core >=3.2,<4"]\nbuild-backend = "flit_core.buildapi"\n\n[something]\nname = "something"\nversion = "2023.0.0"\nrequires-python = ">=3.7"\ndependencies = ["attrs>=21.3.0", "flake8-csv"]\n ', + ); + return someFile; +} + +suite('Create Env content button settings tests', () => { + let executeCommandStub: sinon.SinonStub; + const disposables: IDisposableRegistry = []; + let getOpenTextDocumentsStub: sinon.SinonStub; + let onDidOpenTextDocumentStub: sinon.SinonStub; + let onDidSaveTextDocumentStub: sinon.SinonStub; + let onDidCloseTextDocumentStub: sinon.SinonStub; + let onDidChangeDiagnosticsStub: sinon.SinonStub; + let onDidChangeActiveTextEditorStub: sinon.SinonStub; + let createDiagnosticCollectionStub: sinon.SinonStub; + let diagnosticCollection: typemoq.IMock; + let getActiveTextEditorStub: sinon.SinonStub; + let textEditor: typemoq.IMock; + let getInstalledPackagesDiagnosticsStub: sinon.SinonStub; + let interpreterPathService: typemoq.IMock; + + setup(() => { + executeCommandStub = sinon.stub(cmdApis, 'executeCommand'); + + getOpenTextDocumentsStub = sinon.stub(workspaceApis, 'getOpenTextDocuments'); + getOpenTextDocumentsStub.returns([]); + + onDidOpenTextDocumentStub = sinon.stub(workspaceApis, 'onDidOpenTextDocument'); + onDidSaveTextDocumentStub = sinon.stub(workspaceApis, 'onDidSaveTextDocument'); + onDidCloseTextDocumentStub = sinon.stub(workspaceApis, 'onDidCloseTextDocument'); + onDidOpenTextDocumentStub.returns(new FakeDisposable()); + onDidSaveTextDocumentStub.returns(new FakeDisposable()); + onDidCloseTextDocumentStub.returns(new FakeDisposable()); + + onDidChangeDiagnosticsStub = sinon.stub(languageApis, 'onDidChangeDiagnostics'); + onDidChangeDiagnosticsStub.returns(new FakeDisposable()); + createDiagnosticCollectionStub = sinon.stub(languageApis, 'createDiagnosticCollection'); + diagnosticCollection = typemoq.Mock.ofType(); + diagnosticCollection.setup((d) => d.set(typemoq.It.isAny(), typemoq.It.isAny())).returns(() => undefined); + diagnosticCollection.setup((d) => d.clear()).returns(() => undefined); + diagnosticCollection.setup((d) => d.delete(typemoq.It.isAny())).returns(() => undefined); + diagnosticCollection.setup((d) => d.has(typemoq.It.isAny())).returns(() => false); + createDiagnosticCollectionStub.returns(diagnosticCollection.object); + + onDidChangeActiveTextEditorStub = sinon.stub(windowApis, 'onDidChangeActiveTextEditor'); + onDidChangeActiveTextEditorStub.returns(new FakeDisposable()); + getActiveTextEditorStub = sinon.stub(windowApis, 'getActiveTextEditor'); + textEditor = typemoq.Mock.ofType(); + getActiveTextEditorStub.returns(textEditor.object); + + getInstalledPackagesDiagnosticsStub = sinon.stub(installUtils, 'getInstalledPackagesDiagnostics'); + interpreterPathService = typemoq.Mock.ofType(); + interpreterPathService + .setup((i) => i.onDidChange(typemoq.It.isAny(), undefined, undefined)) + .returns(() => new FakeDisposable()); + }); + + teardown(() => { + sinon.restore(); + disposables.forEach((d) => d.dispose()); + }); + + test('Ensure nothing is run if there are no open documents', () => { + registerInstalledPackagesDiagnosticsProvider(disposables, interpreterPathService.object); + assert.ok(executeCommandStub.notCalled); + assert.ok(getInstalledPackagesDiagnosticsStub.notCalled); + }); + + test('Should not run packages check if opened files are not dep files', () => { + const someFile = getSomeFile(); + const someTomlFile = getSomeTomlFile(); + getOpenTextDocumentsStub.returns([someFile.object, someTomlFile.object]); + registerInstalledPackagesDiagnosticsProvider(disposables, interpreterPathService.object); + assert.ok(executeCommandStub.notCalled); + assert.ok(getInstalledPackagesDiagnosticsStub.notCalled); + }); + + test('Should run packages check if opened files are dep files', () => { + const reqFile = getSomeRequirementFile(); + const tomlFile = getPyProjectTomlFile(); + getOpenTextDocumentsStub.returns([reqFile.object, tomlFile.object]); + registerInstalledPackagesDiagnosticsProvider(disposables, interpreterPathService.object); + assert.ok(getInstalledPackagesDiagnosticsStub.calledTwice); + }); + + [getSomeRequirementFile().object, getPyProjectTomlFile().object].forEach((file) => { + test(`Should run packages check on open of a dep file: ${file.fileName}`, () => { + let handler: (doc: TextDocument) => void = () => { + /* do nothing */ + }; + onDidOpenTextDocumentStub.callsFake((callback) => { + handler = callback; + return new FakeDisposable(); + }); + + registerInstalledPackagesDiagnosticsProvider(disposables, interpreterPathService.object); + getInstalledPackagesDiagnosticsStub.reset(); + + getInstalledPackagesDiagnosticsStub.returns(Promise.resolve(MISSING_PACKAGES)); + + handler(file); + assert.ok(getInstalledPackagesDiagnosticsStub.calledOnce); + }); + + test(`Should run packages check on save of a dep file: ${file.fileName}`, () => { + let handler: (doc: TextDocument) => void = () => { + /* do nothing */ + }; + onDidSaveTextDocumentStub.callsFake((callback) => { + handler = callback; + return new FakeDisposable(); + }); + + registerInstalledPackagesDiagnosticsProvider(disposables, interpreterPathService.object); + getInstalledPackagesDiagnosticsStub.reset(); + + getInstalledPackagesDiagnosticsStub.returns(Promise.resolve(MISSING_PACKAGES)); + + handler(file); + assert.ok(getInstalledPackagesDiagnosticsStub.calledOnce); + }); + + test(`Should run packages check on close of a dep file: ${file.fileName}`, () => { + let handler: (doc: TextDocument) => void = () => { + /* do nothing */ + }; + onDidCloseTextDocumentStub.callsFake((callback) => { + handler = callback; + return new FakeDisposable(); + }); + + registerInstalledPackagesDiagnosticsProvider(disposables, interpreterPathService.object); + + diagnosticCollection.reset(); + diagnosticCollection.setup((d) => d.delete(typemoq.It.isAny())).verifiable(typemoq.Times.once()); + diagnosticCollection + .setup((d) => d.has(typemoq.It.isAny())) + .returns(() => true) + .verifiable(typemoq.Times.once()); + + handler(file); + diagnosticCollection.verifyAll(); + }); + + test(`Should trigger a context update on active editor switch to dep file: ${file.fileName}`, () => { + let handler: () => void = () => { + /* do nothing */ + }; + onDidChangeActiveTextEditorStub.callsFake((callback) => { + handler = callback; + return new FakeDisposable(); + }); + + registerInstalledPackagesDiagnosticsProvider(disposables, interpreterPathService.object); + + getActiveTextEditorStub.returns({ document: file }); + diagnosticCollection.setup((d) => d.get(typemoq.It.isAny())).returns(() => MISSING_PACKAGES); + + handler(); + assert.ok(executeCommandStub.calledOnceWithExactly('setContext', DEPS_NOT_INSTALLED_KEY, true)); + }); + + test(`Should trigger a context update to true on diagnostic change to dep file: ${file.fileName}`, () => { + let handler: () => void = () => { + /* do nothing */ + }; + onDidChangeDiagnosticsStub.callsFake((callback) => { + handler = callback; + return new FakeDisposable(); + }); + + registerInstalledPackagesDiagnosticsProvider(disposables, interpreterPathService.object); + + getActiveTextEditorStub.returns({ document: file }); + diagnosticCollection.setup((d) => d.get(typemoq.It.isAny())).returns(() => MISSING_PACKAGES); + + handler(); + assert.ok(executeCommandStub.calledOnceWithExactly('setContext', DEPS_NOT_INSTALLED_KEY, true)); + }); + }); + + [getSomeFile().object, getSomeTomlFile().object].forEach((file) => { + test(`Should not run packages check on open of a non dep file: ${file.fileName}`, () => { + let handler: (doc: TextDocument) => void = () => { + /* do nothing */ + }; + onDidOpenTextDocumentStub.callsFake((callback) => { + handler = callback; + return new FakeDisposable(); + }); + + registerInstalledPackagesDiagnosticsProvider(disposables, interpreterPathService.object); + getInstalledPackagesDiagnosticsStub.reset(); + + getInstalledPackagesDiagnosticsStub.returns(Promise.resolve(MISSING_PACKAGES)); + + handler(file); + assert.ok(getInstalledPackagesDiagnosticsStub.notCalled); + }); + + test(`Should not run packages check on save of a non dep file: ${file.fileName}`, () => { + let handler: (doc: TextDocument) => void = () => { + /* do nothing */ + }; + onDidSaveTextDocumentStub.callsFake((callback) => { + handler = callback; + return new FakeDisposable(); + }); + + registerInstalledPackagesDiagnosticsProvider(disposables, interpreterPathService.object); + getInstalledPackagesDiagnosticsStub.reset(); + + getInstalledPackagesDiagnosticsStub.returns(Promise.resolve(MISSING_PACKAGES)); + + handler(file); + assert.ok(getInstalledPackagesDiagnosticsStub.notCalled); + }); + + test(`Should trigger a context update on active editor switch to non-dep file: ${file.fileName}`, () => { + let handler: () => void = () => { + /* do nothing */ + }; + onDidChangeActiveTextEditorStub.callsFake((callback) => { + handler = callback; + return new FakeDisposable(); + }); + + registerInstalledPackagesDiagnosticsProvider(disposables, interpreterPathService.object); + + getActiveTextEditorStub.returns({ document: file }); + diagnosticCollection.setup((d) => d.get(typemoq.It.isAny())).returns(() => []); + + handler(); + assert.ok(executeCommandStub.calledOnceWithExactly('setContext', DEPS_NOT_INSTALLED_KEY, false)); + }); + + test(`Should trigger a context update to false on diagnostic change to non-dep file: ${file.fileName}`, () => { + let handler: () => void = () => { + /* do nothing */ + }; + onDidChangeDiagnosticsStub.callsFake((callback) => { + handler = callback; + return new FakeDisposable(); + }); + + registerInstalledPackagesDiagnosticsProvider(disposables, interpreterPathService.object); + + getActiveTextEditorStub.returns({ document: file }); + diagnosticCollection.setup((d) => d.get(typemoq.It.isAny())).returns(() => []); + + handler(); + assert.ok(executeCommandStub.calledOnceWithExactly('setContext', DEPS_NOT_INSTALLED_KEY, false)); + }); + }); +}); diff --git a/extensions/positron-python/src/test/pythonEnvironments/creation/pyprojectTomlCreateEnv.unit.test.ts b/extensions/positron-python/src/test/pythonEnvironments/creation/pyProjectTomlContext.unit.test.ts similarity index 61% rename from extensions/positron-python/src/test/pythonEnvironments/creation/pyprojectTomlCreateEnv.unit.test.ts rename to extensions/positron-python/src/test/pythonEnvironments/creation/pyProjectTomlContext.unit.test.ts index 3f19aa5775b3..7106ee64162f 100644 --- a/extensions/positron-python/src/test/pythonEnvironments/creation/pyprojectTomlCreateEnv.unit.test.ts +++ b/extensions/positron-python/src/test/pythonEnvironments/creation/pyProjectTomlContext.unit.test.ts @@ -6,11 +6,11 @@ import * as chaiAsPromised from 'chai-as-promised'; import * as sinon from 'sinon'; import * as typemoq from 'typemoq'; import { assert, use as chaiUse } from 'chai'; -import { TextDocument, TextDocumentChangeEvent } from 'vscode'; +import { TextDocument } from 'vscode'; import * as cmdApis from '../../../client/common/vscodeApis/commandApis'; import * as workspaceApis from '../../../client/common/vscodeApis/workspaceApis'; import { IDisposableRegistry } from '../../../client/common/types'; -import { registerPyProjectTomlCreateEnvFeatures } from '../../../client/pythonEnvironments/creation/pyprojectTomlCreateEnv'; +import { registerPyProjectTomlFeatures } from '../../../client/pythonEnvironments/creation/pyProjectTomlContext'; chaiUse(chaiAsPromised); @@ -56,16 +56,16 @@ suite('PyProject.toml Create Env Features', () => { const disposables: IDisposableRegistry = []; let getOpenTextDocumentsStub: sinon.SinonStub; let onDidOpenTextDocumentStub: sinon.SinonStub; - let onDidChangeTextDocumentStub: sinon.SinonStub; + let onDidSaveTextDocumentStub: sinon.SinonStub; setup(() => { executeCommandStub = sinon.stub(cmdApis, 'executeCommand'); getOpenTextDocumentsStub = sinon.stub(workspaceApis, 'getOpenTextDocuments'); onDidOpenTextDocumentStub = sinon.stub(workspaceApis, 'onDidOpenTextDocument'); - onDidChangeTextDocumentStub = sinon.stub(workspaceApis, 'onDidChangeTextDocument'); + onDidSaveTextDocumentStub = sinon.stub(workspaceApis, 'onDidSaveTextDocument'); onDidOpenTextDocumentStub.returns(new FakeDisposable()); - onDidChangeTextDocumentStub.returns(new FakeDisposable()); + onDidSaveTextDocumentStub.returns(new FakeDisposable()); }); teardown(() => { @@ -77,27 +77,27 @@ suite('PyProject.toml Create Env Features', () => { const pyprojectToml = getInstallableToml(); getOpenTextDocumentsStub.returns([pyprojectToml.object]); - registerPyProjectTomlCreateEnvFeatures(disposables); + registerPyProjectTomlFeatures(disposables); - assert.ok(executeCommandStub.calledOnceWithExactly('setContext', 'pipInstallableToml', true)); + assert.ok(executeCommandStub.calledWithExactly('setContext', 'pipInstallableToml', true)); }); test('Non installable pyproject.toml is already open in the editor on extension activate', async () => { const pyprojectToml = getNonInstallableToml(); getOpenTextDocumentsStub.returns([pyprojectToml.object]); - registerPyProjectTomlCreateEnvFeatures(disposables); + registerPyProjectTomlFeatures(disposables); - assert.ok(executeCommandStub.calledOnceWithExactly('setContext', 'pipInstallableToml', false)); + assert.ok(executeCommandStub.calledWithExactly('setContext', 'pipInstallableToml', false)); }); test('Some random file open in the editor on extension activate', async () => { const someFile = getSomeFile(); getOpenTextDocumentsStub.returns([someFile.object]); - registerPyProjectTomlCreateEnvFeatures(disposables); + registerPyProjectTomlFeatures(disposables); - assert.ok(executeCommandStub.notCalled); + assert.ok(executeCommandStub.calledWithExactly('setContext', 'pipInstallableToml', false)); }); test('Installable pyproject.toml is opened in the editor', async () => { @@ -113,10 +113,11 @@ suite('PyProject.toml Create Env Features', () => { const pyprojectToml = getInstallableToml(); - registerPyProjectTomlCreateEnvFeatures(disposables); - handler(pyprojectToml.object); + registerPyProjectTomlFeatures(disposables); + assert.ok(executeCommandStub.neverCalledWith('setContext', 'pipInstallableToml', true)); - assert.ok(executeCommandStub.calledOnceWithExactly('setContext', 'pipInstallableToml', true)); + handler(pyprojectToml.object); + assert.ok(executeCommandStub.calledWithExactly('setContext', 'pipInstallableToml', true)); }); test('Non Installable pyproject.toml is opened in the editor', async () => { @@ -132,10 +133,13 @@ suite('PyProject.toml Create Env Features', () => { const pyprojectToml = getNonInstallableToml(); - registerPyProjectTomlCreateEnvFeatures(disposables); + registerPyProjectTomlFeatures(disposables); + assert.ok(executeCommandStub.calledWithExactly('setContext', 'pipInstallableToml', false)); + executeCommandStub.reset(); + handler(pyprojectToml.object); - assert.ok(executeCommandStub.calledOnceWithExactly('setContext', 'pipInstallableToml', false)); + assert.ok(executeCommandStub.calledWithExactly('setContext', 'pipInstallableToml', false)); }); test('Some random file is opened in the editor', async () => { @@ -151,65 +155,111 @@ suite('PyProject.toml Create Env Features', () => { const someFile = getSomeFile(); - registerPyProjectTomlCreateEnvFeatures(disposables); + registerPyProjectTomlFeatures(disposables); + assert.ok(executeCommandStub.calledWithExactly('setContext', 'pipInstallableToml', false)); + executeCommandStub.reset(); + handler(someFile.object); - assert.ok(executeCommandStub.notCalled); + assert.ok(executeCommandStub.neverCalledWith('setContext', 'pipInstallableToml', false)); }); test('Installable pyproject.toml is changed', async () => { getOpenTextDocumentsStub.returns([]); - let handler: (d: TextDocumentChangeEvent) => void = () => { + let handler: (d: TextDocument) => void = () => { /* do nothing */ }; - onDidChangeTextDocumentStub.callsFake((callback) => { + onDidSaveTextDocumentStub.callsFake((callback) => { handler = callback; return new FakeDisposable(); }); const pyprojectToml = getInstallableToml(); - registerPyProjectTomlCreateEnvFeatures(disposables); - handler({ contentChanges: [], document: pyprojectToml.object, reason: undefined }); + registerPyProjectTomlFeatures(disposables); + assert.ok(executeCommandStub.calledWithExactly('setContext', 'pipInstallableToml', false)); - assert.ok(executeCommandStub.calledOnceWithExactly('setContext', 'pipInstallableToml', true)); + handler(pyprojectToml.object); + + assert.ok(executeCommandStub.calledWithExactly('setContext', 'pipInstallableToml', true)); }); test('Non Installable pyproject.toml is changed', async () => { getOpenTextDocumentsStub.returns([]); - let handler: (d: TextDocumentChangeEvent) => void = () => { + let handler: (d: TextDocument) => void = () => { /* do nothing */ }; - onDidChangeTextDocumentStub.callsFake((callback) => { + onDidSaveTextDocumentStub.callsFake((callback) => { handler = callback; return new FakeDisposable(); }); const pyprojectToml = getNonInstallableToml(); - registerPyProjectTomlCreateEnvFeatures(disposables); - handler({ contentChanges: [], document: pyprojectToml.object, reason: undefined }); + registerPyProjectTomlFeatures(disposables); + assert.ok(executeCommandStub.calledWithExactly('setContext', 'pipInstallableToml', false)); + executeCommandStub.reset(); + + handler(pyprojectToml.object); + + assert.ok(executeCommandStub.calledOnceWithExactly('setContext', 'pipInstallableToml', false)); + }); + + test('Non Installable pyproject.toml is changed to Installable', async () => { + getOpenTextDocumentsStub.returns([]); + + let openHandler: (doc: TextDocument) => void = () => { + /* do nothing */ + }; + onDidOpenTextDocumentStub.callsFake((callback) => { + openHandler = callback; + return new FakeDisposable(); + }); + let changeHandler: (d: TextDocument) => void = () => { + /* do nothing */ + }; + onDidSaveTextDocumentStub.callsFake((callback) => { + changeHandler = callback; + return new FakeDisposable(); + }); + + const nonInatallablePyprojectToml = getNonInstallableToml(); + const installablePyprojectToml = getInstallableToml(); + + registerPyProjectTomlFeatures(disposables); + assert.ok(executeCommandStub.calledWithExactly('setContext', 'pipInstallableToml', false)); + executeCommandStub.reset(); + + openHandler(nonInatallablePyprojectToml.object); assert.ok(executeCommandStub.calledOnceWithExactly('setContext', 'pipInstallableToml', false)); + executeCommandStub.reset(); + + changeHandler(installablePyprojectToml.object); + + assert.ok(executeCommandStub.calledOnceWithExactly('setContext', 'pipInstallableToml', true)); }); test('Some random file is changed', async () => { getOpenTextDocumentsStub.returns([]); - let handler: (d: TextDocumentChangeEvent) => void = () => { + let handler: (d: TextDocument) => void = () => { /* do nothing */ }; - onDidChangeTextDocumentStub.callsFake((callback) => { + onDidSaveTextDocumentStub.callsFake((callback) => { handler = callback; return new FakeDisposable(); }); const someFile = getSomeFile(); - registerPyProjectTomlCreateEnvFeatures(disposables); - handler({ contentChanges: [], document: someFile.object, reason: undefined }); + registerPyProjectTomlFeatures(disposables); + assert.ok(executeCommandStub.calledWithExactly('setContext', 'pipInstallableToml', false)); + executeCommandStub.reset(); + + handler(someFile.object); assert.ok(executeCommandStub.notCalled); }); diff --git a/extensions/positron-python/src/test/serviceRegistry.ts b/extensions/positron-python/src/test/serviceRegistry.ts index 1b8a9d78d580..c20a84b1e25a 100644 --- a/extensions/positron-python/src/test/serviceRegistry.ts +++ b/extensions/positron-python/src/test/serviceRegistry.ts @@ -5,10 +5,9 @@ import { Container } from 'inversify'; import { anything, instance, mock, when } from 'ts-mockito'; import * as TypeMoq from 'typemoq'; import { Disposable, Memento } from 'vscode'; -import { IS_WINDOWS } from '../client/common/platform/constants'; import { FileSystem } from '../client/common/platform/fileSystem'; import { PathUtils } from '../client/common/platform/pathUtils'; -import { PlatformService } from '../client/common/platform/platformService'; +import { PlatformService, isWindows } from '../client/common/platform/platformService'; import { RegistryImplementation } from '../client/common/platform/registry'; import { registerTypes as platformRegisterTypes } from '../client/common/platform/serviceRegistry'; import { IFileSystem, IPlatformService, IRegistry } from '../client/common/platform/types'; @@ -195,7 +194,7 @@ export class IocContainer { } public registerMockProcess(): void { - this.serviceManager.addSingletonInstance(IsWindows, IS_WINDOWS); + this.serviceManager.addSingletonInstance(IsWindows, isWindows()); this.serviceManager.addSingleton(IPathUtils, PathUtils); this.serviceManager.addSingleton(ICurrentProcess, MockProcess); diff --git a/extensions/positron-python/src/test/terminals/codeExecution/codeExecutionManager.unit.test.ts b/extensions/positron-python/src/test/terminals/codeExecution/codeExecutionManager.unit.test.ts index 3676834873a0..30f95c94d217 100644 --- a/extensions/positron-python/src/test/terminals/codeExecution/codeExecutionManager.unit.test.ts +++ b/extensions/positron-python/src/test/terminals/codeExecution/codeExecutionManager.unit.test.ts @@ -77,12 +77,15 @@ suite('Terminal - Code Execution Manager', () => { executionManager.registerCommands(); const sorted = registered.sort(); - expect(sorted).to.deep.equal([ - Commands.Exec_In_Terminal, - Commands.Exec_In_Terminal_Icon, - Commands.Exec_Selection_In_Django_Shell, - Commands.Exec_Selection_In_Terminal, - ]); + expect(sorted).to.deep.equal( + [ + Commands.Exec_In_Separate_Terminal, + Commands.Exec_In_Terminal, + Commands.Exec_In_Terminal_Icon, + Commands.Exec_Selection_In_Django_Shell, + Commands.Exec_Selection_In_Terminal, + ].sort(), + ); }); test('Ensure executeFileInterTerminal will do nothing if no file is avialble', async () => { @@ -135,7 +138,10 @@ suite('Terminal - Code Execution Manager', () => { const fileToExecute = Uri.file('x'); await commandHandler!(fileToExecute); helper.verify(async (h) => h.getFileToExecute(), TypeMoq.Times.never()); - executionService.verify(async (e) => e.executeFile(TypeMoq.It.isValue(fileToExecute)), TypeMoq.Times.once()); + executionService.verify( + async (e) => e.executeFile(TypeMoq.It.isValue(fileToExecute), TypeMoq.It.isAny()), + TypeMoq.Times.once(), + ); }); test('Ensure executeFileInterTerminal will use active file', async () => { @@ -164,7 +170,10 @@ suite('Terminal - Code Execution Manager', () => { .returns(() => executionService.object); await commandHandler!(fileToExecute); - executionService.verify(async (e) => e.executeFile(TypeMoq.It.isValue(fileToExecute)), TypeMoq.Times.once()); + executionService.verify( + async (e) => e.executeFile(TypeMoq.It.isValue(fileToExecute), TypeMoq.It.isAny()), + TypeMoq.Times.once(), + ); }); async function testExecutionOfSelectionWithoutAnyActiveDocument(commandId: string, executionSericeId: string) { diff --git a/extensions/positron-python/src/test/terminals/codeExecution/helper.test.ts b/extensions/positron-python/src/test/terminals/codeExecution/helper.test.ts index 57bf51883eb8..ac47037a2344 100644 --- a/extensions/positron-python/src/test/terminals/codeExecution/helper.test.ts +++ b/extensions/positron-python/src/test/terminals/codeExecution/helper.test.ts @@ -8,8 +8,13 @@ import * as fs from 'fs-extra'; import * as path from 'path'; import { SemVer } from 'semver'; import * as TypeMoq from 'typemoq'; -import { EventEmitter, Position, Range, Selection, TextDocument, TextEditor, TextLine, Uri } from 'vscode'; -import { IApplicationShell, ICommandManager, IDocumentManager } from '../../../client/common/application/types'; +import { Position, Range, Selection, TextDocument, TextEditor, TextLine, Uri } from 'vscode'; +import { + IApplicationShell, + ICommandManager, + IDocumentManager, + IWorkspaceService, +} from '../../../client/common/application/types'; import { EXTENSION_ROOT_DIR, PYTHON_LANGUAGE } from '../../../client/common/constants'; import '../../../client/common/extensions'; import { ProcessService } from '../../../client/common/process/proc'; @@ -38,6 +43,7 @@ suite('Terminal - Code Execution Helper', () => { let processService: TypeMoq.IMock; let interpreterService: TypeMoq.IMock; let commandManager: TypeMoq.IMock; + let workspaceService: TypeMoq.IMock; const workingPython: PythonEnvironment = { path: PYTHON_PATH, version: new SemVer('3.6.6-final'), @@ -51,6 +57,7 @@ suite('Terminal - Code Execution Helper', () => { setup(() => { const serviceContainer = TypeMoq.Mock.ofType(); commandManager = TypeMoq.Mock.ofType(); + workspaceService = TypeMoq.Mock.ofType(); documentManager = TypeMoq.Mock.ofType(); applicationShell = TypeMoq.Mock.ofType(); const envVariablesProvider = TypeMoq.Mock.ofType(); @@ -69,6 +76,9 @@ suite('Terminal - Code Execution Helper', () => { envVariablesProvider .setup((e) => e.getEnvironmentVariables(TypeMoq.It.isAny())) .returns(() => Promise.resolve({})); + serviceContainer + .setup((c) => c.get(TypeMoq.It.isValue(IWorkspaceService))) + .returns(() => workspaceService.object); serviceContainer .setup((c) => c.get(TypeMoq.It.isValue(IProcessServiceFactory), TypeMoq.It.isAny())) .returns(() => processServiceFactory.object); @@ -367,20 +377,13 @@ suite('Terminal - Code Execution Helper', () => { .setup((d) => d.textDocuments) .returns(() => [document.object]) .verifiable(TypeMoq.Times.once()); - const saveEmitter = new EventEmitter(); - documentManager.setup((d) => d.onDidSaveTextDocument).returns(() => saveEmitter.event); document.setup((doc) => doc.isUntitled).returns(() => true); document.setup((doc) => doc.isDirty).returns(() => true); document.setup((doc) => doc.languageId).returns(() => PYTHON_LANGUAGE); const untitledUri = Uri.file('Untitled-1'); document.setup((doc) => doc.uri).returns(() => untitledUri); - const savedDocument = TypeMoq.Mock.ofType(); const expectedSavedUri = Uri.file('one.py'); - savedDocument.setup((doc) => doc.uri).returns(() => expectedSavedUri); - commandManager - .setup((c) => c.executeCommand('workbench.action.files.save', untitledUri)) - .callback(() => saveEmitter.fire(savedDocument.object)) - .returns(() => Promise.resolve()); + workspaceService.setup((w) => w.save(TypeMoq.It.isAny())).returns(() => Promise.resolve(expectedSavedUri)); const savedUri = await helper.saveFileIfDirty(untitledUri); diff --git a/extensions/positron-python/src/test/testing/common/debugLauncher.unit.test.ts b/extensions/positron-python/src/test/testing/common/debugLauncher.unit.test.ts index b8b7d5c55130..41b95c66040e 100644 --- a/extensions/positron-python/src/test/testing/common/debugLauncher.unit.test.ts +++ b/extensions/positron-python/src/test/testing/common/debugLauncher.unit.test.ts @@ -29,6 +29,7 @@ import { ITestingSettings } from '../../../client/testing/configuration/types'; import { TestProvider } from '../../../client/testing/types'; import { isOs, OSType } from '../../common'; import { IEnvironmentActivationService } from '../../../client/interpreter/activation/types'; +import * as util from '../../../client/testing/testController/common/utils'; use(chaiAsPromised); @@ -45,6 +46,7 @@ suite('Unit Tests - Debug Launcher', () => { let getWorkspaceFoldersStub: sinon.SinonStub; let pathExistsStub: sinon.SinonStub; let readFileStub: sinon.SinonStub; + let pythonTestAdapterRewriteEnabledStub: sinon.SinonStub; const envVars = { FOO: 'BAR' }; setup(async () => { @@ -65,6 +67,8 @@ suite('Unit Tests - Debug Launcher', () => { getWorkspaceFoldersStub = sinon.stub(workspaceApis, 'getWorkspaceFolders'); pathExistsStub = sinon.stub(fs, 'pathExists'); readFileStub = sinon.stub(fs, 'readFile'); + pythonTestAdapterRewriteEnabledStub = sinon.stub(util, 'pythonTestAdapterRewriteEnabled'); + pythonTestAdapterRewriteEnabledStub.returns(false); const appShell = TypeMoq.Mock.ofType(undefined, TypeMoq.MockBehavior.Strict); appShell.setup((a) => a.showErrorMessage(TypeMoq.It.isAny())).returns(() => Promise.resolve(undefined)); @@ -143,19 +147,22 @@ suite('Unit Tests - Debug Launcher', () => { uri: Uri.file(folderPath), }; } - function getTestLauncherScript(testProvider: TestProvider) { - switch (testProvider) { - case 'unittest': { - return path.join(EXTENSION_ROOT_DIR, 'pythonFiles', 'visualstudio_py_testlauncher.py'); - } - case 'pytest': { - return path.join(EXTENSION_ROOT_DIR, 'pythonFiles', 'testlauncher.py'); - } - default: { - throw new Error(`Unknown test provider '${testProvider}'`); + function getTestLauncherScript(testProvider: TestProvider, pythonTestAdapterRewriteExperiment?: boolean) { + if (!pythonTestAdapterRewriteExperiment) { + switch (testProvider) { + case 'unittest': { + return path.join(EXTENSION_ROOT_DIR, 'pythonFiles', 'visualstudio_py_testlauncher.py'); + } + case 'pytest': { + return path.join(EXTENSION_ROOT_DIR, 'pythonFiles', 'testlauncher.py'); + } + default: { + throw new Error(`Unknown test provider '${testProvider}'`); + } } } } + function getDefaultDebugConfig(): DebugConfiguration { return { name: 'Debug Unit Test', @@ -178,7 +185,7 @@ suite('Unit Tests - Debug Launcher', () => { expected?: DebugConfiguration, debugConfigs?: string | DebugConfiguration[], ) { - const testLaunchScript = getTestLauncherScript(testProvider); + const testLaunchScript = getTestLauncherScript(testProvider, false); const workspaceFolders = [createWorkspaceFolder(options.cwd), createWorkspaceFolder('five/six/seven')]; getWorkspaceFoldersStub.returns(workspaceFolders); diff --git a/extensions/positron-python/src/test/testing/testController/unittest/testExecutionAdapter.unit.test.ts b/extensions/positron-python/src/test/testing/testController/unittest/testExecutionAdapter.unit.test.ts index d88f033d39a4..e5495629bf28 100644 --- a/extensions/positron-python/src/test/testing/testController/unittest/testExecutionAdapter.unit.test.ts +++ b/extensions/positron-python/src/test/testing/testController/unittest/testExecutionAdapter.unit.test.ts @@ -1,118 +1,118 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. -// Licensed under the MIT License. - -import * as assert from 'assert'; -import * as path from 'path'; -import * as typemoq from 'typemoq'; -import { Uri } from 'vscode'; -import { IConfigurationService, ITestOutputChannel } from '../../../../client/common/types'; -import { EXTENSION_ROOT_DIR } from '../../../../client/constants'; -import { ITestServer, TestCommandOptions } from '../../../../client/testing/testController/common/types'; -import { UnittestTestExecutionAdapter } from '../../../../client/testing/testController/unittest/testExecutionAdapter'; - -suite('Unittest test execution adapter', () => { - let stubConfigSettings: IConfigurationService; - let outputChannel: typemoq.IMock; - - setup(() => { - stubConfigSettings = ({ - getSettings: () => ({ - testing: { unittestArgs: ['-v', '-s', '.', '-p', 'test*'] }, - }), - } as unknown) as IConfigurationService; - outputChannel = typemoq.Mock.ofType(); - }); - - test('runTests should send the run command to the test server', async () => { - let options: TestCommandOptions | undefined; - - const stubTestServer = ({ - sendCommand(opt: TestCommandOptions): Promise { - delete opt.outChannel; - options = opt; - return Promise.resolve(); - }, - onDataReceived: () => { - // no body - }, - createUUID: () => '123456789', - } as unknown) as ITestServer; - - const uri = Uri.file('/foo/bar'); - const script = path.join(EXTENSION_ROOT_DIR, 'pythonFiles', 'unittestadapter', 'execution.py'); - - const adapter = new UnittestTestExecutionAdapter(stubTestServer, stubConfigSettings, outputChannel.object); - adapter.runTests(uri, [], false); - - const expectedOptions: TestCommandOptions = { - workspaceFolder: uri, - command: { script, args: ['--udiscovery', '-v', '-s', '.', '-p', 'test*'] }, - cwd: uri.fsPath, - uuid: '123456789', - debugBool: false, - testIds: [], - }; - - assert.deepStrictEqual(options, expectedOptions); - }); - test("onDataReceivedHandler should parse the data if the cwd from the payload matches the test adapter's cwd", async () => { - const stubTestServer = ({ - sendCommand(): Promise { - return Promise.resolve(); - }, - onDataReceived: () => { - // no body - }, - createUUID: () => '123456789', - } as unknown) as ITestServer; - - const uri = Uri.file('/foo/bar'); - const data = { status: 'success' }; - const uuid = '123456789'; - - const adapter = new UnittestTestExecutionAdapter(stubTestServer, stubConfigSettings, outputChannel.object); - - // triggers runTests flow which will run onDataReceivedHandler and the - // promise resolves into the parsed data. - const promise = adapter.runTests(uri, [], false); - - adapter.onDataReceivedHandler({ uuid, data: JSON.stringify(data) }); - - const result = await promise; - - assert.deepStrictEqual(result, data); - }); - test("onDataReceivedHandler should ignore the data if the cwd from the payload does not match the test adapter's cwd", async () => { - const correctUuid = '123456789'; - const incorrectUuid = '987654321'; - const stubTestServer = ({ - sendCommand(): Promise { - return Promise.resolve(); - }, - onDataReceived: () => { - // no body - }, - createUUID: () => correctUuid, - } as unknown) as ITestServer; - - const uri = Uri.file('/foo/bar'); - - const adapter = new UnittestTestExecutionAdapter(stubTestServer, stubConfigSettings, outputChannel.object); - - // triggers runTests flow which will run onDataReceivedHandler and the - // promise resolves into the parsed data. - const promise = adapter.runTests(uri, [], false); - - const data = { status: 'success' }; - // will not resolve due to incorrect UUID - adapter.onDataReceivedHandler({ uuid: incorrectUuid, data: JSON.stringify(data) }); - - const nextData = { status: 'error' }; - // will resolve and nextData will be returned as result - adapter.onDataReceivedHandler({ uuid: correctUuid, data: JSON.stringify(nextData) }); - - const result = await promise; - - assert.deepStrictEqual(result, nextData); - }); -}); +// // Copyright (c) Microsoft Corporation. All rights reserved. +// // Licensed under the MIT License. + +// import * as assert from 'assert'; +// import * as path from 'path'; +// import * as typemoq from 'typemoq'; +// import { Uri } from 'vscode'; +// import { IConfigurationService, ITestOutputChannel } from '../../../../client/common/types'; +// import { EXTENSION_ROOT_DIR } from '../../../../client/constants'; +// import { ITestServer, TestCommandOptions } from '../../../../client/testing/testController/common/types'; +// import { UnittestTestExecutionAdapter } from '../../../../client/testing/testController/unittest/testExecutionAdapter'; + +// suite('Unittest test execution adapter', () => { +// let stubConfigSettings: IConfigurationService; +// let outputChannel: typemoq.IMock; + +// setup(() => { +// stubConfigSettings = ({ +// getSettings: () => ({ +// testing: { unittestArgs: ['-v', '-s', '.', '-p', 'test*'] }, +// }), +// } as unknown) as IConfigurationService; +// outputChannel = typemoq.Mock.ofType(); +// }); + +// test('runTests should send the run command to the test server', async () => { +// let options: TestCommandOptions | undefined; + +// const stubTestServer = ({ +// sendCommand(opt: TestCommandOptions, runTestIdPort?: string): Promise { +// delete opt.outChannel; +// options = opt; +// assert(runTestIdPort !== undefined); +// return Promise.resolve(); +// }, +// onDataReceived: () => { +// // no body +// }, +// createUUID: () => '123456789', +// } as unknown) as ITestServer; + +// const uri = Uri.file('/foo/bar'); +// const script = path.join(EXTENSION_ROOT_DIR, 'pythonFiles', 'unittestadapter', 'execution.py'); + +// const adapter = new UnittestTestExecutionAdapter(stubTestServer, stubConfigSettings, outputChannel.object); +// adapter.runTests(uri, [], false).then(() => { +// const expectedOptions: TestCommandOptions = { +// workspaceFolder: uri, +// command: { script, args: ['--udiscovery', '-v', '-s', '.', '-p', 'test*'] }, +// cwd: uri.fsPath, +// uuid: '123456789', +// debugBool: false, +// testIds: [], +// }; +// assert.deepStrictEqual(options, expectedOptions); +// }); +// }); +// test("onDataReceivedHandler should parse the data if the cwd from the payload matches the test adapter's cwd", async () => { +// const stubTestServer = ({ +// sendCommand(): Promise { +// return Promise.resolve(); +// }, +// onDataReceived: () => { +// // no body +// }, +// createUUID: () => '123456789', +// } as unknown) as ITestServer; + +// const uri = Uri.file('/foo/bar'); +// const data = { status: 'success' }; +// const uuid = '123456789'; + +// const adapter = new UnittestTestExecutionAdapter(stubTestServer, stubConfigSettings, outputChannel.object); + +// // triggers runTests flow which will run onDataReceivedHandler and the +// // promise resolves into the parsed data. +// const promise = adapter.runTests(uri, [], false); + +// adapter.onDataReceivedHandler({ uuid, data: JSON.stringify(data) }); + +// const result = await promise; + +// assert.deepStrictEqual(result, data); +// }); +// test("onDataReceivedHandler should ignore the data if the cwd from the payload does not match the test adapter's cwd", async () => { +// const correctUuid = '123456789'; +// const incorrectUuid = '987654321'; +// const stubTestServer = ({ +// sendCommand(): Promise { +// return Promise.resolve(); +// }, +// onDataReceived: () => { +// // no body +// }, +// createUUID: () => correctUuid, +// } as unknown) as ITestServer; + +// const uri = Uri.file('/foo/bar'); + +// const adapter = new UnittestTestExecutionAdapter(stubTestServer, stubConfigSettings, outputChannel.object); + +// // triggers runTests flow which will run onDataReceivedHandler and the +// // promise resolves into the parsed data. +// const promise = adapter.runTests(uri, [], false); + +// const data = { status: 'success' }; +// // will not resolve due to incorrect UUID +// adapter.onDataReceivedHandler({ uuid: incorrectUuid, data: JSON.stringify(data) }); + +// const nextData = { status: 'error' }; +// // will resolve and nextData will be returned as result +// adapter.onDataReceivedHandler({ uuid: correctUuid, data: JSON.stringify(nextData) }); + +// const result = await promise; + +// assert.deepStrictEqual(result, nextData); +// }); +// }); diff --git a/extensions/positron-python/tsconfig.json b/extensions/positron-python/tsconfig.json index a594636ff243..dd6065a14860 100644 --- a/extensions/positron-python/tsconfig.json +++ b/extensions/positron-python/tsconfig.json @@ -48,6 +48,7 @@ "typings/*.d.ts", "typings/vscode-proposed/*.d.ts", "types/*.d.ts", + "types/src/vscode-dts/*.d.ts", "positron-dts/positron.d.ts" ] } diff --git a/extensions/positron-python/types/src/vscode-dts/vscode.proposed.envCollectionOptions.d.ts b/extensions/positron-python/types/src/vscode-dts/vscode.proposed.envCollectionOptions.d.ts new file mode 100644 index 000000000000..d25a92725a4d --- /dev/null +++ b/extensions/positron-python/types/src/vscode-dts/vscode.proposed.envCollectionOptions.d.ts @@ -0,0 +1,56 @@ +/*--------------------------------------------------------------------------------------------- + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. See License.txt in the project root for license information. + *--------------------------------------------------------------------------------------------*/ + +declare module 'vscode' { + + // https://github.com/microsoft/vscode/issues/179476 + + /** + * Options applied to the mutator. + */ + export interface EnvironmentVariableMutatorOptions { + /** + * Apply to the environment just before the process is created. + * + * Defaults to true. + */ + applyAtProcessCreation?: boolean; + + /** + * Apply to the environment in the shell integration script. Note that this _will not_ apply + * the mutator if shell integration is disabled or not working for some reason. + * + * Defaults to false. + */ + applyAtShellIntegration?: boolean; + } + + /** + * A type of mutation and its value to be applied to an environment variable. + */ + export interface EnvironmentVariableMutator { + /** + * Options applied to the mutator. + */ + readonly options: EnvironmentVariableMutatorOptions; + } + + export interface EnvironmentVariableCollection extends Iterable<[variable: string, mutator: EnvironmentVariableMutator]> { + /** + * @param options Options applied to the mutator. + */ + replace(variable: string, value: string, options?: EnvironmentVariableMutatorOptions): void; + + /** + * @param options Options applied to the mutator. + */ + append(variable: string, value: string, options?: EnvironmentVariableMutatorOptions): void; + + /** + * @param options Options applied to the mutator. + */ + prepend(variable: string, value: string, options?: EnvironmentVariableMutatorOptions): void; + } +} diff --git a/extensions/positron-python/types/vscode.proposed.envCollectionWorkspace.d.ts b/extensions/positron-python/types/vscode.proposed.envCollectionWorkspace.d.ts index b1176a9d46c2..d778e53e5086 100644 --- a/extensions/positron-python/types/vscode.proposed.envCollectionWorkspace.d.ts +++ b/extensions/positron-python/types/vscode.proposed.envCollectionWorkspace.d.ts @@ -5,34 +5,30 @@ declare module 'vscode' { - // https://github.com/microsoft/vscode/issues/171173 + // https://github.com/microsoft/vscode/issues/182069 - export interface EnvironmentVariableMutator { - readonly type: EnvironmentVariableMutatorType; - readonly value: string; - readonly scope: EnvironmentVariableScope | undefined; - } - - export interface EnvironmentVariableCollection extends Iterable<[variable: string, mutator: EnvironmentVariableMutator]> { - /** - * Sets a description for the environment variable collection, this will be used to describe the changes in the UI. - * @param description A description for the environment variable collection. - * @param scope Specific scope to which this description applies to. - */ - setDescription(description: string | MarkdownString | undefined, scope?: EnvironmentVariableScope): void; - replace(variable: string, value: string, scope?: EnvironmentVariableScope): void; - append(variable: string, value: string, scope?: EnvironmentVariableScope): void; - prepend(variable: string, value: string, scope?: EnvironmentVariableScope): void; - get(variable: string, scope?: EnvironmentVariableScope): EnvironmentVariableMutator | undefined; - delete(variable: string, scope?: EnvironmentVariableScope): void; - clear(scope?: EnvironmentVariableScope): void; - - } + // export interface ExtensionContext { + // /** + // * Gets the extension's environment variable collection for this workspace, enabling changes + // * to be applied to terminal environment variables. + // * + // * @param scope The scope to which the environment variable collection applies to. + // */ + // readonly environmentVariableCollection: EnvironmentVariableCollection & { getScopedEnvironmentVariableCollection(scope: EnvironmentVariableScope): EnvironmentVariableCollection }; + // } export type EnvironmentVariableScope = { /** - * The workspace folder to which this collection applies to. If unspecified, collection applies to all workspace folders. - */ + * Any specific workspace folder to get collection for. If unspecified, collection applicable to all workspace folders is returned. + */ workspaceFolder?: WorkspaceFolder; }; + + export interface EnvironmentVariableCollection extends Iterable<[variable: string, mutator: EnvironmentVariableMutator]> { + /** + * A description for the environment variable collection, this will be used to describe the + * changes in the UI. + */ + description: string | MarkdownString | undefined; + } } diff --git a/extensions/positron-python/typings/vscode-proposed/vscode.proposed.saveEditor.d.ts b/extensions/positron-python/typings/vscode-proposed/vscode.proposed.saveEditor.d.ts new file mode 100644 index 000000000000..9088939a4649 --- /dev/null +++ b/extensions/positron-python/typings/vscode-proposed/vscode.proposed.saveEditor.d.ts @@ -0,0 +1,34 @@ +/*--------------------------------------------------------------------------------------------- + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. See License.txt in the project root for license information. + *--------------------------------------------------------------------------------------------*/ + +// https://github.com/microsoft/vscode/issues/178713 + +declare module 'vscode' { + + export namespace workspace { + + /** + * Saves the editor identified by the given resource and returns the resulting resource or `undefined` + * if save was not successful. + * + * **Note** that an editor with the provided resource must be opened in order to be saved. + * + * @param uri the associated uri for the opened editor to save. + * @return A thenable that resolves when the save operation has finished. + */ + export function save(uri: Uri): Thenable; + + /** + * Saves the editor identified by the given resource to a new file name as provided by the user and + * returns the resulting resource or `undefined` if save was not successful or cancelled. + * + * **Note** that an editor with the provided resource must be opened in order to be saved as. + * + * @param uri the associated uri for the opened editor to save as. + * @return A thenable that resolves when the save-as operation has finished. + */ + export function saveAs(uri: Uri): Thenable; + } +}