Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update tests to permit any order of output lines by sorting the editorconfig output #23

Merged
merged 5 commits into from
Nov 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 31 additions & 6 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
#
# Copyright (c) 2011-2012 EditorConfig Team
# Copyright (c) 2011-2018 EditorConfig Team
# All rights reserved.
#
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
#
# 1. Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
Expand All @@ -29,11 +29,15 @@
# testing only.
project(editorconfig-core-test NONE)

# Where this file lives
set(tests_cmakelists_dir "${CMAKE_CURRENT_LIST_DIR}")
#message(STATUS "Tests are in ${tests_cmakelists_dir}")

# Only when we are using editorconfig-core-test independently should we check
# cmake version, set EDITORCONFIG_CMD as cache string, and enable_testing()
# here.
if(${CMAKE_SOURCE_DIR} STREQUAL ${CMAKE_CURRENT_SOURCE_DIR})
cmake_minimum_required(VERSION 2.6)
cmake_minimum_required(VERSION 3.5)
set(EDITORCONFIG_CMD "editorconfig" CACHE STRING "editorconfig command.")
enable_testing()
endif()
Expand All @@ -58,9 +62,30 @@ function(new_ec_test_full_ec_file_path name ec_file src_file regex)
set_tests_properties(${name} PROPERTIES PASS_REGULAR_EXPRESSION "${regex}")
endfunction()

# A test that returns multiple lines of output. CAUTION: filenames used in
# these tests may not contain semicolons.
function(new_ec_test_multiline name ec_file src_file regex)
#message(STATUS "Building multiline test ${name} with tests_cmakelists_dir ${tests_cmakelists_dir}")
#message(STATUS "List dir ${CMAKE_CURRENT_LIST_DIR}, source dir ${CMAKE_CURRENT_SOURCE_DIR}")
add_test(${name} "cmake"
"-D" "EDITORCONFIG_CMD=${EDITORCONFIG_CMD}"
# Since variables aren't autpmatically passed to the inner cmake
"-D" "ECARGS:LIST=-f;${ec_file};${CMAKE_CURRENT_SOURCE_DIR}/${src_file}"
# Note: the semicolons separate list elements.
"-P" "${tests_cmakelists_dir}/cmake/ec_sort.cmake")
set_tests_properties(${name} PROPERTIES PASS_REGULAR_EXPRESSION
"^[\n]*${regex}$")
# Permit leading \n's because I can't always get rid of them using
# only CMake-provided facilities.
endfunction()


# First, make sure the test harness works.
add_subdirectory(meta)

# Then test the core.
add_subdirectory(glob)
add_subdirectory(properties)
add_subdirectory(parser)
add_subdirectory(filetree)
add_subdirectory(cli)

2 changes: 1 addition & 1 deletion LICENSE.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright (c) 2011-2016 EditorConfig Team
Copyright (c) 2011-2018 EditorConfig Team
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
Expand Down
15 changes: 9 additions & 6 deletions cli/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
#
# Copyright (c) 2011-2012 EditorConfig Team
# Copyright (c) 2011-2018 EditorConfig Team
# All rights reserved.
#
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
#
# 1. Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
Expand Down Expand Up @@ -40,10 +40,13 @@ set_tests_properties(test_short_version_switch PROPERTIES

# Test for multiple input files

# when files are specified on command line
# when files are specified on command line. The files can appear in either
# order in the output, but each file's output line must be grouped with its
# file header. Handle this by listing both possibilities manually in the regex.
add_test(multiple_files_on_command_line ${EDITORCONFIG_CMD} -f cli.in
"${CMAKE_CURRENT_SOURCE_DIR}/file1.c"
"${CMAKE_CURRENT_SOURCE_DIR}/file2.cpp")
set_tests_properties(multiple_files_on_command_line PROPERTIES
PASS_REGULAR_EXPRESSION
"^\\[${CMAKE_CURRENT_SOURCE_DIR}/file1.c\\][ \t]*[\n\r]+key1=value1[ \t]*[\n\r]+\\[${CMAKE_CURRENT_SOURCE_DIR}/file2.cpp\\][ \t]*[\n\r]+key2=value2[ \t\n\r]*$")
"^(\\[${CMAKE_CURRENT_SOURCE_DIR}/file1.c\\][ \t]*[\n\r]+key1=value1[ \t]*[\n\r]+\\[${CMAKE_CURRENT_SOURCE_DIR}/file2.cpp\\][ \t]*[\n\r]+key2=value2)|(\\[${CMAKE_CURRENT_SOURCE_DIR}/file2.cpp\\][ \t]*[\n\r]+key2=value2[ \t]*[\n\r]+\\[${CMAKE_CURRENT_SOURCE_DIR}/file1.c\\][ \t]*[\n\r]+key1=value1)[ \t\n\r]*$"
)
44 changes: 44 additions & 0 deletions cmake/almostcat.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# almostcat.cmake: print the file named in ${WHICH} to **stderr**. Puts an
# extra space at the beginning to suppress CMake's word wrapping. May add
# an extra \n at the end because that's what message() does.

