Skip to content

Commit

Permalink
Add exists, is_file, is_dir, is_link and is_readable assertion methods
Browse files Browse the repository at this point in the history
Also includes documentation and tests for the above
  • Loading branch information
molovo committed Sep 26, 2016
1 parent a093434 commit 7106491
Show file tree
Hide file tree
Showing 5 changed files with 194 additions and 1 deletion.
42 changes: 41 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,47 @@ assert 'a' in 'a' 'b' 'c'
Asserts that a value is not included in the comparison array.

```sh
asserts 'a' not_in 'x' 'y' 'z'
assert 'a' not_in 'x' 'y' 'z'
```

#### exists

Asserts that the given path exists

```sh
assert /path/to/file exists
```

#### is_file

Asserts that the given path exists and is a file

```sh
assert /path/to/file is_file
```

#### is_dir

Asserts that the given path exists and is a directory

```sh
assert /path/to/dir is_dir
```

#### is_link

Asserts that the given path exists and is a symbolic link

```sh
assert /path/to/link is_link
```

#### is_readable

Asserts that the given path exists and is a symbolic readable

```sh
assert /path/to/file is_readable
```

### Loading scripts
Expand Down
1 change: 1 addition & 0 deletions tests/_support/assertions.zunit.link
Empty file.
67 changes: 67 additions & 0 deletions tests/assertions.zunit
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,70 @@
assert "$state" equals 1
assert "$output" same_as "'a' is in (a ; b ; c)"
}

@test 'Test _zunit_assert_exists success' {
run assert './assertions.zunit' exists
assert "$state" equals 0
assert "$output" is_empty
}

@test 'Test _zunit_assert_exists failure' {
run assert './non-existent-file' exists
assert "$state" equals 1
assert "$output" same_as "'./non-existent-file' does not exist"
}

@test 'Test _zunit_assert_is_file success' {
run assert './assertions.zunit' is_file
assert "$state" equals 0
assert "$output" is_empty
}

@test 'Test _zunit_assert_is_file failure' {
run assert './_support' is_file
assert "$state" equals 1
assert "$output" same_as "'./_support' does not exist or is not a file"
}

@test 'Test _zunit_assert_is_dir success' {
run assert './_support' is_dir
assert "$state" equals 0
assert "$output" is_empty
}

@test 'Test _zunit_assert_is_dir failure' {
run assert './assertions.zunit' is_dir
assert "$state" equals 1
assert "$output" same_as "'./assertions.zunit' does not exist or is not a directory"
}

@test 'Test _zunit_assert_is_link success' {
run assert './_support/assertions.zunit.link' is_link
assert "$state" equals 0
assert "$output" is_empty
}

@test 'Test _zunit_assert_is_link failure' {
run assert './assertions.zunit' is_link
assert "$state" equals 1
assert "$output" same_as "'./assertions.zunit' does not exist or is not a symbolic link"
}

@test 'Test _zunit_assert_is_readable success' {
run assert './assertions.zunit' is_readable
assert "$state" equals 0
assert "$output" is_empty
}

@test 'Test _zunit_assert_is_readable failure' {
# Make file unreadable before checking it
chmod 000 "$testdir/_support/non-readable-file"

run assert './_support/non-readable-file' is_readable

# Change permissions back before the test makes its assertions
chmod 644 "$testdir/_support/non-readable-file"

assert "$state" equals 1
assert "$output" same_as "'./_support/non-readable-file' does not exist or is not readable"
}
85 changes: 85 additions & 0 deletions zunit
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,91 @@ function _zunit_assert_not_in() {
exit 1
}

###
# Assert the a path exists
###
function _zunit_assert_exists() {
local path=$1

# If filepath is relative, prepend the test directory
if [[ "${path:0:1}" != "/" ]]; then
filepath="$testdir/${path}"
fi

[[ -e $filepath ]] && return 0

echo "'$path' does not exist"
exit 1
}

###
# Assert the a path exists and is a file
###
function _zunit_assert_is_file() {
local path=$1

# If filepath is relative, prepend the test directory
if [[ "${path:0:1}" != "/" ]]; then
filepath="$testdir/${path}"
fi

[[ -f $filepath ]] && return 0

echo "'$path' does not exist or is not a file"
exit 1
}

###
# Assert the a path exists and is a directory
###
function _zunit_assert_is_dir() {
local path=$1

# If filepath is relative, prepend the test directory
if [[ "${path:0:1}" != "/" ]]; then
filepath="$testdir/${path}"
fi

[[ -d $filepath ]] && return 0

echo "'$path' does not exist or is not a directory"
exit 1
}

###
# Assert the a path exists and is a symbolic link
###
function _zunit_assert_is_link() {
local path=$1

# If filepath is relative, prepend the test directory
if [[ "${path:0:1}" != "/" ]]; then
filepath="$testdir/${path}"
fi

[[ -h $filepath ]] && return 0

echo "'$path' does not exist or is not a symbolic link"
exit 1
}

###
# Assert the a path exists and is readable
###
function _zunit_assert_is_readable() {
local path=$1

# If filepath is relative, prepend the test directory
if [[ "${path:0:1}" != "/" ]]; then
filepath="$testdir/${path}"
fi

[[ -r $filepath ]] && return 0

echo "'$path' does not exist or is not readable"
exit 1
}

################################
# Helpers for use within tests #
################################
Expand Down

0 comments on commit 7106491

Please sign in to comment.