예제 #1
0
파일: utils_test.py 프로젝트: ybc1991/Cirq
def test_rewrite_notebook_unused_patterns():
    directory, ipynb_path = write_test_data('d = 5\nd = 4', 'd = 2->d = 3')

    with pytest.raises(AssertionError, match='re.compile'):
        _ = dt.rewrite_notebook(ipynb_path)

    shutil.rmtree(directory)
예제 #2
0
파일: utils_test.py 프로젝트: ybc1991/Cirq
def test_rewrite_notebook_extra_seperator():
    directory, ipynb_path = write_test_data('d = 5\nd = 4',
                                            'd = 5->d = 3->d = 1')

    with pytest.raises(AssertionError, match='only contain one'):
        _ = dt.rewrite_notebook(ipynb_path)

    shutil.rmtree(directory)
예제 #3
0
def test_notebooks_against_released_cirq(partition, notebook_path, base_env):
    """Tests the notebooks in isolated virtual environments.

    In order to speed up the execution of these tests an auxiliary file may be supplied which
    performs substitutions on the notebook to make it faster.

    Specifically for a notebook file notebook.ipynb, one can supply a file notebook.tst which
    contains the substitutes.  The substitutions are provide in the form `pattern->replacement`
    where the pattern is what is matched and replaced. While the pattern is compiled as a
    regular expression, it is considered best practice to not use complicated regular expressions.
    Lines in this file that do not have `->` are ignored.
    """
    notebook_file = os.path.basename(notebook_path)
    notebook_rel_dir = os.path.dirname(os.path.relpath(notebook_path, "."))
    out_path = f"out/{notebook_rel_dir}/{notebook_file[:-6]}.out.ipynb"
    tmpdir, proto_dir = base_env

    notebook_file = os.path.basename(notebook_path)
    dir_name = notebook_file.rstrip(".ipynb")

    notebook_env = os.path.join(tmpdir, f"{dir_name}")

    rewritten_notebook_descriptor, rewritten_notebook_path = rewrite_notebook(
        notebook_path)

    cmd = f"""
mkdir -p out/{notebook_rel_dir}
{proto_dir}/bin/virtualenv-clone {proto_dir} {notebook_env}
cd {notebook_env}
. ./bin/activate
papermill {rewritten_notebook_path} {os.getcwd()}/{out_path}"""
    _, stderr, status = shell_tools.run_shell(
        cmd=cmd,
        log_run_to_stderr=False,
        raise_on_fail=False,
        out=shell_tools.TeeCapture(),
        err=shell_tools.TeeCapture(),
        # important to get rid of PYTHONPATH specifically, which contains
        # the Cirq repo path due to check/pytest
        env={},
    )

    if status != 0:
        print(stderr)
        pytest.fail(
            f"Notebook failure: {notebook_file}, please see {out_path} for the output "
            f"notebook (in Github Actions, you can download it from the workflow artifact"
            f" 'notebook-outputs'). \n"
            f"If this is a new failure in this notebook due to a new change, "
            f"that is only available in master for now, consider adding `pip install --pre cirq` "
            f"instead of `pip install cirq` to this notebook, and exclude it from "
            f"dev_tools/notebooks/isolated_notebook_test.py.")

    if rewritten_notebook_descriptor:
        os.close(rewritten_notebook_descriptor)
예제 #4
0
파일: utils_test.py 프로젝트: ybc1991/Cirq
def test_rewrite_notebook_no_tst_file():
    directory = tempfile.mkdtemp()
    ipynb_path = os.path.join(directory, 'test.ipynb')
    with open(ipynb_path, 'w') as f:
        f.write('d = 5\nd = 4')

    descriptor, path = dt.rewrite_notebook(ipynb_path)

    assert descriptor is None
    assert path == ipynb_path

    shutil.rmtree(directory)
예제 #5
0
파일: utils_test.py 프로젝트: ybc1991/Cirq
def test_rewrite_notebook_ignore_non_seperator_lines():
    directory, ipynb_path = write_test_data('d = 5\nd = 4',
                                            'd = 5->d = 3\n# comment')

    descriptor, path = dt.rewrite_notebook(ipynb_path)

    with open(path, 'r') as f:
        rewritten = f.read()
        assert rewritten == 'd = 3\nd = 4'

    os.close(descriptor)
    shutil.rmtree(directory)
예제 #6
0
파일: utils_test.py 프로젝트: ybc1991/Cirq
def test_rewrite_notebook_multiple():
    directory, ipynb_path = write_test_data('d = 5\nd = 4',
                                            'd = 5->d = 3\nd = 4->d = 1')

    descriptor, path = dt.rewrite_notebook(ipynb_path)

    with open(path, 'r') as f:
        rewritten = f.read()
        assert rewritten == 'd = 3\nd = 1'

    os.close(descriptor)
    shutil.rmtree(directory)
예제 #7
0
def test_notebooks_against_released_cirq(notebook_path):
    """Test that jupyter notebooks execute.

    In order to speed up the execution of these tests an auxiliary file may be supplied which
    performs substitutions on the notebook to make it faster.

    Specifically for a notebook file notebook.ipynb, one can supply a file notebook.tst which
    contains the substitutes.  The substitutions are provide in the form `pattern->replacement`
    where the pattern is what is matched and replaced. While the pattern is compiled as a
    regular expression, it is considered best practice to not use complicated regular expressions.
    Lines in this file that do not have `->` are ignored.
    """
    notebook_file = os.path.basename(notebook_path)
    notebook_rel_dir = os.path.dirname(os.path.relpath(notebook_path, "."))
    out_path = f"out/{notebook_rel_dir}/{notebook_file[:-6]}.out.ipynb"
    rewritten_notebook_descriptor, rewritten_notebook_path = rewrite_notebook(notebook_path)
    papermill_flags = "--no-request-save-on-cell-execute --autosave-cell-every 0"
    cmd = f"""mkdir -p out/{notebook_rel_dir}
papermill {rewritten_notebook_path} {out_path} {papermill_flags}"""

    _, stderr, status = shell_tools.run_shell(
        cmd=cmd,
        log_run_to_stderr=False,
        raise_on_fail=False,
        out=shell_tools.TeeCapture(),
        err=shell_tools.TeeCapture(),
    )

    if status != 0:
        print(stderr)
        pytest.fail(
            f"Notebook failure: {notebook_file}, please see {out_path} for the output "
            f"notebook (in Github Actions, you can download it from the workflow artifact"
            f" 'notebook-outputs')"
        )

    if rewritten_notebook_descriptor:
        os.close(rewritten_notebook_descriptor)