Skip to content

Commit 89fcc5e

Browse files
authored
[tools] Make not tool respect environment variables
The not utility did not capture the parent's environment variables on POSIX platforms. This has been fixed. Tests have also been added for the utility - even for basic functionality.
1 parent ee0573e commit 89fcc5e

File tree

8 files changed

+58
-1
lines changed

8 files changed

+58
-1
lines changed

tools/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,7 @@ else()
4141
endif()
4242

4343
add_executable(not ${CMAKE_CURRENT_SOURCE_DIR}/not.cpp)
44+
45+
# Always add the test subdirectory. After all, we really should test these
46+
# tools.
47+
add_subdirectory(test)

tools/not.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ int main(int argc, char* const* argv) {
5858

5959
#if defined(__unix__) || defined(__APPLE__)
6060
pid_t pid;
61-
if (posix_spawn(&pid, argv[0], NULL, NULL, argv, NULL))
61+
extern char** environ;
62+
if (posix_spawn(&pid, argv[0], NULL, NULL, argv, environ))
6263
return EXIT_FAILURE;
6364
if (waitpid(pid, &result, WUNTRACED | WCONTINUED) == -1)
6465
return EXIT_FAILURE;

tools/test/CMakeLists.txt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Copy these files to the build directory so that the tests can be run even
2+
# without the source directory.
3+
configure_file(test_not.sh test_not.sh
4+
@ONLY)
5+
6+
add_executable(ret1 ret1.c)
7+
llvm_test_run(EXECUTABLE "%b/not" "%b/test/ret1")
8+
llvm_add_test_for_target(ret1)
9+
10+
add_executable(ret0 ret0.c)
11+
llvm_test_run(EXECUTABLE "%b/not" "%b/not" "%b/test/ret0")
12+
llvm_add_test_for_target(ret0)
13+
14+
# Check that expected crashes are handled correctly.
15+
add_executable(abrt abort.c)
16+
llvm_test_run(EXECUTABLE "%b/not" "--crash" "%b/test/abrt")
17+
llvm_add_test_for_target(abrt)
18+
19+
# Check that not passes environment variables to the called executable.
20+
add_executable(check_env check_env.c)
21+
llvm_test_run(EXECUTABLE "/bin/bash" "%b/test/test_not.sh %b")
22+
llvm_add_test(test_not.test test_not.sh)

tools/test/abort.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#include <stdlib.h>
2+
3+
int main(int argc, char* argv[]) {
4+
abort();
5+
}

tools/test/check_env.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#include <stdlib.h>
2+
3+
int main(int argc, char* argv[]) {
4+
// SET_IN_PARENT will have been set in by the driver, test_not.sh.
5+
// NOT_IN_PARENT will not have been set. Calling getenv with the former will
6+
// return non-NULL, the latter will return NULL. If both conditions are true,
7+
// this will return non-zero (which is an "error" code). This is intentional
8+
// because this will be passed to not which expects an error code.
9+
10+
return getenv("SET_IN_PARENT") && !getenv("NOT_IN_PARENT");
11+
}

tools/test/ret0.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
int main(int argc, char* argv[]) {
2+
return 0;
3+
}

tools/test/ret1.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
int main(int argc, char* argv[]) {
2+
return 1;
3+
}

tools/test/test_not.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/usr/bin/env sh
2+
#
3+
# USAGE: test_not.sh ${bindir}
4+
#
5+
# where bindir is ${CMAKE_BINARY_DIR}/tools
6+
7+
export SET_IN_PARENT="something"
8+
$1/not $1/test/check_env

0 commit comments

Comments
 (0)