Example #1
0
def check_xform(path_to_xform):
    """
    Check the form with the Enketo validator.

    - return code 1: raise error with the stderr content.
    - return code 0: append warning with the stdout content (possibly none).

    :param path_to_xform: Path to the XForm to be validated.
    :return: warnings or List[str]
    """
    if not _node_installed():
        raise EnvironmentError(
            "pyxform enketo validate dependency: node not found")

    returncode, timeout, stdout, stderr = run_popen_with_timeout(
        ["node", ENKETO_VALIDATE_JS, path_to_xform], 100)
    warnings = []
    stderr = decode_stream(stderr)
    stdout = decode_stream(stdout)

    if timeout:
        return ["XForm took to long to completely validate."]
    else:
        if returncode > 0:  # Error invalid
            raise EnketoValidateError('Enketo Validate Errors:\n' +
                                      ErrorCleaner.enketo_validate(stderr))
        elif returncode == 0:
            if stdout:
                warnings.append('Enketo Validate Warnings:\n' + stdout)
            return warnings
        elif returncode < 0:
            return ["Bad return code from Enketo Validate."]
Example #2
0
 def test_cleanup_error_message(self):
     test_str = self.config.get(self.cls_name,
                                "test_cleanup_error_message_test")
     expected_str = self.config.get(self.cls_name,
                                    "test_cleanup_error_message_expected")
     self.assertEqual(ErrorCleaner.odk_validate(test_str),
                      expected_str.strip())
Example #3
0
def check_xform(path_to_xform):
    """
    Returns an array of warnings if the form is valid.
    Throws an exception if it is not
    """
    # provide useful error message if java is not installed
    if not _java_installed():
        raise EnvironmentError(
            "pyxform odk validate dependency: java not found")

    # resultcode indicates validity of the form
    # timeout indicates whether validation ran out of time to complete
    # stdout is not used because it has some warnings that always
    # appear and can be ignored.
    # stderr is treated as a warning if the form is valid or an error
    # if it is invalid.
    returncode, timeout, stdout, stderr = run_popen_with_timeout(
        ["java", "-jar", ODK_VALIDATE_JAR, path_to_xform], 100)
    warnings = []
    stderr = decode_stream(stderr)

    if timeout:
        return ["XForm took to long to completely validate."]
    else:
        if returncode > 0:  # Error invalid
            raise ODKValidateError(
                'ODK Validate Errors:\n' + ErrorCleaner.odk_validate(stderr))
        elif returncode == 0:
            if stderr:
                warnings.append('ODK Validate Warnings:\n' + stderr)
            return warnings
        elif returncode < 0:
            return ["Bad return code from ODK Validate."]
Example #4
0
def check_xform(path_to_xform):
    """Run ODK Validate against the XForm in `path_to_xform`.

    Returns an array of warnings if the form is valid.
    Throws an exception if it is not
    """
    # check for available java version
    check_java_version()

    # resultcode indicates validity of the form
    # timeout indicates whether validation ran out of time to complete
    # stdout is not used because it has some warnings that always
    # appear and can be ignored.
    # stderr is treated as a warning if the form is valid or an error
    # if it is invalid.
    returncode, timeout, _stdout, stderr = _call_validator(
        path_to_xform=path_to_xform)
    warnings = []
    stderr = decode_stream(stderr)

    if timeout:
        return ["XForm took to long to completely validate."]
    else:
        if returncode > 0:  # Error invalid
            raise ODKValidateError("ODK Validate Errors:\n" +
                                   ErrorCleaner.odk_validate(stderr))
        elif returncode == 0:
            if stderr:
                warnings.append("ODK Validate Warnings:\n" + stderr)
        elif returncode < 0:
            return ["Bad return code from ODK Validate."]

    return warnings
Example #5
0
def check_xform(path_to_xform):
    """Run ODK Validate against the XForm in `path_to_xform`.

    Returns an array of warnings if the form is valid.
    Throws an exception if it is not
    """
    # check for available java version
    check_java_version()

    # resultcode indicates validity of the form
    # timeout indicates whether validation ran out of time to complete
    # stdout is not used because it has some warnings that always
    # appear and can be ignored.
    # stderr is treated as a warning if the form is valid or an error
    # if it is invalid.
    returncode, timeout, _stdout, stderr = _call_validator(path_to_xform=path_to_xform)
    warnings = []
    stderr = decode_stream(stderr)

    if timeout:
        return ["XForm took to long to completely validate."]
    else:
        if returncode > 0:  # Error invalid
            raise ODKValidateError(
                b"ODK Validate Errors:\n"
                + ErrorCleaner.odk_validate(stderr).encode("utf-8")
            )
        elif returncode == 0:
            if stderr:
                warnings.append("ODK Validate Warnings:\n" + stderr)
        elif returncode < 0:
            return ["Bad return code from ODK Validate."]

    return warnings
