def tester(import_list_dict, capsys): # http://nbviewer.ipython.org/gist/fperez/9716279 for import_dict in import_list_dict: cell_code = [import_dict['code']] target = import_dict['targets'] with write_notebook(cell_code) as fname: # parse the notebook! assert target == main.notebook_path_to_dependencies(fname) # check the notebook cli _run_cli(path_to_check=fname) stdout, stderr = capsys.readouterr() assert target == eval(stdout)
def test_multiple_code_cells(capsys): targets = defaultdict(set) import_list_dict = complex_imports + relative_imports + simple_imports # http://nbviewer.ipython.org/gist/fperez/9716279 code_for_cells = [] for import_dict in import_list_dict: code_for_cells.append(import_dict['code']) target = import_dict['targets'] for k, v in target.items(): targets[k].update(set(v)) # turn targets into a dict of sorted lists targets = {k: sorted(list(v)) for k, v in targets.items()} with write_notebook(code_for_cells) as fname: # parse the notebook! assert targets == main.notebook_path_to_dependencies(fname) # check the notebook cli _run_cli(path_to_check=fname) stdout, stderr = capsys.readouterr() assert targets == eval(stdout)
def test_notebook_remapping(): code = "import mpl_toolkits" with write_notebook([code]) as fname: deps = main.notebook_path_to_dependencies(fname, remap=False) assert {'required': ['mpl_toolkits']} == deps assert {} == main.notebook_path_to_dependencies(fname)