示例#1
0
def test_compare_cells_text_unequal(cell_type):
    """Test comparing cells that contain text and are unequal."""
    diff = diff_notebooks(
        get_test_cell(cell_type, "variable1"),
        get_test_cell(cell_type, "variable2"),
        initial_path="/cells/0",
    )
    assert diff != []
示例#2
0
def test_diff_to_string(file_regression):
    initial = nbformat.read(os.path.join(path, "raw_files",
                                         "different_outputs.ipynb"),
                            as_version=4)
    final = nbformat.read(os.path.join(path, "raw_files",
                                       "different_outputs_altered.ipynb"),
                          as_version=4)
    diff = diff_notebooks(initial, final)
    file_regression.check(diff_to_string(initial, diff))
示例#3
0
def test_notebooks_unequal(data_regression):
    initial = nbformat.read(os.path.join(path, "raw_files",
                                         "different_outputs.ipynb"),
                            as_version=4)
    final = nbformat.read(os.path.join(path, "raw_files",
                                       "different_outputs_altered.ipynb"),
                          as_version=4)
    diff = diff_notebooks(initial, final)
    data_regression.check(mapping_to_dict(diff))
    def check(
        self, path: Union[TextIO, str], raise_errors: bool = True
    ) -> NBRegressionResult:
        """Execute the Notebook and compare its initial vs. final contents.

        if ``force_regen`` is True, the new notebook will be written to ``path``

        if ``raise_errors`` is True:

        :raise nbconvert.preprocessors.CellExecutionError: if error in execution
        :raise NBConfigValidationError: if the notebook metadata is invalid
        :raise NBRegressionError: if diffs present

        :rtype: NBRegressionResult

        """
        __tracebackhide__ = True
        if hasattr(path, "name"):
            abspath = os.path.abspath(path.name)
        else:
            abspath = os.path.abspath(str(path))
        logger.debug(f"Checking file: {abspath}")

        nb_initial, nb_config = load_notebook_with_config(path)

        resources = copy.deepcopy(self.process_resources)
        if not self.exec_cwd:
            self.exec_cwd = os.path.dirname(abspath)

        if self.exec_notebook:
            logger.debug("Executing notebook.")
            exec_results = execute_notebook(
                nb_initial,
                resources=resources,
                cwd=self.exec_cwd,
                timeout=self.exec_timeout,
                allow_errors=self.exec_allow_errors,
                with_coverage=self.coverage,
                cov_config_file=self.cov_config,
                cov_source=self.cov_source,
            )
            exec_error = exec_results.exec_error
            nb_final = exec_results.notebook
            resources = exec_results.resources
        else:
            exec_error = None
            nb_final = nb_initial

        # TODO merge on fail option (using pytest-cov --no-cov-on-fail)
        if self.cov_merge and exec_results.has_coverage:
            logger.info(f"Merging coverage.")
            self.cov_merge.data.update(
                exec_results.coverage_data(self.cov_merge.debug),
                aliases=_get_coverage_aliases(self.cov_merge),
            )
            # we also take this opportunity to remove ''
            # from the unmatched source packages, which is caused by using `--cov=`
            self.cov_merge.source_pkgs_unmatched = [
                p for p in self.cov_merge.source_pkgs_unmatched if p
            ]

        for proc_name in self.post_processors:
            logger.debug(f"Applying post processor: {proc_name}")
            post_proc = load_processor(proc_name)
            nb_final, resources = post_proc(nb_final, resources)

        regex_replace = list(self.diff_replace) + list(nb_config.diff_replace)

        if regex_replace:
            logger.debug(f"Applying replacements: {regex_replace}")
            nb_initial_replace = regex_replace_nb(nb_initial, regex_replace)
            nb_final_replace = regex_replace_nb(nb_final, regex_replace)
        else:
            nb_initial_replace = nb_initial
            nb_final_replace = nb_final

        full_diff = diff_notebooks(nb_initial_replace, nb_final_replace)

        diff_ignore = copy.deepcopy(nb_config.diff_ignore)
        diff_ignore.update(self.diff_ignore)
        logger.debug(f"filtering diff by ignoring: {diff_ignore}")
        filtered_diff = filter_diff(full_diff, diff_ignore)

        diff_string = diff_to_string(
            nb_initial_replace,
            filtered_diff,
            use_color=self.diff_use_color,
            color_words=self.diff_color_words,
        )
        # TODO optionally write diff to file

        regen_exc = None
        if filtered_diff and self.force_regen and not exec_error:

            if hasattr(path, "close") and hasattr(path, "name"):
                path.close()
                with open(path.name, "w") as handle:
                    nbformat.write(nb_final, handle)
            else:
                nbformat.write(nb_final, str(path))

            regen_exc = NBRegressionError(
                f"Files differ and --nb-force-regen set, "
                f"regenerating file at:\n- {abspath}"
            )

        if not raise_errors:
            pass
        elif exec_error:
            print("Diff up to exception:\n" + diff_string, file=sys.stderr)
            raise exec_error
        elif regen_exc:
            print("Diff before regeneration:\n" + diff_string, file=sys.stderr)
            raise regen_exc
        elif filtered_diff:
            raise NBRegressionError(diff_string)

        return NBRegressionResult(
            nb_initial, nb_final, full_diff, filtered_diff, diff_string, resources
        )
示例#5
0
def test_compare_cells_image_unequal(cell_in, cell_out):
    """Test comparing cells that contain images and are unequal."""
    diff = diff_notebooks(
        get_test_cell(cell_in), get_test_cell(cell_out), initial_path="/cells/0"
    )
    assert diff != []
示例#6
0
def test_compare_cells_equal(cell_type):
    """Test comparing cells that are equal."""
    diff = diff_notebooks(
        get_test_cell(cell_type), get_test_cell(cell_type), initial_path="/cells/0"
    )
    assert diff == []
示例#7
0
def test_notebooks_equal(data_regression):
    initial = nbformat.read(os.path.join(path, "raw_files",
                                         "different_outputs.ipynb"),
                            as_version=4)
    diff = diff_notebooks(initial, initial)
    assert diff == []