Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions src/backend/workers/python/papyros/papyros.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ def _flush_open_files(self):
pass
self._tracked_files -= closed

def _emit_created_files(self, emit_empty=False):
def _emit_created_files(self):
with self._without_file_tracking():
cwd = os.getcwd()
result = {}
Expand All @@ -186,11 +186,10 @@ def _emit_created_files(self, emit_empty=False):
except Exception:
return
snapshot = json.dumps(result, sort_keys=True)
if snapshot == self._last_emitted_snapshot and not emit_empty:
if snapshot == self._last_emitted_snapshot:
return
self._last_emitted_snapshot = snapshot
if result or emit_empty:
self.callback("files", data=snapshot, contentType="text/json")
self.callback("files", data=snapshot, contentType="text/json")

@contextmanager
def _execute_context(self):
Expand Down Expand Up @@ -232,7 +231,7 @@ async def run_async(self, source_code, mode="exec", top_level_await=True):
from tracer import JSONTracer
def frame_callback(frame):
self._flush_open_files()
self._emit_created_files(emit_empty=True)
self._emit_created_files()
self.callback("frame", data=frame, contentType="application/json")

result = JSONTracer(frame_callback=frame_callback, module_name=MODULE_NAME).runscript(source_code)
Expand Down
1 change: 0 additions & 1 deletion src/frontend/state/InputOutput.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,6 @@ export class InputOutput extends State {
this.output = [];
this.prompt = "";
this.awaitingInput = false;
Comment thread
chvp marked this conversation as resolved.
this.files = [];
this.activeEditorTab = CODE_TAB;
}
}
43 changes: 42 additions & 1 deletion test/__tests__/state/Files.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Papyros } from "../../../src/frontend/state/Papyros";
import { expect, it, describe } from "vitest";
import { ProgrammingLanguage } from "../../../src/ProgrammingLanguage";
import { waitForFiles, waitForPapyrosReady, waitForInputReady, waitForOutput } from "../../helpers";
import { waitForFiles, waitForPapyrosReady, waitForInputReady, waitForOutput, waitForAwaitingInput } from "../../helpers";
import { isValidFileName } from "../../../src/util/Util";

describe("isValidFileName", () => {
Expand Down Expand Up @@ -117,6 +117,47 @@ raise ValueError("intentional error")
expect(papyros.io.files[0].content).toBe("before crash");
});

it("files from previous run persist while awaiting input in next run", async () => {
const papyros = new Papyros();
await papyros.launch();
papyros.runner.programmingLanguage = ProgrammingLanguage.Python;
papyros.runner.code = `open("persist.txt", "w").write("hello")`;
await waitForInputReady();
await papyros.runner.start();
await waitForFiles(papyros, 1);
expect(papyros.io.files.length).toBe(1);
expect(papyros.io.files[0].name).toBe("persist.txt");

papyros.runner.code = `x = input("name?")`;
await waitForPapyrosReady(papyros);
void papyros.runner.start();
await waitForAwaitingInput(papyros);
Comment thread
chvp marked this conversation as resolved.
expect(papyros.io.files.length).toBe(1);
expect(papyros.io.files[0].name).toBe("persist.txt");

papyros.io.provideInput("test");
await waitForPapyrosReady(papyros);
expect(papyros.io.files.length).toBe(1);
expect(papyros.io.files[0].name).toBe("persist.txt");
});

it("files from previous run are cleared when next run deletes them", async () => {
const papyros = new Papyros();
await papyros.launch();
papyros.runner.programmingLanguage = ProgrammingLanguage.Python;
papyros.runner.code = `open("temp.txt", "w").write("hello")`;
await waitForInputReady();
await papyros.runner.start();
await waitForFiles(papyros, 1);
expect(papyros.io.files.length).toBe(1);
expect(papyros.io.files[0].name).toBe("temp.txt");

papyros.runner.code = `import os; os.remove("temp.txt")`;
await papyros.runner.start();
await waitForPapyrosReady(papyros);
expect(papyros.io.files.length).toBe(0);
});

it("updateFileContent updates the in-memory content of an existing file", async () => {
const papyros = new Papyros();
await papyros.launch();
Expand Down
Loading