Skip to content

Commit

Permalink
Added assert_symlink_to
Browse files Browse the repository at this point in the history
  • Loading branch information
marema31 committed Jul 24, 2017
1 parent 2fddb2b commit 95896f3
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 0 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,24 @@ path : /path/to/existing-file
--
```
### `assert_symlink_to`
Fail if the given file is not a symbolic to a defined target.
```bash
@test 'assert_symlink_to() {
assert_symlink_to /path/to/source-file /path/to/symlink
}
```

On failure, the path is displayed.

```
-- symbolic link does not have the correct target --
path : /path/to/symlink
--
```


## Working with temporary directories

Expand Down
33 changes: 33 additions & 0 deletions src/file.bash
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,36 @@ assert_file_not_exist() {
| fail
fi
}

# Fail and display path of the file (or directory) if it is not a symlink.
#
# Globals:
# BATSLIB_FILE_PATH_REM
# BATSLIB_FILE_PATH_ADD
# Arguments:
# $1 - source
# $2 - destination
# Returns:
# 0 - link to correct target
# 1 - otherwise
# Outputs:
# STDERR - details, on failure
assert_symlink_to() {
local -r sourcefile="$1"
local -r link="$2"
if [ ! -L $link ]; then
local -r rem="$BATSLIB_FILE_PATH_REM"
local -r add="$BATSLIB_FILE_PATH_ADD"
batslib_print_kv_single 4 'path' "${link/$rem/$add}" \
| batslib_decorate 'file is not a symbolic link' \
| fail
fi
local -r realsource=$( readlink -f "$link" )
if [ ! "$realsource" = "$sourcefile" ]; then
local -r rem="$BATSLIB_FILE_PATH_REM"
local -r add="$BATSLIB_FILE_PATH_ADD"
batslib_print_kv_single 4 'path' "${link/$rem/$add}" \
| batslib_decorate 'symbolic link does not have the correct target' \
| fail
fi
}
25 changes: 25 additions & 0 deletions test/50-assert-12-assert_symlink_to.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/usr/bin/env bats

load 'test_helper'
fixtures 'symlink'

# 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}/link"
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_exist"
local -r link="${TEST_FIXTURE_ROOT}/link"
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[2]}" == '--' ]
}

Empty file added test/fixtures/symlink/file
Empty file.
1 change: 1 addition & 0 deletions test/fixtures/symlink/symlink

1 comment on commit 95896f3

@peshay
Copy link

@peshay peshay commented on 95896f3 Nov 29, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@marema31 Kinda similar change has been merged to my fork of the project: bats-core#18

Please sign in to comment.