Skip to content

Commit 7fed570

Browse files
committed
Correct bug from plugin extension.
1 parent 9a198e9 commit 7fed570

File tree

5 files changed

+271
-0
lines changed

5 files changed

+271
-0
lines changed

source/extensions/plugin_extension/source/plugin_extension.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,13 @@ void *plugin_load_from_path(size_t argc, void *args[], void *data)
6767
}
6868

6969
std::string ext_path(metacall_value_to_string(args[0]));
70+
71+
if (fs::is_directory(fs::path(ext_path)) == false)
72+
{
73+
log_write("metacall", LOG_LEVEL_WARNING, "Folder %s not found, plugins will not be loaded", ext_path.c_str());
74+
return metacall_value_create_int(4);
75+
}
76+
7077
void **handle_ptr = NULL;
7178

7279
if (argc == 2)

source/tests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,7 @@ add_subdirectory(metacall_ext_test)
261261
add_subdirectory(metacall_plugin_extension_test)
262262
add_subdirectory(metacall_plugin_extension_local_test)
263263
add_subdirectory(metacall_plugin_extension_destroy_order_test)
264+
add_subdirectory(metacall_plugin_extension_invalid_path_test)
264265
add_subdirectory(metacall_cli_core_plugin_test)
265266
add_subdirectory(metacall_cli_core_plugin_await_test)
266267
add_subdirectory(metacall_backtrace_plugin_test)
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
# Check if this loader is enabled
2+
if(NOT OPTION_BUILD_LOADERS OR NOT OPTION_BUILD_LOADERS_EXT OR NOT OPTION_BUILD_EXTENSIONS OR NOT OPTION_BUILD_CLI)
3+
return()
4+
endif()
5+
6+
#
7+
# Executable name and options
8+
#
9+
10+
# Target name
11+
set(target metacall-plugin-extension-invalid-path-test)
12+
message(STATUS "Test ${target}")
13+
14+
#
15+
# Compiler warnings
16+
#
17+
18+
include(Warnings)
19+
20+
#
21+
# Compiler security
22+
#
23+
24+
include(SecurityFlags)
25+
26+
#
27+
# Sources
28+
#
29+
30+
set(include_path "${CMAKE_CURRENT_SOURCE_DIR}/include/${target}")
31+
set(source_path "${CMAKE_CURRENT_SOURCE_DIR}/source")
32+
33+
set(sources
34+
${source_path}/main.cpp
35+
${source_path}/metacall_plugin_extension_invalid_path_test.cpp
36+
)
37+
38+
# Group source files
39+
set(header_group "Header Files (API)")
40+
set(source_group "Source Files")
41+
source_group_by_path(${include_path} "\\\\.h$|\\\\.hpp$"
42+
${header_group} ${headers})
43+
source_group_by_path(${source_path} "\\\\.cpp$|\\\\.c$|\\\\.h$|\\\\.hpp$"
44+
${source_group} ${sources})
45+
46+
#
47+
# Create executable
48+
#
49+
50+
# Build executable
51+
add_executable(${target}
52+
${sources}
53+
)
54+
55+
# Create namespaced alias
56+
add_executable(${META_PROJECT_NAME}::${target} ALIAS ${target})
57+
58+
#
59+
# Project options
60+
#
61+
62+
set_target_properties(${target}
63+
PROPERTIES
64+
${DEFAULT_PROJECT_OPTIONS}
65+
FOLDER "${IDE_FOLDER}"
66+
)
67+
68+
#
69+
# Include directories
70+
#
71+
72+
target_include_directories(${target}
73+
PRIVATE
74+
${DEFAULT_INCLUDE_DIRECTORIES}
75+
${PROJECT_BINARY_DIR}/source/include
76+
)
77+
78+
#
79+
# Libraries
80+
#
81+
82+
target_link_libraries(${target}
83+
PRIVATE
84+
${DEFAULT_LIBRARIES}
85+
86+
GTest
87+
88+
${META_PROJECT_NAME}::metacall
89+
)
90+
91+
#
92+
# Compile definitions
93+
#
94+
95+
set(METACALL_PLUGIN_PATH "${PROJECT_OUTPUT_DIR}/this/folder/does/not/exist")
96+
97+
target_compile_definitions(${target}
98+
PRIVATE
99+
${DEFAULT_COMPILE_DEFINITIONS}
100+
101+
# Plugin path
102+
METACALL_PLUGIN_PATH="${METACALL_PLUGIN_PATH}"
103+
)
104+
105+
#
106+
# Compile options
107+
#
108+
109+
target_compile_options(${target}
110+
PRIVATE
111+
${DEFAULT_COMPILE_OPTIONS}
112+
)
113+
114+
#
115+
# Linker options
116+
#
117+
118+
target_link_libraries(${target}
119+
PRIVATE
120+
${DEFAULT_LINKER_OPTIONS}
121+
)
122+
123+
#
124+
# Define test
125+
#
126+
127+
add_test(NAME ${target}
128+
COMMAND $<TARGET_FILE:${target}>
129+
)
130+
131+
#
132+
# Define dependencies
133+
#
134+
135+
add_dependencies(${target}
136+
ext_loader
137+
plugin_extension
138+
cli_core_plugin # Requires cli_core_plugin (from CLI) for reproducing it
139+
)
140+
141+
#
142+
# Define test properties
143+
#
144+
145+
set_property(TEST ${target}
146+
PROPERTY LABELS ${target}
147+
)
148+
149+
include(TestEnvironmentVariables)
150+
151+
test_environment_variables(${target}
152+
""
153+
${TESTS_ENVIRONMENT_VARIABLES}
154+
)
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* MetaCall Library by Parra Studios
3+
* A library for providing a foreign function interface calls.
4+
*
5+
* Copyright (C) 2016 - 2024 Vicente Eduardo Ferrer Garcia <[email protected]>
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*
19+
*/
20+
21+
#include <gtest/gtest.h>
22+
23+
int main(int argc, char *argv[])
24+
{
25+
::testing::InitGoogleTest(&argc, argv);
26+
27+
return RUN_ALL_TESTS();
28+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
* MetaCall Library by Parra Studios
3+
* A library for providing a foreign function interface calls.
4+
*
5+
* Copyright (C) 2016 - 2024 Vicente Eduardo Ferrer Garcia <[email protected]>
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*
19+
*/
20+
21+
#include <gtest/gtest.h>
22+
23+
#include <metacall/metacall.h>
24+
#include <metacall/metacall_loaders.h>
25+
26+
class metacall_plugin_extension_invalid_path_test : public testing::Test
27+
{
28+
public:
29+
};
30+
31+
TEST_F(metacall_plugin_extension_invalid_path_test, DefaultConstructor)
32+
{
33+
metacall_print_info();
34+
35+
ASSERT_EQ((int)0, (int)metacall_initialize());
36+
37+
/* Extension */
38+
void *handle = metacall_plugin_extension();
39+
40+
ASSERT_NE((void *)NULL, (void *)handle);
41+
42+
void *args[] = {
43+
metacall_value_create_string(METACALL_PLUGIN_PATH, sizeof(METACALL_PLUGIN_PATH) - 1),
44+
metacall_value_create_ptr(&handle)
45+
};
46+
47+
void *result = metacallhv_s(handle, "plugin_load_from_path", args, sizeof(args) / sizeof(args[0]));
48+
49+
ASSERT_NE((void *)NULL, (void *)result);
50+
51+
EXPECT_EQ((enum metacall_value_id)METACALL_INT, (enum metacall_value_id)metacall_value_id(result));
52+
53+
EXPECT_EQ((int)4, (int)metacall_value_to_int(result));
54+
55+
metacall_value_destroy(args[0]);
56+
metacall_value_destroy(args[1]);
57+
metacall_value_destroy(result);
58+
59+
/* Print inspect information */
60+
{
61+
size_t size = 0;
62+
63+
struct metacall_allocator_std_type std_ctx = { &std::malloc, &std::realloc, &std::free };
64+
65+
void *allocator = metacall_allocator_create(METACALL_ALLOCATOR_STD, (void *)&std_ctx);
66+
67+
char *inspect_str = metacall_inspect(&size, allocator);
68+
69+
EXPECT_NE((char *)NULL, (char *)inspect_str);
70+
71+
EXPECT_GT((size_t)size, (size_t)0);
72+
73+
std::cout << inspect_str << std::endl;
74+
75+
metacall_allocator_free(allocator, inspect_str);
76+
77+
metacall_allocator_destroy(allocator);
78+
}
79+
80+
EXPECT_EQ((int)0, (int)metacall_destroy());
81+
}

0 commit comments

Comments
 (0)