Skip to content

Commit

Permalink
Merge pull request #56 from julian-mateu/fix-assert_symlink_to-for-te…
Browse files Browse the repository at this point in the history
…mp-files

Fix assert_symlink_to for temp files in OSX
  • Loading branch information
martin-schulze-vireso committed Mar 17, 2023
2 parents bba751f + 7d6add4 commit cc3fb8c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 13 deletions.
9 changes: 5 additions & 4 deletions src/file.bash
Original file line number Diff line number Diff line change
Expand Up @@ -444,13 +444,13 @@ assert_sticky_bit() {

_bats_file_readlinkf_macos() {
local TARGET_FILE=$1
cd "$(dirname "$TARGET_FILE")" || return
cd "$(dirname "$TARGET_FILE")" 2>/dev/null || return
TARGET_FILE=$(basename "$TARGET_FILE")
# Iterate down a (possible) chain of symlinks
while [ -L "$TARGET_FILE" ]
do
TARGET_FILE=$(readlink "$TARGET_FILE")
cd "$(dirname "$TARGET_FILE")" || return
cd "$(dirname "$TARGET_FILE")" 2>/dev/null || return
TARGET_FILE=$(basename "$TARGET_FILE")
done
# Compute the canonicalized name by finding the physical path
Expand Down Expand Up @@ -488,8 +488,9 @@ assert_symlink_to() {
| fail
fi

local -r realsource=$( "${readlink_command[@]}" "$link" )
if [ ! "$realsource" = "$sourcefile" ]; then
local realsource; realsource=$( "${readlink_command[@]}" "$link" ); readonly realsource
local realexpectedsource; realexpectedsource=$( "${readlink_command[@]}" "$sourcefile" ); readonly realexpectedsource
if [ ! "${realsource}" = "${realexpectedsource}" ]; then
local -r rem="${BATSLIB_FILE_PATH_REM-}"
local -r add="${BATSLIB_FILE_PATH_ADD-}"
batslib_print_kv_single 4 'path' "${link/$rem/$add}" \
Expand Down
28 changes: 19 additions & 9 deletions test/65-assert-10-assert_symlink_to.bats
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,39 @@ load 'test_helper'
fixtures 'symlink'

setup () {
touch ${TEST_FIXTURE_ROOT}/file ${TEST_FIXTURE_ROOT}/notasymlink
ln -s ${TEST_FIXTURE_ROOT}/file ${TEST_FIXTURE_ROOT}/symlink

touch "${TEST_FIXTURE_ROOT}/file" "${TEST_FIXTURE_ROOT}/notasymlink"
ln -s "${TEST_FIXTURE_ROOT}/file" "${TEST_FIXTURE_ROOT}/symlink"
TEMP_FOLDER="$(temp_make)"
}
teardown () {
rm -f ${TEST_FIXTURE_ROOT}/file ${TEST_FIXTURE_ROOT}/notasymlink ${TEST_FIXTURE_ROOT}/symlink
rm -f "${TEST_FIXTURE_ROOT}/file" "${TEST_FIXTURE_ROOT}/notasymlink" "${TEST_FIXTURE_ROOT}/symlink"
temp_del "${TEMP_FOLDER}"
}

# Correctness
@test 'assert_symlink_to() <file> <link>: returns 0 if <link> exists and is a symbolic link to <file>' {
local -r file="${TEST_FIXTURE_ROOT}/file"
local -r link="${TEST_FIXTURE_ROOT}/symlink"
run assert_symlink_to $file $link
[ "$status" -eq 0 ]
run assert_symlink_to "${file}" "${link}"
[ "${status}" -eq 0 ]
[ "${#lines[@]}" -eq 0 ]
}
@test 'assert_symlink_to() <file> <link>: returns 1 and displays path if <link> is not a symbolic link to <file>' {
local -r file="${TEST_FIXTURE_ROOT}/dir/file.does_not_exists"
local -r link="${TEST_FIXTURE_ROOT}/symlink"
run assert_symlink_to $file $link
[ "$status" -eq 1 ]
run assert_symlink_to "${file}" "${link}"
[ "${status}" -eq 1 ]
[ "${#lines[@]}" -eq 3 ]
[ "${lines[0]}" == '-- symbolic link does not have the correct target --' ]
[ "${lines[1]}" == "path : $link" ]
[ "${lines[1]}" == "path : ${link}" ]
[ "${lines[2]}" == '--' ]
}
@test 'assert_symlink_to() <temp_file> <link>: returns 0 if <link> exists and is a symbolic link to <temp_file>' {
touch "${TEMP_FOLDER}/file" "${TEMP_FOLDER}/notasymlink"
ln -s "${TEMP_FOLDER}/file" "${TEMP_FOLDER}/symlink"
local -r file="${TEMP_FOLDER}/file"
local -r link="${TEMP_FOLDER}/symlink"
run assert_symlink_to "${file}" "${link}"
[ "${status}" -eq 0 ]
[ "${#lines[@]}" -eq 0 ]
}

0 comments on commit cc3fb8c

Please sign in to comment.