Skip to content
This repository has been archived by the owner on Apr 28, 2022. It is now read-only.

Commit

Permalink
Merge pull request #5 from hunter-packages/pr.resdir
Browse files Browse the repository at this point in the history
Add GAUZE_RESOUCE_DIR
  • Loading branch information
ruslo authored Nov 12, 2017
2 parents 9b66d10 + 7d5b022 commit 7791735
Show file tree
Hide file tree
Showing 14 changed files with 259 additions and 31 deletions.
1 change: 1 addition & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ Unified CTest-friendly testing framework for Windows, Linux, OSX, iOS, Android.
--param1 1
--param2 2
--resource $<GAUZE_RESOURCE_FILE:${CMAKE_CURRENT_LIST_DIR}/data/input.txt>
--directory-with-resources $<GAUZE_RESOURCE_DIR:${CMAKE_CURRENT_LIST_DIR}/data/myres/>
--just-string ${CMAKE_CURRENT_LIST_DIR}/data/just-string.txt
)
Expand Down
5 changes: 0 additions & 5 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,6 @@ environment:
- TOOLCHAIN: "vs-14-2015"
CONFIG: "Debug"

- TOOLCHAIN: "vs-9-2008"
CONFIG: "Release"
- TOOLCHAIN: "vs-9-2008"
CONFIG: "Debug"

- TOOLCHAIN: "mingw"
CONFIG: "Release"
- TOOLCHAIN: "mingw"
Expand Down
1 change: 1 addition & 0 deletions cmake/gauze_add_test.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ function(gauze_add_test)
foreach(x ${APP_ARGUMENTS})
# Use resources as is
string(REGEX REPLACE "^\\$<GAUZE_RESOURCE_FILE:\(.*\)>$" "\\1" x "${x}")
string(REGEX REPLACE "^\\$<GAUZE_RESOURCE_DIR:\(.*\)>$" "\\1" x "${x}")
list(APPEND arguments "${x}")
endforeach()
add_test(NAME "${x_NAME}" COMMAND ${APP_TARGET} ${arguments})
Expand Down
90 changes: 77 additions & 13 deletions cmake/templates/AndroidTest.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -74,33 +74,96 @@ set(app_arguments "@APP_ARGUMENTS@")
set(arguments)
foreach(arg ${app_arguments})
string(
REGEX REPLACE "^\\$<GAUZE_RESOURCE_FILE:\(.*\)>$" "\\1" resource "${arg}"
REGEX
REPLACE
"^\\$<GAUZE_RESOURCE_FILE:\(.*\)>$"
"\\1"
resource_file
"${arg}"
)
if("${resource}" STREQUAL "${arg}")
# nothing replaced => not a resource
set(arguments "${arguments} \"${arg}\"")

string(
REGEX
REPLACE
"^\\$<GAUZE_RESOURCE_DIR:\(.*\)>$"
"\\1"
resource_dir
"${arg}"
)

set(is_resource_file FALSE)
set(is_resource_dir FALSE)

if("${resource_file}" STREQUAL "${arg}")
# nothing replaced => not a resource file
if("${resource_dir}" STREQUAL "${arg}")
# nothing replaced => not a directory with resources
else()
# '$<GAUZE_RESOURCE_DIR:...>' detected => directory with resources
set(is_resource_dir TRUE)
endif()
else()
# '$<GAUZE_RESOURCE_FILE:...>' detected => resource file
message("Resource detected: '${resource}'")
if(NOT IS_ABSOLUTE "${resource}")
set(resource "@RESOURCE_DIR@/${resource}")
set(is_resource_file TRUE)
endif()

if(is_resource_file)
message("Resource detected: '${resource_file}'")
if(NOT IS_ABSOLUTE "${resource_file}")
set(resource_file "@RESOURCE_DIR@/${resource_file}")
endif()
if(NOT EXISTS "${resource_file}")
message(FATAL_ERROR "File not found: ${resource_file}")
endif()
if(NOT EXISTS "${resource}")
message(FATAL_ERROR "File not found: ${resource}")
if(IS_DIRECTORY "${resource_file}")
message(FATAL_ERROR "Not a file but directory: ${resource_file}")
endif()
get_filename_component(res_name "${resource}" NAME)
get_filename_component(res_name "${resource_file}" NAME)
set(res_path "${data_dir}/${res_name}")
set(arguments "${arguments} \"${res_path}\"")

