forked from marimo-team/marimo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_cli_export.py
More file actions
747 lines (671 loc) · 23.5 KB
/
test_cli_export.py
File metadata and controls
747 lines (671 loc) · 23.5 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
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
# Copyright 2024 Marimo. All rights reserved.
from __future__ import annotations
import asyncio
import inspect
import subprocess
import sys
from os import path
from pathlib import Path
from typing import TYPE_CHECKING
import pytest
from marimo import __version__
from marimo._dependencies.dependencies import DependencyManager
from tests._server.templates.utils import normalize_index_html
from tests.mocks import snapshotter
if TYPE_CHECKING:
import pathlib
snapshot = snapshotter(__file__)
def _is_win32() -> bool:
return sys.platform == "win32"
class TestExportHTML:
@staticmethod
def test_cli_export_html(temp_marimo_file: str) -> None:
p = subprocess.run(
["marimo", "export", "html", temp_marimo_file],
capture_output=True,
)
assert p.returncode == 0, p.stderr.decode()
html = normalize_index_html(p.stdout.decode())
# Remove folder path
dirname = path.dirname(temp_marimo_file)
html = html.replace(dirname, "path")
assert '<marimo-code hidden=""></marimo-code>' not in html
@staticmethod
def test_cli_export_html_no_code(temp_marimo_file: str) -> None:
p = subprocess.run(
[
"marimo",
"export",
"html",
temp_marimo_file,
"--no-include-code",
],
capture_output=True,
)
assert p.returncode == 0, p.stderr.decode()
html = normalize_index_html(p.stdout.decode())
# Remove folder path
dirname = path.dirname(temp_marimo_file)
html = html.replace(dirname, "path")
assert '<marimo-code hidden=""></marimo-code>' in html
@staticmethod
def test_cli_export_html_wasm(temp_marimo_file: str) -> None:
out_dir = Path(temp_marimo_file).parent / "out"
p = subprocess.run(
[
"marimo",
"export",
"html-wasm",
temp_marimo_file,
"--mode",
"edit",
"--output",
out_dir,
],
capture_output=True,
)
assert p.returncode == 0, p.stderr.decode()
html = Path(out_dir / "index.html").read_text()
assert "<marimo-mode data-mode='edit'" in html
assert (
'<marimo-code hidden="" data-show-code="false"></marimo-code>'
not in html
)
assert "<marimo-wasm" in html
@staticmethod
def test_cli_export_html_wasm_output_is_file(
temp_marimo_file: str,
) -> None:
out_dir = Path(temp_marimo_file).parent / "out_file"
p = subprocess.run(
[
"marimo",
"export",
"html-wasm",
temp_marimo_file,
"--output",
str(out_dir / "foo.html"),
],
capture_output=True,
)
assert p.returncode == 0, p.stderr.decode()
assert Path(out_dir / "foo.html").exists()
assert not Path(out_dir / "index.html").exists()
html = Path(out_dir / "foo.html").read_text()
assert "<marimo-wasm" in html
@staticmethod
def test_cli_export_html_wasm_read(temp_marimo_file: str) -> None:
out_dir = Path(temp_marimo_file).parent / "out"
p = subprocess.run(
[
"marimo",
"export",
"html-wasm",
temp_marimo_file,
"--mode",
"run",
"--output",
out_dir,
],
capture_output=True,
)
assert p.returncode == 0, p.stderr.decode()
html = Path(out_dir / "index.html").read_text()
assert "<marimo-mode data-mode='read'" in html
assert (
'<marimo-code hidden="" data-show-code="false"></marimo-code>'
not in html
)
assert "<marimo-wasm" in html
@staticmethod
def test_cli_export_async(temp_async_marimo_file: str) -> None:
p = subprocess.run(
["marimo", "export", "html", temp_async_marimo_file],
capture_output=True,
)
assert p.returncode == 0, p.stderr.decode()
assert "ValueError" not in p.stderr.decode()
assert "Traceback" not in p.stderr.decode()
html = normalize_index_html(p.stdout.decode())
# Remove folder path
dirname = path.dirname(temp_async_marimo_file)
html = html.replace(dirname, "path")
assert '<marimo-code hidden=""></marimo-code>' not in html
@staticmethod
def test_export_html_with_errors(
temp_marimo_file_with_errors: str,
) -> None:
p = subprocess.run(
["marimo", "export", "html", temp_marimo_file_with_errors],
capture_output=True,
)
assert p.returncode != 0, p.stderr.decode()
html = normalize_index_html(p.stdout.decode())
# Errors but still produces HTML
assert "ZeroDivisionError" in p.stderr.decode()
assert "<marimo-code" in html
@staticmethod
def test_export_html_with_multiple_definitions(
temp_marimo_file_with_multiple_definitions: str,
) -> None:
p = subprocess.run(
[
"marimo",
"export",
"html",
temp_marimo_file_with_multiple_definitions,
],
capture_output=True,
)
assert p.returncode != 0, p.stderr.decode()
# Errors but still produces HTML
assert "MultipleDefinitionError" in p.stderr.decode()
assert "<marimo-code" in p.stdout.decode()
@pytest.mark.skipif(
condition=DependencyManager.watchdog.has(),
reason="hangs when watchdog is installed",
)
async def test_export_watch(self, temp_marimo_file: str) -> None:
temp_out_file = temp_marimo_file.replace(".py", ".html")
p = subprocess.Popen( # noqa: ASYNC101 ASYNC220
[
"marimo",
"export",
"html",
temp_marimo_file,
"--watch",
"--output",
temp_out_file,
],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
# Wait for the message
while True:
line = p.stdout.readline().decode()
if line:
assert f"Watching {temp_marimo_file}" in line
break
assert not path.exists(temp_out_file)
# Modify file
with open(temp_marimo_file, "a") as f: # noqa: ASYNC101 ASYNC230
f.write("\n# comment\n")
assert p.poll() is None
# Wait for rebuild
# TODO: This hangs when watchdog is installed.
while True:
line = p.stdout.readline().decode()
if line:
assert "Re-exporting" in line
break
@pytest.mark.skipif(
condition=DependencyManager.watchdog.has(),
reason="hangs when watchdog is installed",
)
def test_export_watch_no_out_dir(self, temp_marimo_file: str) -> None:
p = subprocess.Popen(
[
"marimo",
"export",
"html",
temp_marimo_file,
"--watch",
],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
# Should return an error
while True:
line = p.stderr.readline().decode()
if line:
assert (
"Cannot use --watch without providing "
+ "an output file with --output."
in line
)
break
class TestExportHtmlSmokeTests:
def assert_not_errored(
self, p: subprocess.CompletedProcess[bytes]
) -> None:
assert p.returncode == 0, p.stderr.decode()
assert not any(
line.startswith("Traceback")
for line in p.stderr.decode().splitlines()
)
assert not any(
line.startswith("Traceback")
for line in p.stdout.decode().splitlines()
)
def assert_has_errors(self, p: subprocess.CompletedProcess[bytes]) -> None:
assert p.returncode != 0, p.stderr.decode()
assert any(
"Export was successful, but some cells failed to execute" in line
for line in p.stderr.decode().splitlines()
), p.stderr.decode()
assert not any(
line.startswith("Traceback")
for line in p.stdout.decode().splitlines()
), p.stdout.decode()
def test_export_intro_tutorial(self, tmp_path: pathlib.Path) -> None:
from marimo._tutorials import intro
file = tmp_path / "intro.py"
out = tmp_path / "out.html"
file.write_text(inspect.getsource(intro), encoding="utf-8")
p = subprocess.run(
["marimo", "export", "html", str(file), "-o", str(out)],
capture_output=True,
)
assert Path(out).exists()
self.assert_not_errored(p)
def test_export_ui_tutorial(self, tmp_path: pathlib.Path) -> None:
from marimo._tutorials import ui as mod
file = tmp_path / "mod.py"
file.write_text(inspect.getsource(mod), encoding="utf-8")
out = tmp_path / "out.html"
p = subprocess.run(
["marimo", "export", "html", str(file), "-o", str(out)],
capture_output=True,
)
assert Path(out).exists()
self.assert_not_errored(p)
def test_export_dataflow_tutorial(self, tmp_path: pathlib.Path) -> None:
from marimo._tutorials import dataflow as mod
file = tmp_path / "mod.py"
file.write_text(inspect.getsource(mod), encoding="utf-8")
out = tmp_path / "out.html"
p = subprocess.run(
["marimo", "export", "html", str(file), "-o", str(out)],
capture_output=True,
)
self.assert_has_errors(p)
def test_export_layout_tutorial(self, tmp_path: pathlib.Path) -> None:
from marimo._tutorials import layout as mod
file = tmp_path / "mod.py"
file.write_text(inspect.getsource(mod), encoding="utf-8")
out = tmp_path / "out.html"
p = subprocess.run(
["marimo", "export", "html", str(file), "-o", str(out)],
capture_output=True,
)
assert Path(out).exists()
self.assert_not_errored(p)
@pytest.mark.skipif(
condition=not DependencyManager.matplotlib.has(),
reason="matplotlib is not installed",
)
def test_export_plots_tutorial(self, tmp_path: pathlib.Path) -> None:
from marimo._tutorials import plots as mod
file = tmp_path / "plots.py"
file.write_text(inspect.getsource(mod), encoding="utf-8")
out = tmp_path / "out.html"
p = subprocess.run(
["marimo", "export", "html", str(file), "-o", str(out)],
capture_output=True,
)
assert Path(out).exists()
self.assert_not_errored(p)
def test_export_marimo_for_jupyter_users(
self, tmp_path: pathlib.Path
) -> None:
from marimo._tutorials import for_jupyter_users as mod
file = tmp_path / "mod.py"
file.write_text(inspect.getsource(mod), encoding="utf-8")
out = tmp_path / "out.html"
p = subprocess.run(
["marimo", "export", "html", str(file), "-o", str(out)],
capture_output=True,
)
assert Path(out).exists()
self.assert_has_errors(p)
class TestExportScript:
@staticmethod
def test_export_script(temp_marimo_file: str) -> None:
p = subprocess.run(
["marimo", "export", "script", temp_marimo_file],
capture_output=True,
)
assert p.returncode == 0, p.stderr.decode()
output = p.stdout.decode()
output = output.replace(__version__, "0.0.0")
snapshot("script.txt", output)
@staticmethod
def test_export_script_async(temp_async_marimo_file: str) -> None:
p = subprocess.run(
["marimo", "export", "script", temp_async_marimo_file],
capture_output=True,
)
assert p.returncode == 2, p.stderr.decode()
assert (
"Cannot export a notebook with async code to a flat script"
in p.stderr.decode()
)
@staticmethod
def test_export_script_with_multiple_definitions(
temp_marimo_file_with_multiple_definitions: str,
) -> None:
p = subprocess.run(
[
"marimo",
"export",
"script",
temp_marimo_file_with_multiple_definitions,
],
capture_output=True,
)
assert (
p.returncode != 0
), "Expected non-zero return code due to multiple definitions"
error_message = p.stderr.decode()
assert (
"MultipleDefinitionError: This app can't be run because it has multiple definitions of the name x"
in error_message
)
@staticmethod
def test_export_script_with_errors(
temp_marimo_file_with_errors: str,
) -> None:
p = subprocess.run(
["marimo", "export", "script", temp_marimo_file_with_errors],
capture_output=True,
)
assert p.returncode == 0, p.stderr.decode()
output = p.stdout.decode()
output = output.replace(__version__, "0.0.0")
snapshot("script_with_errors.txt", output)
@pytest.mark.skipif(
condition=DependencyManager.watchdog.has() or _is_win32(),
reason="hangs when watchdog is installed, flaky on Windows",
)
async def test_export_watch_script(self, temp_marimo_file: str) -> None:
temp_out_file = temp_marimo_file.replace(".py", ".script.py")
p = subprocess.Popen( # noqa: ASYNC101 ASYNC220
[
"marimo",
"export",
"script",
temp_marimo_file,
"--watch",
"--output",
temp_out_file,
],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
# Wait for the message
while True:
line = p.stdout.readline().decode()
if line:
assert f"Watching {temp_marimo_file}" in line
break
assert not path.exists(temp_out_file)
# Modify file
with open(temp_marimo_file, "a") as f: # noqa: ASYNC101 ASYNC230
f.write("\n# comment\n")
assert p.poll() is None
# Wait for rebuild
while True:
line = p.stdout.readline().decode()
if line:
assert "Re-exporting" in line
break
await asyncio.sleep(0.1)
assert path.exists(temp_out_file)
@pytest.mark.skipif(
condition=DependencyManager.watchdog.has(),
reason="hangs when watchdog is installed",
)
def test_export_watch_script_no_out_dir(
self, temp_marimo_file: str
) -> None:
p = subprocess.Popen(
[
"marimo",
"export",
"script",
temp_marimo_file,
"--watch",
],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
# Should return an error
while True:
line = p.stderr.readline().decode()
if line:
assert (
"Cannot use --watch without providing "
+ "an output file with --output."
in line
)
break
class TestExportMarkdown:
@staticmethod
def test_export_markdown(temp_marimo_file: str) -> None:
p = subprocess.run(
["marimo", "export", "md", temp_marimo_file],
capture_output=True,
)
assert p.returncode == 0, p.stderr.decode()
output = p.stdout.decode()
output = output.replace(__version__, "0.0.0")
snapshot("markdown.txt", output)
@staticmethod
def test_export_markdown_async(temp_async_marimo_file: str) -> None:
p = subprocess.run(
["marimo", "export", "md", temp_async_marimo_file],
capture_output=True,
)
assert p.returncode == 0, p.stderr.decode()
output = p.stdout.decode()
output = output.replace(__version__, "0.0.0")
snapshot("async.txt", output)
@staticmethod
def test_export_markdown_broken(temp_unparsable_marimo_file: str) -> None:
p = subprocess.run(
["marimo", "export", "md", temp_unparsable_marimo_file],
capture_output=True,
)
assert p.returncode == 0, p.stderr.decode()
output = p.stdout.decode()
output = output.replace(__version__, "0.0.0")
snapshot("broken.txt", output)
@staticmethod
def test_export_markdown_with_errors(
temp_marimo_file_with_errors: str,
) -> None:
p = subprocess.run(
["marimo", "export", "md", temp_marimo_file_with_errors],
capture_output=True,
)
assert p.returncode == 0, p.stderr.decode()
output = p.stdout.decode()
output = output.replace(__version__, "0.0.0")
snapshot("export_markdown_with_errors.txt", output)
@pytest.mark.skipif(
condition=DependencyManager.watchdog.has() or _is_win32(),
reason="hangs when watchdog is installed, flaky on Windows",
)
async def test_export_watch_markdown(self, temp_marimo_file: str) -> None:
temp_out_file = temp_marimo_file.replace(".py", ".md")
p = subprocess.Popen( # noqa: ASYNC101 ASYNC220
[
"marimo",
"export",
"md",
temp_marimo_file,
"--watch",
"--output",
temp_out_file,
],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
# Wait for the message
while True:
line = p.stdout.readline().decode()
if line:
assert f"Watching {temp_marimo_file}" in line
break
assert not path.exists(temp_out_file)
# Modify file
with open(temp_marimo_file, "a") as f: # noqa: ASYNC101 ASYNC230
f.write("\n# comment\n")
assert p.poll() is None
# Wait for rebuild
while True:
line = p.stdout.readline().decode()
if line:
assert "Re-exporting" in line
break
await asyncio.sleep(0.1)
assert path.exists(temp_out_file)
@pytest.mark.skipif(
condition=DependencyManager.watchdog.has(),
reason="hangs when watchdog is installed",
)
def test_export_watch_markdown_no_out_dir(
self, temp_marimo_file: str
) -> None:
p = subprocess.Popen(
[
"marimo",
"export",
"md",
temp_marimo_file,
"--watch",
],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
# Should return an error
while True:
line = p.stderr.readline().decode()
if line:
assert (
"Cannot use --watch without providing "
+ "an output file with --output."
in line
)
break
class TestExportIpynb:
@pytest.mark.skipif(
not DependencyManager.nbformat.has(),
reason="This test requires nbformat.",
)
def test_export_ipynb(self, temp_marimo_file_with_md: str) -> None:
p = subprocess.run(
["marimo", "export", "ipynb", temp_marimo_file_with_md],
capture_output=True,
)
assert p.returncode == 0, p.stderr.decode()
output = p.stdout.decode()
# ipynb has non-deterministic ids
snapshot("ipynb.txt", output)
@pytest.mark.skipif(
not DependencyManager.nbformat.has(),
reason="This test requires nbformat.",
)
def test_export_ipynb_sort_modes(
self, temp_marimo_file_with_md: str
) -> None:
# Test topological sort (default)
p = subprocess.run(
["marimo", "export", "ipynb", temp_marimo_file_with_md],
capture_output=True,
)
assert p.returncode == 0, p.stderr.decode()
topo_output = p.stdout.decode()
# Test top-down sort
p = subprocess.run(
[
"marimo",
"export",
"ipynb",
temp_marimo_file_with_md,
"--sort",
"top-down",
],
capture_output=True,
)
assert p.returncode == 0, p.stderr.decode()
topdown_output = p.stdout.decode()
snapshot("ipynb_topdown.txt", topdown_output)
# Outputs should be different since sorting is different
assert topo_output != topdown_output
@pytest.mark.skipif(
not DependencyManager.nbformat.has(),
reason="This test requires nbformat.",
)
def test_export_ipynb_with_outputs(
self, temp_marimo_file_with_md: str
) -> None:
# Test without outputs (default)
p = subprocess.run(
["marimo", "export", "ipynb", temp_marimo_file_with_md],
capture_output=True,
)
assert p.returncode == 0, p.stderr.decode()
no_outputs = p.stdout.decode()
# Test with outputs
p = subprocess.run(
[
"marimo",
"export",
"ipynb",
temp_marimo_file_with_md,
"--include-outputs",
],
capture_output=True,
)
assert p.returncode == 0, p.stderr.decode()
with_outputs = p.stdout.decode()
snapshot("ipynb_with_outputs.txt", with_outputs)
# Outputs should be different since one includes execution results
assert no_outputs != with_outputs
@pytest.mark.skipif(
not DependencyManager.nbformat.has(),
reason="This test requires nbformat.",
)
def test_export_ipynb_with_multiple_definitions(
self, temp_marimo_file_with_multiple_definitions: str
) -> None:
p = subprocess.run(
[
"marimo",
"export",
"ipynb",
temp_marimo_file_with_multiple_definitions,
"--include-outputs",
],
capture_output=True,
)
assert p.returncode != 0, p.stderr.decode()
assert "MultipleDefinitionError" in p.stderr.decode()
assert p.stdout.decode() == ""
@pytest.mark.skipif(
not DependencyManager.nbformat.has(),
reason="This test requires nbformat.",
)
def test_export_ipynb_with_errors(
self, temp_marimo_file_with_errors: str
) -> None:
p = subprocess.run(
[
"marimo",
"export",
"ipynb",
temp_marimo_file_with_errors,
"--include-outputs",
],
capture_output=True,
)
assert p.returncode != 0, p.stderr.decode()
assert "ZeroDivisionError" in p.stderr.decode()
output = p.stdout.decode()
output = _delete_lines_with_files(output.replace(__version__, "0.0.0"))
snapshot("ipynb_with_errors.txt", output)
def _delete_lines_with_files(output: str) -> str:
return "\n".join(
line for line in output.splitlines() if "File " not in line
)