Skip to content

Commit 17154f1

Browse files
committed
テストでwriteFile後のtimeoutは不要、filesにReadonly追加、typeエラー修正など
1 parent c1a1266 commit 17154f1

File tree

9 files changed

+31
-28
lines changed

9 files changed

+31
-28
lines changed

app/terminal/embedContext.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,21 @@ type Filename = string;
2424
type TerminalId = string;
2525

2626
interface IEmbedContext {
27-
files: Record<Filename, string>;
27+
files: Readonly<Record<Filename, string>>;
2828
// ファイルを書き込む。更新後のページ内の全ファイル内容を返す
2929
// 返り値を使うことで再レンダリングを待たずに最新の内容を取得できる
3030
writeFile: (
31-
updatedFiles: Record<Filename, string>
32-
) => Promise<Record<Filename, string>>;
31+
updatedFiles: Readonly<Record<Filename, string>>
32+
) => Promise<Readonly<Record<Filename, string>>>;
3333

34-
replOutputs: Record<TerminalId, ReplCommand[]>;
34+
replOutputs: Readonly<Record<TerminalId, ReplCommand[]>>;
3535
addReplOutput: (
3636
terminalId: TerminalId,
3737
command: string,
3838
output: ReplOutput[]
3939
) => void;
4040

41-
execResults: Record<Filename, ReplOutput[]>;
41+
execResults: Readonly<Record<Filename, ReplOutput[]>>;
4242
setExecResult: (filename: Filename, output: ReplOutput[]) => void;
4343
}
4444
const EmbedContext = createContext<IEmbedContext>(null!);

app/terminal/exec.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ export function ExecFile(props: ExecProps) {
5252
outputs,
5353
false,
5454
undefined,
55+
null, // ファイル実行で"return"メッセージが返ってくることはないはずなので、Prismを渡す必要はない
5556
props.language
5657
);
5758
// TODO: 1つのファイル名しか受け付けないところに無理やりコンマ区切りで全部のファイル名を突っ込んでいる

