FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Use OS temp dir in runString by StantonMatt · Pull Request #330 · extrabacon/python-shell · GitHub

Repository navigation

Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .ts  (2) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
2 changes: 1 addition & 1 deletion index.ts
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
Expand Up @@ -371,7 +371,7 @@ export class PythonShell extends EventEmitter {
static runString(code: string, options?: Options) {
// put code in temp file
const randomInt = getRandomInt();
const filePath = tmpdir + sep + `pythonShellFile${randomInt}.py`;
const filePath = join(tmpdir(), `pythonShellFile${randomInt}.py`);
writeFileSync(filePath, code);

return PythonShell.run(filePath, options);
Expand Down
32 changes: 30 additions & 2 deletions test/test-python-shell.ts
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
@@ -1,8 +1,9 @@
import * as should from 'should';
import { PythonShell } from '..';
import { sep, join } from 'path';
import { EOL as newline } from 'os';
import { basename, dirname, sep, join } from 'path';
import { EOL as newline, tmpdir } from 'os';
import { chdir, cwd } from 'process';
import { readFileSync, unlinkSync } from 'fs';

describe('PythonShell', function () {
const pythonFolder = 'test/python';
Expand Down Expand Up @@ -147,6 +148,33 @@ describe('PythonShell', function () {
results.should.be.an.Array().and.have.lengthOf(2);
results.should.eql(['hello', 'world']);
});
it('should write runString code to a temporary file', async function () {
const originalRun = PythonShell.run;
const options = { pythonPath: PythonShell.defaultPythonPath };
const code = 'print("hello from temp file")';
let capturedPath: string;
let capturedOptions: object;

PythonShell.run = ((scriptPath: string, runOptions: object) => {
capturedPath = scriptPath;
capturedOptions = runOptions;
return Promise.resolve(['ok']);
}) as typeof PythonShell.run;

try {
let results = await PythonShell.runString(code, options);

results.should.eql(['ok']);
dirname(capturedPath).should.eql(tmpdir());
basename(capturedPath).should.startWith('pythonShellFile');
basename(capturedPath).should.endWith('.py');
readFileSync(capturedPath, 'utf8').should.eql(code);
capturedOptions.should.equal(options);
} finally {
PythonShell.run = originalRun;
if (capturedPath) unlinkSync(capturedPath);
}
});
after(() => {
PythonShell.defaultOptions = {
// reset to match initial value
Expand Down

Back | FazBrowse Home | New Git URL