diff --git a/bin/pwd/Makefile b/bin/pwd/Makefile --- a/bin/pwd/Makefile +++ b/bin/pwd/Makefile @@ -1,7 +1,12 @@ # @(#)Makefile 8.1 (Berkeley) 5/31/93 # $FreeBSD$ +.include +HAS_TESTS= # Add this, mean pwd has test +SUBDIR.${MK_TESTS}+= tests # Add this, for build tests folder. + PACKAGE=runtime PROG= pwd .include + diff --git a/bin/pwd/tests/Makefile b/bin/pwd/tests/Makefile new file mode 100644 --- /dev/null +++ b/bin/pwd/tests/Makefile @@ -0,0 +1,9 @@ +# $FreeBSD$ + +PACKAGE= tests + +ATF_TESTS_SH= pwd_test +PROGS+= get_path +BINDIR= ${TESTSDIR} + +.include diff --git a/bin/pwd/tests/get_path.c b/bin/pwd/tests/get_path.c new file mode 100644 --- /dev/null +++ b/bin/pwd/tests/get_path.c @@ -0,0 +1,20 @@ +#include +#include +#include +#include +#include + +int +main(void) +{ + char current_path[PATH_MAX]; + char resolved_path[PATH_MAX]; + + if (getcwd(current_path, sizeof(current_path)) == NULL) + err(1, "%s", "getcwd error"); + else if (realpath(current_path, resolved_path) == NULL) + err(1, "%s", "realpath error"); + + printf("%s %s", current_path, resolved_path); + return EXIT_SUCCESS; +} diff --git a/bin/pwd/tests/pwd_test.sh b/bin/pwd/tests/pwd_test.sh new file mode 100644 --- /dev/null +++ b/bin/pwd/tests/pwd_test.sh @@ -0,0 +1,69 @@ +get_output() { + # PWD environment variable which is logical path + pwd_environment="$PWD" + get_path_output="$("${0%/*}"/get_path)" + echo "$pwd_environment $get_path_output" +} + +atf_test_case basic +basic_head() +{ + atf_set "descr" "Basic test case" +} +basic_body() +{ + output="$(get_output)" + echo "$output" | while IFS=" " read pwd_environment getcwd realpath; do + atf_check -o inline:"${getcwd}\n" pwd + atf_check -o inline:"${realpath}\n" pwd + atf_check -o inline:"${pwd_environment}\n" pwd -L + atf_check -o inline:"${getcwd}\n" pwd -P + atf_check -o inline:"${realpath}\n" pwd -P + done +} + +atf_test_case soft_link +soft_link_head() +{ + atf_set "descr" "Test on the path which has softlink" +} +soft_link_body() +{ + # Create a soft_link and enter it + mkdir soft_link_src + ln -s soft_link_src soft_link_dst + cd soft_link_dst || exit + + output="$(get_output)" + echo "$output" | while IFS=" " read pwd_environment getcwd realpath; do + atf_check -o inline:"${getcwd}\n" pwd + atf_check -o inline:"${realpath}\n" pwd + atf_check -o inline:"${pwd_environment}\n" pwd -L + atf_check -o inline:"${getcwd}\n" pwd -P + atf_check -o inline:"${realpath}\n" pwd -P + done +} + +atf_test_case broken_soft_link +broken_soft_link_head() +{ + atf_set "descr" "Test on the path which has broken softlink" +} +broken_soft_link_body() +{ + mkdir broken_soft_link_src + ln -s broken_soft_link_src broken_soft_link_dst + cd broken_soft_link_dst || exit + rm -r ../broken_soft_link_src + + atf_check -s exit:1 -e inline:"pwd: .: No such file or directory\n" pwd + atf_check -s exit:1 -e inline:"pwd: .: No such file or directory\n" pwd -L + atf_check -s exit:1 -e inline:"pwd: .: No such file or directory\n" pwd -P +} + +atf_init_test_cases() +{ + atf_add_test_case basic + atf_add_test_case soft_link + atf_add_test_case broken_soft_link +} diff --git a/etc/mtree/BSD.tests.dist b/etc/mtree/BSD.tests.dist --- a/etc/mtree/BSD.tests.dist +++ b/etc/mtree/BSD.tests.dist @@ -39,6 +39,8 @@ rm .. rmdir + .. + pwd .. sh builtins