Skip to content

Commit

Permalink
assert_equal_files, assert_not_equal_files added to file.bash, README…
Browse files Browse the repository at this point in the history
….… (#15)

* assert_equal_files assert_not_equal_files added to file.bash, README.md and tests
  • Loading branch information
SalimHouari authored and peshay committed Nov 12, 2018
1 parent 3308542 commit ddc08e1
Show file tree
Hide file tree
Showing 7 changed files with 274 additions and 4 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ All notable changes to this project will be documented in this file.
This project adheres to [Semantic Versioning](http://semver.org/).


## [0.3.0] - 2018-10-28

### Added

- Added assert_files_equal assert_files_not_equal functions!
- Added 2 test scripts for assert_files_equal assert_files_not_equal


## [0.2.0] - 2016-12-07

### Added
Expand Down
43 changes: 40 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ path : /path/to/non-existent-fifo-file
Fail if the given file is not executable.

```bash
@test 'assert_file_executable() {
@test 'assert_file_executable()' {
assert_file_executable /path/to/executable-file
}
```
Expand All @@ -190,6 +190,24 @@ path : /path/to/executable-file
--
```

### `assert_files_equal`

Fail if the given files are not the same.

```bash
@test 'assert_files_equal()' {
assert_files_equal /path/to/file1 /path/to/file2
}
```

On failure, the path is displayed.

```
-- files are not the same --
path1 : /path/to/file
path2 : /path/to/same_file
--
```


### `assert_file_not_exist`
Expand Down Expand Up @@ -306,7 +324,7 @@ path : /path/to/existing-socket
Fail if the given named pipe exists.

```bash
@test 'assert_fifo_not_exist() {
@test 'assert_fifo_not_exist()' {
assert_file_not_exist /path/to/existing-fifo-file
}
```
Expand All @@ -325,7 +343,7 @@ path : /path/to/existing-fifo-file
Fail if the given file is executable.

```bash
@test 'assert_file_not_executable() {
@test 'assert_file_not_executable()' {
assert_file_not_executable /path/to/executable-file
}
```
Expand All @@ -339,6 +357,25 @@ path : /path/to/executable-file
```


### `assert_files_not_equal`

Fail if the given files are the same.

```bash
@test 'assert_files_not_equal()' {
assert_files_not_equal /path/to/file1 /path/to/file2
}
```

On failure, the path is displayed.

```
-- files are the same --
path1 : /path/to/file
path2 : /path/to/same_file
--
```


## Working with temporary directories

Expand Down
50 changes: 49 additions & 1 deletion src/file.bash
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,6 @@ assert_fifo_exist() {
fi
}


# Fail and display path of the named file if it is not executable.
# This function is the logical complement of `assert_file_not_executable'.
#
Expand All @@ -243,6 +242,30 @@ assert_file_executable() {
fi
}

# This function is the logical complement of `assert_files_not_equal'.
#
# Globals:
# BATSLIB_FILE_PATH_REM
# BATSLIB_FILE_PATH_ADD
# Arguments:
# $1 - 1st path
# $2 - 2nd path
# Returns:
# 0 - named files are the same
# 1 - otherwise
# Outputs:
# STDERR - details, on failure
assert_files_equal() {
local -r file1="$1"
local -r file2="$2"
if ! `cmp -s "$file1" "$file2"` ; then
local -r rem="$BATSLIB_FILE_PATH_REM"
local -r add="$BATSLIB_FILE_PATH_ADD"
batslib_print_kv_single 4 'path' "${file1/$rem/$add}" 'path' "${file2/$rem/$add}" \
| batslib_decorate 'files are not the same' \
| fail
fi
}

# Fail and display path of the file (or directory) if it exists. This
# function is the logical complement of `assert_exist'.
Expand Down Expand Up @@ -463,3 +486,28 @@ assert_file_not_executable() {
| fail
fi
}

# This function is the logical complement of `assert_files_equal'.
#
# Globals:
# BATSLIB_FILE_PATH_REM
# BATSLIB_FILE_PATH_ADD
# Arguments:
# $1 - 1st path
# $2 - 2nd path
# Returns:
# 0 - named files are the same
# 1 - otherwise
# Outputs:
# STDERR - details, on failure
assert_files_not_equal() {
local -r file1="$1"
local -r file2="$2"
if `cmp -s "$file1" "$file2"` ; then
local -r rem="$BATSLIB_FILE_PATH_REM"
local -r add="$BATSLIB_FILE_PATH_ADD"
batslib_print_kv_single 4 'path' "${file1/$rem/$add}" 'path' "${file2/$rem/$add}" \
| batslib_decorate 'files are the same' \
| fail
fi
}
83 changes: 83 additions & 0 deletions test/58-assert-10-assert_equal_files.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#!/usr/bin/env bats

load 'test_helper'
fixtures 'exist'

# Correctness
@test 'assert_files_equal() <file>: returns 0 if <file1> and <file2> are the same' {
local -r file1="${TEST_FIXTURE_ROOT}/dir/file_with_text"
local -r file2="${TEST_FIXTURE_ROOT}/dir/same_file_with_text"
run assert_files_equal "$file1" "$file2"
[ "$status" -eq 0 ]
[ "${#lines[@]}" -eq 0 ]
}


@test 'assert_files_equal() <file>: returns 1 if <file1> and <file2> are not the same' {
local -r file1="${TEST_FIXTURE_ROOT}/dir/same_file_with_text"
local -r file2="${TEST_FIXTURE_ROOT}/dir/file"
run assert_files_equal "$file1" "$file2"

[ "$status" -eq 1 ]
[ "${#lines[@]}" -eq 4 ]
[ "${lines[0]}" == '-- files are not the same --' ]
[ "${lines[1]}" == "path : $file1" ]
[ "${lines[2]}" == "path : $file2" ]
[ "${lines[3]}" == "--" ]
}

# Transforming path
@test 'assert_files_equal() <file>: used <file2> as a directory' {
local -r file2="${TEST_FIXTURE_ROOT}/dir/same_file_with_text"
local -r file1="${TEST_FIXTURE_ROOT}/dir"
run assert_files_equal "$file1" "$file2"
[ "$status" -eq 1 ]
[ "${#lines[@]}" -eq 5 ]
[ "${lines[0]}" == "cmp: ${TEST_FIXTURE_ROOT}/dir: Is a directory" ]
[ "${lines[1]}" == "-- files are not the same --" ]
[ "${lines[2]}" == "path : $file1" ]
[ "${lines[3]}" == "path : $file2" ]
[ "${lines[4]}" == "--" ]
}

@test 'assert_files_equal() <file>: replace prefix of displayed path' {
local -r file2="${TEST_FIXTURE_ROOT}/dir/same_file_with_text"
local -r file1="${TEST_FIXTURE_ROOT}/dir/file"
local -r BATSLIB_FILE_PATH_REM="#${TEST_FIXTURE_ROOT}"
local -r BATSLIB_FILE_PATH_ADD='..'
run assert_files_equal "$file1" "$file2"
[ "$status" -eq 1 ]
[ "${#lines[@]}" -eq 4 ]
[ "${lines[0]}" == '-- files are not the same --' ]
[ "${lines[1]}" == "path : ../dir/file" ]
[ "${lines[2]}" == "path : ../dir/same_file_with_text" ]
[ "${lines[3]}" == "--" ]
}

@test 'assert_files_equal() <file>: replace suffix of displayed path' {
local -r file2="${TEST_FIXTURE_ROOT}/dir/same_file_with_text"
local -r file1="${TEST_FIXTURE_ROOT}/same_file_with_text"
local -r BATSLIB_FILE_PATH_REM='%same_file_with_text'
local -r BATSLIB_FILE_PATH_ADD='..'
run assert_files_equal "$file1" "$file2"
[ "$status" -eq 1 ]
[ "${#lines[@]}" -eq 4 ]
[ "${lines[0]}" == '-- files are not the same --' ]
[ "${lines[1]}" == "path : ${TEST_FIXTURE_ROOT}/.." ]
[ "${lines[2]}" == "path : ${TEST_FIXTURE_ROOT}/dir/.." ]
[ "${lines[3]}" == "--" ]
}

@test 'assert_files_equal() <file>: replace infix of displayed path' {
local -r file2="${TEST_FIXTURE_ROOT}/dir/same_file_with_text"
local -r file1="${TEST_FIXTURE_ROOT}/dir/file"
local -r BATSLIB_FILE_PATH_REM='dir'
local -r BATSLIB_FILE_PATH_ADD='..'
run assert_files_equal "$file1" "$file2"
[ "$status" -eq 1 ]
[ "${#lines[@]}" -eq 4 ]
[ "${lines[0]}" == '-- files are not the same --' ]
[ "${lines[1]}" == "path : ${TEST_FIXTURE_ROOT}/../file" ]
[ "${lines[2]}" == "path : ${TEST_FIXTURE_ROOT}/../same_file_with_text" ]
[ "${lines[3]}" == "--" ]
}
78 changes: 78 additions & 0 deletions test/58-assert-11-assert_not_equal_files.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#!/usr/bin/env bats

load 'test_helper'
fixtures 'exist'

# Correctness
@test 'assert_files_not_equal() <file1> <file2>: returns 0 if <file1> and <file2> are not the same' {
local -r file1="${TEST_FIXTURE_ROOT}/dir/file_with_text"
local -r file2="${TEST_FIXTURE_ROOT}/dir/file"
run assert_files_not_equal "$file1" "$file2"
[ "$status" -eq 0 ]
[ "${#lines[@]}" -eq 0 ]
}


@test 'assert_files_not_equal() <file1> <file2>: returns 1 if <file1> and <file2> are the same' {
local -r file1="${TEST_FIXTURE_ROOT}/dir/same_file_with_text"
local -r file2="${TEST_FIXTURE_ROOT}/dir/file_with_text"
run assert_files_not_equal "$file1" "$file2"
[ "$status" -eq 1 ]
[ "${#lines[@]}" -eq 4 ]
[ "${lines[0]}" == '-- files are the same --' ]
[ "${lines[1]}" == "path : $file1" ]
[ "${lines[2]}" == "path : $file2" ]
[ "${lines[3]}" == "--" ]
}

# Transforming path
@test 'assert_files_not_equal() <file1> <file2>: used <file2> as a directory' {
local -r file2="${TEST_FIXTURE_ROOT}/dir/same_file_with_text"
local -r file1="${TEST_FIXTURE_ROOT}/dir"
run assert_files_not_equal "$file1" "$file2"
[ "$status" -eq 0 ]
[ "${#lines[@]}" -eq 1 ]
[ "${lines[0]}" == "cmp: ${TEST_FIXTURE_ROOT}/dir: Is a directory" ]
}

@test 'assert_files_not_equal() <file>: replace prefix of displayed path' {
local -r file2="${TEST_FIXTURE_ROOT}/dir/same_file_with_text"
local -r file1="${TEST_FIXTURE_ROOT}/dir/file_with_text"
local -r BATSLIB_FILE_PATH_REM="#${TEST_FIXTURE_ROOT}"
local -r BATSLIB_FILE_PATH_ADD='..'
run assert_files_not_equal "$file1" "$file2"
[ "$status" -eq 1 ]
[ "${#lines[@]}" -eq 4 ]
[ "${lines[0]}" == '-- files are the same --' ]
[ "${lines[1]}" == "path : ../dir/file_with_text" ]
[ "${lines[2]}" == "path : ../dir/same_file_with_text" ]
[ "${lines[3]}" == "--" ]
}

@test 'assert_files_not_equal() <file>: replace suffix of displayed path' {
local -r file2="${TEST_FIXTURE_ROOT}/dir/same_file_with_text"
local -r file1="${TEST_FIXTURE_ROOT}/dir/file_with_text"
local -r BATSLIB_FILE_PATH_REM='%same_file_with_text'
local -r BATSLIB_FILE_PATH_ADD='..'
run assert_files_not_equal "$file1" "$file2"
[ "$status" -eq 1 ]
[ "${#lines[@]}" -eq 4 ]
[ "${lines[0]}" == '-- files are the same --' ]
[ "${lines[1]}" == "path : ${TEST_FIXTURE_ROOT}/dir/file_with_text" ]
[ "${lines[2]}" == "path : ${TEST_FIXTURE_ROOT}/dir/.." ]
[ "${lines[3]}" == "--" ]
}

@test 'assert_files_not_equal() <file>: replace infix of displayed path' {
local -r file2="${TEST_FIXTURE_ROOT}/dir/same_file_with_text"
local -r file1="${TEST_FIXTURE_ROOT}/dir/file_with_text"
local -r BATSLIB_FILE_PATH_REM='dir'
local -r BATSLIB_FILE_PATH_ADD='..'
run assert_files_not_equal "$file1" "$file2"
[ "$status" -eq 1 ]
[ "${#lines[@]}" -eq 4 ]
[ "${lines[0]}" == '-- files are the same --' ]
[ "${lines[1]}" == "path : ${TEST_FIXTURE_ROOT}/../file_with_text" ]
[ "${lines[2]}" == "path : ${TEST_FIXTURE_ROOT}/../same_file_with_text" ]
[ "${lines[3]}" == "--" ]
}
8 changes: 8 additions & 0 deletions test/fixtures/exist/dir/file_with_text
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
sesetsetasdfasfdjlsakjdfklasjd
sldkfjaslkdfjasdkfj
slkdfjhaskldfjhlakshdfklasjdfh
sdkfjhslkjfhsdlkjhfg
lksajdfklöjsafkljsadafsjflkjdf
sadfasfsad
sadfasfdsadsfasdfasf

8 changes: 8 additions & 0 deletions test/fixtures/exist/dir/same_file_with_text
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
sesetsetasdfasfdjlsakjdfklasjd
sldkfjaslkdfjasdkfj
slkdfjhaskldfjhlakshdfklasjdfh
sdkfjhslkjfhsdlkjhfg
lksajdfklöjsafkljsadafsjflkjdf
sadfasfsad
sadfasfdsadsfasdfasf

0 comments on commit ddc08e1

Please sign in to comment.