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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
|
- import { exec } from 'child_process';
- import fs from 'fs';
- import path from 'path';
- import util from 'util';
- import dedent from 'dedent';
- import logger from '../../src/logger';
- import { runPython } from '../../src/python/pythonUtils';
- const execPromise = util.promisify(exec);
- describe('pythonUtils Integration Tests', () => {
- const scriptsDir = path.join(__dirname, 'scripts');
- beforeAll(() => {
- if (!fs.existsSync(scriptsDir)) {
- fs.mkdirSync(scriptsDir);
- }
- fs.writeFileSync(
- path.join(scriptsDir, 'simple.py'),
- dedent`
- import json
- import sys
- def main(*args):
- message = ' '.join(str(arg) for arg in args)
- return {
- 'message': message,
- 'success': True
- }
- def print_to_stdout(*args):
- message = ' '.join(str(arg) for arg in args)
- print(message)
- return main(*args)
- class TestClass:
- @classmethod
- def class_method(cls, *args):
- return main(*args)
- async def async_function(*args):
- return main(*args)
- `,
- );
- fs.writeFileSync(
- path.join(scriptsDir, 'with_imports.py'),
- dedent`
- import os
- import datetime
- def get_env_and_date():
- return {
- 'env': os.environ.get('TEST_ENV', 'not set'),
- 'date': str(datetime.datetime.now().date())
- }
- `,
- );
- });
- afterAll(() => {
- fs.rmSync(path.join(scriptsDir), { recursive: true, force: true });
- });
- it('should be able to call Python directly', async () => {
- const { stdout } = await execPromise('python --version');
- expect(stdout).toContain('Python');
- });
- it('should successfully run a simple Python script', async () => {
- const result = await runPython(path.join(scriptsDir, 'simple.py'), 'main', ['Hello, World!']);
- expect(result).toEqual({
- message: 'Hello, World!',
- success: true,
- });
- }, 10000);
- it('should handle multiple arguments', async () => {
- const result = await runPython(path.join(scriptsDir, 'simple.py'), 'main', [
- 'Multiple',
- 'Arguments',
- ]);
- expect(result).toEqual({
- message: 'Multiple Arguments',
- success: true,
- });
- });
- it('should handle empty string argument', async () => {
- const result = await runPython(path.join(scriptsDir, 'simple.py'), 'main', ['']);
- expect(result).toEqual({
- message: '',
- success: true,
- });
- });
- it('should handle non-string argument', async () => {
- const result = await runPython(path.join(scriptsDir, 'simple.py'), 'main', [123]);
- expect(result).toEqual({
- message: '123',
- success: true,
- });
- });
- it('should throw an error for non-existent script', async () => {
- const nonExistentPath = path.join(scriptsDir, 'non_existent.py');
- await expect(runPython(nonExistentPath, 'main', ['test'])).rejects.toThrow(expect.any(Error));
- });
- it('should handle Python script that prints to stdout', async () => {
- const result = await runPython(path.join(scriptsDir, 'simple.py'), 'print_to_stdout', [
- 'Print to stdout',
- ]);
- expect(result).toEqual({
- message: 'Print to stdout',
- success: true,
- });
- });
- it('should handle class methods', async () => {
- const result = await runPython(path.join(scriptsDir, 'simple.py'), 'TestClass.class_method', [
- 'Class method',
- ]);
- expect(result).toEqual({
- message: 'Class method',
- success: true,
- });
- });
- it('should handle async functions', async () => {
- const result = await runPython(path.join(scriptsDir, 'simple.py'), 'async_function', [
- 'Async function',
- ]);
- expect(result).toEqual({
- message: 'Async function',
- success: true,
- });
- });
- it('should handle scripts with imports', async () => {
- const result = await runPython(
- path.join(scriptsDir, 'with_imports.py'),
- 'get_env_and_date',
- [],
- );
- expect(result).toHaveProperty('env');
- expect(result).toHaveProperty('date');
- expect((result as any).env).toBe('not set');
- expect(new Date((result as any).date)).toBeInstanceOf(Date);
- });
- it('should handle scripts with environment variables', async () => {
- process.env.TEST_ENV = 'test_value';
- const result = await runPython(
- path.join(scriptsDir, 'with_imports.py'),
- 'get_env_and_date',
- [],
- );
- expect((result as any).env).toBe('test_value');
- delete process.env.TEST_ENV;
- });
- it('should log debug messages', async () => {
- jest.clearAllMocks();
- const result = await runPython(path.join(scriptsDir, 'simple.py'), 'main', ['Debug Test']);
- expect(result).toEqual({
- message: 'Debug Test',
- success: true,
- });
- expect(logger.debug).toHaveBeenCalledWith(
- expect.stringContaining('Running Python wrapper with args'),
- );
- expect(logger.debug).toHaveBeenCalledWith(expect.stringContaining('Python script'));
- });
- });
|