Ejemplo n.º 1
0
def test_check_config(conf_file, expected, tmpdir):
    """Test config check."""
    conf_file, mp_location = conf_file
    settings_file = conf_file
    init_cwd = str(Path(".").absolute())
    try:
        # If we want to test against config files in isolated directory
        if mp_location != TestSubdir.NONE:
            # Read contents of source file
            tgt_file = Path(conf_file).name
            file_txt = Path(conf_file).read_text()

            dest_file = ("config.json" if tgt_file.endswith(".json") else
                         "msticpyconfig.yaml")
            # write the file to the folder
            tmpdir.join(dest_file).write(file_txt)
            cwd_path = tmpdir
            # If sub-dir, change to the directory, so WorkspaceConfig has to search.
            if mp_location in (TestSubdir.MAIN_ENV_PTR, TestSubdir.SEARCH):
                cwd_path = tmpdir.mkdir("sub_folder")
            os.chdir(cwd_path)
            if mp_location == TestSubdir.SEARCH:
                # Pass non-existing file to custom_mp_config to bypass default settings
                settings_file = "missing_file"
            else:
                settings_file = Path(str(tmpdir)).joinpath(dest_file)

        with custom_mp_config(settings_file, path_check=False):
            if mp_location in (TestSubdir.SEARCH, TestSubdir.NONE):
                with pytest.warns(UserWarning):
                    result, (errs, warns) = _check_config()
            else:
                result, (errs, warns) = _check_config()

            print("result=", result)
            print("errs=", "\n".join(errs) if errs else "no errors")
            print("warnings=", "\n".join(warns) if warns else "no warnings")
            check.equal(result, expected.res, "Result")
            reported_errs = 0 if not errs else len(errs)
            reported_warns = 0 if not warns else len(warns)
            if isinstance(expected.errs, tuple):
                check.is_in(reported_errs, expected.errs, "Num errors")
            else:
                check.equal(reported_errs, expected.errs, "Num errors")
            if isinstance(expected.wrns, tuple):
                check.is_in(reported_warns, expected.wrns, "Num errors")
            else:
                check.equal(reported_warns, expected.wrns, "Num warnings")
    finally:
        os.chdir(init_cwd)
Ejemplo n.º 2
0
def test_check_config():
    """Test config check."""
    mp_var = os.environ.get("MSTICPYCONFIG")
    mp_file = TEST_DATA_PATH + "/msticpyconfig.yaml"
    os.environ["MSTICPYCONFIG"] = mp_file
    result, err_warn = _check_config()
    if not result:
        # If failed - err_warn should be set
        # and item 0 should be populated with errors
        check.is_not_none(err_warn)
        check.is_true(err_warn[0])
    else:
        # otherwise we have no errors or warnings or
        # just warnings
        if err_warn:
            check.is_false(err_warn[0])
            check.is_true(err_warn[1])
    os.environ["MSTICPYCONFIG"] = mp_var