-
Notifications
You must be signed in to change notification settings - Fork 42
Add run-button uv-mode test coverage #1494
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
Copilot
wants to merge
11
commits into
main
Choose a base branch
from
copilot/update-run-button-for-uv
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
349866d
feat: map run button to uv run
Copilot f517bd9
fix: import shell execution test type
Copilot b3f8667
fix: scope uv run setting lookup
Copilot b51423a
fix: remove duplicate task variable
Copilot 8b5c11a
fix: correct uv task test assertions
Copilot d8ecfda
fix: finalize uv run task tests
Copilot c0172dd
test: add run-button uv-mode test cases
Copilot 9b4b7be
Merge branch 'main' into copilot/update-run-button-for-uv
eleanorjboyd 2151183
Address Copilot review feedback
eleanorjboyd a01a406
Add PEP 723 inline script detection for uv run button
eleanorjboyd 50a681b
Fix pep723 test setup: stub require('fs-extra') instead of namespace …
eleanorjboyd File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| import * as fse from 'fs-extra'; | ||
|
|
||
| /** | ||
| * Checks if a Python script file uses PEP 723 inline script metadata. | ||
| * | ||
| * PEP 723 scripts declare their own Python version and dependency requirements | ||
| * via a `# /// script` block and should be run with `uv run <script>` without | ||
| * specifying a `--python` interpreter — uv resolves and manages the environment | ||
| * itself based on the inline metadata. | ||
| * | ||
| * @param filePath - Absolute path to the Python script file to inspect. | ||
| * @returns True if the file contains a PEP 723 `# /// script` opening marker, | ||
| * false if the marker is absent or the file cannot be read. | ||
| */ | ||
| export async function isPep723Script(filePath: string): Promise<boolean> { | ||
| try { | ||
| const content = await fse.readFile(filePath, 'utf-8'); | ||
| // A PEP 723 script tag opens with a line that is exactly `# /// script` | ||
| // (optional trailing whitespace permitted). | ||
| return /^# \/\/\/ script\s*$/m.test(content); | ||
| } catch { | ||
| return false; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| import * as assert from 'assert'; | ||
| import * as sinon from 'sinon'; | ||
| import { isPep723Script } from '../../../features/execution/pep723'; | ||
|
|
||
| suite('isPep723Script Tests', () => { | ||
| let readFileStub: sinon.SinonStub; | ||
|
|
||
| setup(() => { | ||
| // TypeScript compiles `import * as fse from 'fs-extra'` into a namespace wrapper whose | ||
| // properties are non-configurable getters — sinon cannot stub them directly. The actual | ||
| // `require('fs-extra')` object has writable/configurable properties AND the namespace | ||
| // wrapper's getters delegate to it, so stubbing the real module object is intercepted by | ||
| // the source-under-test as well. | ||
| // eslint-disable-next-line @typescript-eslint/no-require-imports | ||
| readFileStub = sinon.stub(require('fs-extra'), 'readFile'); | ||
| }); | ||
|
|
||
| teardown(() => { | ||
| sinon.restore(); | ||
| }); | ||
|
|
||
| test('should return true for a script with a PEP 723 marker at the top', async () => { | ||
| const content = [ | ||
| '# /// script', | ||
| '# requires-python = ">=3.11"', | ||
| '# dependencies = ["requests"]', | ||
| '# ///', | ||
| '', | ||
| 'import requests', | ||
| 'print(requests.get("https://example.com").status_code)', | ||
| ].join('\n'); | ||
|
|
||
| readFileStub.resolves(content); | ||
|
|
||
| const result = await isPep723Script('/some/script.py'); | ||
| assert.strictEqual(result, true, 'Should detect the PEP 723 marker'); | ||
| }); | ||
|
|
||
| test('should return true when marker appears mid-file (non-standard but still matches)', async () => { | ||
| const content = [ | ||
| '# Normal comment', | ||
| '', | ||
| '# /// script', | ||
| '# requires-python = ">=3.9"', | ||
| '# ///', | ||
| ].join('\n'); | ||
|
|
||
| readFileStub.resolves(content); | ||
|
|
||
| const result = await isPep723Script('/some/script.py'); | ||
| assert.strictEqual(result, true, 'Should detect the marker wherever it appears'); | ||
| }); | ||
|
|
||
| test('should return true when marker has trailing whitespace', async () => { | ||
| const content = '# /// script \nimport sys\n'; | ||
|
|
||
| readFileStub.resolves(content); | ||
|
|
||
| const result = await isPep723Script('/some/script.py'); | ||
| assert.strictEqual(result, true, 'Should accept trailing whitespace after the marker'); | ||
| }); | ||
|
|
||
| test('should return false for a standard Python script with no PEP 723 block', async () => { | ||
| const content = [ | ||
| '#!/usr/bin/env python3', | ||
| '# Normal script', | ||
| 'import sys', | ||
| 'print(sys.version)', | ||
| ].join('\n'); | ||
|
|
||
| readFileStub.resolves(content); | ||
|
|
||
| const result = await isPep723Script('/some/script.py'); | ||
| assert.strictEqual(result, false, 'Should not detect PEP 723 in a regular script'); | ||
| }); | ||
|
|
||
| test('should return false for a comment that looks similar but is not the marker', async () => { | ||
| const content = [ | ||
| '# // script', // only two slashes | ||
| '# //// script', // four slashes | ||
| '# ///script', // no space between /// and script | ||
| '# /// Script', // wrong case | ||
| ].join('\n'); | ||
|
|
||
| readFileStub.resolves(content); | ||
|
|
||
| const result = await isPep723Script('/some/script.py'); | ||
| assert.strictEqual(result, false, 'Should not match near-miss patterns'); | ||
| }); | ||
|
|
||
| test('should return false when file cannot be read (graceful fallback)', async () => { | ||
| readFileStub.rejects(new Error('ENOENT: no such file or directory')); | ||
|
|
||
| const result = await isPep723Script('/nonexistent/script.py'); | ||
| assert.strictEqual(result, false, 'Should return false rather than throwing when file is unreadable'); | ||
| }); | ||
|
|
||
| test('should return false for an empty file', async () => { | ||
| readFileStub.resolves(''); | ||
|
|
||
| const result = await isPep723Script('/some/empty.py'); | ||
| assert.strictEqual(result, false, 'Should return false for an empty file'); | ||
| }); | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.