-
Notifications
You must be signed in to change notification settings - Fork 391
/
test_cli.py
1047 lines (782 loc) · 30.5 KB
/
test_cli.py
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
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
import os
import sys
import time
from io import StringIO
try:
import unittest.mock as mock
except ImportError:
import mock
import pytest
import nbformat
import itertools
from shutil import copyfile
from jupytext.compare import compare
from argparse import ArgumentTypeError
from nbformat.v4.nbbase import new_notebook, new_markdown_cell, new_code_cell
from jupyter_client.kernelspec import find_kernel_specs, get_kernel_spec
from jupytext import __version__
from jupytext import read, reads, write, writes
from jupytext.cli import parse_jupytext_args, jupytext, system, str2bool
from jupytext.compare import compare_notebooks
from jupytext.paired_paths import paired_paths, InconsistentPath
from jupytext.formats import long_form_one_format, JupytextFormatError
from .utils import list_notebooks, requires_sphinx_gallery
from .utils import requires_jupytext_installed, requires_pandoc, requires_myst
def test_str2bool():
assert str2bool("d") is None
assert str2bool("TRUE") is True
assert str2bool("0") is False
with pytest.raises(ArgumentTypeError):
str2bool("UNEXPECTED")
@pytest.mark.parametrize("nb_file", list_notebooks())
def test_cli_single_file(nb_file):
assert parse_jupytext_args([nb_file] + ["--to", "py"]).notebooks == [nb_file]
@pytest.mark.parametrize("nb_files", [list_notebooks()])
def test_cli_multiple_files(nb_files):
assert parse_jupytext_args(nb_files + ["--to", "py"]).notebooks == nb_files
@pytest.mark.parametrize("nb_file", list_notebooks("ipynb_py"))
def test_convert_single_file_in_place(nb_file, tmpdir):
nb_org = str(tmpdir.join(os.path.basename(nb_file)))
copyfile(nb_file, nb_org)
base, ext = os.path.splitext(nb_org)
nb_other = base + ".py"
jupytext([nb_org, "--to", "py"])
nb1 = read(nb_org)
nb2 = read(nb_other)
compare_notebooks(nb2, nb1)
@pytest.mark.parametrize("nb_file", list_notebooks("ipynb") + list_notebooks("Rmd"))
def test_convert_single_file(nb_file, tmpdir, capsys):
nb_org = str(tmpdir.join(os.path.basename(nb_file)))
copyfile(nb_file, nb_org)
nb1 = read(nb_file)
pynb = writes(nb1, "py")
jupytext([nb_org, "--to", "py", "-o", "-"])
out, err = capsys.readouterr()
assert err == ""
compare(out, pynb)
def test_jupytext_version(capsys):
jupytext(["--version"])
out, err = capsys.readouterr()
assert err == ""
compare(out, __version__ + "\n")
def test_wildcard(tmpdir):
nb1_ipynb = str(tmpdir.join("nb1.ipynb"))
nb2_ipynb = str(tmpdir.join("nb2.ipynb"))
nb1_py = str(tmpdir.join("nb1.py"))
nb2_py = str(tmpdir.join("nb2.py"))
write(new_notebook(metadata={"notebook": 1}), nb1_ipynb)
write(new_notebook(metadata={"notebook": 2}), nb2_ipynb)
os.chdir(str(tmpdir))
jupytext(["nb*.ipynb", "--to", "py"])
assert os.path.isfile(nb1_py)
assert os.path.isfile(nb2_py)
with pytest.raises(IOError):
jupytext(["nb3.ipynb", "--to", "py"])
@pytest.mark.parametrize("nb_file", list_notebooks("ipynb_cpp"))
def test_to_cpluplus(nb_file, tmpdir, capsys):
nb_org = str(tmpdir.join(os.path.basename(nb_file)))
copyfile(nb_file, nb_org)
nb1 = read(nb_org)
text_cpp = writes(nb1, "cpp")
jupytext([nb_org, "--to", "c++", "--output", "-"])
out, err = capsys.readouterr()
assert err == ""
compare(out, text_cpp)
@pytest.mark.parametrize("nb_files", [list_notebooks("ipynb_py")])
def test_convert_multiple_file(nb_files, tmpdir):
nb_orgs = []
nb_others = []
for nb_file in nb_files:
nb_org = str(tmpdir.join(os.path.basename(nb_file)))
base, ext = os.path.splitext(nb_org)
nb_other = base + ".py"
copyfile(nb_file, nb_org)
nb_orgs.append(nb_org)
nb_others.append(nb_other)
jupytext(nb_orgs + ["--to", "py"])
for nb_org, nb_other in zip(nb_orgs, nb_others):
nb1 = read(nb_org)
nb2 = read(nb_other)
compare_notebooks(nb2, nb1)
def test_error_not_notebook_ext_input(tmpdir, capsys):
tmp_file = str(tmpdir.join("notebook.ext"))
with open(tmp_file, "w") as fp:
fp.write("\n")
with pytest.raises(
InconsistentPath, match="is not a notebook. Supported extensions are"
):
jupytext([tmp_file, "--to", "py"])
@pytest.fixture
def tmp_ipynb(tmpdir):
tmp_file = str(tmpdir.join("notebook.ipynb"))
write(new_notebook(), tmp_file)
return tmp_file
@pytest.fixture
def tmp_py(tmpdir):
tmp_file = str(tmpdir.join("notebook.py"))
with open(tmp_file, "w") as fp:
fp.write("\n")
return tmp_file
def test_error_not_notebook_ext_to(tmp_ipynb):
with pytest.raises(JupytextFormatError, match="'ext' is not a notebook extension"):
jupytext([tmp_ipynb, "--to", "ext"])
def test_error_not_notebook_ext_output(tmp_ipynb, tmpdir):
with pytest.raises(
JupytextFormatError,
match="Extension '.ext' is not a notebook extension. Please use one of",
):
jupytext([tmp_ipynb, "-o", str(tmpdir.join("not.ext"))])
def test_error_not_same_ext(tmp_ipynb, tmpdir):
with pytest.raises(InconsistentPath):
jupytext([tmp_ipynb, "--to", "py", "-o", str(tmpdir.join("not.md"))])
def test_error_no_action(tmp_ipynb):
with pytest.raises(ValueError, match="Please provide one of"):
jupytext([tmp_ipynb])
def test_error_update_not_ipynb(tmp_py):
with pytest.raises(ValueError, match="--update is only for ipynb files"):
jupytext([tmp_py, "--to", "py", "--update"])
def test_error_multiple_input(tmp_ipynb):
with pytest.raises(
ValueError, match="Please input a single notebook when using --output"
):
jupytext([tmp_ipynb, tmp_ipynb, "--to", "py", "-o", "notebook.py"])
def test_error_opt_missing_equal(tmp_ipynb):
with pytest.raises(ValueError, match="key=value"):
jupytext([tmp_ipynb, "--to", "py", "--opt", "missing_equal"])
def test_error_unknown_opt(tmp_ipynb):
with pytest.raises(ValueError, match="is not a valid format option"):
jupytext([tmp_ipynb, "--to", "py", "--opt", "unknown=true"])
def test_combine_same_version_ok(tmpdir):
tmp_ipynb = str(tmpdir.join("notebook.ipynb"))
tmp_nbpy = str(tmpdir.join("notebook.py"))
tmp_rmd = str(tmpdir.join("notebook.Rmd"))
with open(tmp_nbpy, "w") as fp:
fp.write(
"""# ---
# jupyter:
# jupytext_formats: ipynb,py
# jupytext_format_version: '1.2'
# ---
# New cell
"""
)
nb = new_notebook(metadata={"jupytext_formats": "ipynb,py"})
write(nb, tmp_ipynb)
# to jupyter notebook
jupytext([tmp_nbpy, "--to", "ipynb", "--update"])
# test round trip
jupytext([tmp_nbpy, "--to", "notebook", "--test"])
# test ipynb to rmd
jupytext([tmp_ipynb, "--to", "rmarkdown"])
nb = read(tmp_ipynb)
cells = nb["cells"]
assert len(cells) == 1
assert cells[0].cell_type == "markdown"
assert cells[0].source == "New cell"
nb = read(tmp_rmd)
cells = nb["cells"]
assert len(cells) == 1
assert cells[0].cell_type == "markdown"
assert cells[0].source == "New cell"
def test_combine_lower_version_raises(tmpdir):
tmp_ipynb = str(tmpdir.join("notebook.ipynb"))
tmp_nbpy = str(tmpdir.join("notebook.py"))
with open(tmp_nbpy, "w") as fp:
fp.write(
"""# ---
# jupyter:
# jupytext_formats: ipynb,py
# jupytext_format_version: '0.0'
# ---
# New cell
"""
)
nb = new_notebook(metadata={"jupytext_formats": "ipynb,py"})
write(nb, tmp_ipynb)
with pytest.raises(ValueError, match="Please remove one or the other file"):
jupytext([tmp_nbpy, "--to", "ipynb", "--update"])
@pytest.mark.parametrize("nb_file", list_notebooks("ipynb_py"))
def test_ipynb_to_py_then_update_test(nb_file, tmpdir):
"""Reproduce https://github.com/mwouts/jupytext/issues/83"""
tmp_ipynb = str(tmpdir.join("notebook.ipynb"))
tmp_nbpy = str(tmpdir.join("notebook.py"))
copyfile(nb_file, tmp_ipynb)
jupytext(["--to", "py", tmp_ipynb])
jupytext(["--test", "--update", "--to", "ipynb", tmp_nbpy])
def test_test_to_ipynb_ignore_version_number_414(
tmpdir,
text="""# ---
# jupyter:
# jupytext:
# text_representation:
# extension: .py
# format_name: light
# format_version: '1.4'
# jupytext_version: 1.1.0
# kernelspec:
# display_name: Python 3
# language: python
# name: python3
# ---
# A short markdown cell
# Followed by a code cell
2 + 2
""",
):
tmp_py = str(tmpdir.join("script.py"))
with open(tmp_py, "w") as fp:
fp.write(text)
assert jupytext(["--test", "--to", "ipynb", tmp_py]) == 0
@pytest.mark.parametrize("nb_file", list_notebooks("ipynb_py"))
def test_convert_to_percent_format(nb_file, tmpdir):
tmp_ipynb = str(tmpdir.join("notebook.ipynb"))
tmp_nbpy = str(tmpdir.join("notebook.py"))
copyfile(nb_file, tmp_ipynb)
jupytext(["--to", "py:percent", tmp_ipynb])
with open(tmp_nbpy) as stream:
py_script = stream.read()
assert "format_name: percent" in py_script
nb1 = read(tmp_ipynb)
nb2 = read(tmp_nbpy)
compare_notebooks(nb2, nb1)
@pytest.mark.parametrize("nb_file", list_notebooks("ipynb_py"))
def test_convert_to_percent_format_and_keep_magics(nb_file, tmpdir):
tmp_ipynb = str(tmpdir.join("notebook.ipynb"))
tmp_nbpy = str(tmpdir.join("notebook.py"))
copyfile(nb_file, tmp_ipynb)
jupytext(["--to", "py:percent", "--opt", "comment_magics=False", tmp_ipynb])
with open(tmp_nbpy) as stream:
py_script = stream.read()
assert "format_name: percent" in py_script
assert "comment_magics: false" in py_script
assert "# %%time" not in py_script
nb1 = read(tmp_ipynb)
nb2 = read(tmp_nbpy)
compare_notebooks(nb2, nb1)
@pytest.mark.parametrize("py_file", list_notebooks("python"))
def test_set_formats(py_file, tmpdir):
tmp_py = str(tmpdir.join("notebook.py"))
tmp_ipynb = str(tmpdir.join("notebook.ipynb"))
copyfile(py_file, tmp_py)
jupytext([tmp_py, "--set-formats", "ipynb,py:light"])
nb = read(tmp_ipynb)
assert nb.metadata["jupytext"]["formats"] == "ipynb,py:light"
@pytest.mark.parametrize("py_file", list_notebooks("python"))
def test_update_metadata(py_file, tmpdir, capsys):
tmp_py = str(tmpdir.join("notebook.py"))
tmp_ipynb = str(tmpdir.join("notebook.ipynb"))
copyfile(py_file, tmp_py)
jupytext(
[
"--to",
"ipynb",
tmp_py,
"--update-metadata",
'{"jupytext":{"formats":"ipynb,py:light"}}',
]
)
nb = read(tmp_ipynb)
assert nb.metadata["jupytext"]["formats"] == "ipynb,py:light"
jupytext(
["--to", "py", tmp_ipynb, "--update-metadata", '{"jupytext":{"formats":null}}']
)
nb = read(tmp_py)
assert "formats" not in nb.metadata["jupytext"]
with pytest.raises(SystemExit):
jupytext(["--to", "ipynb", tmp_py, "--update-metadata", '{"incorrect": "JSON"'])
out, err = capsys.readouterr()
assert "invalid" in err
@pytest.mark.parametrize("py_file", list_notebooks("python"))
def test_set_kernel_inplace(py_file, tmpdir):
tmp_py = str(tmpdir.join("notebook.py"))
copyfile(py_file, tmp_py)
jupytext([tmp_py, "--set-kernel", "-"])
nb = read(tmp_py)
kernel_name = nb.metadata["kernelspec"]["name"]
cmd = get_kernel_spec(kernel_name).argv[0]
assert cmd == "python" or os.path.samefile(cmd, sys.executable)
@pytest.mark.parametrize("py_file", list_notebooks("python"))
def test_set_kernel_auto(py_file, tmpdir):
tmp_py = str(tmpdir.join("notebook.py"))
tmp_ipynb = str(tmpdir.join("notebook.ipynb"))
copyfile(py_file, tmp_py)
jupytext(["--to", "ipynb", tmp_py, "--set-kernel", "-"])
nb = read(tmp_ipynb)
kernel_name = nb.metadata["kernelspec"]["name"]
cmd = get_kernel_spec(kernel_name).argv[0]
assert cmd == "python" or os.path.samefile(cmd, sys.executable)
@pytest.mark.parametrize("py_file", list_notebooks("python"))
def test_set_kernel_with_name(py_file, tmpdir):
tmp_py = str(tmpdir.join("notebook.py"))
tmp_ipynb = str(tmpdir.join("notebook.ipynb"))
copyfile(py_file, tmp_py)
for kernel in find_kernel_specs():
jupytext(["--to", "ipynb", tmp_py, "--set-kernel", kernel])
nb = read(tmp_ipynb)
assert nb.metadata["kernelspec"]["name"] == kernel
with pytest.raises(KeyError):
jupytext(["--to", "ipynb", tmp_py, "--set-kernel", "non_existing_env"])
@pytest.mark.parametrize("nb_file", list_notebooks("ipynb_py"))
def test_paired_paths(nb_file, tmpdir, capsys):
tmp_ipynb = str(tmpdir.join("notebook.ipynb"))
nb = read(nb_file)
nb.metadata.setdefault("jupytext", {})[
"formats"
] = "ipynb,_light.py,_percent.py:percent"
write(nb, tmp_ipynb)
jupytext(["--paired-paths", tmp_ipynb])
out, err = capsys.readouterr()
assert not err
formats = nb.metadata.get("jupytext", {}).get("formats")
assert set(out.splitlines()).union([tmp_ipynb]) == set(
[path for path, _ in paired_paths(tmp_ipynb, "ipynb", formats)]
)
@pytest.mark.parametrize("nb_file", list_notebooks("ipynb_py"))
def test_sync(nb_file, tmpdir, capsys):
tmp_ipynb = str(tmpdir.join("notebook.ipynb"))
tmp_py = str(tmpdir.join("notebook.py"))
tmp_rmd = str(tmpdir.join("notebook.Rmd"))
nb = read(nb_file)
write(nb, tmp_ipynb)
# Test that sync issues a warning when the notebook is not paired
jupytext(["--sync", tmp_ipynb])
_, err = capsys.readouterr()
assert "is not a paired notebook" in err
# Now with a pairing information
nb.metadata.setdefault("jupytext", {})["formats"] = "py,Rmd,ipynb"
write(nb, tmp_ipynb)
# Test that missing files are created
jupytext(["--sync", tmp_ipynb])
assert os.path.isfile(tmp_py)
compare_notebooks(read(tmp_py), nb)
assert os.path.isfile(tmp_rmd)
compare_notebooks(read(tmp_rmd), nb, "Rmd")
write(nb, tmp_rmd, "Rmd")
jupytext(["--sync", tmp_ipynb])
nb2 = read(tmp_ipynb)
compare_notebooks(nb2, nb, "Rmd", compare_outputs=True)
write(nb, tmp_py, "py")
jupytext(["--sync", tmp_ipynb])
nb2 = read(tmp_ipynb)
compare_notebooks(nb2, nb, compare_outputs=True)
# Finally we recreate the ipynb
os.remove(tmp_ipynb)
time.sleep(0.1)
jupytext(["--sync", tmp_py])
nb2 = read(tmp_ipynb)
compare_notebooks(nb2, nb)
# ipynb must be older than py file, otherwise our Contents Manager will complain
assert os.path.getmtime(tmp_ipynb) <= os.path.getmtime(tmp_py)
@requires_pandoc
@pytest.mark.parametrize(
"nb_file", list_notebooks("ipynb_py", skip="(Notebook with|flavors|305)")
)
def test_sync_pandoc(nb_file, tmpdir, capsys):
tmp_ipynb = str(tmpdir.join("notebook.ipynb"))
tmp_md = str(tmpdir.join("notebook.md"))
nb = read(nb_file)
write(nb, tmp_ipynb)
# Test that sync issues a warning when the notebook is not paired
jupytext(["--sync", tmp_ipynb])
_, err = capsys.readouterr()
assert "is not a paired notebook" in err
# Now with a pairing information
nb.metadata.setdefault("jupytext", {})["formats"] = "ipynb,md:pandoc"
write(nb, tmp_ipynb)
# Test that missing files are created
jupytext(["--sync", tmp_ipynb])
assert os.path.isfile(tmp_md)
compare_notebooks(read(tmp_md), nb, "md:pandoc")
with open(tmp_md) as fp:
assert "pandoc" in fp.read()
@pytest.mark.parametrize(
"nb_file,ext",
[(nb_file, ".py") for nb_file in list_notebooks("ipynb_py")]
+ [(nb_file, ".R") for nb_file in list_notebooks("ipynb_R")]
+ [(nb_file, ".jl") for nb_file in list_notebooks("ipynb_julia")],
)
def test_cli_can_infer_jupytext_format(nb_file, ext, tmpdir):
tmp_ipynb = str(tmpdir.join("notebook.ipynb"))
tmp_text = str(tmpdir.join("notebook" + ext))
nb = read(nb_file)
# Light format to Jupyter notebook
write(nb, tmp_text)
jupytext(["--to", "notebook", tmp_text])
nb2 = read(tmp_ipynb)
compare_notebooks(nb2, nb)
# Percent format to Jupyter notebook
write(nb, tmp_text, ext + ":percent")
jupytext(["--to", "notebook", tmp_text])
nb2 = read(tmp_ipynb)
compare_notebooks(nb2, nb)
@pytest.mark.parametrize(
"nb_file,ext",
[(nb_file, ".py") for nb_file in list_notebooks("ipynb_py")]
+ [(nb_file, ".R") for nb_file in list_notebooks("ipynb_R")],
)
def test_cli_to_script(nb_file, ext, tmpdir):
tmp_ipynb = str(tmpdir.join("notebook.ipynb"))
tmp_text = str(tmpdir.join("notebook" + ext))
nb = read(nb_file)
write(nb, tmp_ipynb)
jupytext(["--to", "script", tmp_ipynb])
nb2 = read(tmp_text)
compare_notebooks(nb2, nb)
@pytest.mark.parametrize(
"nb_file,ext",
[(nb_file, ".py") for nb_file in list_notebooks("ipynb_py")]
+ [(nb_file, ".R") for nb_file in list_notebooks("ipynb_R")],
)
def test_cli_to_auto(nb_file, ext, tmpdir):
tmp_ipynb = str(tmpdir.join("notebook.ipynb"))
tmp_text = str(tmpdir.join("notebook" + ext))
nb = read(nb_file)
write(nb, tmp_ipynb)
jupytext(["--to", "auto", tmp_ipynb])
nb2 = read(tmp_text)
compare_notebooks(nb2, nb)
@pytest.mark.parametrize("nb_file", list_notebooks("ipynb_py"))
def test_cli_can_infer_jupytext_format_from_stdin(nb_file, tmpdir):
tmp_ipynb = str(tmpdir.join("notebook.ipynb"))
tmp_py = str(tmpdir.join("notebook.py"))
tmp_rmd = str(tmpdir.join("notebook.Rmd"))
nb = read(nb_file)
# read ipynb notebook on stdin, write to python
with open(nb_file) as fp, mock.patch("sys.stdin", fp):
jupytext(["--to", "py:percent", "-o", tmp_py])
nb2 = read(tmp_py)
compare_notebooks(nb2, nb)
# read python notebook on stdin, write to ipynb
with open(tmp_py) as fp, mock.patch("sys.stdin", fp):
jupytext(["-o", tmp_ipynb])
nb2 = read(tmp_ipynb)
compare_notebooks(nb2, nb)
# read ipynb notebook on stdin, write to R markdown
with open(nb_file) as fp, mock.patch("sys.stdin", fp):
jupytext(["-o", tmp_rmd])
nb2 = read(tmp_rmd)
compare_notebooks(nb2, nb, "Rmd")
# read markdown notebook on stdin, write to ipynb
with open(tmp_rmd) as fp, mock.patch("sys.stdin", fp):
jupytext(["-o", tmp_ipynb])
nb2 = read(tmp_ipynb)
compare_notebooks(nb2, nb, "Rmd")
def test_set_kernel_works_with_pipes_326(capsys):
md = u"""```python
1 + 1
```"""
with mock.patch("sys.stdin", StringIO(md)):
jupytext(["--to", "ipynb", "--set-kernel", "-", "-"])
out, err = capsys.readouterr()
assert err == ""
nb = reads(out, "ipynb")
assert "kernelspec" in nb.metadata
def test_utf8_out_331(capsys, caplog):
py = u"from IPython.core.display import HTML; HTML(u'\xd7')"
with mock.patch("sys.stdin", StringIO(py)):
jupytext(["--to", "ipynb", "--execute", "-"])
if "Timeout" in caplog.text:
pytest.skip(caplog.text) # Issue 489
out, err = capsys.readouterr()
assert err == ""
nb = reads(out, "ipynb")
assert len(nb.cells) == 1
print(nb.cells[0].outputs)
assert nb.cells[0].outputs[0]["data"]["text/html"] == u"\xd7"
@requires_jupytext_installed
def test_cli_expect_errors(tmp_ipynb):
with pytest.raises(ValueError):
jupytext([])
with pytest.raises(ValueError):
jupytext(["--sync"])
with pytest.raises(ValueError):
jupytext([tmp_ipynb, tmp_ipynb, "--paired-paths"])
with pytest.raises(ValueError):
jupytext(["--pre-commit", "notebook.ipynb"])
with pytest.raises(ValueError):
jupytext(["notebook.ipynb", "--from", "py:percent", "--to", "md"])
with pytest.raises(ValueError):
jupytext([])
with pytest.raises(
(SystemExit, TypeError)
): # SystemExit on Windows, TypeError on Linux
system("jupytext", ["notebook.ipynb", "--from", "py:percent", "--to", "md"])
def test_format_prefix_suffix(tmpdir):
os.makedirs(str(tmpdir.join("notebooks")))
tmp_ipynb = str(tmpdir.join("notebooks/notebook_name.ipynb"))
tmp_py = str(tmpdir.join("scripts/notebook_name.py"))
write(new_notebook(), tmp_ipynb)
jupytext([tmp_ipynb, "--to", os.path.join("..", "scripts//py")])
assert os.path.isfile(tmp_py)
os.remove(tmp_py)
jupytext([tmp_ipynb, "--to", "scripts//py", "--from", "notebooks//ipynb"])
assert os.path.isfile(tmp_py)
os.remove(tmp_py)
tmp_ipynb = str(tmpdir.join("notebooks/nb_prefix_notebook_name.ipynb"))
tmp_py = str(tmpdir.join("scripts/script_prefix_notebook_name.py"))
write(new_notebook(), tmp_ipynb)
jupytext(
[
tmp_ipynb,
"--to",
"scripts/script_prefix_/py",
"--from",
"notebooks/nb_prefix_/ipynb",
]
)
assert os.path.isfile(tmp_py)
os.remove(tmp_py)
tmp_ipynb = str(tmpdir.join("notebooks/nb_prefix_notebook_name_nb_suffix.ipynb"))
tmp_py = str(tmpdir.join("scripts/script_prefix_notebook_name_script_suffix.py"))
write(new_notebook(), tmp_ipynb)
jupytext(
[
tmp_ipynb,
"--to",
"scripts/script_prefix_/_script_suffix.py",
"--from",
"notebooks/nb_prefix_/_nb_suffix.ipynb",
]
)
assert os.path.isfile(tmp_py)
os.remove(tmp_py)
def test_cli_sync_file_with_suffix(tmpdir):
tmp_ipynb = str(tmpdir.join("notebook.ipynb"))
tmp_pct_py = str(tmpdir.join("notebook.pct.py"))
tmp_lgt_py = str(tmpdir.join("notebook.lgt.py"))
tmp_rmd = str(tmpdir.join("notebook.Rmd"))
nb = new_notebook(
cells=[new_code_cell(source="1+1")],
metadata={"jupytext": {"formats": "ipynb,.pct.py:percent,.lgt.py:light,Rmd"}},
)
write(nb, tmp_pct_py, ".pct.py:percent")
jupytext(["--sync", tmp_pct_py])
assert os.path.isfile(tmp_lgt_py)
assert os.path.isfile(tmp_rmd)
assert os.path.isfile(tmp_ipynb)
jupytext(["--sync", tmp_lgt_py])
jupytext(["--sync", tmp_ipynb])
assert open(tmp_lgt_py).read().splitlines()[-2:] == ["", "1+1"]
assert open(tmp_pct_py).read().splitlines()[-3:] == ["", "# %%", "1+1"]
assert open(tmp_rmd).read().splitlines()[-4:] == ["", "```{python}", "1+1", "```"]
@requires_sphinx_gallery
def test_rst2md(tmpdir):
tmp_py = str(tmpdir.join("notebook.py"))
tmp_ipynb = str(tmpdir.join("notebook.ipynb"))
# Write notebook in sphinx format
nb = new_notebook(
cells=[
new_markdown_cell("A short sphinx notebook"),
new_markdown_cell(":math:`1+1`"),
]
)
write(nb, tmp_py, fmt="py:sphinx")
jupytext(
[
tmp_py,
"--from",
"py:sphinx",
"--to",
"ipynb",
"--opt",
"rst2md=True",
"--opt",
"cell_metadata_filter=-all",
]
)
assert os.path.isfile(tmp_ipynb)
nb = read(tmp_ipynb)
assert nb.metadata["jupytext"]["cell_metadata_filter"] == "-all"
assert nb.metadata["jupytext"]["rst2md"] is False
# Was rst to md conversion effective?
assert nb.cells[2].source == "$1+1$"
def test_remove_jupytext_metadata(tmpdir):
tmp_ipynb = str(tmpdir.join("notebook.ipynb"))
nb = new_notebook(
metadata={
"jupytext": {
"main_language": "python",
"text_representation": {
"extension": ".md",
"format_name": "markdown",
"format_version": "1.0",
"jupytext_version": "0.8.6",
},
}
}
)
nbformat.write(nb, tmp_ipynb, version=nbformat.NO_CONVERT)
# Jupytext removes the 'text_representation' information from the notebook
jupytext([tmp_ipynb, "--update-metadata", '{"jupytext":{"main_language":null}}'])
nb2 = read(tmp_ipynb)
assert not nb2.metadata
nbformat.write(nb, tmp_ipynb, version=nbformat.NO_CONVERT)
jupytext([tmp_ipynb, "--set-formats", "ipynb,py:light"])
nb2 = read(tmp_ipynb)
assert nb2.metadata == {
"jupytext": {"formats": "ipynb,py:light", "main_language": "python"}
}
@pytest.mark.parametrize(
"nb_file,fmt",
itertools.product(list_notebooks("ipynb_py"), ["py:light", "py:percent", "md"]),
)
def test_convert_and_update_preserves_notebook(nb_file, fmt, tmpdir):
# cannot encode magic parameters in markdown yet
if "magic" in nb_file and fmt == "md":
return
tmp_ipynb = str(tmpdir.join("notebook.ipynb"))
copyfile(nb_file, tmp_ipynb)
ext = long_form_one_format(fmt)["extension"]
tmp_text = str(tmpdir.join("notebook" + ext))
jupytext(["--to", fmt, tmp_ipynb])
jupytext(["--to", "ipynb", "--update", tmp_text])
nb_org = read(nb_file)
nb_now = read(tmp_ipynb)
compare(nb_now, nb_org)
def test_incorrect_notebook_causes_early_exit(tmpdir):
incorrect_ipynb = str(tmpdir.join("incorrect.ipynb"))
incorrect_md = str(tmpdir.join("incorrect.md"))
with open(incorrect_ipynb, "w") as fp:
fp.write(
'{"nbformat": 4, "nbformat_minor": 2, "metadata": {INCORRECT}, "cells": []}'
)
correct_ipynb = str(tmpdir.join("correct.ipynb"))
correct_md = str(tmpdir.join("correct.md"))
with open(correct_ipynb, "w") as fp:
fp.write('{"nbformat": 4, "nbformat_minor": 2, "metadata": {}, "cells": []}')
with pytest.raises(
nbformat.reader.NotJSONError, match="Notebook does not appear to be JSON"
):
jupytext([incorrect_ipynb, correct_ipynb, "--to", "md"])
assert not os.path.exists(incorrect_md)
assert not os.path.exists(correct_md)
def test_warn_only_skips_incorrect_notebook(tmpdir, capsys):
incorrect_ipynb = str(tmpdir.join("incorrect.ipynb"))
incorrect_md = str(tmpdir.join("incorrect.md"))
with open(incorrect_ipynb, "w") as fp:
fp.write(
'{"nbformat": 4, "nbformat_minor": 2, "metadata": {INCORRECT}, "cells": []}'
)
correct_ipynb = str(tmpdir.join("correct.ipynb"))
correct_md = str(tmpdir.join("correct.md"))
with open(correct_ipynb, "w") as fp:
fp.write('{"nbformat": 4, "nbformat_minor": 2, "metadata": {}, "cells": []}')
jupytext([incorrect_ipynb, correct_ipynb, "--to", "md", "--warn-only"])
_, err = capsys.readouterr()
assert "Notebook does not appear to be JSON" in str(err)
assert not os.path.exists(incorrect_md)
assert os.path.exists(correct_md)
@pytest.mark.parametrize("fmt", ["md", "Rmd", "py", "py:percent", "py:hydrogen"])
def test_339_ipynb(tmpdir, fmt):
tmp_ipynb = str(tmpdir.join("test.ipynb"))
nb = new_notebook(cells=[new_code_cell("cat = 42")])
nbformat.write(nb, tmp_ipynb)
assert jupytext([tmp_ipynb, "--to", fmt, "--test-strict"]) == 0
def test_339_py(tmpdir):
"""Test that an incorrect round trip conversion on the text file is detected"""
tmp_py = str(tmpdir.join("test.py"))
with open(tmp_py, "w") as fp:
fp.write(
"""# %%
cat = 42
"""
)
def erroneous_is_magic(line, language, comment_magics, explicitly_code):
return "cat" in line
with mock.patch("jupytext.magics.is_magic", erroneous_is_magic):
assert jupytext([tmp_py, "--to", "ipynb", "--test-strict"]) != 0
def test_339_require_to(tmpdir):
"""Test that the `--to` argument is asked for when a `--test` command is provided"""
tmp_py = str(tmpdir.join("test.py"))
with pytest.raises(ValueError, match="--to"):
jupytext([tmp_py, "--test-strict"])
def test_399_to_script_then_set_formats(tmpdir):
nb = new_notebook(cells=[new_code_cell("1 + 1")])
tmp_py = str(tmpdir.join("notebook_first.py"))
tmp_ipynb = str(tmpdir.join("notebook_first.ipynb"))
nbformat.write(nb, tmp_ipynb)
jupytext(["--to", "py:percent", tmp_ipynb])
assert os.path.isfile(tmp_py)
jupytext(["--set-formats", "ipynb,py:percent", tmp_ipynb])
def test_set_format_with_subfolder(tmpdir):
"""Here we reproduce issue #450"""
py = """# %% [markdown]
# A short notebook
"""
tmpdir.mkdir("python_scripts")
with open(
str(tmpdir.join("python_scripts").join("01_tabular_data_exploration.py")), "w"
) as fp:
fp.write(py)
os.chdir(str(tmpdir))
jupytext(
[
"--set-formats",
"python_scripts//py:percent,notebooks//ipynb",
"python_scripts/01_tabular_data_exploration.py",
]
)
@requires_myst
@requires_pandoc
@pytest.mark.parametrize("format_name", ["md", "md:myst", "md:pandoc"])
def test_create_header_with_set_formats(format_name, tmpdir):
"""Test jupytext --set-formats <format_name> #485"""
tmp_md = str(tmpdir.join("notebook.md"))
with open(tmp_md, "w") as fp:
fp.write("\n")
os.chdir(str(tmpdir))
jupytext(["--set-formats", format_name, "notebook.md"])
nb = read(tmp_md)
assert nb["metadata"]["jupytext"]["formats"] == format_name
@requires_myst
@requires_pandoc
@pytest.mark.parametrize(
"format_name", ["md", "md:myst", "md:pandoc", "py:light", "py:percent"]
)
def test_create_header_with_set_formats_and_set_kernel(format_name, tmpdir):
"""Test jupytext --set-formats <format_name> --set-kernel - #485"""
ext = format_name.split(":")[0]
tmp_nb = str(tmpdir.join("notebook.{}".format(ext)))
with open(tmp_nb, "w") as fp:
fp.write("\n")
jupytext(["--set-formats", format_name, "--set-kernel", "-", tmp_nb])
nb = read(tmp_nb)
assert nb["metadata"]["jupytext"]["formats"] == format_name
assert "kernelspec" in nb["metadata"]
def test_set_option_split_at_heading(tmpdir):
tmp_rmd = tmpdir.join("notebook.Rmd")