-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-shell-approach.js
More file actions
executable file
Β·150 lines (128 loc) Β· 5.03 KB
/
test-shell-approach.js
File metadata and controls
executable file
Β·150 lines (128 loc) Β· 5.03 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
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
#!/usr/bin/env node
// Test script to verify the updated shell script piping approach for codex.ts
require('dotenv/config');
const childProcess = require('child_process');
const fs = require('fs');
const os = require('os');
const path = require('path');
console.log('π VERIFYING CODEX CLI SHELL SCRIPT PIPING APPROACH');
// Create a test directory
const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'codex-shell-test-'));
console.log(`Created test directory: ${tempDir}`);
// Save original functions
const originalExecSync = childProcess.execSync;
const originalWriteFileSync = fs.writeFileSync;
const originalUnlinkSync = fs.unlinkSync;
const originalChmodSync = fs.chmodSync;
// Track files and commands
const createdFiles = [];
const executedCommands = [];
// Mock fs.writeFileSync to track created script files
fs.writeFileSync = function(filePath, content, options) {
if (typeof filePath === 'string' && filePath.includes('run_codex.sh')) {
console.log(`β
Creating shell script: ${filePath}`);
if (typeof content === 'string') {
console.log('Script content:');
console.log('-----------------------------------');
console.log(content);
console.log('-----------------------------------');
createdFiles.push({
path: filePath,
content: content,
options: options
});
}
}
return originalWriteFileSync(filePath, content, options);
};
// Mock fs.chmodSync to track permissions
fs.chmodSync = function(path, mode) {
if (typeof path === 'string' && path.includes('run_codex.sh')) {
console.log(`β
Setting execute permissions on ${path}: ${mode.toString(8)}`);
}
return originalChmodSync(path, mode);
};
// Mock execSync to track shell script execution
childProcess.execSync = function(command, options) {
if (typeof command === 'string') {
executedCommands.push({
command: command,
options: options
});
if (command.endsWith('run_codex.sh')) {
console.log(`β
Executing shell script: ${command}`);
// Check if script exists in our tracked files
const scriptFile = createdFiles.find(f => f.path === command);
if (scriptFile) {
console.log('β
The shell script was properly created before execution');
// Verify the script contains echo and pipe to codex
if (scriptFile.content.includes('echo') &&
scriptFile.content.includes('npx @openai/codex') &&
scriptFile.content.includes('|')) {
console.log('β
Script correctly pipes echo output to codex');
// Check for quotes escaping in the script
if (scriptFile.content.includes("\\'")) {
console.log('β
Script correctly escapes single quotes in the prompt');
}
} else {
console.log('β Script does not correctly pipe stdin to codex');
}
} else {
console.log('β Shell script not found in tracked files');
}
// Return a mock codex response
return "This is a mock response from Codex CLI";
}
}
return '';
};
// Create a simple test prompt with single quotes to test escaping
const testPrompt = "Here's a test prompt with 'single quotes' that need escaping";
try {
// Implement a simplified version of the codexRepository approach
console.log('\nπ TESTING SHELL SCRIPT APPROACH');
console.log('Creating and executing shell script for codex...');
// Create shell script
const scriptContent = `#!/bin/bash
export OPENAI_API_KEY=test_key
echo '${testPrompt.replace(/'/g, "'\\''")}' | npx @openai/codex --approval-mode full-auto
`;
const scriptPath = path.join(tempDir, "run_codex.sh");
fs.writeFileSync(scriptPath, scriptContent, { mode: 0o755 });
// Execute the script
const output = childProcess.execSync(scriptPath, {
cwd: tempDir,
env: { FORCE_COLOR: "0", CI: "true" },
encoding: "utf-8"
});
console.log(`Received output: ${output}`);
// Clean up
fs.unlinkSync(scriptPath);
console.log(`β
Cleaned up script: ${scriptPath}`);
// Report on results
console.log('\nπ RESULTS');
console.log(`Created ${createdFiles.length} shell script(s)`);
console.log(`Executed ${executedCommands.length} command(s)`);
if (createdFiles.length > 0 && executedCommands.length > 0) {
console.log('\nβ
VERIFICATION SUCCESSFUL');
console.log('The shell script approach should properly handle the stdin piping');
console.log('This should resolve the "Please pass patch text through stdin" error');
} else {
console.log('\nβ VERIFICATION FAILED');
}
} catch (error) {
console.error('Error during verification:', error);
} finally {
// Restore original functions
childProcess.execSync = originalExecSync;
fs.writeFileSync = originalWriteFileSync;
fs.unlinkSync = originalUnlinkSync;
fs.chmodSync = originalChmodSync;
// Clean up test directory
try {
fs.rmSync(tempDir, { recursive: true, force: true });
console.log(`\nCleaned up test directory: ${tempDir}`);
} catch (e) {
console.error('Failed to clean up test directory:', e);
}
}