# BSD-2-Clause
# Copyright 2018 Christopher White (cxw42 at GitHub; http://devwrench.com)
# All rights reserved
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.

cmake_minimum_required(VERSION 3.5)

if("${WHICH}" STREQUAL "")
message(FATAL_ERROR "No WHICH parameter specified")
return()
endif()

#message(FATAL_ERROR " Reading ${WHICH}") # Uncomment for debugging
file(READ "${WHICH}" contents)

# message() will give us an extra \n, so trim one if we can.
string(REGEX REPLACE "(\r\n|\r|\n)$" "" lines "${lines}")

# Print, with a leading space as noted above.
message(" ${contents}")
70 changes: 70 additions & 0 deletions cmake/ec_sort.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# ec_sort.cmake: Run editorconfig and sort its output
# For use in editorconfig; see
# https://github.com/editorconfig/editorconfig/issues/375 .

# Call as, e.g.:
# cmake -D EDITORCONFIG_CMD="../editorconfig" -D ECARGS:LIST="-f;.editorconfig;foo" -P cmake/ec_sort.cmake
# EDITORCONFIG_CMD may also be list-valued. EDITORCONFIG_CMD and ECARGS
# are put together on the command line, in that order, and split by CMake.

# BSD-2-Clause
# Copyright 2018 Christopher White (cxw42 at GitHub; http://devwrench.com)
# All rights reserved
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.

cmake_minimum_required(VERSION 3.5)

# See documentation links at https://stackoverflow.com/q/12802377/2877364
set(tests_cmake_ec_sort_dir "${CMAKE_CURRENT_LIST_DIR}")
list(APPEND CMAKE_MODULE_PATH "${tests_cmake_ec_sort_dir}/../cmake")
include(runandsort)

# Required parameters are in variables: EDITORCONFIG_CMD and ECARGS
if("${EDITORCONFIG_CMD}" STREQUAL "")
message(FATAL_ERROR "No EDITORCONFIG_CMD parameter specified")
return()
endif()

if("${ECARGS}" STREQUAL "")
message(FATAL_ERROR "No ECARGS parameter specified")
return()
endif()

# Uncomment for debugging
#message(FATAL_ERROR " Running ${EDITORCONFIG_CMD} with ${ECARGS}")

run_and_sort(RETVAL lines RETVAL_FAILURE did_fail
CMDLINE ${EDITORCONFIG_CMD} ${ECARGS}
)

if(${did_fail})
message(FATAL_ERROR "${EDITORCONFIG_CMD} ${ECARGS} returned a nonzero exit code")
return()
endif()

# message() will give us an extra \n, so trim one if we can.
string(REGEX REPLACE "(\r\n|\r|\n)$" "" lines "${lines}")

# Output **to stderr**. If we used message(STATUS...), it would print to
# stdout but also emit a leading "--".
message("${lines}")
131 changes: 131 additions & 0 deletions cmake/runandsort.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
# runandsort.cmake: Run a program and sort its output
# For use in editorconfig; see
# https://github.com/editorconfig/editorconfig/issues/375 .