Example #6
0
def check_xform(path_to_xform):
    """
    Check the form with the Enketo validator.

    - return code 1: raise error with the stderr content.
    - return code 0: append warning with the stdout content (possibly none).

    :param path_to_xform: Path to the XForm to be validated.
    :param bin_path: Path to the Enketo-validate binary.
    :return: warnings or List[str]
    """
    if not install_exists():
        raise EnvironmentError(
            "Enketo-validate dependency not found. "
            "Please use the updater tool to install the latest version.")

    returncode, timeout, stdout, stderr = _call_validator(
        path_to_xform=path_to_xform)
    warnings = []
    stderr = decode_stream(stderr)
    stdout = decode_stream(stdout)

    if timeout:
        return ["XForm took to long to completely validate."]
    else:
        if returncode > 0:  # Error invalid
            raise EnketoValidateError(
                'Enketo Validate Errors:\n' +
                ErrorCleaner.enketo_validate(stderr))
        elif returncode == 0:
            if stdout:
                warnings.append('Enketo Validate Warnings:\n' + stdout)
            return warnings
        elif returncode < 0:
            return ["Bad return code from Enketo Validate."]
 def test_cleanup_error_message(self):
     test_str = self.config.get(
         self.cls_name, "test_cleanup_error_message_test")
     expected_str = self.config.get(
         self.cls_name, "test_cleanup_error_message_expected")
     self.assertEqual(
         ErrorCleaner.odk_validate(test_str), expected_str.strip())
Example #8
0
def check_xform(path_to_xform):
    """
    Check the form with the Enketo validator.

    - return code 1: raise error with the stderr content.
    - return code 0: append warning with the stdout content (possibly none).

    :param path_to_xform: Path to the XForm to be validated.
    :param bin_path: Path to the Enketo-validate binary.
    :return: warnings or List[str]
    """
    if not install_exists():
        raise EnvironmentError(
            "Enketo-validate dependency not found. "
            "Please use the updater tool to install the latest version."
        )

    returncode, timeout, stdout, stderr = _call_validator(path_to_xform=path_to_xform)
    warnings = []
    stderr = decode_stream(stderr)
    stdout = decode_stream(stdout)

    if timeout:
        return ["XForm took to long to completely validate."]
    else:
        if returncode > 0:  # Error invalid
            raise EnketoValidateError(
                "Enketo Validate Errors:\n" + ErrorCleaner.enketo_validate(stderr)
            )
        elif returncode == 0:
            if stdout:
                warnings.append("Enketo Validate Warnings:\n" + stdout)
            return warnings
        elif returncode < 0:
            return ["Bad return code from Enketo Validate."]
Example #9
0
 def test_jarfile_error_returned_asis(self):
     """Should return a jarfile error as-is, to avoid tokenising the path."""
     test_str = self.config.get(self.cls_name,
                                "test_jarfile_error_returned_asis_test")
     expected_str = self.config.get(
         self.cls_name, "test_jarfile_error_returned_asis_expected")
     self.assertEqual(ErrorCleaner.odk_validate(test_str),
                      expected_str.strip())
Example #10
0
 def test_single_line_error_still_output(self):
     """Should emit errors that are a single line of text."""
     test_str = self.config.get(self.cls_name,
                                "test_single_line_error_still_output_test")
     expected_str = self.config.get(
         self.cls_name, "test_single_line_error_still_output_expected")
     self.assertEqual(ErrorCleaner.odk_validate(test_str),
                      expected_str.strip())
 def test_jarfile_error_returned_asis(self):
     """Should return a jarfile error as-is, to avoid tokenising the path."""
     test_str = self.config.get(
         self.cls_name, "test_jarfile_error_returned_asis_test")
     expected_str = self.config.get(
         self.cls_name, "test_jarfile_error_returned_asis_expected")
     self.assertEqual(
         ErrorCleaner.odk_validate(test_str), expected_str.strip())
 def test_single_line_error_still_output(self):
     """Should emit errors that are a single line of text."""
     test_str = self.config.get(
         self.cls_name, "test_single_line_error_still_output_test")
     expected_str = self.config.get(
         self.cls_name, "test_single_line_error_still_output_expected")
     self.assertEqual(
         ErrorCleaner.odk_validate(test_str), expected_str.strip())
Example #13
0
 def test_do_not_over_trim_javarosa_errors(self):
     test_str = self.config.get(
         self.cls_name, "test_do_not_over_trim_javarosa_errors_test")
     # Unescape tabs in string
     test_str = test_str.replace("\n\\t", "\n\t")
     expected_str = self.config.get(
         self.cls_name, "test_do_not_over_trim_javarosa_errors_expected")
     self.assertEqual(ErrorCleaner.odk_validate(test_str),
                      expected_str.strip())
 def test_do_not_over_trim_javarosa_errors(self):
     test_str = self.config.get(
         self.cls_name, "test_do_not_over_trim_javarosa_errors_test")
     # Unescape tabs in string
     test_str = test_str.replace("\n\\t", "\n\t")
     expected_str = self.config.get(
         self.cls_name, "test_do_not_over_trim_javarosa_errors_expected")
     self.assertEqual(
         ErrorCleaner.odk_validate(test_str), expected_str.strip())
Example #15
0
 def should_clean_enketo_validate_stacktrace(self):
     message = """Error in gugu/gaga\nError in gugu/gaga"""
     cleaned = ErrorCleaner.enketo_validate(message)
     self.assertEqual(cleaned, "Error in gugu/gaga")
Example #16
0
 def should_clean_odk_validate_stacktrace(self):
     message = """java.lang.NullPointerException Null Pointer\norg.javarosa.xform.parse.XFormParseException Parser"""
     cleaned = ErrorCleaner.odk_validate(message)
     self.assertEqual(cleaned, " Null Pointer\n Parser")