def test_only_whitespace(): for x in _empty_empty: assert only_whitespace(x) is True for x in _empty_nonempty: assert only_whitespace(x) is False for x in _nonempty_nonempty: assert only_whitespace(x) is False
def get_celltests(path_to_notebook): """ Return a dictionary of {code cell number: celltest_info} for all non-empty code cells in the given notebook. celltest_info is a dictionary containing: * 'source' string of the test supplied for a cell, plus the cell itself wherever injected into the test using %cell. * 'cell_injected' flag indicating whether the cell was injected into the test """ notebook = nbformat.read(path_to_notebook, 4) celltests = {} code_cell = 0 for i, cell in enumerate(notebook.cells, start=1): test_source = lines2source(get_test(cell)) test_ast_empty = empty_ast(_inject_cell_into_test("pass", test_source)) if cell.get('cell_type') != 'code': if not test_ast_empty: raise ValueError( "Cell %d is not a code cell, but metadata contains test code!" % i) continue code_cell += 1 if empty_ast( cell['source']): # TODO: maybe this should be only_whitespace? if not test_ast_empty: raise ValueError( "Code cell %d is empty, but test contains code." % code_cell) continue cell_injected = cell_injected_into_test(test_source) if only_whitespace(test_source): celltest = cell['source'] cell_injected = True elif cell_injected is None: raise ValueError( r"Test {}: cell code not injected into test; either add '{}' to the test, or add '{}' to deliberately suppress cell execution" .format(code_cell, CELL_INJ_TOKEN, CELL_SKIP_TOKEN)) elif cell_injected is False: celltest = test_source else: celltest = _inject_cell_into_test(cell['source'], test_source) celltests[code_cell] = { 'source': celltest, 'cell_injected': cell_injected } return celltests