From ddc08e152b62475ed8720548b34cdae78ba09362 Mon Sep 17 00:00:00 2001 From: KingKabyle <43536211+KingKabyle@users.noreply.github.com> Date: Mon, 12 Nov 2018 17:35:27 +0100 Subject: [PATCH] =?UTF-8?q?assert=5Fequal=5Ffiles,=20assert=5Fnot=5Fequal?= =?UTF-8?q?=5Ffiles=20added=20to=20file.bash,=20README.=E2=80=A6=20(#15)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * assert_equal_files assert_not_equal_files added to file.bash, README.md and tests --- CHANGELOG.md | 8 ++ README.md | 43 +++++++++- src/file.bash | 50 ++++++++++- test/58-assert-10-assert_equal_files.bats | 83 +++++++++++++++++++ test/58-assert-11-assert_not_equal_files.bats | 78 +++++++++++++++++ test/fixtures/exist/dir/file_with_text | 8 ++ test/fixtures/exist/dir/same_file_with_text | 8 ++ 7 files changed, 274 insertions(+), 4 deletions(-) create mode 100644 test/58-assert-10-assert_equal_files.bats create mode 100644 test/58-assert-11-assert_not_equal_files.bats create mode 100644 test/fixtures/exist/dir/file_with_text create mode 100644 test/fixtures/exist/dir/same_file_with_text diff --git a/CHANGELOG.md b/CHANGELOG.md index 049740a..de46064 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/README.md b/README.md index 35f909a..995069e 100644 --- a/README.md +++ b/README.md @@ -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 } ``` @@ -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` @@ -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 } ``` @@ -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 } ``` @@ -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 diff --git a/src/file.bash b/src/file.bash index 495eff0..04d7b86 100644 --- a/src/file.bash +++ b/src/file.bash @@ -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'. # @@ -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'. @@ -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 +} diff --git a/test/58-assert-10-assert_equal_files.bats b/test/58-assert-10-assert_equal_files.bats new file mode 100644 index 0000000..dad0596 --- /dev/null +++ b/test/58-assert-10-assert_equal_files.bats @@ -0,0 +1,83 @@ +#!/usr/bin/env bats + +load 'test_helper' +fixtures 'exist' + +# Correctness +@test 'assert_files_equal() : returns 0 if and 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() : returns 1 if and 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() : used 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() : 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() : 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() : 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]}" == "--" ] +} diff --git a/test/58-assert-11-assert_not_equal_files.bats b/test/58-assert-11-assert_not_equal_files.bats new file mode 100644 index 0000000..a750e79 --- /dev/null +++ b/test/58-assert-11-assert_not_equal_files.bats @@ -0,0 +1,78 @@ +#!/usr/bin/env bats + +load 'test_helper' +fixtures 'exist' + +# Correctness +@test 'assert_files_not_equal() : returns 0 if and 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() : returns 1 if and 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() : used 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() : 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() : 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() : 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]}" == "--" ] +} diff --git a/test/fixtures/exist/dir/file_with_text b/test/fixtures/exist/dir/file_with_text new file mode 100644 index 0000000..753c8d9 --- /dev/null +++ b/test/fixtures/exist/dir/file_with_text @@ -0,0 +1,8 @@ +sesetsetasdfasfdjlsakjdfklasjd +sldkfjaslkdfjasdkfj +slkdfjhaskldfjhlakshdfklasjdfh +sdkfjhslkjfhsdlkjhfg +lksajdfklöjsafkljsadafsjflkjdf +sadfasfsad +sadfasfdsadsfasdfasf + diff --git a/test/fixtures/exist/dir/same_file_with_text b/test/fixtures/exist/dir/same_file_with_text new file mode 100644 index 0000000..753c8d9 --- /dev/null +++ b/test/fixtures/exist/dir/same_file_with_text @@ -0,0 +1,8 @@ +sesetsetasdfasfdjlsakjdfklasjd +sldkfjaslkdfjasdkfj +slkdfjhaskldfjhlakshdfklasjdfh +sdkfjhslkjfhsdlkjhfg +lksajdfklöjsafkljsadafsjflkjdf +sadfasfsad +sadfasfdsadsfasdfasf +