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 ed0c593..62687da 100644 --- a/README.md +++ b/README.md @@ -208,6 +208,7 @@ path1 : /path/to/file path2 : /path/to/same_file -- ``` + ### `assert_symlink_to` Fail if the given file is not a symbolic to a defined target. ```bash @@ -222,7 +223,6 @@ path : /path/to/symlink -- ``` - ### `assert_file_not_exist` Fail if the given file or directory exists. diff --git a/src/file.bash b/src/file.bash index b1eda01..c9c5c83 100644 --- a/src/file.bash +++ b/src/file.bash @@ -324,7 +324,6 @@ RESULT=$PHYS_DIR/$TARGET_FILE echo $RESULT } - if [ ! -L $link ]; then local -r rem="$BATSLIB_FILE_PATH_REM" local -r add="$BATSLIB_FILE_PATH_ADD" 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 +