diff --git a/helper/build_config b/helper/build_config index 919540df..e1c351af 100755 --- a/helper/build_config +++ b/helper/build_config @@ -102,6 +102,7 @@ log_debug " and: move ${tt_tmp_output_file} ${output_file}" # We need to use «readlink -f» so that we do not destroy any symlink pointing # to the real file, which we were previously preserving while using «cat». if "$TT_WRAPPER" "${input_file}" > "${tt_tmp_output_file}" 2>/dev/null && + ! grep -q -E '^file error' "${tt_tmp_output_file}" 2>/dev/null && move "${tt_tmp_output_file}" "$(readlink -f "${output_file}")" ; then log_info "Generating ${output_file}: OK" RC=0 @@ -109,6 +110,11 @@ else log_error "Generating ${output_file} based on ${input_file}: FAILED" RC=1 + if [[ -r "${tt_tmp_output_file}" ]] && grep -q -E '^file error' "${tt_tmp_output_file}" ; then + log_error "from generated file:" + log_error " $(grep -E '^file error' "${tt_tmp_output_file}")" + fi + log_info "NOTE: Check those files for valid syntax and encoding:" for f in "${input_file}" ${host_conf:-} ${local_conf:-} "$NGCPCTL_CONFIG" "${NETWORK_CONFIG:-}" ${EXTRA_CONFIG_FILES:-} "$CONSTANTS_CONFIG" ; do [ -r "$f" ] && log_info "$f" diff --git a/t/fixtures/output/bad-syntax.txt b/t/fixtures/output/bad-syntax.txt new file mode 100644 index 00000000..7ee962dc --- /dev/null +++ b/t/fixtures/output/bad-syntax.txt @@ -0,0 +1,2 @@ +file error - parse error - input file handle line 1: unexpected end of directive + [% IF %] \ No newline at end of file diff --git a/t/fixtures/repos/templates/etc/bad-syntax.txt.tt2 b/t/fixtures/repos/templates/etc/bad-syntax.txt.tt2 new file mode 100644 index 00000000..9da11897 --- /dev/null +++ b/t/fixtures/repos/templates/etc/bad-syntax.txt.tt2 @@ -0,0 +1 @@ +[% IF %] diff --git a/t/test_ngcpcfg_build_syntax.py b/t/test_ngcpcfg_build_syntax.py new file mode 100644 index 00000000..6c8a3e10 --- /dev/null +++ b/t/test_ngcpcfg_build_syntax.py @@ -0,0 +1,60 @@ +#!/usr/bin/env py.test-3 + +import filecmp +import os +import pytest +import re +import tempfile +import sys + + +@pytest.mark.tt_46601 +def test_bad_syntax(ngcpcfgcli, tmpdir): + tmpdir = tempfile.mkdtemp(prefix='ngcp-', suffix='-pytest-output') + out = ngcpcfgcli("build", "--ignore-branch-check", + "/etc/bad-syntax.txt", + env={ + 'NGCP_BASE_TT2': os.getcwd(), + 'NGCP_SOCKETFILE': '/tmp/ngcpcfg.socket', + 'OUTPUT_DIRECTORY': tmpdir, + 'NGCPCFG': 'fixtures/ngcpcfg_carrier.cfg', + }) + + # debug, only printed in logs in case of error + print("stdout:") + print(out.stdout.replace("\\n", "\n")) + print("stderr:") + print(out.stderr.replace("\\n", "\n")) + + regex1 = re.compile(r"NOTE: Check those files for valid syntax and " + "encoding.*etc/bad-syntax\.txt\.tt2.*or inspecting " + "temporary /tmp/ngcpcfg\.bad-syntax\.txt\.") + assert re.search(regex1, out.stdout) + + regex2 = re.compile(r"Error: Generating /tmp/ngcp-.*-pytest-output.*" + "/etc/bad-syntax.txt based on .*" + "/etc/bad-syntax\.txt\.tt2: FAILED") + assert re.search(regex2, out.stderr) + + regex3 = re.compile(r"Error: file error - parse error - input file " + "handle line 1: unexpected end of directive") + assert re.search(regex3, out.stderr) + + output_file = os.path.join(tmpdir, "etc/bad-syntax.txt") + test_file = "fixtures/output/bad-syntax.txt" + output_temp_file = re.sub(r".* or inspecting temporary ", "", out.stdout) + output_temp_file = re.sub(r"\\.*", "", output_temp_file) + # debug + print("Output temp file: '%s'" % (output_temp_file)) + + assert not os.path.exists(output_file) + assert os.path.exists(output_temp_file) + assert os.path.exists(test_file) + + # debug + if not filecmp.cmp(output_temp_file, test_file): + print("output_temp_file:") + print(open(output_temp_file).read()) + print("test_file:") + print(open(test_file).read()) + assert filecmp.cmp(output_temp_file, test_file)