From aa4cdd1007282a5a63596d6d0f2a6deb5ffd3b40 Mon Sep 17 00:00:00 2001 From: katarn Date: Wed, 13 Sep 2023 20:31:45 +0200 Subject: [PATCH 01/12] Test check docs --- .github/workflows/check-docs.yml | 37 ++++++++++++++++++++++++++++++++ docs/assertions.md | 3 +++ 2 files changed, 40 insertions(+) create mode 100644 .github/workflows/check-docs.yml diff --git a/.github/workflows/check-docs.yml b/.github/workflows/check-docs.yml new file mode 100644 index 00000000..e65f77fe --- /dev/null +++ b/.github/workflows/check-docs.yml @@ -0,0 +1,37 @@ +name: Deploy Docs + +on: + pull_request: + + workflow_dispatch: + +permissions: + contents: read + pages: write + id-token: write + +concurrency: + group: pages + cancel-in-progress: false + +jobs: + build: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v3 + with: + fetch-depth: 0 + - name: Setup Node + uses: actions/setup-node@v3 + with: + node-version: 18 + cache: yarn + - name: Setup Pages + uses: actions/configure-pages@v3 + - name: Install dependencies + run: yarn install + - name: Build with VitePress + run: | + yarn docs:build + touch docs/.vitepress/dist/.nojekyll diff --git a/docs/assertions.md b/docs/assertions.md index 66db1658..552aaeac 100644 --- a/docs/assertions.md +++ b/docs/assertions.md @@ -1,5 +1,8 @@ # Assertions +When creating tests, you'll need to verify your commands and functions. We provide assertions for these checks. +Below is their documentation. + ## assertEquals **Syntax** ```bash From 6a64e38d1ab8554fb541e4fed406e892c319d0f5 Mon Sep 17 00:00:00 2001 From: katarn Date: Wed, 13 Sep 2023 20:59:43 +0200 Subject: [PATCH 02/12] Update assertEquals doc --- .github/workflows/check-docs.yml | 2 +- docs/assertions.md | 17 ++++++++++------- docs/index.md | 8 -------- 3 files changed, 11 insertions(+), 16 deletions(-) diff --git a/.github/workflows/check-docs.yml b/.github/workflows/check-docs.yml index e65f77fe..80d59a2b 100644 --- a/.github/workflows/check-docs.yml +++ b/.github/workflows/check-docs.yml @@ -1,4 +1,4 @@ -name: Deploy Docs +name: Check Docs on: pull_request: diff --git a/docs/assertions.md b/docs/assertions.md index 552aaeac..800ef257 100644 --- a/docs/assertions.md +++ b/docs/assertions.md @@ -4,15 +4,18 @@ When creating tests, you'll need to verify your commands and functions. We provi Below is their documentation. ## assertEquals -**Syntax** -```bash -assertEquals "expected" "actual" -``` +> `assertEquals "expected" "actual"` -**Example:** +Reports an error if the two variables `expected` and `actual` are not equal. + +*Example:* ```bash -function test_text_should_be_equal() { - assertEquals "expected 123" "expected 123" +function test_success() { + assertEquals "expected" "expected" +} + +function test_failure() { + assertEquals "expected" "unexpected" } ``` diff --git a/docs/index.md b/docs/index.md index 66bad2df..82d2bfdd 100644 --- a/docs/index.md +++ b/docs/index.md @@ -12,13 +12,5 @@ hero: - theme: alt text: Assertions link: /assertions - -features: - - title: Feature A - details: Lorem ipsum dolor sit amet, consectetur adipiscing elit - - title: Feature B - details: Lorem ipsum dolor sit amet, consectetur adipiscing elit - - title: Feature C - details: Lorem ipsum dolor sit amet, consectetur adipiscing elit --- From 0d56d83fc7740d3b56a941fdfd1850377c11fa72 Mon Sep 17 00:00:00 2001 From: katarn Date: Wed, 13 Sep 2023 21:11:03 +0200 Subject: [PATCH 03/12] Docs assertNotEquals --- docs/assertions.md | 47 ++++++++++++++++++++++++++++++++++------------ 1 file changed, 35 insertions(+), 12 deletions(-) diff --git a/docs/assertions.md b/docs/assertions.md index 800ef257..d63d5cba 100644 --- a/docs/assertions.md +++ b/docs/assertions.md @@ -8,27 +8,32 @@ Below is their documentation. Reports an error if the two variables `expected` and `actual` are not equal. +[assertNotEquals](#assertnotequals) is the inverse of this assertion and takes the same arguments. + *Example:* ```bash function test_success() { - assertEquals "expected" "expected" + assertEquals "foo" "foo" } function test_failure() { - assertEquals "expected" "unexpected" + assertEquals "foo" "bar" } ``` ## assertContains -**Syntax** -```bash -assertContains "expected" "actual" -``` +> `assertContains "needle" "haystack"` -**Example:** +Reports an error if `needle` is not a substring of `haystack`. + +*Example:* ```bash -function test_text_should_contain() { - assertContains "expect" "expected 123" +function test_success() { + assertContains "expected" "expected" +} + +function test_failure() { + assertContains "expected" "unexpected" } ``` @@ -38,7 +43,7 @@ function test_text_should_contain() { assertNotContains "expected" "actual" ``` -**Example:** +*Example:* ```bash function test_text_should_not_contain() { assertNotContains "expecs" "expected 123" @@ -51,7 +56,7 @@ function test_text_should_not_contain() { assertMatches "expected" "actual" ``` -**Example:** +*Example:* ```bash function test_text_should_not_contain() { assertMatches ".*xpec*" "expected 123" @@ -64,7 +69,7 @@ function test_text_should_not_contain() { assertNotMatches "expected" "actual" ``` -**Example:** +*Example:* ```bash function test_text_should_not_contain() { assertNotMatches ".*xpes.*" "expected 123" @@ -187,6 +192,24 @@ function test_should_assert_that_an_array_not_contains_1234() { } ``` +## assertNotEquals +> `assertNotEquals "expected" "actual"` + +Reports an error if the two variables `expected` and `actual` are equal. + +[assertEquals](#assertequals) is the inverse of this assertion and takes the same arguments. + +*Example:* +```bash +function test_success() { + assertNotEquals "foo" "bar" +} + +function test_failure() { + assertNotEquals "foo" "foo" +} +``` + ## Example Check out this [simple example](https://github.com/TypedDevs/bashunit/tree/main/example) using **bashunit**, or a more "real" example in the original repository where the idea grew up: [Chemaclass/conventional-commits](https://github.com/Chemaclass/conventional-commits/blob/main/tests/prepare-commit-msg_test.sh). From 2b131b77f94ff0c57f1b2dfc7c14b733f08e6723 Mon Sep 17 00:00:00 2001 From: katarn Date: Wed, 13 Sep 2023 23:49:10 +0200 Subject: [PATCH 04/12] Docs assertContains + assertNotContains --- docs/assertions.md | 37 ++++++++++++++++++++++--------------- 1 file changed, 22 insertions(+), 15 deletions(-) diff --git a/docs/assertions.md b/docs/assertions.md index d63d5cba..4af30af7 100644 --- a/docs/assertions.md +++ b/docs/assertions.md @@ -26,27 +26,16 @@ function test_failure() { Reports an error if `needle` is not a substring of `haystack`. +[assertNotContains](#assertnotcontains) is the inverse of this assertion and takes the same arguments. + *Example:* ```bash function test_success() { - assertContains "expected" "expected" + assertContains "foo" "foobar" } function test_failure() { - assertContains "expected" "unexpected" -} -``` - -## assertNotContains -**Syntax** -```bash -assertNotContains "expected" "actual" -``` - -*Example:* -```bash -function test_text_should_not_contain() { - assertNotContains "expecs" "expected 123" + assertContains "baz" "foobar" } ``` @@ -210,6 +199,24 @@ function test_failure() { } ``` +## assertNotContains +> `assertNotContains "needle" "haystack"` + +Reports an error if `needle` is a substring of `haystack`. + +[assertContains](#assertcontains) is the inverse of this assertion and takes the same arguments. + +*Example:* +```bash +function test_success() { + assertNotContains "baz" "foobar" +} + +function test_failure() { + assertNotContains "foo" "foobar" +} +``` + ## Example Check out this [simple example](https://github.com/TypedDevs/bashunit/tree/main/example) using **bashunit**, or a more "real" example in the original repository where the idea grew up: [Chemaclass/conventional-commits](https://github.com/Chemaclass/conventional-commits/blob/main/tests/prepare-commit-msg_test.sh). From 0940bcb90b643149e5c6f30afadb2ea2daa3386d Mon Sep 17 00:00:00 2001 From: katarn Date: Wed, 13 Sep 2023 23:51:05 +0200 Subject: [PATCH 05/12] Put WIP docs on separated temporary file --- docs/assertions.md | 146 ----------------------------------------- docs/wip_assertions.md | 145 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 145 insertions(+), 146 deletions(-) create mode 100644 docs/wip_assertions.md diff --git a/docs/assertions.md b/docs/assertions.md index 4af30af7..bda40f01 100644 --- a/docs/assertions.md +++ b/docs/assertions.md @@ -39,148 +39,6 @@ function test_failure() { } ``` -## assertMatches -**Syntax** -```bash -assertMatches "expected" "actual" -``` - -*Example:* -```bash -function test_text_should_not_contain() { - assertMatches ".*xpec*" "expected 123" -} -``` - -## assertNotMatches -**Syntax** -```bash -assertNotMatches "expected" "actual" -``` - -*Example:* -```bash -function test_text_should_not_contain() { - assertNotMatches ".*xpes.*" "expected 123" -} -``` - -## assertExitCode -**Syntax** -```bash -assertExitCode "expected" [execution of the function to test] -``` - -**Examples:** -```bash -function test_should_validate_a_non_ok_exit_code() { - function fake_function() { - return 1 - } - fake_function - assertExitCode "1" -} -``` -```bash -function test_other_way_of_using_the_exit_code() { - function fake_function() { - return 1 - } - assertExitCode "1" "$(fake_function)" -} -``` - -## assertSuccessfulCode -**Syntax** -```bash -assertSuccessfulCode [execute the function or command to assert] -``` - -**Examples:** -```bash -function test_successful_exit_code() { - function fake_function() { - return 0 - } - assertSuccessfulCode "$(fake_function)" -} -``` -```bash -function test_other_way_of_using_the_successful_exit_code() { - function fake_function() { - return 0 - } - fake_function - assertSuccessfulCode -} -``` - -## assertGeneralError -**Syntax** -```bash -assertGeneralError [execute the function or command to assert] -``` - -**Examples:** -```bash -function test_general_error() { - function fake_function() { - return 1 - } - assertGeneralError "$(fake_function)" -} -``` -```bash -function test_other_way_of_using_the_general_error() { - function fake_function() { - return 1 - } - fake_function - assertGeneralError -} -``` - -## assertCommandNotFound -**Syntax** -```bash -assertGeneralError [execute the function or command to assert] -``` - -**Examples:** -```bash -function test_should_assert_exit_code_of_a_non_existing_command() { - assertCommandNotFound "$(a_non_existing_function > /dev/null 2>&1)" -} -``` - -## assertArrayContains -**Syntax** -```bash -assertArrayContains "expected" "actual elements on the array" -``` - -**Examples:** -```bash -function test_should_assert_that_an_array_contains_1234() { - local distros=(Ubuntu 1234 Linux\ Mint) - assertArrayContains "1234" "${distros[@]}" -} -``` - -## assertArrayNotContains -**Syntax** -```bash -assertArrayNotContains "expected" "actual elements on the array" -``` - -**Examples:** -```bash -function test_should_assert_that_an_array_not_contains_1234() { - local distros=(Ubuntu 1234 Linux\ Mint) - assertArrayNotContains "a_non_existing_element" "${distros[@]}" -} -``` - ## assertNotEquals > `assertNotEquals "expected" "actual"` @@ -216,7 +74,3 @@ function test_failure() { assertNotContains "foo" "foobar" } ``` - -## Example - -Check out this [simple example](https://github.com/TypedDevs/bashunit/tree/main/example) using **bashunit**, or a more "real" example in the original repository where the idea grew up: [Chemaclass/conventional-commits](https://github.com/Chemaclass/conventional-commits/blob/main/tests/prepare-commit-msg_test.sh). diff --git a/docs/wip_assertions.md b/docs/wip_assertions.md new file mode 100644 index 00000000..6cc044bf --- /dev/null +++ b/docs/wip_assertions.md @@ -0,0 +1,145 @@ +## assertMatches +**Syntax** +```bash +assertMatches "expected" "actual" +``` + +*Example:* +```bash +function test_text_should_not_contain() { + assertMatches ".*xpec*" "expected 123" +} +``` + +## assertNotMatches +**Syntax** +```bash +assertNotMatches "expected" "actual" +``` + +*Example:* +```bash +function test_text_should_not_contain() { + assertNotMatches ".*xpes.*" "expected 123" +} +``` + +## assertExitCode +**Syntax** +```bash +assertExitCode "expected" [execution of the function to test] +``` + +**Examples:** +```bash +function test_should_validate_a_non_ok_exit_code() { + function fake_function() { + return 1 + } + fake_function + assertExitCode "1" +} +``` +```bash +function test_other_way_of_using_the_exit_code() { + function fake_function() { + return 1 + } + assertExitCode "1" "$(fake_function)" +} +``` + +## assertSuccessfulCode +**Syntax** +```bash +assertSuccessfulCode [execute the function or command to assert] +``` + +**Examples:** +```bash +function test_successful_exit_code() { + function fake_function() { + return 0 + } + assertSuccessfulCode "$(fake_function)" +} +``` +```bash +function test_other_way_of_using_the_successful_exit_code() { + function fake_function() { + return 0 + } + fake_function + assertSuccessfulCode +} +``` + +## assertGeneralError +**Syntax** +```bash +assertGeneralError [execute the function or command to assert] +``` + +**Examples:** +```bash +function test_general_error() { + function fake_function() { + return 1 + } + assertGeneralError "$(fake_function)" +} +``` +```bash +function test_other_way_of_using_the_general_error() { + function fake_function() { + return 1 + } + fake_function + assertGeneralError +} +``` + +## assertCommandNotFound +**Syntax** +```bash +assertGeneralError [execute the function or command to assert] +``` + +**Examples:** +```bash +function test_should_assert_exit_code_of_a_non_existing_command() { + assertCommandNotFound "$(a_non_existing_function > /dev/null 2>&1)" +} +``` + +## assertArrayContains +**Syntax** +```bash +assertArrayContains "expected" "actual elements on the array" +``` + +**Examples:** +```bash +function test_should_assert_that_an_array_contains_1234() { + local distros=(Ubuntu 1234 Linux\ Mint) + assertArrayContains "1234" "${distros[@]}" +} +``` + +## assertArrayNotContains +**Syntax** +```bash +assertArrayNotContains "expected" "actual elements on the array" +``` + +**Examples:** +```bash +function test_should_assert_that_an_array_not_contains_1234() { + local distros=(Ubuntu 1234 Linux\ Mint) + assertArrayNotContains "a_non_existing_element" "${distros[@]}" +} +``` + +## Example + +Check out this [simple example](https://github.com/TypedDevs/bashunit/tree/main/example) using **bashunit**, or a more "real" example in the original repository where the idea grew up: [Chemaclass/conventional-commits](https://github.com/Chemaclass/conventional-commits/blob/main/tests/prepare-commit-msg_test.sh). From e9386b4fb541fa82f0f045fe728c43f8d988e8c9 Mon Sep 17 00:00:00 2001 From: katarn Date: Wed, 13 Sep 2023 23:54:02 +0200 Subject: [PATCH 06/12] Fix default soft color --- docs/.vitepress/theme/style.css | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/.vitepress/theme/style.css b/docs/.vitepress/theme/style.css index c40cab80..9a9fa3f0 100644 --- a/docs/.vitepress/theme/style.css +++ b/docs/.vitepress/theme/style.css @@ -65,7 +65,7 @@ h1, h2, h3, h4, h5, h6 { --vp-c-default-1: #d3d7cf; --vp-c-default-2: #e1e3de; --vp-c-default-3: #eceeee; - --vp-c-default-soft: #5557532e; + --vp-c-default-soft: #d3d7cf2e; --vp-c-brand-1: #4e9a06; --vp-c-brand-2: #6cbe1d; From 5b4054c3aa78d47754ebf727466e21e81ef1424a Mon Sep 17 00:00:00 2001 From: Chemaclass Date: Thu, 14 Sep 2023 11:16:48 +0200 Subject: [PATCH 07/12] Add features for docs index page --- docs/index.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/docs/index.md b/docs/index.md index 82d2bfdd..04ed99a9 100644 --- a/docs/index.md +++ b/docs/index.md @@ -12,5 +12,13 @@ hero: - theme: alt text: Assertions link: /assertions + +features: + - title: Enjoy the DX + details: Focus on simplicity and developer experience. + - title: Testing library + details: Ensure the behavior of your logic. + - title: Bash unix shell + details: The most popular command language shell. --- From ea818ca0fb88783f814c790e3ed78afde355cc30 Mon Sep 17 00:00:00 2001 From: katarn Date: Thu, 14 Sep 2023 22:30:27 +0200 Subject: [PATCH 08/12] Docs assertMatches + assertNotMatches --- docs/assertions.md | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/docs/assertions.md b/docs/assertions.md index bda40f01..09422fea 100644 --- a/docs/assertions.md +++ b/docs/assertions.md @@ -39,6 +39,24 @@ function test_failure() { } ``` +## assertMatches +> `assertMatches "pattern" "value"` + +Reports an error if `value` does not match the regular expression `pattern`. + +[assertNotMatches](#assertnotmatches) is the inverse of this assertion and takes the same arguments. + +*Example:* +```bash +function test_success() { + assertMatches "^foo" "foobar" +} + +function test_failure() { + assertMatches "^bar" "foobar" +} +``` + ## assertNotEquals > `assertNotEquals "expected" "actual"` @@ -74,3 +92,21 @@ function test_failure() { assertNotContains "foo" "foobar" } ``` + +## assertNotMatches +> `assertNotMatches "pattern" "value"` + +Reports an error if `value` matches the regular expression `pattern`. + +[assertMatches](#assertmatches) is the inverse of this assertion and takes the same arguments. + +*Example:* +```bash +function test_success() { + assertNotMatches "foo$" "foobar" +} + +function test_failure() { + assertNotMatches "bar$" "foobar" +} +``` From 27265f7df58e304fe11ac55f1266190037701013 Mon Sep 17 00:00:00 2001 From: katarn Date: Thu, 14 Sep 2023 22:47:05 +0200 Subject: [PATCH 09/12] Docs assertExitCode --- docs/assertions.md | 36 +++++++++++++++++++++++++++++ docs/wip_assertions.md | 51 ------------------------------------------ 2 files changed, 36 insertions(+), 51 deletions(-) diff --git a/docs/assertions.md b/docs/assertions.md index 09422fea..af56b1f2 100644 --- a/docs/assertions.md +++ b/docs/assertions.md @@ -57,6 +57,42 @@ function test_failure() { } ``` +## assertExitCode +> `assertExitCode "expected" ["callable"]` + +Reports an error if the exit code of `callable` is not equal to `expected`. + +If `callable` is not provided, it takes the last executed command or function instead. + +*Example:* +```bash +function test_without_callable() { + function fake_function() { + return 1 + } + + fake_function # function took instead `callable` + + assertExitCode "1" +} + +function test_with_callable() { + function fake_function() { + return 1 + } + + assertExitCode "1" "$(fake_function)" +} + +function test_failure() { + function fake_function() { + return 1 + } + + assertExitCode "0" "$(fake_function)" +} +``` + ## assertNotEquals > `assertNotEquals "expected" "actual"` diff --git a/docs/wip_assertions.md b/docs/wip_assertions.md index 6cc044bf..1c0bfe51 100644 --- a/docs/wip_assertions.md +++ b/docs/wip_assertions.md @@ -1,54 +1,3 @@ -## assertMatches -**Syntax** -```bash -assertMatches "expected" "actual" -``` - -*Example:* -```bash -function test_text_should_not_contain() { - assertMatches ".*xpec*" "expected 123" -} -``` - -## assertNotMatches -**Syntax** -```bash -assertNotMatches "expected" "actual" -``` - -*Example:* -```bash -function test_text_should_not_contain() { - assertNotMatches ".*xpes.*" "expected 123" -} -``` - -## assertExitCode -**Syntax** -```bash -assertExitCode "expected" [execution of the function to test] -``` - -**Examples:** -```bash -function test_should_validate_a_non_ok_exit_code() { - function fake_function() { - return 1 - } - fake_function - assertExitCode "1" -} -``` -```bash -function test_other_way_of_using_the_exit_code() { - function fake_function() { - return 1 - } - assertExitCode "1" "$(fake_function)" -} -``` - ## assertSuccessfulCode **Syntax** ```bash From 192ffbc38b4036ee71f32b9611fcff33c167f6bb Mon Sep 17 00:00:00 2001 From: katarn Date: Thu, 14 Sep 2023 23:00:22 +0200 Subject: [PATCH 10/12] Docs assertSuccessfulCode + assertGeneralError --- docs/assertions.md | 86 ++++++++++++++++++++++++++++++++++++++++-- docs/wip_assertions.md | 50 ------------------------ 2 files changed, 82 insertions(+), 54 deletions(-) diff --git a/docs/assertions.md b/docs/assertions.md index af56b1f2..6e4acafe 100644 --- a/docs/assertions.md +++ b/docs/assertions.md @@ -64,9 +64,19 @@ Reports an error if the exit code of `callable` is not equal to `expected`. If `callable` is not provided, it takes the last executed command or function instead. +[assertSuccessfulCode](#assertsuccessfulcode) and [assertGeneralError](#assertgeneralerror) are more semantic versions of this assertion, for which you don't need to specify an exit code. + *Example:* ```bash -function test_without_callable() { +function test_success_with_callable() { + function fake_function() { + return 1 + } + + assertExitCode "1" "$(fake_function)" +} + +function test_success_without_callable() { function fake_function() { return 1 } @@ -76,12 +86,42 @@ function test_without_callable() { assertExitCode "1" } -function test_with_callable() { +function test_failure() { function fake_function() { return 1 } - assertExitCode "1" "$(fake_function)" + assertExitCode "0" "$(fake_function)" +} +``` + +## assertSuccessfulCode +> `assertSuccessfulCode ["callable"]` + +Reports an error if the exit code of `callable` is not successful (`0`). + +If `callable` is not provided, it takes the last executed command or function instead. + +[assertExitCode](#assertexitcode) is the full version of this assertion where you can specify the expected exit code. + +*Example:* +```bash +function test_success_with_callable() { + function fake_function() { + return 0 + } + + assertSuccessfulCode "$(fake_function)" +} + +function test_success_without_callable() { + function fake_function() { + return 0 + } + + fake_function # function took instead `callable` + + assertSuccessfulCode } function test_failure() { @@ -89,7 +129,45 @@ function test_failure() { return 1 } - assertExitCode "0" "$(fake_function)" + assertSuccessfulCode "$(fake_function)" +} +``` + +## assertGeneralError +> `assertGeneralError ["callable"]` + +Reports an error if the exit code of `callable` is not a general error (`1`). + +If `callable` is not provided, it takes the last executed command or function instead. + +[assertExitCode](#assertexitcode) is the full version of this assertion where you can specify the expected exit code. + +*Example:* +```bash +function test_success_with_callable() { + function fake_function() { + return 1 + } + + assertGeneralError "$(fake_function)" +} + +function test_success_without_callable() { + function fake_function() { + return 1 + } + + fake_function # function took instead `callable` + + assertGeneralError +} + +function test_failure() { + function fake_function() { + return 0 + } + + assertGeneralError "$(fake_function)" } ``` diff --git a/docs/wip_assertions.md b/docs/wip_assertions.md index 1c0bfe51..a0368590 100644 --- a/docs/wip_assertions.md +++ b/docs/wip_assertions.md @@ -1,53 +1,3 @@ -## assertSuccessfulCode -**Syntax** -```bash -assertSuccessfulCode [execute the function or command to assert] -``` - -**Examples:** -```bash -function test_successful_exit_code() { - function fake_function() { - return 0 - } - assertSuccessfulCode "$(fake_function)" -} -``` -```bash -function test_other_way_of_using_the_successful_exit_code() { - function fake_function() { - return 0 - } - fake_function - assertSuccessfulCode -} -``` - -## assertGeneralError -**Syntax** -```bash -assertGeneralError [execute the function or command to assert] -``` - -**Examples:** -```bash -function test_general_error() { - function fake_function() { - return 1 - } - assertGeneralError "$(fake_function)" -} -``` -```bash -function test_other_way_of_using_the_general_error() { - function fake_function() { - return 1 - } - fake_function - assertGeneralError -} -``` - ## assertCommandNotFound **Syntax** ```bash From 1757138eedc64603038a9c0faef94c882803f25e Mon Sep 17 00:00:00 2001 From: katarn Date: Thu, 14 Sep 2023 23:15:53 +0200 Subject: [PATCH 11/12] Docs assertCommandNotFound --- .editorconfig | 5 +-- docs/assertions.md | 71 +++++++++++++++++++++++++++++++++--------- docs/wip_assertions.md | 13 -------- 3 files changed, 60 insertions(+), 29 deletions(-) diff --git a/.editorconfig b/.editorconfig index 18e39c66..e752dd6d 100644 --- a/.editorconfig +++ b/.editorconfig @@ -5,12 +5,13 @@ end_of_line = lf insert_final_newline = true charset = utf-8 trim_trailing_whitespace = true - -[**.sh] indent_style = space indent_size = 2 max_line_length = 120 +[**.md] +max_line_length = 80 + [{Makefile,**.mk}] indent_style = tab max_line_length = off diff --git a/docs/assertions.md b/docs/assertions.md index 6e4acafe..cf67398b 100644 --- a/docs/assertions.md +++ b/docs/assertions.md @@ -1,14 +1,15 @@ # Assertions -When creating tests, you'll need to verify your commands and functions. We provide assertions for these checks. -Below is their documentation. +When creating tests, you'll need to verify your commands and functions. We +provide assertions for these checks. Below is their documentation. ## assertEquals > `assertEquals "expected" "actual"` Reports an error if the two variables `expected` and `actual` are not equal. -[assertNotEquals](#assertnotequals) is the inverse of this assertion and takes the same arguments. +[assertNotEquals](#assertnotequals) is the inverse of this assertion and takes +the same arguments. *Example:* ```bash @@ -26,7 +27,8 @@ function test_failure() { Reports an error if `needle` is not a substring of `haystack`. -[assertNotContains](#assertnotcontains) is the inverse of this assertion and takes the same arguments. +[assertNotContains](#assertnotcontains) is the inverse of this assertion and +takes the same arguments. *Example:* ```bash @@ -44,7 +46,8 @@ function test_failure() { Reports an error if `value` does not match the regular expression `pattern`. -[assertNotMatches](#assertnotmatches) is the inverse of this assertion and takes the same arguments. +[assertNotMatches](#assertnotmatches) is the inverse of this assertion and takes +the same arguments. *Example:* ```bash @@ -62,9 +65,13 @@ function test_failure() { Reports an error if the exit code of `callable` is not equal to `expected`. -If `callable` is not provided, it takes the last executed command or function instead. +If `callable` is not provided, it takes the last executed command or function +instead. -[assertSuccessfulCode](#assertsuccessfulcode) and [assertGeneralError](#assertgeneralerror) are more semantic versions of this assertion, for which you don't need to specify an exit code. +[assertSuccessfulCode](#assertsuccessfulcode), +[assertGeneralError](#assertgeneralerror) and +[assertCommandNotFound](#assertcommandnotfound) are more semantic versions of +this assertion, for which you don't need to specify an exit code. *Example:* ```bash @@ -100,9 +107,11 @@ function test_failure() { Reports an error if the exit code of `callable` is not successful (`0`). -If `callable` is not provided, it takes the last executed command or function instead. +If `callable` is not provided, it takes the last executed command or function +instead. -[assertExitCode](#assertexitcode) is the full version of this assertion where you can specify the expected exit code. +[assertExitCode](#assertexitcode) is the full version of this assertion where +you can specify the expected exit code. *Example:* ```bash @@ -138,9 +147,11 @@ function test_failure() { Reports an error if the exit code of `callable` is not a general error (`1`). -If `callable` is not provided, it takes the last executed command or function instead. +If `callable` is not provided, it takes the last executed command or function +instead. -[assertExitCode](#assertexitcode) is the full version of this assertion where you can specify the expected exit code. +[assertExitCode](#assertexitcode) is the full version of this assertion where +you can specify the expected exit code. *Example:* ```bash @@ -171,12 +182,42 @@ function test_failure() { } ``` +## assertCommandNotFound +> `assertGeneralError ["callable"]` + +Reports an error if `callable` exists. In other words, if executing `callable` +does not return a command not found exit code (`127`). + +If `callable` is not provided, it takes the last executed command or function +instead. + +[assertExitCode](#assertexitcode) is the full version of this assertion where +you can specify the expected exit code. + +*Example:* +```bash +function test_success_with_callable() { + assertCommandNotFound "$(non_existing_callable > /dev/null 2>&1)" +} + +function test_success_without_callable() { + non_existing_callable > /dev/null 2>&1 + + assertCommandNotFound +} + +function test_failure() { + assertCommandNotFound "$(ls > /dev/null 2>&1)" +} +``` + ## assertNotEquals > `assertNotEquals "expected" "actual"` Reports an error if the two variables `expected` and `actual` are equal. -[assertEquals](#assertequals) is the inverse of this assertion and takes the same arguments. +[assertEquals](#assertequals) is the inverse of this assertion and takes the +same arguments. *Example:* ```bash @@ -194,7 +235,8 @@ function test_failure() { Reports an error if `needle` is a substring of `haystack`. -[assertContains](#assertcontains) is the inverse of this assertion and takes the same arguments. +[assertContains](#assertcontains) is the inverse of this assertion and takes the +same arguments. *Example:* ```bash @@ -212,7 +254,8 @@ function test_failure() { Reports an error if `value` matches the regular expression `pattern`. -[assertMatches](#assertmatches) is the inverse of this assertion and takes the same arguments. +[assertMatches](#assertmatches) is the inverse of this assertion and takes the +same arguments. *Example:* ```bash diff --git a/docs/wip_assertions.md b/docs/wip_assertions.md index a0368590..15334ef0 100644 --- a/docs/wip_assertions.md +++ b/docs/wip_assertions.md @@ -1,16 +1,3 @@ -## assertCommandNotFound -**Syntax** -```bash -assertGeneralError [execute the function or command to assert] -``` - -**Examples:** -```bash -function test_should_assert_exit_code_of_a_non_existing_command() { - assertCommandNotFound "$(a_non_existing_function > /dev/null 2>&1)" -} -``` - ## assertArrayContains **Syntax** ```bash From 80df0cfabba11f8e00f0cfc4cce06022f4a3470d Mon Sep 17 00:00:00 2001 From: katarn Date: Thu, 14 Sep 2023 23:45:31 +0200 Subject: [PATCH 12/12] Fix styling --- .editorconfig | 6 +- docs/assertions.md | 138 +++++++++++++++++++++++++---------------- docs/wip_assertions.md | 31 --------- 3 files changed, 87 insertions(+), 88 deletions(-) delete mode 100644 docs/wip_assertions.md diff --git a/.editorconfig b/.editorconfig index e752dd6d..d470bce6 100644 --- a/.editorconfig +++ b/.editorconfig @@ -5,17 +5,17 @@ end_of_line = lf insert_final_newline = true charset = utf-8 trim_trailing_whitespace = true + +[**.sh] indent_style = space indent_size = 2 max_line_length = 120 [**.md] -max_line_length = 80 +indent_style = space [{Makefile,**.mk}] indent_style = tab -max_line_length = off - [tests/acceptance/**.sh] indent_size = unset diff --git a/docs/assertions.md b/docs/assertions.md index cf67398b..8e5a4889 100644 --- a/docs/assertions.md +++ b/docs/assertions.md @@ -1,15 +1,15 @@ # Assertions -When creating tests, you'll need to verify your commands and functions. We -provide assertions for these checks. Below is their documentation. +When creating tests, you'll need to verify your commands and functions. +We provide assertions for these checks. +Below is their documentation. ## assertEquals > `assertEquals "expected" "actual"` Reports an error if the two variables `expected` and `actual` are not equal. -[assertNotEquals](#assertnotequals) is the inverse of this assertion and takes -the same arguments. +[assertNotEquals](#assertnotequals) is the inverse of this assertion and takes the same arguments. *Example:* ```bash @@ -27,8 +27,7 @@ function test_failure() { Reports an error if `needle` is not a substring of `haystack`. -[assertNotContains](#assertnotcontains) is the inverse of this assertion and -takes the same arguments. +[assertNotContains](#assertnotcontains) is the inverse of this assertion and takes the same arguments. *Example:* ```bash @@ -46,8 +45,7 @@ function test_failure() { Reports an error if `value` does not match the regular expression `pattern`. -[assertNotMatches](#assertnotmatches) is the inverse of this assertion and takes -the same arguments. +[assertNotMatches](#assertnotmatches) is the inverse of this assertion and takes the same arguments. *Example:* ```bash @@ -65,40 +63,59 @@ function test_failure() { Reports an error if the exit code of `callable` is not equal to `expected`. -If `callable` is not provided, it takes the last executed command or function -instead. +If `callable` is not provided, it takes the last executed command or function instead. -[assertSuccessfulCode](#assertsuccessfulcode), -[assertGeneralError](#assertgeneralerror) and -[assertCommandNotFound](#assertcommandnotfound) are more semantic versions of -this assertion, for which you don't need to specify an exit code. +[assertSuccessfulCode](#assertsuccessfulcode), [assertGeneralError](#assertgeneralerror) and [assertCommandNotFound](#assertcommandnotfound) +are more semantic versions of this assertion, for which you don't need to specify an exit code. *Example:* ```bash function test_success_with_callable() { - function fake_function() { + function foo() { return 1 } - assertExitCode "1" "$(fake_function)" + assertExitCode "1" "$(foo)" } function test_success_without_callable() { - function fake_function() { + function foo() { return 1 } - fake_function # function took instead `callable` + foo # function took instead `callable` assertExitCode "1" } function test_failure() { - function fake_function() { + function foo() { return 1 } - assertExitCode "0" "$(fake_function)" + assertExitCode "0" "$(foo)" +} +``` + +## assertArrayContains +> `assertArrayContains "needle" "haystack"` + +Reports an error if `needle` is not an element of `haystack`. + +[assertArrayNotContains](#assertarraynotcontains) is the inverse of this assertion and takes the same arguments. + +*Example:* +```bash +function test_success() { + local haystack=(foo bar baz) + + assertArrayContains "bar" "${haystack[@]}" +} + +function test_failure() { + local haystack=(foo bar baz) + + assertArrayContains "foobar" "${haystack[@]}" } ``` @@ -107,38 +124,36 @@ function test_failure() { Reports an error if the exit code of `callable` is not successful (`0`). -If `callable` is not provided, it takes the last executed command or function -instead. +If `callable` is not provided, it takes the last executed command or function instead. -[assertExitCode](#assertexitcode) is the full version of this assertion where -you can specify the expected exit code. +[assertExitCode](#assertexitcode) is the full version of this assertion where you can specify the expected exit code. *Example:* ```bash function test_success_with_callable() { - function fake_function() { + function foo() { return 0 } - assertSuccessfulCode "$(fake_function)" + assertSuccessfulCode "$(foo)" } function test_success_without_callable() { - function fake_function() { + function foo() { return 0 } - fake_function # function took instead `callable` + foo # function took instead `callable` assertSuccessfulCode } function test_failure() { - function fake_function() { + function foo() { return 1 } - assertSuccessfulCode "$(fake_function)" + assertSuccessfulCode "$(foo)" } ``` @@ -147,61 +162,57 @@ function test_failure() { Reports an error if the exit code of `callable` is not a general error (`1`). -If `callable` is not provided, it takes the last executed command or function -instead. +If `callable` is not provided, it takes the last executed command or function instead. -[assertExitCode](#assertexitcode) is the full version of this assertion where -you can specify the expected exit code. +[assertExitCode](#assertexitcode) is the full version of this assertion where you can specify the expected exit code. *Example:* ```bash function test_success_with_callable() { - function fake_function() { + function foo() { return 1 } - assertGeneralError "$(fake_function)" + assertGeneralError "$(foo)" } function test_success_without_callable() { - function fake_function() { + function foo() { return 1 } - fake_function # function took instead `callable` + foo # function took instead `callable` assertGeneralError } function test_failure() { - function fake_function() { + function foo() { return 0 } - assertGeneralError "$(fake_function)" + assertGeneralError "$(foo)" } ``` ## assertCommandNotFound > `assertGeneralError ["callable"]` -Reports an error if `callable` exists. In other words, if executing `callable` -does not return a command not found exit code (`127`). +Reports an error if `callable` exists. +In other words, if executing `callable` does not return a command not found exit code (`127`). -If `callable` is not provided, it takes the last executed command or function -instead. +If `callable` is not provided, it takes the last executed command or function instead. -[assertExitCode](#assertexitcode) is the full version of this assertion where -you can specify the expected exit code. +[assertExitCode](#assertexitcode) is the full version of this assertion where you can specify the expected exit code. *Example:* ```bash function test_success_with_callable() { - assertCommandNotFound "$(non_existing_callable > /dev/null 2>&1)" + assertCommandNotFound "$(foo > /dev/null 2>&1)" } function test_success_without_callable() { - non_existing_callable > /dev/null 2>&1 + foo > /dev/null 2>&1 assertCommandNotFound } @@ -216,8 +227,7 @@ function test_failure() { Reports an error if the two variables `expected` and `actual` are equal. -[assertEquals](#assertequals) is the inverse of this assertion and takes the -same arguments. +[assertEquals](#assertequals) is the inverse of this assertion and takes the same arguments. *Example:* ```bash @@ -235,8 +245,7 @@ function test_failure() { Reports an error if `needle` is a substring of `haystack`. -[assertContains](#assertcontains) is the inverse of this assertion and takes the -same arguments. +[assertContains](#assertcontains) is the inverse of this assertion and takes the same arguments. *Example:* ```bash @@ -254,8 +263,7 @@ function test_failure() { Reports an error if `value` matches the regular expression `pattern`. -[assertMatches](#assertmatches) is the inverse of this assertion and takes the -same arguments. +[assertMatches](#assertmatches) is the inverse of this assertion and takes the same arguments. *Example:* ```bash @@ -267,3 +275,25 @@ function test_failure() { assertNotMatches "bar$" "foobar" } ``` + +## assertArrayNotContains +> `assertArrayNotContains "needle" "haystack"` + +Reports an error if `needle` is an element of `haystack`. + +[assertArrayContains](#assertarraycontains) is the inverse of this assertion and takes the same arguments. + +*Example:* +```bash +function test_success() { + local haystack=(foo bar baz) + + assertArrayNotContains "foobar" "${haystack[@]}" +} + +function test_failure() { + local haystack=(foo bar baz) + + assertArrayNotContains "baz" "${haystack[@]}" +} +``` diff --git a/docs/wip_assertions.md b/docs/wip_assertions.md deleted file mode 100644 index 15334ef0..00000000 --- a/docs/wip_assertions.md +++ /dev/null @@ -1,31 +0,0 @@ -## assertArrayContains -**Syntax** -```bash -assertArrayContains "expected" "actual elements on the array" -``` - -**Examples:** -```bash -function test_should_assert_that_an_array_contains_1234() { - local distros=(Ubuntu 1234 Linux\ Mint) - assertArrayContains "1234" "${distros[@]}" -} -``` - -## assertArrayNotContains -**Syntax** -```bash -assertArrayNotContains "expected" "actual elements on the array" -``` - -**Examples:** -```bash -function test_should_assert_that_an_array_not_contains_1234() { - local distros=(Ubuntu 1234 Linux\ Mint) - assertArrayNotContains "a_non_existing_element" "${distros[@]}" -} -``` - -## Example - -Check out this [simple example](https://github.com/TypedDevs/bashunit/tree/main/example) using **bashunit**, or a more "real" example in the original repository where the idea grew up: [Chemaclass/conventional-commits](https://github.com/Chemaclass/conventional-commits/blob/main/tests/prepare-commit-msg_test.sh).