-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathpep723.unit.test.ts
More file actions
104 lines (82 loc) · 3.88 KB
/
pep723.unit.test.ts
File metadata and controls
104 lines (82 loc) · 3.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
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');
});
});