message("Push resource to Android device:")
message(" '${resource}'")
message(" '${resource_file}'")
message(" -> '${res_path}'")

set(cmd ${adb_command} push "${resource}" "${res_path}")
set(cmd ${adb_command} push "${resource_file}" "${res_path}")
execute_process(COMMAND ${cmd} RESULT_VARIABLE result)
if(NOT result EQUAL 0)
message(FATAL_ERROR "Command failed: ${cmd}")
endif()
elseif(is_resource_dir)
message("Directory with resources detected: '${resource_dir}'")
if(NOT IS_ABSOLUTE "${resource_dir}")
set(resource_dir "@RESOURCE_DIR@/${resource_dir}")
endif()
if(NOT EXISTS "${resource_dir}")
message(FATAL_ERROR "Directory not found: ${resource_dir}")
endif()
if(NOT IS_DIRECTORY "${resource_dir}")
message(FATAL_ERROR "Not a directory: ${resource_dir}")
endif()
get_filename_component(res_name "${resource_dir}" NAME)
set(res_path "${data_dir}/${res_name}")
set(arguments "${arguments} \"${res_path}\"")

message("Remove destination directory: '${res_path}'")
set(cmd ${adb_command} shell rm -rf \"${res_path}\")
execute_process(COMMAND ${cmd} RESULT_VARIABLE result)
if(NOT result EQUAL 0)
message(FATAL_ERROR "Command failed: ${cmd}")
endif()

message("Push directory to Android device:")
message(" '${resource_dir}'")
message(" -> '${res_path}'")

set(cmd ${adb_command} push "${resource_dir}" "${res_path}")
execute_process(COMMAND ${cmd} RESULT_VARIABLE result)
if(NOT result EQUAL 0)
message(FATAL_ERROR "Command failed: ${cmd}")
endif()
else()
set(arguments "${arguments} \"${arg}\"")
endif()
endforeach()

Expand Down Expand Up @@ -132,9 +195,10 @@ endif()
message("Set LD_LIBRARY_PATH to '${libs_dir}'")
message("Run command on Android device:")
message("[@TESTING_DIR@]> \"${exe_path}\" ${arguments}")
# Note: Add '2>&1' redirection because otherwise 'cerr' will mess up with 'echo $?'
execute_process(
COMMAND
${adb_command} shell "cd \"@TESTING_DIR@\"; export LD_LIBRARY_PATH=\"${libs_dir}\"; \"${exe_path}\" ${arguments}; echo $?"
${adb_command} shell "cd \"@TESTING_DIR@\"; export LD_LIBRARY_PATH=\"${libs_dir}\"; \"${exe_path}\" ${arguments} 2>&1; echo $?"
RESULT_VARIABLE result
OUTPUT_VARIABLE output
ERROR_VARIABLE output
Expand Down
81 changes: 69 additions & 12 deletions cmake/templates/iOSTest.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -93,32 +93,89 @@ set(app_arguments "@APP_ARGUMENTS@")
set(arguments)
foreach(arg ${app_arguments})
string(
REGEX REPLACE "^\\$<GAUZE_RESOURCE_FILE:\(.*\)>$" "\\1" resource "${arg}"
REGEX
REPLACE
"^\\$<GAUZE_RESOURCE_FILE:\(.*\)>$"
"\\1"
resource_file
"${arg}"
)
if("${resource}" STREQUAL "${arg}")
# nothing replaced => not a resource
set(arguments "${arguments} \"${arg}\"")

string(
REGEX
REPLACE
"^\\$<GAUZE_RESOURCE_DIR:\(.*\)>$"
"\\1"
resource_dir
"${arg}"
)

set(is_resource_file FALSE)
set(is_resource_dir FALSE)

if("${resource_file}" STREQUAL "${arg}")
# nothing replaced => not a resource file
if("${resource_dir}" STREQUAL "${arg}")
# nothing replaced => not a directory with resources
else()
# '$<GAUZE_RESOURCE_DIR:...>' detected => directory with resources
set(is_resource_dir TRUE)
endif()
else()
# '$<GAUZE_RESOURCE_FILE:...>' detected => resource file
message("Resource detected: '${resource}'")
if(NOT IS_ABSOLUTE "${resource}")
set(resource "@RESOURCE_DIR@/${resource}")
set(is_resource_file TRUE)
endif()

if(is_resource_file)
message("Resource detected: '${resource_file}'")
if(NOT IS_ABSOLUTE "${resource_file}")
set(resource_file "@RESOURCE_DIR@/${resource_file}")
endif()
if(NOT EXISTS "${resource}")
message(FATAL_ERROR "File not found: ${resource}")
if(NOT EXISTS "${resource_file}")
message(FATAL_ERROR "File not found: ${resource_file}")
endif()
get_filename_component(res_name "${resource}" NAME)
if(IS_DIRECTORY "${resource_file}")
message(FATAL_ERROR "Not a file but directory: ${resource_file}")
endif()
get_filename_component(res_name "${resource_file}" NAME)
set(res_path "@GAUZE_IOS_UPLOAD_ROOT@/${res_name}")

# We need to keep GAUZE_RESOURCE_FILE for one more round of
# expansion because we need runtime HOME variable (see 'gauze.cpp')
set(arguments "${arguments} \"\$<GAUZE_RESOURCE_FILE:${res_path}>\"")

message("Push resource to iOS device (bundle-id '@BUNDLE_ID@'):")
message(" '${resource}'")
message(" '${resource_file}'")
message(" -> '${res_path}'")

run_cmd(${ios_deploy} --bundle_id "@BUNDLE_ID@" --upload "${resource}" --to "${res_path}")
run_cmd(${ios_deploy} --bundle_id "@BUNDLE_ID@" --upload "${resource_file}" --to "${res_path}")
elseif(is_resource_dir)
message("Directory with resources detected: '${resource_dir}'")
if(NOT IS_ABSOLUTE "${resource_dir}")
set(resource_dir "@RESOURCE_DIR@/${resource_dir}")
endif()
if(NOT EXISTS "${resource_dir}")
message(FATAL_ERROR "Directory not found: ${resource_dir}")
endif()
if(NOT IS_DIRECTORY "${resource_dir}")
message(FATAL_ERROR "Not a directory: ${resource_dir}")
endif()
get_filename_component(res_name "${resource_dir}" NAME)
set(res_path "@GAUZE_IOS_UPLOAD_ROOT@/${res_name}")

# We need to keep GAUZE_RESOURCE_FILE for one more round of
# expansion because we need runtime HOME variable (see 'gauze.cpp')
# NOTE: GAUZE_RESOURCE_FILE used instead of GAUZE_RESOURCE_DIR
# to simplify logic (see gauze.cpp)
set(arguments "${arguments} \"\$<GAUZE_RESOURCE_FILE:${res_path}>\"")

message("Push directory to iOS device (bundle-id '@BUNDLE_ID@'):")
message(" '${resource_dir}'")
message(" -> '${res_path}'")

run_cmd(${ios_deploy} --bundle_id "@BUNDLE_ID@" --upload "${resource_dir}" --to "${res_path}")
else()
set(arguments "${arguments} \"${arg}\"")
endif()
endforeach()

Expand Down
40 changes: 40 additions & 0 deletions docs/examples/directory.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
Directory
---------

To copy directory with resources use ``$<GAUZE_RESOURCE_DIR:...>``:

.. literalinclude:: /../test/gauze/directory/CMakeLists.txt
:language: cmake
:emphasize-lines: 16

Read files in directory:

.. literalinclude:: /../test/gauze/directory/main.cpp
:language: cpp
:emphasize-lines: 28, 39

Running this test on Android device:

.. code-block:: none
:emphasize-lines: 1, 9, 11-16
> ctest -VV -R gauze_directory
4: Command output (with exit code):
4: *** BEGIN ***
4: argc = 6
4: argv[0] = /data/local/tmp/gauze/android-ndk-r10e-api-19-armeabi-v7a-neon/bin/gauze_directory
4: argv[1] = arg1
4: argv[2] = arg2
4: argv[3] = arg3
4: argv[4] = /data/local/tmp/gauze/android-ndk-r10e-api-19-armeabi-v7a-neon/data/resdir
4: argv[5] = /.../gauze/test/gauze/directory/resdir/just_a_string.txt
4: Processing file: "/data/local/tmp/gauze/android-ndk-r10e-api-19-armeabi-v7a-neon/data/resdir/file.0"
4: Content: 'Content 0'
4: Processing file: "/data/local/tmp/gauze/android-ndk-r10e-api-19-armeabi-v7a-neon/data/resdir/file.1"
4: Content: 'Content 1'
4: Processing file: "/data/local/tmp/gauze/android-ndk-r10e-api-19-armeabi-v7a-neon/data/resdir/file.2"
4: Content: 'Content 2'
4: 0
4: *** END ***
4: Done
1/1 Test #4: gauze_directory .................. Passed 0.54 sec
4 changes: 3 additions & 1 deletion docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ wraps standard
and in case of testing on host (Linux on Linux, OSX on OSX, etc.) just
forwards ``NAME`` and ``COMMAND`` arguments. Main functionality of the framework
is uploading/starting binaries for iOS/Android testing. CMake style generator
expression ``$<GAUZE_RESOURCE_FILE:...>`` can be used for managing resources.
expressions ``$<GAUZE_RESOURCE_FILE:...>`` and ``$<GAUZE_RESOURCE_DIR:...>``
can be used for managing resources.

.. admonition:: Hunter

Expand All @@ -28,5 +29,6 @@ expression ``$<GAUZE_RESOURCE_FILE:...>`` can be used for managing resources.
/examples/simple
/examples/args
/examples/resource
/examples/directory
/examples/deps
/examples/gtest
3 changes: 3 additions & 0 deletions lib/gauze/gauze/gauze.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ int main(int argc, char** argv)
}

std::vector<char*> new_argv(argc);

// NOTE: GAUZE_RESOURCE_DIR converted to GAUZE_RESOURCE_FILE
// to simplify logic (see iOSTest.cmake.in)
const std::regex r("\\$<GAUZE_RESOURCE_FILE:(.*)>$");

std::string fmt(home);
Expand Down
1 change: 1 addition & 0 deletions test/gauze/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
add_subdirectory(simple)
add_subdirectory(args)
add_subdirectory(resource)
add_subdirectory(directory)
add_subdirectory(deplib) # before 'deps'
add_subdirectory(deps)
add_subdirectory(gtest)
18 changes: 18 additions & 0 deletions test/gauze/directory/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
hunter_add_package(Boost COMPONENTS filesystem system)
find_package(Boost REQUIRED COMPONENTS filesystem system)

add_executable(gauze_directory main.cpp)
target_link_libraries(gauze_directory PRIVATE Boost::filesystem Boost::system)

set(data_dir "${CMAKE_CURRENT_LIST_DIR}/resdir")

gauze_add_test(
NAME gauze_directory
COMMAND
gauze_directory
arg1
arg2
arg3
$<GAUZE_RESOURCE_DIR:${data_dir}>
${data_dir}/just_a_string.txt
)
43 changes: 43 additions & 0 deletions test/gauze/directory/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include <cstdlib> // EXIT_SUCCESS
#include <fstream> // std::ifstream
#include <iostream> // std::cout
#include <string> // std::getline
#include <boost/filesystem.hpp>
#include <sstream> // std::ostringstream

int gauze_main(int argc, char** argv) {
std::cout << "argc = " << argc << std::endl;
for (int i=0; i<argc; ++i) {
std::cout << "argv[" << i << "] = " << argv[i] << std::endl;
}

if(argc < 6) {
std::cerr << "Unexpected number of arguments: " << argc << std::endl;
return EXIT_FAILURE;
}

boost::filesystem::path resdir(argv[4]);

for (int i=0; i<3; ++i) {
std::ostringstream file_name;
file_name << "file." << i;

boost::filesystem::path file_path(resdir);
file_path /= file_name.str();

std::cout << "Processing file: " << file_path << std::endl;
std::ifstream file(file_path.string());

std::string content;
std::getline(file, content);

if(!file) {
std::cerr << "Can't read file: " << file_path << std::endl;
return EXIT_FAILURE;
}

std::cout << "Content: '" << content << "'" << std::endl;
}

return EXIT_SUCCESS;
}
1 change: 1 addition & 0 deletions test/gauze/directory/resdir/file.0
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Content 0
1 change: 1 addition & 0 deletions test/gauze/directory/resdir/file.1
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Content 1
1 change: 1 addition & 0 deletions test/gauze/directory/resdir/file.2
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Content 2

0 comments on commit 7791735

Please sign in to comment.