app/terminal/page.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,9 @@ function MochaTest() {
147147
const [mochaState, setMochaState] = useState<"idle" | "running" | "finished">(
148148
"idle"
149149
);
150-
const { writeFile } = useEmbedContext();
150+
const { files } = useEmbedContext();
151+
const filesRef = useRef(files);
152+
filesRef.current = files;
151153

152154
const runTest = async () => {
153155
if(typeof window !== "undefined") {
@@ -158,7 +160,7 @@ function MochaTest() {
158160
mocha.setup("bdd");
159161

160162
for (const lang of Object.keys(runtimeRef.current) as RuntimeLang[]) {
161-
defineTests(lang, runtimeRef, writeFile);
163+
defineTests(lang, runtimeRef, filesRef);
162164
}
163165

164166
const runner = mocha.run();

app/terminal/repl.tsx

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ export function writeOutput(
3030
outputs: ReplOutput[],
3131
endNewLine: boolean,
3232
returnPrefix: string | undefined,
33+
Prism: typeof import("prismjs") | null,
3334
language: RuntimeLang
3435
) {
3536
for (let i = 0; i < outputs.length; i++) {
@@ -53,7 +54,12 @@ export function writeOutput(
5354
if (returnPrefix) {
5455
term.write(returnPrefix);
5556
}
56-
term.write(highlightCodeToAnsi(message, language));
57+
if (Prism) {
58+
term.write(highlightCodeToAnsi(Prism, message, language));
59+
} else {
60+
console.warn("Prism is not loaded, cannot highlight return value");
61+
term.write(message);
62+
}
5763
break;
5864
default:
5965
term.write(message);
@@ -79,7 +85,7 @@ export function ReplTerminal({
7985

8086
const [Prism, setPrism] = useState<typeof import("prismjs") | null>(null);
8187
useEffect(() => {
82-
if(Prism === null){
88+
if (Prism === null) {
8389
importPrism().then((prism) => setPrism(prism));
8490
}
8591
}, [Prism]);
@@ -180,6 +186,7 @@ export function ReplTerminal({
180186
outputs,
181187
true,
182188
returnPrefix,
189+
Prism,
183190
language
184191
);
185192
// 出力が終わったらプロンプトを表示

app/terminal/runtime.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export interface RuntimeContext {
2424
checkSyntax?: (code: string) => Promise<SyntaxStatus>;
2525
splitReplExamples?: (content: string) => ReplCommand[];
2626
// file
27-
runFiles: (filenames: string[], files: Record<string, string>) => Promise<ReplOutput[]>;
27+
runFiles: (filenames: string[], files: Readonly<Record<string, string>>) => Promise<ReplOutput[]>;
2828
getCommandlineStr?: (filenames: string[]) => string;
2929
}
3030
export interface LangConstants {

app/terminal/tests.ts

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { emptyMutex, RuntimeContext, RuntimeLang } from "./runtime";
55
export function defineTests(
66
lang: RuntimeLang,
77
runtimeRef: RefObject<Record<RuntimeLang, RuntimeContext>>,
8-
writeFile: (name: string, content: string) => void
8+
filesRef: RefObject<Readonly<Record<string, string>>>,
99
) {
1010
describe(`${lang} Runtime`, function () {
1111
this.timeout(
@@ -136,7 +136,6 @@ export function defineTests(
136136
while (!runtimeRef.current[lang].ready) {
137137
await new Promise((resolve) => setTimeout(resolve, 100));
138138
}
139-
await new Promise((resolve) => setTimeout(resolve, 100));
140139
const result = await (
141140
runtimeRef.current[lang].mutex || emptyMutex
142141
).runExclusive(() =>
@@ -169,10 +168,7 @@ export function defineTests(
169168
if (!filename || !code) {
170169
this.skip();
171170
}
172-
writeFile(filename, code);
173-
// use setTimeout to wait for writeFile to propagate.
174-
await new Promise((resolve) => setTimeout(resolve, 100));
175-
const result = await runtimeRef.current[lang].runFiles([filename]);
171+
const result = await runtimeRef.current[lang].runFiles([filename], {[filename]: code});
176172
console.log(`${lang} single file stdout test: `, result);
177173
expect(result).to.be.deep.equal([
178174
{
@@ -198,9 +194,7 @@ export function defineTests(
198194
if (!filename || !code) {
199195
this.skip();
200196
}
201-
writeFile(filename, code);
202-
await new Promise((resolve) => setTimeout(resolve, 100));
203-
const result = await runtimeRef.current[lang].runFiles([filename]);
197+
const result = await runtimeRef.current[lang].runFiles([filename], {[filename]: code});
204198
console.log(`${lang} single file error capture test: `, result);
205199
// eslint-disable-next-line @typescript-eslint/no-unused-expressions
206200
expect(result.filter((r) => r.message.includes(errorMsg))).to.not.be
@@ -245,11 +239,7 @@ export function defineTests(
245239
if (!codes || !execFiles) {
246240
this.skip();
247241
}
248-
for (const [filename, code] of Object.entries(codes)) {
249-
writeFile(filename, code);
250-
}
251-
await new Promise((resolve) => setTimeout(resolve, 100));
252-
const result = await runtimeRef.current[lang].runFiles(execFiles);
242+
const result = await runtimeRef.current[lang].runFiles(execFiles, codes);
253243
console.log(`${lang} multifile stdout test: `, result);
254244
expect(result).to.be.deep.equal([
255245
{

app/terminal/typescript/runtime.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ export function useTypeScript(jsEval: RuntimeContext): RuntimeContext {
8080

8181
const { writeFile } = useEmbedContext();
8282
const runFiles = useCallback(
83-
async (filenames: string[], files: Record<string, string>) => {
83+
async (filenames: string[], files: Readonly<Record<string, string>>) => {
8484
if (tsEnv === null || typeof window === "undefined") {
8585
return [
8686
{ type: "error" as const, message: "TypeScript is not ready yet." },

app/terminal/wandbox/runtime.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ interface IWandboxContext {
2424
lang: WandboxLang
2525
) => (
2626
filenames: string[],
27-
files: Record<string, string>
27+
files: Readonly<Record<string, string>>
2828
) => Promise<ReplOutput[]>;
2929
}
3030

@@ -70,7 +70,7 @@ export function WandboxProvider({ children }: { children: ReactNode }) {
7070
// Curried function for language-specific file execution
7171
const runFilesWithLang = useCallback(
7272
(lang: WandboxLang) =>
73-
async (filenames: string[], files: Record<string, string>) => {
73+
async (filenames: string[], files: Readonly<Record<string, string>>) => {
7474
if (!selectedCompiler) {
7575
return [
7676
{ type: "error" as const, message: "Wandbox is not ready yet." },

app/terminal/worker/runtime.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,10 @@ export function WorkerProvider({
237237
);
238238

239239
const runFiles = useCallback(
240-
async (filenames: string[], files: Record<string, string>): Promise<ReplOutput[]> => {
240+
async (
241+
filenames: string[],
242+
files: Readonly<Record<string, string>>
243+
): Promise<ReplOutput[]> => {
241244
if (filenames.length !== 1) {
242245
return [
243246
{

0 commit comments

Comments
 (0)