# BSD-2-Clause
# Copyright 2018 Christopher White (cxw42 at GitHub; http://devwrench.com)
# All rights reserved
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.

cmake_minimum_required(VERSION 3.5)

# Conventions:
# - "P_*" are parameters parsed using cmake_parse_arguments
# - Any parameter called "RETVAL", "RETVAL_*", or "*_NAME" should be given
# the name of a variable in the caller's scope into which to
# store results.

# Run a program with the given arguments, and return its sorted output as
# a string.
# CAUTION 1: May not produce correct output if any output line includes
# a semicolon, because that is a list separator in CMake.
# CAUTION 2: Depends on sort order of CMake; see discussion at
# https://gitlab.kitware.com/cmake/cmake/issues/18551
#
# Limitations:
# Any \x01 in the string will be corrupted - this routine uses those to work
# around CMake limitations.

# Arguments:
# RETVAL . . . . . . . The name of the variable to store the result in
# CMDLINE . . . . . . . The program to run, and any arguments
# RETVAL_FAILURE . . . . If present, a variable that will be set to TRUE
# if PGM returns a non-zero exit code.
# CAPTURE_STDERR . . . . If present, capture stderr instead of stdout
# TRIM_INITIAL_LEADING_SPACE If present, remove initial spaces from the
# first line. This is to work around a hack
# in almostcat.cmake.
#
# Returns:
# The sorted stdout of PGM, or the FAILURE string if PGM failed.
# PGM's stderr is ignored.

function(run_and_sort)
# Argument parsing
set(option_keywords CAPTURE_STDERR TRIM_INITIAL_LEADING_SPACE)
set(one_value_keywords RETVAL RETVAL_FAILURE)
set(multi_value_keywords CMDLINE ARGS)
cmake_parse_arguments(P "${option_keywords}" "${one_value_keywords}"
"${multi_value_keywords}" ${ARGN})

#message(STATUS "Running ${P_CMDLINE}")
execute_process(COMMAND ${P_CMDLINE}
RESULT_VARIABLE ep_retval
OUTPUT_VARIABLE ep_stdout
ERROR_VARIABLE ep_stderr
)

# Which one are we processing?
if(${P_CAPTURE_STDERR})
set(ep_out "${ep_stderr}")
else()
set(ep_out "${ep_stdout}")
endif()

#message(STATUS "Got stdout =${ep_stdout}=")
#message(STATUS "Got stderr =${ep_stderr}=")

# Early bail on failure
if(NOT("${ep_retval}" EQUAL "0"))
set(${P_RETVAL} "" PARENT_SCOPE)
if("${P_RETVAL_FAILURE}" MATCHES ".") # if we got a name
set(${P_RETVAL_FAILURE} TRUE PARENT_SCOPE)
endif()
return()
endif()

# Trim hack
if(${P_TRIM_INITIAL_LEADING_SPACE})
string(REGEX REPLACE "^[ ]+" "" ep_out "${ep_out}")
endif()

# Change all the semicolons in the output to \x01
string(ASCII 1 ONE)
string(REPLACE ";" "${ONE}" ep_out "${ep_out}")
#message(STATUS "After escaping =${ep_out}=")

# Normalize line endings, just in case
string(REGEX REPLACE "\r|\n|\r\n" "\n" ep_out "${ep_out}")
#message(STATUS "After line-endings =${ep_out}=")

# Turn the string into a list
string(REPLACE "\n" ";" ep_out "${ep_out}")
#message(STATUS "After listifying =${ep_out}=")

# Sort the list
list(SORT ep_out)

# Back to individual lines
string(REPLACE ";" "\n" ep_out "${ep_out}")
#message(STATUS "After back to lines =${ep_out}=")

# And back to semicolons. Note: I am not trying to reverse line endings.
string(REPLACE "${ONE}" ";" ep_out "${ep_out}")
#message(STATUS "After unescaping =${ep_out}=")

# Out to the caller
set(${P_RETVAL} "${ep_out}" PARENT_SCOPE)
#message(STATUS "Returned =${ep_out}=")

endfunction(run_and_sort)

Loading