├── .gitattributes ├── .github ├── FUNDING.yml └── workflows │ ├── bash_unit.yml │ └── pre-commit.yml ├── .gitignore ├── .pre-commit-config.yaml ├── .pre-commit-hooks.yaml ├── CONTRIBUTING.md ├── CONTRIBUTING_CODE.md ├── LICENSE ├── README.adoc ├── bash_unit ├── docs └── man │ └── man1 │ └── bash_unit.1 ├── img ├── bash_unit_100.png ├── bash_unit_300.png ├── bu_100.png ├── bu_50.png └── demo.gif ├── install.sh ├── release └── tests ├── test_cli.sh ├── test_core.sh ├── test_doc.sh └── test_tap_format /.gitattributes: -------------------------------------------------------------------------------- 1 | bash_unit filter=insert_tag 2 | -------------------------------------------------------------------------------- /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | # These are supported funding model platforms 2 | 3 | github: [pgrange] 4 | # patreon: # Replace with a single Patreon username 5 | # open_collective: # Replace with a single Open Collective username 6 | # ko_fi: # Replace with a single Ko-fi username 7 | # tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel 8 | # community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry 9 | liberapay: bash_unit 10 | # issuehunt: # Replace with a single IssueHunt username 11 | # otechie: # Replace with a single Otechie username 12 | # lfx_crowdfunding: # Replace with a single LFX Crowdfunding project-name e.g., cloud-foundry 13 | # custom: https://liberapay.com/bash_unit/donate 14 | -------------------------------------------------------------------------------- /.github/workflows/bash_unit.yml: -------------------------------------------------------------------------------- 1 | name: bash_unit tests 2 | on: 3 | push: 4 | branches: [ main ] 5 | pull_request: 6 | branches: [ main ] 7 | 8 | jobs: 9 | ubuntu: 10 | runs-on: ubuntu-latest 11 | steps: 12 | - uses: actions/checkout@v4 13 | - name: Unit testing with bash_unit 14 | run: FORCE_COLOR=true ./bash_unit tests/test* 15 | -------------------------------------------------------------------------------- /.github/workflows/pre-commit.yml: -------------------------------------------------------------------------------- 1 | --- 2 | name: pre-commit 3 | on: 4 | push: 5 | branches: [ main ] 6 | pull_request: 7 | branches: [ main ] 8 | jobs: 9 | pre-commit: 10 | runs-on: ubuntu-latest 11 | env: 12 | RAW_LOG: pre-commit.log 13 | CS_XML: pre-commit.xml 14 | steps: 15 | - run: sudo apt-get update && sudo apt-get install cppcheck 16 | if: false 17 | - uses: actions/checkout@v4 18 | - uses: actions/setup-python@v4 19 | if: false 20 | with: 21 | cache: pip 22 | python-version: 3.12.1 23 | - run: python -m pip install pre-commit regex 24 | - uses: actions/cache/restore@v4 25 | with: 26 | path: | 27 | ~/.cache/pre-commit/ 28 | bash_unit 29 | key: pre-commit-4|${{ env.pythonLocation }}|${{ hashFiles('.pre-commit-config.yaml') 30 | }} 31 | - name: Run pre-commit hooks 32 | env: 33 | SKIP: no-commit-to-branch 34 | run: | 35 | set -o pipefail 36 | pre-commit gc 37 | # Run default pre-commit hooks 38 | pre-commit run --show-diff-on-failure --color=always --all-files | tee ${RAW_LOG} 39 | # Run this version of bash_unit using it's current pre-commit-hook.yaml config 40 | # Useful for testing that the current version works 41 | pre-commit try-repo . --verbose --all-files | tee -a ${RAW_LOG} 42 | - name: Convert Raw Log to annotations 43 | uses: mdeweerd/logToCheckStyle@v2024.2.3 44 | if: ${{ failure() }} 45 | with: 46 | in: ${{ env.RAW_LOG }} 47 | # Out can be omitted if you do not need the xml output 48 | # out: ${{ env.CS_XML }} 49 | 50 | - uses: actions/cache/save@v4 51 | if: ${{ always() }} 52 | with: 53 | path: | 54 | ~/.cache/pre-commit/ 55 | bash_unit 56 | key: pre-commit-4|${{ env.pythonLocation }}|${{ hashFiles('.pre-commit-config.yaml') 57 | }} 58 | - name: Provide log as artifact 59 | uses: actions/upload-artifact@v4 60 | if: ${{ always() }} 61 | with: 62 | name: precommit-logs 63 | path: | 64 | ${{ env.RAW_LOG }} 65 | ${{ env.CS_XML }} 66 | retention-days: 2 67 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.swp 2 | token 3 | 4 | # BSD sed generated files 5 | *-e 6 | -------------------------------------------------------------------------------- /.pre-commit-config.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | repos: 3 | - repo: https://github.com/pre-commit/pre-commit-hooks 4 | rev: v4.5.0 5 | hooks: 6 | - id: check-yaml 7 | args: [--unsafe] 8 | - id: check-json 9 | - id: mixed-line-ending 10 | - id: trailing-whitespace 11 | exclude_types: [markdown] 12 | - id: end-of-file-fixer 13 | exclude: ^docs/man/.*$ 14 | - id: check-merge-conflict 15 | - id: check-executables-have-shebangs 16 | - id: check-shebang-scripts-are-executable 17 | exclude: ^tests/test_.*$ 18 | - id: fix-byte-order-marker 19 | - id: check-case-conflict 20 | 21 | # Beautify shell scripts 22 | # Deactivated because of an issue with deprecated python dependencies. 23 | # See https://github.com/lovesegfault/beautysh/issues/248 and its fix to re-activate 24 | # - repo: https://github.com/lovesegfault/beautysh.git 25 | # rev: v6.2.1 26 | # hooks: 27 | # - id: beautysh 28 | # exclude: (?x)^(tests/test_(.*))$ 29 | # args: [-i, "2"] 30 | 31 | # Run local script 32 | - repo: local 33 | hooks: 34 | - id: local-precommit-script 35 | name: Run local script before commit if it exists 36 | language: system 37 | entry: bash -c '[ ! -x local.sh ] || ./local.sh' 38 | pass_filenames: false 39 | 40 | # Prettier (format code, only for non common files) 41 | - repo: https://github.com/pre-commit/mirrors-prettier 42 | rev: v4.0.0-alpha.8 43 | hooks: 44 | - id: prettier 45 | stages: [manual] 46 | exclude_types: 47 | - executable 48 | - binary 49 | - shell 50 | - markdown 51 | 52 | # Check format of yaml files 53 | - repo: https://github.com/adrienverge/yamllint.git 54 | rev: v1.35.1 55 | hooks: 56 | - id: yamllint 57 | args: 58 | - --no-warnings 59 | - -d 60 | - '{extends: relaxed, rules: {line-length: {max: 120}}}' 61 | 62 | # Execute codespell to fix typo errors (setup of codespell into dev/tools/codespell/) 63 | - repo: https://github.com/codespell-project/codespell 64 | rev: v2.2.6 65 | hooks: 66 | - id: codespell 67 | args: 68 | - --ignore-words-list=master,als 69 | - --builtin=clear,rare,informal,usage,code,names,en-GB_to_en-US 70 | exclude_types: [image] 71 | exclude: ^docs/man/.*$ 72 | 73 | # Check some shell scripts 74 | - repo: https://github.com/shellcheck-py/shellcheck-py 75 | rev: v0.9.0.6 76 | hooks: 77 | - id: shellcheck 78 | args: [-W, '100'] 79 | 80 | # Run tests 81 | - repo: local 82 | hooks: 83 | - id: tests 84 | stages: [manual] 85 | name: Run tests 86 | language: system 87 | entry: bash -c "./bash_unit tests/*" 88 | pass_filenames: false 89 | 90 | # This is how one can use bash_unit with pre-commit 91 | # Only for documentation purpose as it can't be 92 | # used in bash_unit itself: we always want to test 93 | # bash_unit with the last version of bash_unit. 94 | # Otherwise tests would always fail when we introduce 95 | # a new feature. 96 | # - repo: https://github.com/pgrange/bash_unit 97 | # rev: v2.2.0 98 | # hooks: 99 | # - id: bash-unit 100 | # stages: [manual] 101 | # always_run: true 102 | # verbose: true 103 | -------------------------------------------------------------------------------- /.pre-commit-hooks.yaml: -------------------------------------------------------------------------------- 1 | # Define hooks that this repository provides for pre-commit 2 | - id: bash-unit 3 | name: Run tests with `bash_unit` 4 | description: This hook runs tests using `bash_unit` 5 | entry: ./bash_unit 6 | language: script 7 | files: ^tests/(.*/)?test_.*\.sh$ 8 | types: [shell] 9 | pass_filenames: true 10 | # Duplicated with different id name for convienience 11 | - id: bash_unit 12 | name: Run tests with `bash_unit` 13 | description: This hook runs tests using `bash_unit` 14 | entry: ./bash_unit 15 | language: script 16 | files: ^tests/(.*/)?test_.*\.sh$ 17 | types: [shell] 18 | pass_filenames: true 19 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | **bash unit testing enterprise edition framework for professionals ![bash_unit](img/bu_50.png)** 2 | 3 | If you like bash_unit and want to contribute, you can do so in one of the 4 | following ways (order might matter). 5 | 6 | # Spread the word 7 | 8 | Talking about bash_unit is a very welcomed contribution. 9 | 10 | Share your success with bash_unit on your blog, twitter, stackoverflow... 11 | 12 | Nice words to share your experience with bash_unit is always appreciated. 13 | If you want to say something nice about bash_unit but can not think of a 14 | place for that right now, just add a comment to 15 | [issue #37](https://github.com/pgrange/bash_unit/issues/37). 16 | 17 | Starring the project is also appreciated. 18 | 19 | # Suggest improvements 20 | 21 | When you see something that is not working with bash_unit, or if you feel 22 | something is missing, open an 23 | [issue](https://github.com/pgrange/bash_unit/issues). 24 | 25 | Opening issues helps improving bash_unit. 26 | 27 | # Contribute code 28 | 29 | If you use a tool like bash_unit, odds are high that you already heard 30 | about software development. Just code the improvement you have in mind 31 | and create a pull-request. 32 | 33 | You can contribute with new features you think are missing but you can 34 | also find inspiration with the 35 | [open issues](https://github.com/pgrange/bash_unit/issues) of bash_unit. 36 | 37 | See [how to contribute code](CONTRIBUTING_CODE.md). 38 | 39 | # Donate 40 | 41 | bash_unit is free software I develop during my (not so) free time. If you feel 42 | bash_unit is helpful enough to you that you want to compensate for some of 43 | the time spent developing or promoting it, you can contribute with your money 44 | on liberapay: 45 | 46 | [![liberapay](https://liberapay.com/assets/widgets/donate.svg)](https://liberapay.com/bash_unit/donate) 47 | -------------------------------------------------------------------------------- /CONTRIBUTING_CODE.md: -------------------------------------------------------------------------------- 1 | To perform a new release, run ./release in current directory. 2 | 3 | See help for more information. 4 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | 635 | Copyright (C) 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | Copyright (C) 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | -------------------------------------------------------------------------------- /README.adoc: -------------------------------------------------------------------------------- 1 | ifdef::backend-manpage[] 2 | = BASH_UNIT(1) 3 | 4 | == NAME 5 | endif::[] 6 | 7 | ifndef::backend-manpage[] 8 | image::img/bu_50.png[bash_unit] 9 | endif::[] 10 | 11 | bash_unit - bash unit testing enterprise edition framework for professionals! 12 | 13 | == Synopsis 14 | 15 | *bash_unit* [-f tap] [-p ] [-s ] [-r] [test_file] 16 | 17 | == Description 18 | 19 | *bash_unit* allows you to write unit tests (functions starting with *test*), 20 | run them and, in case of failure, displays the stack trace 21 | with source file and line number indications to locate the problem. 22 | 23 | Need a quick start? The 24 | https://github.com/bash-unit/getting_started/[getting started project] 25 | will help you get on track in no time. 26 | 27 | The following functions are available in your tests (see below for detailed documentation): 28 | 29 | * `fail [message]` 30 | * `assert [message]` 31 | * `assert_fail [message]` 32 | * `assert_status_code [message]` 33 | * `assert_equals [message]` 34 | * `assert_not_equals [message]` 35 | * `assert_matches [message]` 36 | * `assert_not_matches [message]` 37 | * `assert_within_delta [message]` 38 | * `assert_no_diff [message]` 39 | * `skip_if ` 40 | * `fake [replacement code]` 41 | 42 | ifndef::backend-manpage[] 43 | image::img/demo.gif[demo] 44 | endif::[] 45 | 46 | _(by the way, the documentation you are reading is itself tested with bash-unit)_ 47 | 48 | *bash_unit* is free software you may contribute to. See link:CONTRIBUTING.md[CONTRIBUTING.md]. 49 | 50 | 51 | == Options 52 | 53 | *-p* _pattern_:: 54 | filters tests to run based on the given pattern. 55 | You can specify several patterns by repeating this option 56 | for each pattern. 57 | 58 | *-s* _pattern_:: 59 | skip tests which name matches the given pattern. 60 | You can specify several patterns by repeating this option 61 | for each pattern. 62 | Tests will appear in *bash_unit* output as _skipped_. 63 | (see also _skip_if_) 64 | 65 | *-r*:: 66 | executes test cases in random order. 67 | Only affects the order within a test file (files are always 68 | executed in the order in which they are specified on the 69 | command line). 70 | 71 | *-f* _output_format_:: 72 | specify an alternative output format. 73 | The only supported value is *tap*. 74 | 75 | *-q*:: 76 | quiet mode. 77 | Will only output the status of each test with no further 78 | information even in case of failure. 79 | 80 | ifndef::backend-manpage[] 81 | 82 | == How to install *bash_unit* 83 | 84 | === installing on Archlinux 85 | 86 | *bash_unit* package is available on Archlinux through AUR. In order to install, issue the following command : 87 | 88 | yaourt -Sys bash_unit 89 | 90 | === installing via link:https://nixos.org/[Nix/NixOS] 91 | 92 | *bash_unit* package has been added to link:https://github.com/nixos/nixpkgs[nixpkgs]. You can use it with the following command: 93 | 94 | nix-shell -p bash_unit 95 | 96 | === installing via link:https://brew.sh[Homebrew] 97 | 98 | *bash_unit* is available by invoking brew: 99 | 100 | brew install bash_unit 101 | 102 | === other installation 103 | 104 | This will install *bash_unit* in your current working directory: 105 | 106 | curl -s https://raw.githubusercontent.com/pgrange/bash_unit/master/install.sh | bash 107 | 108 | You can also download it from the https://github.com/pgrange/bash_unit/releases[release page]. 109 | 110 | endif::[] 111 | 112 | === GitHub Actions 113 | 114 | Here is an example of how you could integrate *bash_unit* with https://docs.github.com/fr/actions[GitHub Actions]: 115 | 116 | ``` 117 | name: bash_unit tests 118 | on: 119 | push: 120 | branches: [ main ] 121 | pull_request: 122 | branches: [ main ] 123 | 124 | jobs: 125 | ubuntu: 126 | runs-on: ubuntu-latest 127 | steps: 128 | - uses: actions/checkout@v4 129 | - name: Unit testing with bash_unit 130 | run: | 131 | curl -s https://raw.githubusercontent.com/pgrange/bash_unit/master/install.sh | bash 132 | FORCE_COLOR=true ./bash_unit tests/test_* 133 | ``` 134 | 135 | See this bash_unit https://github.com/pgrange/bash_unit_getting_started[getting started github project] for a working example. 136 | 137 | === GitLab CI 138 | 139 | Here is an example of how you could integrate *bash_unit* with https://docs.gitlab.com/ee/ci/[GitLab CI]: 140 | 141 | ``` 142 | test: 143 | image: debian 144 | script: 145 | - apt-get update 146 | - apt-get install --no-install-recommends -y curl ca-certificates 147 | - curl -s https://raw.githubusercontent.com/pgrange/bash_unit/master/install.sh | bash 148 | - FORCE_COLOR=true ./bash_unit tests/test_* 149 | ``` 150 | 151 | See this bash_unit https://gitlab.com/pgrange/bash_unit_getting_started[getting started gitlab project] for a working example. 152 | 153 | === pre-commit hook 154 | 155 | You can run `+bash_unit+` as a https://pre-commit.com[pre-commit] hook. 156 | 157 | Add the following to your pre-commit configuration. By default it will run scripts that are identified as shell scripts that match the path `+^tests/(.*/)?test_.*\.sh$+`. 158 | 159 | [.pre-commit-config,yaml] 160 | ``` 161 | repos: 162 | - repo: https://github.com/pgrange/bash_unit 163 | rev: v2.2.0 164 | hooks: 165 | - id: bash-unit 166 | always-run: true 167 | ``` 168 | 169 | == How to run tests 170 | 171 | To run tests, simply call *bash_unit* with all your tests files as parameter. For instance to run some *bash_unit* tests, from *bash_unit* directory: 172 | 173 | ```test 174 | ./bash_unit tests/test_core.sh 175 | ``` 176 | 177 | ```output 178 | Running tests in tests/test_core.sh 179 | Running test_assert_equals_fails_when_not_equal ... SUCCESS 180 | Running test_assert_equals_succeed_when_equal ... SUCCESS 181 | Running test_assert_fails ... SUCCESS 182 | Running test_assert_fails_fails ... SUCCESS 183 | Running test_assert_fails_succeeds ... SUCCESS 184 | Running test_assert_matches_fails_when_not_matching ... SUCCESS 185 | Running test_assert_matches_succeed_when_matching ... SUCCESS 186 | Running test_assert_no_diff_fails_when_diff ... SUCCESS 187 | Running test_assert_no_diff_succeeds_when_no_diff ... SUCCESS 188 | Running test_assert_not_equals_fails_when_equal ... SUCCESS 189 | Running test_assert_not_equals_succeeds_when_not_equal ... SUCCESS 190 | Running test_assert_not_matches_fails_when_matching ... SUCCESS 191 | Running test_assert_not_matches_succeed_when_not_matching ... SUCCESS 192 | Running test_assert_shows_stderr_on_failure ... SUCCESS 193 | Running test_assert_shows_stdout_on_failure ... SUCCESS 194 | Running test_assert_status_code_fails ... SUCCESS 195 | Running test_assert_status_code_succeeds ... SUCCESS 196 | Running test_assert_succeeds ... SUCCESS 197 | Running test_assert_within_delta_fails ... SUCCESS 198 | Running test_assert_within_delta_succeeds ... SUCCESS 199 | Running test_fail_fails ... SUCCESS 200 | Running test_fail_prints_failure_message ... SUCCESS 201 | Running test_fail_prints_where_is_error ... SUCCESS 202 | Running test_fake_actually_fakes_the_command ... SUCCESS 203 | Running test_fake_can_fake_inline ... SUCCESS 204 | Running test_fake_echo_stdin_when_no_params ... SUCCESS 205 | Running test_fake_exports_faked_in_subshells ... SUCCESS 206 | Running test_fake_transmits_params_to_fake_code ... SUCCESS 207 | Running test_fake_transmits_params_to_fake_code_as_array ... SUCCESS 208 | Running test_should_pretty_format_even_when_LANG_is_unset ... SUCCESS 209 | Overall result: SUCCESS 210 | ``` 211 | 212 | You might also want to run only specific tests, you may do so with the 213 | _-p_ option. This option accepts a pattern as parameter and filters test 214 | functions against this pattern. 215 | 216 | ```test 217 | ./bash_unit -p fail_fails -p assert tests/test_core.sh 218 | ``` 219 | 220 | ```output 221 | Running tests in tests/test_core.sh 222 | Running test_assert_equals_fails_when_not_equal ... SUCCESS 223 | Running test_assert_equals_succeed_when_equal ... SUCCESS 224 | Running test_assert_fails ... SUCCESS 225 | Running test_assert_fails_fails ... SUCCESS 226 | Running test_assert_fails_succeeds ... SUCCESS 227 | Running test_assert_matches_fails_when_not_matching ... SUCCESS 228 | Running test_assert_matches_succeed_when_matching ... SUCCESS 229 | Running test_assert_no_diff_fails_when_diff ... SUCCESS 230 | Running test_assert_no_diff_succeeds_when_no_diff ... SUCCESS 231 | Running test_assert_not_equals_fails_when_equal ... SUCCESS 232 | Running test_assert_not_equals_succeeds_when_not_equal ... SUCCESS 233 | Running test_assert_not_matches_fails_when_matching ... SUCCESS 234 | Running test_assert_not_matches_succeed_when_not_matching ... SUCCESS 235 | Running test_assert_shows_stderr_on_failure ... SUCCESS 236 | Running test_assert_shows_stdout_on_failure ... SUCCESS 237 | Running test_assert_status_code_fails ... SUCCESS 238 | Running test_assert_status_code_succeeds ... SUCCESS 239 | Running test_assert_succeeds ... SUCCESS 240 | Running test_assert_within_delta_fails ... SUCCESS 241 | Running test_assert_within_delta_succeeds ... SUCCESS 242 | Running test_fail_fails ... SUCCESS 243 | Overall result: SUCCESS 244 | ``` 245 | 246 | You can combine the _-p_ option with _-s_ to skip some of the tests. This option accepts a pattern 247 | as parameter and mark as skipped any test function which matches this pattern. 248 | 249 | ```test 250 | ./bash_unit -p fail_fails -p assert -s no -s status tests/test_core.sh 251 | ``` 252 | 253 | ```output 254 | Running tests in tests/test_core.sh 255 | Running test_assert_equals_fails_when_not_equal ... SKIPPED 256 | Running test_assert_matches_fails_when_not_matching ... SKIPPED 257 | Running test_assert_no_diff_fails_when_diff ... SKIPPED 258 | Running test_assert_no_diff_succeeds_when_no_diff ... SKIPPED 259 | Running test_assert_not_equals_fails_when_equal ... SKIPPED 260 | Running test_assert_not_equals_succeeds_when_not_equal ... SKIPPED 261 | Running test_assert_not_matches_fails_when_matching ... SKIPPED 262 | Running test_assert_not_matches_succeed_when_not_matching ... SKIPPED 263 | Running test_assert_status_code_fails ... SKIPPED 264 | Running test_assert_status_code_succeeds ... SKIPPED 265 | Running test_assert_equals_succeed_when_equal ... SUCCESS 266 | Running test_assert_fails ... SUCCESS 267 | Running test_assert_fails_fails ... SUCCESS 268 | Running test_assert_fails_succeeds ... SUCCESS 269 | Running test_assert_matches_succeed_when_matching ... SUCCESS 270 | Running test_assert_shows_stderr_on_failure ... SUCCESS 271 | Running test_assert_shows_stdout_on_failure ... SUCCESS 272 | Running test_assert_succeeds ... SUCCESS 273 | Running test_assert_within_delta_fails ... SUCCESS 274 | Running test_assert_within_delta_succeeds ... SUCCESS 275 | Running test_fail_fails ... SUCCESS 276 | Overall result: SUCCESS 277 | ``` 278 | 279 | *bash_unit* supports the http://testanything.org/[Test Anything Protocol] so you can ask for a tap formatted 280 | output with the _-f_ option. 281 | 282 | ```test 283 | ./bash_unit -f tap tests/test_core.sh 284 | ``` 285 | 286 | ```output 287 | # Running tests in tests/test_core.sh 288 | ok - test_assert_equals_fails_when_not_equal 289 | ok - test_assert_equals_succeed_when_equal 290 | ok - test_assert_fails 291 | ok - test_assert_fails_fails 292 | ok - test_assert_fails_succeeds 293 | ok - test_assert_matches_fails_when_not_matching 294 | ok - test_assert_matches_succeed_when_matching 295 | ok - test_assert_no_diff_fails_when_diff 296 | ok - test_assert_no_diff_succeeds_when_no_diff 297 | ok - test_assert_not_equals_fails_when_equal 298 | ok - test_assert_not_equals_succeeds_when_not_equal 299 | ok - test_assert_not_matches_fails_when_matching 300 | ok - test_assert_not_matches_succeed_when_not_matching 301 | ok - test_assert_shows_stderr_on_failure 302 | ok - test_assert_shows_stdout_on_failure 303 | ok - test_assert_status_code_fails 304 | ok - test_assert_status_code_succeeds 305 | ok - test_assert_succeeds 306 | ok - test_assert_within_delta_fails 307 | ok - test_assert_within_delta_succeeds 308 | ok - test_fail_fails 309 | ok - test_fail_prints_failure_message 310 | ok - test_fail_prints_where_is_error 311 | ok - test_fake_actually_fakes_the_command 312 | ok - test_fake_can_fake_inline 313 | ok - test_fake_echo_stdin_when_no_params 314 | ok - test_fake_exports_faked_in_subshells 315 | ok - test_fake_transmits_params_to_fake_code 316 | ok - test_fake_transmits_params_to_fake_code_as_array 317 | ok - test_should_pretty_format_even_when_LANG_is_unset 318 | 1..30 319 | ``` 320 | 321 | == How to write tests 322 | 323 | Write your test functions in a file. The name of a test function has to start with *test*. Only functions starting with *test* will be tested. 324 | 325 | Use the *bash_unit* assertion functions in your test functions, see below. 326 | 327 | You may write a *setup* function that will be executed before each test is run. 328 | 329 | You may write a *teardown* function that will be executed after each test is run. 330 | 331 | You may write a *setup_suite* function that will be executed only once before all the tests of your test file. 332 | 333 | You may write a *teardown_suite* function that will be executed only once after all the tests of your test file. 334 | 335 | If you write code outside of any bash function, this code will be executed once at test file loading time since 336 | your file is a bash script and *bash_unit* sources it before running your tests. It is suggested to write a 337 | *setup_suite* function and avoid any code outside a bash function. you must not use any bash_unit assertion 338 | in setup_suite or use exit in setup_suite for teardown_suite to be run. 339 | See https://github.com/pgrange/bash_unit/issues/43[issue 43] for more details. 340 | 341 | If you want to keep an eye on a test not yet implemented, prefix the name of the function by *todo* instead of test. 342 | Test to do are not executed and do not impact the global status of your test suite but are displayed in *bash_unit* output. 343 | 344 | *bash_unit* changes the current working directory to the one of the running test file. If you need to access files from your test code, for instance the script under test, use path relative to the test file. 345 | 346 | You may need to change the behavior of some commands to create conditions for your code under test to behave as expected. The *fake* function may help you to do that, see below. 347 | 348 | == Test functions 349 | 350 | *bash_unit* supports several shell oriented assertion functions. 351 | 352 | === *fail* 353 | 354 | fail [message] 355 | 356 | Fails the test and displays an optional message. 357 | 358 | ```test 359 | test_can_fail() { 360 | fail "this test failed on purpose" 361 | } 362 | ``` 363 | 364 | ```output 365 | Running test_can_fail ... FAILURE 366 | this test failed on purpose 367 | doc:2:test_can_fail() 368 | ``` 369 | 370 | === *assert* 371 | 372 | assert [message] 373 | 374 | Evaluates _assertion_ and fails if _assertion_ fails. 375 | 376 | _assertion_ fails if its evaluation returns a status code different from 0. 377 | 378 | In case of failure, the standard output and error of the evaluated _assertion_ is displayed. The optional message is also displayed. 379 | 380 | ```test 381 | test_assert_fails() { 382 | assert false "this test failed, obviously" 383 | } 384 | test_assert_succeed() { 385 | assert true 386 | } 387 | ``` 388 | 389 | ```output 390 | Running test_assert_fails ... FAILURE 391 | this test failed, obviously 392 | doc:2:test_assert_fails() 393 | Running test_assert_succeed ... SUCCESS 394 | ``` 395 | 396 | But you probably want to assert less obvious facts. 397 | 398 | ```test 399 | code() { 400 | touch /tmp/the_file 401 | } 402 | 403 | test_code_creates_the_file() { 404 | code 405 | 406 | assert "test -e /tmp/the_file" 407 | } 408 | 409 | test_code_makes_the_file_executable() { 410 | code 411 | 412 | assert "test -x /tmp/the_file" "/tmp/the_file should be executable" 413 | } 414 | ``` 415 | 416 | ```output 417 | Running test_code_creates_the_file ... SUCCESS 418 | Running test_code_makes_the_file_executable ... FAILURE 419 | /tmp/the_file should be executable 420 | doc:14:test_code_makes_the_file_executable() 421 | ``` 422 | 423 | It may also be fun to use assert to check for the expected content of a file. 424 | 425 | ```test 426 | code() { 427 | echo 'not so cool' > /tmp/the_file 428 | } 429 | 430 | test_code_write_appropriate_content_in_the_file() { 431 | code 432 | 433 | assert "diff <(echo 'this is cool') /tmp/the_file" 434 | } 435 | ``` 436 | 437 | ```output 438 | Running test_code_write_appropriate_content_in_the_file ... FAILURE 439 | out> 1c1 440 | out> < this is cool 441 | out> --- 442 | out> > not so cool 443 | doc:8:test_code_write_appropriate_content_in_the_file() 444 | ``` 445 | 446 | === *assert_fail* 447 | 448 | assert_fail [message] 449 | 450 | Asserts that _assertion_ fails. This is the opposite of *assert*. 451 | 452 | _assertion_ fails if its evaluation returns a status code different from 0. 453 | 454 | If the evaluated expression does not fail, then *assert_fail* will fail and display the standard output and error of the evaluated _assertion_. The optional message is also displayed. 455 | 456 | ```test 457 | code() { 458 | echo 'not so cool' > /tmp/the_file 459 | } 460 | 461 | test_code_does_not_write_cool_in_the_file() { 462 | code 463 | 464 | assert_fails "grep cool /tmp/the_file" "should not write 'cool' in /tmp/the_file" 465 | } 466 | 467 | test_code_does_not_write_this_in_the_file() { 468 | code 469 | 470 | assert_fails "grep this /tmp/the_file" "should not write 'this' in /tmp/the_file" 471 | } 472 | ``` 473 | 474 | ```output 475 | Running test_code_does_not_write_cool_in_the_file ... FAILURE 476 | should not write 'cool' in /tmp/the_file 477 | out> not so cool 478 | doc:8:test_code_does_not_write_cool_in_the_file() 479 | Running test_code_does_not_write_this_in_the_file ... SUCCESS 480 | ``` 481 | 482 | === *assert_status_code* 483 | 484 | assert_status_code [message] 485 | 486 | Checks for a precise status code of the evaluation of _assertion_. 487 | 488 | It may be useful if you want to distinguish between several error conditions in your code. 489 | 490 | In case of failure, the standard output and error of the evaluated _assertion_ is displayed. The optional message is also displayed. 491 | 492 | ```test 493 | code() { 494 | exit 23 495 | } 496 | 497 | test_code_should_fail_with_code_25() { 498 | assert_status_code 25 code 499 | } 500 | ``` 501 | 502 | ```output 503 | Running test_code_should_fail_with_code_25 ... FAILURE 504 | expected status code 25 but was 23 505 | doc:6:test_code_should_fail_with_code_25() 506 | ``` 507 | 508 | === *assert_equals* 509 | 510 | assert_equals [message] 511 | 512 | Asserts for equality of the two strings _expected_ and _actual_. 513 | 514 | ```test 515 | test_obvious_inequality_with_assert_equals(){ 516 | assert_equals "a string" "another string" "a string should be another string" 517 | } 518 | test_obvious_equality_with_assert_equals(){ 519 | assert_equals a a 520 | } 521 | 522 | ``` 523 | 524 | ```output 525 | Running test_obvious_equality_with_assert_equals ... SUCCESS 526 | Running test_obvious_inequality_with_assert_equals ... FAILURE 527 | a string should be another string 528 | expected [a string] but was [another string] 529 | doc:2:test_obvious_inequality_with_assert_equals() 530 | ``` 531 | 532 | === *assert_not_equals* 533 | 534 | assert_not_equals [message] 535 | 536 | Asserts for inequality of the two strings _unexpected_ and _actual_. 537 | 538 | ```test 539 | test_obvious_equality_with_assert_not_equals(){ 540 | assert_not_equals "a string" "a string" "a string should be different from another string" 541 | } 542 | test_obvious_inequality_with_assert_not_equals(){ 543 | assert_not_equals a b 544 | } 545 | 546 | ``` 547 | 548 | ```output 549 | Running test_obvious_equality_with_assert_not_equals ... FAILURE 550 | a string should be different from another string 551 | expected different value than [a string] but was the same 552 | doc:2:test_obvious_equality_with_assert_not_equals() 553 | Running test_obvious_inequality_with_assert_not_equals ... SUCCESS 554 | ``` 555 | 556 | === *assert_matches* 557 | 558 | assert_matches [message] 559 | 560 | Asserts that the string _actual_ matches the regex pattern _expected-regex_. 561 | 562 | ```test 563 | test_obvious_notmatching_with_assert_matches(){ 564 | assert_matches "a str.*" "another string" "'another string' should not match 'a str.*'" 565 | } 566 | test_obvious_matching_with_assert_matches(){ 567 | assert_matches "a[nN].t{0,1}.*r str.*" "another string" 568 | } 569 | 570 | ``` 571 | 572 | ```output 573 | Running test_obvious_matching_with_assert_matches ... SUCCESS 574 | Running test_obvious_notmatching_with_assert_matches ... FAILURE 575 | 'another string' should not match 'a str.*' 576 | expected regex [a str.*] to match [another string] 577 | doc:2:test_obvious_notmatching_with_assert_matches() 578 | ``` 579 | 580 | === *assert_not_matches* 581 | 582 | assert_not_matches [message] 583 | 584 | Asserts that the string _actual_ does not match the regex pattern _unexpected-regex_. 585 | 586 | ```test 587 | test_obvious_matching_with_assert_not_matches(){ 588 | assert_not_matches "a str.*" "a string" "'a string' should not match 'a str.*'" 589 | } 590 | test_obvious_notmatching_with_assert_not_matches(){ 591 | assert_not_matches "a str.*" "another string" 592 | } 593 | 594 | ``` 595 | 596 | ```output 597 | Running test_obvious_matching_with_assert_not_matches ... FAILURE 598 | 'a string' should not match 'a str.*' 599 | expected regex [a str.*] should not match but matched [a string] 600 | doc:2:test_obvious_matching_with_assert_not_matches() 601 | Running test_obvious_notmatching_with_assert_not_matches ... SUCCESS 602 | ``` 603 | 604 | === *assert_within_delta* 605 | 606 | assert_within_delta [message] 607 | 608 | Asserts that the expected num matches the actual num up to a given max delta. 609 | This function only support integers. 610 | Given an expectation of 5 and a delta of 2 this would match 3, 4, 5, 6, and 7: 611 | 612 | ```test 613 | test_matches_within_delta(){ 614 | assert_within_delta 5 3 2 615 | assert_within_delta 5 4 2 616 | assert_within_delta 5 5 2 617 | assert_within_delta 5 6 2 618 | assert_within_delta 5 7 2 619 | } 620 | test_does_not_match_within_delta(){ 621 | assert_within_delta 5 2 2 622 | } 623 | 624 | ``` 625 | 626 | ```output 627 | Running test_does_not_match_within_delta ... FAILURE 628 | expected value [5] to match [2] with a maximum delta of [2] 629 | doc:9:test_does_not_match_within_delta() 630 | Running test_matches_within_delta ... SUCCESS 631 | ``` 632 | 633 | === *assert_no_diff* 634 | 635 | assert_no_diff [message] 636 | 637 | Asserts that the content of the file _actual_ does not have any differences to the one _expected_. 638 | 639 | ```test 640 | test_obvious_notmatching_with_assert_no_diff(){ 641 | assert_no_diff <(echo foo) <(echo bar) 642 | } 643 | test_obvious_matching_with_assert_assert_no_diff(){ 644 | assert_no_diff bash_unit bash_unit 645 | } 646 | 647 | ``` 648 | 649 | ```output 650 | Running test_obvious_matching_with_assert_assert_no_diff ... SUCCESS 651 | Running test_obvious_notmatching_with_assert_no_diff ... FAILURE 652 | expected 'doc' to be identical to 'doc' but was different 653 | out> 1c1 654 | out> < foo 655 | out> --- 656 | out> > bar 657 | doc:2:test_obvious_notmatching_with_assert_no_diff() 658 | ``` 659 | 660 | == *skip_if* function 661 | 662 | skip_if 663 | 664 | If _condition_ is true, will skip all the tests in the current file which match the given _pattern_. 665 | 666 | This can be useful when one has tests that are dependent on system environment, for instance: 667 | 668 | ```test 669 | skip_if "uname | grep Darwin" linux 670 | skip_if "uname | grep Linux" darwin 671 | 672 | test_linux_proc_exists() { 673 | assert "ls /proc/" "there should exist /proc on Linux" 674 | } 675 | test_darwin_proc_does_not_exist() { 676 | assert_fail "ls /proc/" "there should not exist /proc on Darwin" 677 | } 678 | ``` 679 | 680 | will output, on a Linux system: 681 | 682 | ```output 683 | Running test_darwin_proc_does_not_exist ... SKIPPED 684 | Running test_linux_proc_exists ... SUCCESS 685 | ``` 686 | 687 | == *fake* function 688 | 689 | fake [replacement code] 690 | 691 | Fakes _command_ and replaces it with _replacement code_ (if code is specified) for the rest of the execution of your test. If no replacement code is specified, then it replaces command by one that echoes stdin of fake. This may be useful if you need to simulate an environment for you code under test. 692 | 693 | For instance: 694 | 695 | ```test 696 | fake ps echo hello world 697 | ps 698 | ``` 699 | 700 | will output: 701 | 702 | ```output 703 | hello world 704 | ``` 705 | 706 | We can do the same using _stdin_ of fake: 707 | 708 | ```test 709 | fake ps << EOF 710 | hello world 711 | EOF 712 | ps 713 | ``` 714 | 715 | ```output 716 | hello world 717 | ``` 718 | 719 | ifndef::backend-manpage[] 720 | It has been asked whether using *fake* results in creating actual fakes or stubs or mocks? or may be spies? or may be they are dummies? 721 | The first answer to this question is: it depends. The second is: read this 722 | https://www.google.fr/search?tbm=isch&q=fake%20mock%20stub[great and detailed literature] on this subject. 723 | endif::[] 724 | 725 | === Using stdin 726 | 727 | Here is an example, parameterizing fake with its _stdin_ to test that code fails when some process does not run and succeeds otherwise: 728 | 729 | ```test 730 | code() { 731 | ps a | grep apache 732 | } 733 | 734 | test_code_succeeds_if_apache_runs() { 735 | fake ps </dev/null 862 | } 863 | ``` 864 | 865 | This test calls _code_, which calls _ps_, which is actually implemented by __ps_. Since _code_ does not use _ax_ but only _a_ as parameters, this test should fail. But ... 866 | 867 | ```output 868 | Running test_code_gives_ps_appropriate_parameters ... SUCCESS 869 | ``` 870 | 871 | The problem here is that _ps_ fail (because of the failed *assert_equals* assertion). But _ps_ is piped with _grep_: 872 | 873 | ```shell 874 | code() { 875 | ps a | grep apache 876 | } 877 | ``` 878 | 879 | With bash, the result code of a pipeline equals the result code of the last command of the pipeline. The last command is _grep_ and since grep succeeds, the failure of __ps_ is lost and our test succeeds. We have only succeeded in messing with the test output, nothing more. 880 | 881 | An alternative may be to activate bash _pipefail_ option but this may introduce unwanted side effects. We can also simply not output anything in __ps_ so that _grep_ fails: 882 | 883 | ```shell 884 | code() { 885 | ps a | grep apache 886 | } 887 | 888 | test_code_gives_ps_appropriate_parameters() { 889 | _ps() { 890 | assert_equals ax "${FAKE_PARAMS[@]}" 891 | } 892 | export -f _ps 893 | fake ps _ps 894 | 895 | code >/dev/null 896 | } 897 | ``` 898 | 899 | The problem here is that we use a trick to make the code under test fail but the 900 | failure has nothing to do with the actual *assert_equals* failure. This is really 901 | bad, don't do that. 902 | 903 | Moreover, *assert_equals* output is captured by _ps_ and this just messes with the display of our test results: 904 | 905 | ```shell 906 | Running test_code_gives_ps_appropriate_parameters ... 907 | ``` 908 | 909 | The only correct alternative is for the fake _ps_ to write _FAKE_PARAMS_ in a file descriptor 910 | so that your test can grab them after code execution and assert their value. For instance 911 | by writing to a file: 912 | 913 | ```test 914 | code() { 915 | ps a | grep apache 916 | } 917 | 918 | test_code_gives_ps_appropriate_parameters() { 919 | _ps() { 920 | echo ${FAKE_PARAMS[@]} > /tmp/fake_params 921 | } 922 | export -f _ps 923 | fake ps _ps 924 | 925 | code || true 926 | 927 | assert_equals ax "$(head -n1 /tmp/fake_params)" 928 | } 929 | 930 | setup() { 931 | rm -f /tmp/fake_params 932 | } 933 | ``` 934 | 935 | Here our fake writes to _/tmp/fake_. We delete this file in *setup* to be 936 | sure that we do not get inappropriate data from a previous test. We assert 937 | that the first line of _/tmp/fake_ equals _ax_. Also, note that we know 938 | that _code_ will fail and write this to ignore the error: `code || true`. 939 | 940 | 941 | ```output 942 | Running test_code_gives_ps_appropriate_parameters ... FAILURE 943 | expected [ax] but was [a] 944 | doc:14:test_code_gives_ps_appropriate_parameters() 945 | ``` 946 | 947 | We can also compact the fake definition: 948 | 949 | ```test 950 | code() { 951 | ps a | grep apache 952 | } 953 | 954 | test_code_gives_ps_appropriate_parameters() { 955 | fake ps 'echo ${FAKE_PARAMS[@]} >/tmp/fake_params' 956 | 957 | code || true 958 | 959 | assert_equals ax "$(head -n1 /tmp/fake_params)" 960 | } 961 | 962 | setup() { 963 | rm -f /tmp/fake_params 964 | } 965 | ``` 966 | 967 | ```output 968 | Running test_code_gives_ps_appropriate_parameters ... FAILURE 969 | expected [ax] but was [a] 970 | doc:10:test_code_gives_ps_appropriate_parameters() 971 | ``` 972 | 973 | Finally, we can avoid the _/tmp/fake_params_ temporary file by using _coproc_: 974 | 975 | ```test 976 | code() { 977 | ps a | grep apache 978 | } 979 | 980 | test_get_data_from_fake() { 981 | #Fasten you seat belt ... 982 | coproc cat 983 | exec {test_channel}>&${COPROC[1]} 984 | fake ps 'echo ${FAKE_PARAMS[@]} >&$test_channel' 985 | 986 | code || true 987 | 988 | assert_equals ax "$(head -n1 <&${COPROC[0]})" 989 | } 990 | 991 | ``` 992 | 993 | ```output 994 | Running test_get_data_from_fake ... FAILURE 995 | expected [ax] but was [a] 996 | doc:13:test_get_data_from_fake() 997 | ``` 998 | -------------------------------------------------------------------------------- /bash_unit: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | # 3 | # bash unit testing enterprise edition framework for professionals 4 | # Copyright (C) 2011-2016 Pascal Grange 5 | # This program is free software; you can redistribute it and/or modify 6 | # it under the terms of the GNU General Public License as published by 7 | # the Free Software Foundation; either version 3 of the License, or 8 | # (at your option) any later version. 9 | # This program is distributed in the hope that it will be useful, 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | # GNU General Public License for more details. 13 | # You should have received a copy of the GNU General Public License 14 | # along with this program; if not, write to the Free Software Foundation, 15 | # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 16 | # 17 | # https://github.com/pgrange/bash_unit 18 | 19 | # shellcheck disable=2317 # Ignore unreachable - most function are not called. 20 | # shellcheck disable=2155 # Ignore Declare and assign separately 21 | 22 | VERSION=v2.3.2 23 | 24 | ESCAPE=$(printf "\033") 25 | NOCOLOR="${ESCAPE}[0m" 26 | RED="${ESCAPE}[91m" 27 | GREEN="${ESCAPE}[92m" 28 | YELLOW="${ESCAPE}[93m" 29 | BLUE="${ESCAPE}[94m" 30 | 31 | # Make bash_unit immune to some basic unix commands faking 32 | CAT="$(type -P cat)" 33 | SED="$(type -P sed)" 34 | GREP="$(type -P grep)" 35 | RM="$(type -P rm)" 36 | SHUF="$(type -P sort) -R" 37 | 38 | # Store the number of tests run in a file so that the value is available 39 | # from the parent process. This will become an issue if we start considering 40 | # parallel test execution in the future. 41 | TEST_COUNT_FILE="$(mktemp)" 42 | # shellcheck disable=2064 # Use single quotes, expands now, not when signaled. 43 | trap "$RM -f \"$TEST_COUNT_FILE\"" EXIT 44 | 45 | fail() { 46 | local message=${1:-} 47 | local stdout=${2:-} 48 | local stderr=${3:-} 49 | 50 | # shellcheck disable=2154 51 | notify_test_failed "$__bash_unit_current_test__" 52 | notify_message "$message" 53 | [[ -n "$stdout" ]] && [ -s "$stdout" ] && notify_stdout < "$stdout" 54 | [[ -n "$stderr" ]] && [ -s "$stderr" ] && notify_stderr < "$stderr" 55 | 56 | stacktrace | notify_stack 57 | exit 1 58 | } 59 | 60 | assert() { 61 | local assertion=$1 62 | local message=${2:-} 63 | 64 | _assert_expression \ 65 | "$assertion" \ 66 | "[ \$status == 0 ]" \ 67 | "\"$message\"" 68 | } 69 | 70 | assert_fails() { 71 | local assertion=$1 72 | local message=${2:-} 73 | 74 | _assert_expression \ 75 | "$assertion" \ 76 | "[ \$status != 0 ]" \ 77 | "\"$message\"" 78 | } 79 | 80 | assert_fail() { 81 | #deprecated, use assert_fails instead 82 | assert_fails "$@" 83 | } 84 | 85 | assert_status_code() { 86 | local expected_status=$1 87 | local assertion="$2" 88 | local message="${3:-}" 89 | 90 | _assert_expression \ 91 | "$assertion" \ 92 | "[ \$status == $expected_status ]" \ 93 | "\"$message\" expected status code $expected_status but was \$status" 94 | } 95 | 96 | _assert_expression() { 97 | local assertion=$1 98 | local condition=$2 99 | local message=$3 100 | ( 101 | local stdout=$(mktemp) 102 | local stderr=$(mktemp) 103 | # shellcheck disable=2064 # Use single quotes, expands now, not when signaled. 104 | trap "$RM -f \"$stdout\" \"$stderr\"" EXIT 105 | 106 | local status 107 | eval "($assertion)" >"$stdout" 2>"$stderr" && status=$? || status=$? 108 | if ! eval "$condition" 109 | then 110 | fail "$(eval echo "$message")" "$stdout" "$stderr" 111 | fi 112 | ) || exit $? 113 | } 114 | 115 | assert_equals() { 116 | local expected=$1 117 | local actual=$2 118 | local message=${3:-} 119 | [[ -z $message ]] || message="$message\n" 120 | 121 | if [ "$expected" != "$actual" ] 122 | then 123 | fail "$message expected [$expected] but was [$actual]" 124 | fi 125 | } 126 | 127 | assert_not_equals() { 128 | local unexpected=$1 129 | local actual=$2 130 | local message=${3:-} 131 | [[ -z $message ]] || message="$message\n" 132 | 133 | [ "$unexpected" != "$actual" ] || \ 134 | fail "$message expected different value than [$unexpected] but was the same" 135 | } 136 | 137 | assert_matches() { 138 | local expected=$1 139 | local actual=$2 140 | local message=${3:-} 141 | [[ -z $message ]] || message="$message\n" 142 | 143 | if [[ ! "${actual}" =~ ${expected} ]]; then 144 | fail "$message expected regex [$expected] to match [$actual]" 145 | fi 146 | } 147 | 148 | assert_not_matches() { 149 | local unexpected=$1 150 | local actual=$2 151 | local message=${3:-} 152 | [[ -z $message ]] || message="$message\n" 153 | 154 | if [[ "${actual}" =~ ${unexpected} ]]; then 155 | fail "$message expected regex [$unexpected] should not match but matched [$actual]" 156 | fi 157 | } 158 | 159 | assert_within_delta() { 160 | function abs() { 161 | local value=$1 162 | local sign=$(( value < 0 ? -1 : 1 )) 163 | echo $((value * sign)) 164 | } 165 | function is_number() { 166 | local value=$1 167 | test "$value" -eq "$value" 2>/dev/null 168 | } 169 | local expected=$1 170 | local actual=$2 171 | local max_delta=$3 172 | assert "is_number $expected" "$message expected value [$expected] is not a number" 173 | assert "is_number $actual" "$message actual value [$actual] is not a number" 174 | assert "is_number $max_delta" "$message max_delta [$max_delta] is not a number" 175 | local message=${4:-} 176 | [[ -z $message ]] || message="$message\n" 177 | 178 | local actual_delta="$(abs $((expected - actual)))" 179 | 180 | if (( actual_delta > max_delta )); then 181 | fail "$message expected value [$expected] to match [$actual] with a maximum delta of [$max_delta]" 182 | fi 183 | } 184 | 185 | assert_no_diff() { 186 | local expected=$1 187 | local actual=$2 188 | local message=${3:-} 189 | [[ -z "$message" ]] || message="$message\n" 190 | 191 | assert "diff '${expected}' '${actual}'" \ 192 | "$message expected '${actual}' to be identical to '${expected}' but was different" 193 | } 194 | 195 | fake() { 196 | local command=$1 197 | shift 198 | if [ $# -gt 0 ] 199 | then 200 | eval "function $command() { export FAKE_PARAMS=(\"\$@\") ; $* ; }" 201 | else 202 | eval "function $command() { echo \"$($CAT)\" ; }" 203 | fi 204 | export -f "${command?}" 205 | } 206 | 207 | stacktrace() { 208 | local i=1 209 | while [ -n "${BASH_SOURCE[$i]:-}" ] 210 | do 211 | echo "${BASH_SOURCE[$i]}:${BASH_LINENO[$((i-1))]}:${FUNCNAME[$i]}()" 212 | i=$((i + 1)) 213 | done | "$GREP" -v "^$BASH_SOURCE" 214 | } 215 | 216 | run_test_suite() { 217 | local failure=0 218 | 219 | if run_setup_suite 220 | then 221 | run_tests || failure=$? 222 | else 223 | failure=$? 224 | fi 225 | run_teardown_suite 226 | 227 | return $failure 228 | } 229 | 230 | run_setup_suite() { 231 | if declare -F | "$GREP" ' setup_suite$' >/dev/null 232 | then 233 | setup_suite 234 | fi 235 | } 236 | 237 | maybe_shuffle() { 238 | # shellcheck disable=2015 # A && B || C. C may run when A is true. 239 | ((randomize)) && $SHUF || $CAT 240 | } 241 | 242 | run_tests() { 243 | local failure=0 244 | 245 | local pending_tests=$(set | "$GREP" -E '^(pending|todo).* \(\)' | "$GREP" -E "$test_pattern" | "$SED" -e 's: .*::') 246 | if [[ -n "$skip_pattern" ]] 247 | then 248 | local skipped_tests=$(set | "$GREP" -E '^test.* \(\)' | "$GREP" -E "$test_pattern" | "$GREP" -E "$skip_pattern" | "$SED" -e 's: .*::') 249 | local tests_to_run="$(set | "$GREP" -E '^test.* \(\)' | "$GREP" -E "$test_pattern" | "$GREP" -v -E "$skip_pattern" | "$SED" -e 's: .*::' | maybe_shuffle)" 250 | else 251 | local skipped_tests="" 252 | local tests_to_run="$(set | "$GREP" -E '^test.* \(\)' | "$GREP" -E "$test_pattern" | "$SED" -e 's: .*::' | maybe_shuffle)" 253 | fi 254 | 255 | local test_count=$(cat "${TEST_COUNT_FILE}") 256 | test_count=$((test_count + $(count "$pending_tests") + $(count "$tests_to_run") + $(count "$skipped_tests"))) 257 | echo "${test_count}" > "${TEST_COUNT_FILE}" 258 | 259 | for pending_test in $pending_tests 260 | do 261 | notify_test_starting "$pending_test" 262 | notify_test_pending "$pending_test" 263 | done 264 | 265 | for skipped_test in $skipped_tests 266 | do 267 | notify_test_starting "$skipped_test" 268 | notify_test_skipped "$skipped_test" 269 | done 270 | 271 | for test in $tests_to_run 272 | do 273 | ( 274 | local status=0 275 | declare -F | "$GREP" ' setup$' >/dev/null && setup 276 | (__bash_unit_current_test__="$test" run_test) || status=$? 277 | declare -F | "$GREP" ' teardown$' >/dev/null && teardown 278 | exit $status 279 | ) 280 | failure=$(( $? || failure)) 281 | done 282 | 283 | return $failure 284 | } 285 | 286 | run_test() { 287 | set -e 288 | notify_test_starting "$__bash_unit_current_test__" 289 | "$__bash_unit_current_test__" && notify_test_succeeded "$__bash_unit_current_test__" 290 | } 291 | 292 | run_teardown_suite() { 293 | if declare -F | "$GREP" ' teardown_suite$' >/dev/null 294 | then 295 | teardown_suite 296 | fi 297 | } 298 | 299 | usage() { 300 | echo "$1" >&2 301 | echo "$0 [-f ] [-p ] [-p ] [-s ] [-s ] [-r] ... ..." >&2 302 | echo >&2 303 | echo "Runs tests in test files that match s" >&2 304 | echo "Test that match the skip patterns are skipped and displayed as SKIPPED" >&2 305 | echo " is optional only supported value is tap" >&2 306 | echo "-r to execute test cases in random order" >&2 307 | echo "-v to get current version information" >&2 308 | echo "See https://github.com/pgrange/bash_unit" >&2 309 | exit 1 310 | } 311 | 312 | # Formatting 313 | 314 | pretty_success() { 315 | pretty_format "$GREEN" "\u2713" "${1:-}" 316 | } 317 | 318 | pretty_warning() { 319 | pretty_format "$YELLOW" "\u2717" "${1:-}" 320 | } 321 | 322 | pretty_failure() { 323 | pretty_format "$RED" "\u2717" "${1:-}" 324 | } 325 | 326 | pretty_format() { 327 | local color="$1" 328 | local pretty_symbol="$2" 329 | local alt_symbol="${3:-}" 330 | local term_utf8=false 331 | #env 332 | if is_terminal && [[ "${LANG:-}" =~ .*UTF-8.* ]] 333 | then 334 | term_utf8=true 335 | fi 336 | ( 337 | $CAT 338 | if $term_utf8 339 | then 340 | echo -en " $pretty_symbol " 341 | else 342 | [[ -n "$alt_symbol" ]] && echo -en " $alt_symbol " 343 | fi 344 | ) | color "$color" 345 | } 346 | 347 | color() { 348 | _start_color() { 349 | if is_terminal ; then echo -en "$color" ; fi 350 | } 351 | _stop_color() { 352 | if is_terminal ; then echo -en "$NOCOLOR" ; fi 353 | } 354 | local color=$1 355 | shift 356 | _start_color 357 | if [ $# -gt 0 ] 358 | then 359 | echo "$*" 360 | else 361 | $CAT 362 | fi 363 | _stop_color 364 | } 365 | 366 | is_terminal() { 367 | [ -t 1 ] || [[ "${FORCE_COLOR:-}" == true ]] 368 | } 369 | 370 | text_format() { 371 | notify_suite_starting() { 372 | local test_file="$1" 373 | echo "Running tests in $test_file" 374 | } 375 | notify_test_starting() { 376 | local test="$1" 377 | echo -e -n "\tRunning $test ... " | color "$BLUE" 378 | } 379 | notify_test_pending() { 380 | echo -n "PENDING" | pretty_warning 381 | echo 382 | } 383 | notify_test_skipped() { 384 | echo -n "SKIPPED" | pretty_warning 385 | echo 386 | } 387 | notify_test_succeeded() { 388 | echo -n "SUCCESS" | pretty_success 389 | echo 390 | } 391 | notify_test_failed() { 392 | echo -n "FAILURE" | pretty_failure 393 | echo 394 | } 395 | notify_message() { 396 | local message="$1" 397 | # shellcheck disable=SC2059 398 | [[ -z "$message" ]] || printf -- "$message\n" 399 | } 400 | 401 | notify_stdout() { 402 | "$SED" 's:^:out> :' | color "$GREEN" 403 | } 404 | notify_stderr() { 405 | "$SED" 's:^:err> :' | color "$RED" 406 | } 407 | notify_stack() { 408 | color "$YELLOW" 409 | } 410 | notify_suites_succeded() { 411 | echo -n "Overall result: SUCCESS" | pretty_success 412 | echo 413 | } 414 | notify_suites_failed() { 415 | echo -n "Overall result: FAILURE" | pretty_failure 416 | echo 417 | } 418 | } 419 | 420 | tap_format() { 421 | notify_suite_starting() { 422 | local test_file="$1" 423 | echo "# Running tests in $test_file" 424 | } 425 | notify_test_starting() { 426 | : 427 | } 428 | notify_test_pending() { 429 | local test="$1" 430 | echo -n "ok" | pretty_warning - 431 | echo -n "$test" | color "$BLUE" 432 | echo " # todo test to be written" | color "$YELLOW" 433 | } 434 | notify_test_skipped() { 435 | local test="$1" 436 | echo -n "ok" | pretty_warning - 437 | echo -n "$test" | color "$BLUE" 438 | echo " # skip test not run" | color "$YELLOW" 439 | } 440 | notify_test_succeeded() { 441 | local test="$1" 442 | echo -n "ok" | pretty_success - 443 | echo "$test" | color "$BLUE" 444 | } 445 | notify_test_failed() { 446 | local test="$1" 447 | echo -n "not ok" | pretty_failure - 448 | echo "$test" | color "$BLUE" 449 | } 450 | notify_message() { 451 | local message="$1" 452 | [[ -z "$message" ]] || printf -- "%b\n" "$message" | "$SED" -u -e 's/^/# /' 453 | } 454 | notify_stdout() { 455 | "$SED" 's:^:# out> :' | color "$GREEN" 456 | } 457 | notify_stderr() { 458 | "$SED" 's:^:# err> :' | color "$RED" 459 | } 460 | notify_stack() { 461 | "$SED" 's:^:# :' | color "$YELLOW" 462 | } 463 | notify_suites_succeded() { 464 | local message="$1" 465 | echo "1..$message" 466 | } 467 | notify_suites_failed() { 468 | local message="$1" 469 | echo "1..$message" 470 | } 471 | } 472 | 473 | quiet_mode() { 474 | notify_message() { 475 | : 476 | } 477 | notify_stdout() { 478 | : 479 | } 480 | notify_stderr() { 481 | : 482 | } 483 | notify_stack() { 484 | : 485 | } 486 | } 487 | 488 | skip_if() { 489 | local condition="$1" 490 | local pattern="$2" 491 | if eval "$condition" >/dev/null 2>&1 492 | then 493 | skip_pattern="${skip_pattern}${skip_pattern_separator}${pattern}" 494 | skip_pattern_separator="|" 495 | fi 496 | } 497 | 498 | count() { 499 | local tests="$1" 500 | if [[ -z "$tests" ]]; then 501 | echo 0 502 | else 503 | echo "$tests" | wc -l 504 | fi 505 | } 506 | 507 | output_format=text 508 | verbosity=normal 509 | test_pattern="" 510 | test_pattern_separator="" 511 | skip_pattern="" 512 | skip_pattern_separator="" 513 | randomize=0 514 | while getopts "vp:s:f:rq" option 515 | do 516 | case "$option" in 517 | p) 518 | test_pattern="${test_pattern}${test_pattern_separator}${OPTARG}" 519 | test_pattern_separator="|" 520 | ;; 521 | s) 522 | skip_pattern="${skip_pattern}${skip_pattern_separator}${OPTARG}" 523 | skip_pattern_separator="|" 524 | ;; 525 | f) 526 | output_format="${OPTARG}" 527 | ;; 528 | r) 529 | randomize=1 530 | ;; 531 | v) 532 | echo "bash_unit $VERSION" 533 | exit 534 | ;; 535 | q) 536 | verbosity=quiet 537 | ;; 538 | ?) 539 | usage 540 | ;; 541 | esac 542 | done 543 | shift $((OPTIND-1)) 544 | 545 | for test_file in "$@" 546 | do 547 | test -e "$test_file" || usage "file does not exist: $test_file" 548 | test -r "$test_file" || usage "can not read file: $test_file" 549 | done 550 | 551 | case "$output_format" in 552 | text) 553 | text_format 554 | ;; 555 | tap) 556 | tap_format 557 | ;; 558 | *) 559 | usage "unsupported output format: $output_format" 560 | ;; 561 | esac 562 | 563 | if [[ "$verbosity" == quiet ]] 564 | then 565 | quiet_mode 566 | fi 567 | 568 | #run tests received as parameters 569 | failure=0 570 | echo 0 > "${TEST_COUNT_FILE}" 571 | for test_file in "$@" 572 | do 573 | notify_suite_starting "$test_file" 574 | ( 575 | # Ensure bash_unit will exit with failure 576 | # in case of syntax error. 577 | set -e 578 | 579 | if [[ "${STICK_TO_CWD:-}" != true ]] 580 | then 581 | cd "$(dirname "$test_file")" 582 | # shellcheck disable=1090 583 | source "$(basename "$test_file")" 584 | else 585 | # shellcheck disable=1090 586 | source "$test_file" 587 | fi 588 | set +e 589 | run_test_suite 590 | ) 591 | failure=$(( $? || failure)) 592 | done 593 | 594 | if ((failure)) 595 | then 596 | notify_suites_failed "$(cat "${TEST_COUNT_FILE}")" 597 | else 598 | notify_suites_succeded "$(cat "${TEST_COUNT_FILE}")" 599 | fi 600 | 601 | exit $failure 602 | -------------------------------------------------------------------------------- /docs/man/man1/bash_unit.1: -------------------------------------------------------------------------------- 1 | '\" t 2 | .\" Title: bash_unit 3 | .\" Author: [see the "AUTHOR(S)" section] 4 | .\" Generator: Asciidoctor 2.0.23 5 | .\" Date: 2025-01-08 6 | .\" Manual: \ \& 7 | .\" Source: \ \& 8 | .\" Language: English 9 | .\" 10 | .TH "BASH_UNIT" "1" "2025-01-08" "\ \&" "\ \&" 11 | .ie \n(.g .ds Aq \(aq 12 | .el .ds Aq ' 13 | .ss \n[.ss] 0 14 | .nh 15 | .ad l 16 | .de URL 17 | \fI\\$2\fP <\\$1>\\$3 18 | .. 19 | .als MTO URL 20 | .if \n[.g] \{\ 21 | . mso www.tmac 22 | . am URL 23 | . ad l 24 | . . 25 | . am MTO 26 | . ad l 27 | . . 28 | . LINKSTYLE blue R < > 29 | .\} 30 | .SH "NAME" 31 | bash_unit \- bash unit testing enterprise edition framework for professionals! 32 | .SH "SYNOPSIS" 33 | .sp 34 | \fBbash_unit\fP [\-f tap] [\-p ] [\-s ] [\-r] [test_file] 35 | .SH "DESCRIPTION" 36 | .sp 37 | \fBbash_unit\fP allows you to write unit tests (functions starting with \fBtest\fP), 38 | run them and, in case of failure, displays the stack trace 39 | with source file and line number indications to locate the problem. 40 | .sp 41 | You might want to take a look at \c 42 | .URL "getting_started" "how to get started" 43 | before continuing reading this documentation. 44 | .sp 45 | The following functions are available in your tests (see below for detailed documentation): 46 | .sp 47 | .RS 4 48 | .ie n \{\ 49 | \h'-04'\(bu\h'+03'\c 50 | .\} 51 | .el \{\ 52 | . sp -1 53 | . IP \(bu 2.3 54 | .\} 55 | \f(CRfail [message]\fP 56 | .RE 57 | .sp 58 | .RS 4 59 | .ie n \{\ 60 | \h'-04'\(bu\h'+03'\c 61 | .\} 62 | .el \{\ 63 | . sp -1 64 | . IP \(bu 2.3 65 | .\} 66 | \f(CRassert [message]\fP 67 | .RE 68 | .sp 69 | .RS 4 70 | .ie n \{\ 71 | \h'-04'\(bu\h'+03'\c 72 | .\} 73 | .el \{\ 74 | . sp -1 75 | . IP \(bu 2.3 76 | .\} 77 | \f(CRassert_fail [message]\fP 78 | .RE 79 | .sp 80 | .RS 4 81 | .ie n \{\ 82 | \h'-04'\(bu\h'+03'\c 83 | .\} 84 | .el \{\ 85 | . sp -1 86 | . IP \(bu 2.3 87 | .\} 88 | \f(CRassert_status_code [message]\fP 89 | .RE 90 | .sp 91 | .RS 4 92 | .ie n \{\ 93 | \h'-04'\(bu\h'+03'\c 94 | .\} 95 | .el \{\ 96 | . sp -1 97 | . IP \(bu 2.3 98 | .\} 99 | \f(CRassert_equals [message]\fP 100 | .RE 101 | .sp 102 | .RS 4 103 | .ie n \{\ 104 | \h'-04'\(bu\h'+03'\c 105 | .\} 106 | .el \{\ 107 | . sp -1 108 | . IP \(bu 2.3 109 | .\} 110 | \f(CRassert_not_equals [message]\fP 111 | .RE 112 | .sp 113 | .RS 4 114 | .ie n \{\ 115 | \h'-04'\(bu\h'+03'\c 116 | .\} 117 | .el \{\ 118 | . sp -1 119 | . IP \(bu 2.3 120 | .\} 121 | \f(CRassert_matches [message]\fP 122 | .RE 123 | .sp 124 | .RS 4 125 | .ie n \{\ 126 | \h'-04'\(bu\h'+03'\c 127 | .\} 128 | .el \{\ 129 | . sp -1 130 | . IP \(bu 2.3 131 | .\} 132 | \f(CRassert_not_matches [message]\fP 133 | .RE 134 | .sp 135 | .RS 4 136 | .ie n \{\ 137 | \h'-04'\(bu\h'+03'\c 138 | .\} 139 | .el \{\ 140 | . sp -1 141 | . IP \(bu 2.3 142 | .\} 143 | \f(CRassert_within_delta [message]\fP 144 | .RE 145 | .sp 146 | .RS 4 147 | .ie n \{\ 148 | \h'-04'\(bu\h'+03'\c 149 | .\} 150 | .el \{\ 151 | . sp -1 152 | . IP \(bu 2.3 153 | .\} 154 | \f(CRassert_no_diff [message]\fP 155 | .RE 156 | .sp 157 | .RS 4 158 | .ie n \{\ 159 | \h'-04'\(bu\h'+03'\c 160 | .\} 161 | .el \{\ 162 | . sp -1 163 | . IP \(bu 2.3 164 | .\} 165 | \f(CRskip_if \fP 166 | .RE 167 | .sp 168 | .RS 4 169 | .ie n \{\ 170 | \h'-04'\(bu\h'+03'\c 171 | .\} 172 | .el \{\ 173 | . sp -1 174 | . IP \(bu 2.3 175 | .\} 176 | \f(CRfake [replacement code]\fP 177 | .RE 178 | .sp 179 | \fI(by the way, the documentation you are reading is itself tested with bash\-unit)\fP 180 | .sp 181 | \fBbash_unit\fP is free software you may contribute to. See \c 182 | .URL "CONTRIBUTING.md" "" "." 183 | .SH "OPTIONS" 184 | .sp 185 | \fB\-p\fP \fIpattern\fP 186 | .RS 4 187 | filters tests to run based on the given pattern. 188 | You can specify several patterns by repeating this option 189 | for each pattern. 190 | .RE 191 | .sp 192 | \fB\-s\fP \fIpattern\fP 193 | .RS 4 194 | skip tests which name matches the given pattern. 195 | You can specify several patterns by repeating this option 196 | for each pattern. 197 | Tests will appear in \fBbash_unit\fP output as \fIskipped\fP. 198 | (see also \fIskip_if\fP) 199 | .RE 200 | .sp 201 | \fB\-r\fP 202 | .RS 4 203 | executes test cases in random order. 204 | Only affects the order within a test file (files are always 205 | executed in the order in which they are specified on the 206 | command line). 207 | .RE 208 | .sp 209 | \fB\-f\fP \fIoutput_format\fP 210 | .RS 4 211 | specify an alternative output format. 212 | The only supported value is \fBtap\fP. 213 | .RE 214 | .sp 215 | \fB\-q\fP 216 | .RS 4 217 | quiet mode. 218 | Will only output the status of each test with no further 219 | information even in case of failure. 220 | .RE 221 | .SS "GitHub Actions" 222 | .sp 223 | Here is an example of how you could integrate \fBbash_unit\fP with \c 224 | .URL "https://docs.github.com/fr/actions" "GitHub Actions" ":" 225 | .sp 226 | .if n .RS 4 227 | .nf 228 | .fam C 229 | name: bash_unit tests 230 | on: 231 | push: 232 | branches: [ main ] 233 | pull_request: 234 | branches: [ main ] 235 | 236 | jobs: 237 | ubuntu: 238 | runs\-on: ubuntu\-latest 239 | steps: 240 | \- uses: actions/checkout@v4 241 | \- name: Unit testing with bash_unit 242 | run: | 243 | curl \-s https://raw.githubusercontent.com/pgrange/bash_unit/master/install.sh | bash 244 | FORCE_COLOR=true ./bash_unit tests/test_* 245 | .fam 246 | .fi 247 | .if n .RE 248 | .sp 249 | See this bash_unit \c 250 | .URL "https://github.com/pgrange/bash_unit_getting_started" "getting started gitlab project" "" 251 | for a working example. 252 | .SS "GitLab CI" 253 | .sp 254 | Here is an example of how you could integrate \fBbash_unit\fP with \c 255 | .URL "https://docs.gitlab.com/ee/ci/" "GitLab CI" ":" 256 | .sp 257 | .if n .RS 4 258 | .nf 259 | .fam C 260 | test: 261 | image: debian 262 | script: 263 | \- apt\-get update 264 | \- apt\-get install \-\-no\-install\-recommends \-y curl ca\-certificates 265 | \- curl \-s https://raw.githubusercontent.com/pgrange/bash_unit/master/install.sh | bash 266 | \- FORCE_COLOR=true ./bash_unit tests/test_* 267 | .fam 268 | .fi 269 | .if n .RE 270 | .sp 271 | See this bash_unit \c 272 | .URL "https://gitlab.com/pgrange/bash_unit_getting_started" "getting started gitlab project" "" 273 | for a working example. 274 | .SS "pre\-commit hook" 275 | .sp 276 | You can run \f(CRbash_unit\fP as a \c 277 | .URL "https://pre\-commit.com" "pre\-commit" "" 278 | hook. 279 | .sp 280 | Add the following to your pre\-commit configuration. By default it will run scripts that are identified as shell scripts that match the path \f(CR^tests/(.*/)?test_.*\(rs.sh$\fP. 281 | .sp 282 | .if n .RS 4 283 | .nf 284 | .fam C 285 | repos: 286 | \- repo: https://github.com/pgrange/bash_unit 287 | rev: v2.2.0 288 | hooks: 289 | \- id: bash\-unit 290 | always\-run: true 291 | .fam 292 | .fi 293 | .if n .RE 294 | .SH "HOW TO RUN TESTS" 295 | .sp 296 | To run tests, simply call \fBbash_unit\fP with all your tests files as parameter. For instance to run some \fBbash_unit\fP tests, from \fBbash_unit\fP directory: 297 | .sp 298 | .if n .RS 4 299 | .nf 300 | .fam C 301 | \&./bash_unit tests/test_core.sh 302 | .fam 303 | .fi 304 | .if n .RE 305 | .sp 306 | .if n .RS 4 307 | .nf 308 | .fam C 309 | Running tests in tests/test_core.sh 310 | Running test_assert_equals_fails_when_not_equal ... SUCCESS 311 | Running test_assert_equals_succeed_when_equal ... SUCCESS 312 | Running test_assert_fails ... SUCCESS 313 | Running test_assert_fails_fails ... SUCCESS 314 | Running test_assert_fails_succeeds ... SUCCESS 315 | Running test_assert_matches_fails_when_not_matching ... SUCCESS 316 | Running test_assert_matches_succeed_when_matching ... SUCCESS 317 | Running test_assert_no_diff_fails_when_diff ... SUCCESS 318 | Running test_assert_no_diff_succeeds_when_no_diff ... SUCCESS 319 | Running test_assert_not_equals_fails_when_equal ... SUCCESS 320 | Running test_assert_not_equals_succeeds_when_not_equal ... SUCCESS 321 | Running test_assert_not_matches_fails_when_matching ... SUCCESS 322 | Running test_assert_not_matches_succeed_when_not_matching ... SUCCESS 323 | Running test_assert_shows_stderr_on_failure ... SUCCESS 324 | Running test_assert_shows_stdout_on_failure ... SUCCESS 325 | Running test_assert_status_code_fails ... SUCCESS 326 | Running test_assert_status_code_succeeds ... SUCCESS 327 | Running test_assert_succeeds ... SUCCESS 328 | Running test_assert_within_delta_fails ... SUCCESS 329 | Running test_assert_within_delta_succeeds ... SUCCESS 330 | Running test_fail_fails ... SUCCESS 331 | Running test_fail_prints_failure_message ... SUCCESS 332 | Running test_fail_prints_where_is_error ... SUCCESS 333 | Running test_fake_actually_fakes_the_command ... SUCCESS 334 | Running test_fake_can_fake_inline ... SUCCESS 335 | Running test_fake_echo_stdin_when_no_params ... SUCCESS 336 | Running test_fake_exports_faked_in_subshells ... SUCCESS 337 | Running test_fake_transmits_params_to_fake_code ... SUCCESS 338 | Running test_fake_transmits_params_to_fake_code_as_array ... SUCCESS 339 | Running test_should_pretty_format_even_when_LANG_is_unset ... SUCCESS 340 | Overall result: SUCCESS 341 | .fam 342 | .fi 343 | .if n .RE 344 | .sp 345 | You might also want to run only specific tests, you may do so with the 346 | \fI\-p\fP option. This option accepts a pattern as parameter and filters test 347 | functions against this pattern. 348 | .sp 349 | .if n .RS 4 350 | .nf 351 | .fam C 352 | \&./bash_unit \-p fail_fails \-p assert tests/test_core.sh 353 | .fam 354 | .fi 355 | .if n .RE 356 | .sp 357 | .if n .RS 4 358 | .nf 359 | .fam C 360 | Running tests in tests/test_core.sh 361 | Running test_assert_equals_fails_when_not_equal ... SUCCESS 362 | Running test_assert_equals_succeed_when_equal ... SUCCESS 363 | Running test_assert_fails ... SUCCESS 364 | Running test_assert_fails_fails ... SUCCESS 365 | Running test_assert_fails_succeeds ... SUCCESS 366 | Running test_assert_matches_fails_when_not_matching ... SUCCESS 367 | Running test_assert_matches_succeed_when_matching ... SUCCESS 368 | Running test_assert_no_diff_fails_when_diff ... SUCCESS 369 | Running test_assert_no_diff_succeeds_when_no_diff ... SUCCESS 370 | Running test_assert_not_equals_fails_when_equal ... SUCCESS 371 | Running test_assert_not_equals_succeeds_when_not_equal ... SUCCESS 372 | Running test_assert_not_matches_fails_when_matching ... SUCCESS 373 | Running test_assert_not_matches_succeed_when_not_matching ... SUCCESS 374 | Running test_assert_shows_stderr_on_failure ... SUCCESS 375 | Running test_assert_shows_stdout_on_failure ... SUCCESS 376 | Running test_assert_status_code_fails ... SUCCESS 377 | Running test_assert_status_code_succeeds ... SUCCESS 378 | Running test_assert_succeeds ... SUCCESS 379 | Running test_assert_within_delta_fails ... SUCCESS 380 | Running test_assert_within_delta_succeeds ... SUCCESS 381 | Running test_fail_fails ... SUCCESS 382 | Overall result: SUCCESS 383 | .fam 384 | .fi 385 | .if n .RE 386 | .sp 387 | You can combine the \fI\-p\fP option with \fI\-s\fP to skip some of the tests. This option accepts a pattern 388 | as parameter and mark as skipped any test function which matches this pattern. 389 | .sp 390 | .if n .RS 4 391 | .nf 392 | .fam C 393 | \&./bash_unit \-p fail_fails \-p assert \-s no \-s status tests/test_core.sh 394 | .fam 395 | .fi 396 | .if n .RE 397 | .sp 398 | .if n .RS 4 399 | .nf 400 | .fam C 401 | Running tests in tests/test_core.sh 402 | Running test_assert_equals_fails_when_not_equal ... SKIPPED 403 | Running test_assert_matches_fails_when_not_matching ... SKIPPED 404 | Running test_assert_no_diff_fails_when_diff ... SKIPPED 405 | Running test_assert_no_diff_succeeds_when_no_diff ... SKIPPED 406 | Running test_assert_not_equals_fails_when_equal ... SKIPPED 407 | Running test_assert_not_equals_succeeds_when_not_equal ... SKIPPED 408 | Running test_assert_not_matches_fails_when_matching ... SKIPPED 409 | Running test_assert_not_matches_succeed_when_not_matching ... SKIPPED 410 | Running test_assert_status_code_fails ... SKIPPED 411 | Running test_assert_status_code_succeeds ... SKIPPED 412 | Running test_assert_equals_succeed_when_equal ... SUCCESS 413 | Running test_assert_fails ... SUCCESS 414 | Running test_assert_fails_fails ... SUCCESS 415 | Running test_assert_fails_succeeds ... SUCCESS 416 | Running test_assert_matches_succeed_when_matching ... SUCCESS 417 | Running test_assert_shows_stderr_on_failure ... SUCCESS 418 | Running test_assert_shows_stdout_on_failure ... SUCCESS 419 | Running test_assert_succeeds ... SUCCESS 420 | Running test_assert_within_delta_fails ... SUCCESS 421 | Running test_assert_within_delta_succeeds ... SUCCESS 422 | Running test_fail_fails ... SUCCESS 423 | Overall result: SUCCESS 424 | .fam 425 | .fi 426 | .if n .RE 427 | .sp 428 | \fBbash_unit\fP supports the \c 429 | .URL "http://testanything.org/" "Test Anything Protocol" "" 430 | so you can ask for a tap formatted 431 | output with the \fI\-f\fP option. 432 | .sp 433 | .if n .RS 4 434 | .nf 435 | .fam C 436 | \&./bash_unit \-f tap tests/test_core.sh 437 | .fam 438 | .fi 439 | .if n .RE 440 | .sp 441 | .if n .RS 4 442 | .nf 443 | .fam C 444 | # Running tests in tests/test_core.sh 445 | ok \- test_assert_equals_fails_when_not_equal 446 | ok \- test_assert_equals_succeed_when_equal 447 | ok \- test_assert_fails 448 | ok \- test_assert_fails_fails 449 | ok \- test_assert_fails_succeeds 450 | ok \- test_assert_matches_fails_when_not_matching 451 | ok \- test_assert_matches_succeed_when_matching 452 | ok \- test_assert_no_diff_fails_when_diff 453 | ok \- test_assert_no_diff_succeeds_when_no_diff 454 | ok \- test_assert_not_equals_fails_when_equal 455 | ok \- test_assert_not_equals_succeeds_when_not_equal 456 | ok \- test_assert_not_matches_fails_when_matching 457 | ok \- test_assert_not_matches_succeed_when_not_matching 458 | ok \- test_assert_shows_stderr_on_failure 459 | ok \- test_assert_shows_stdout_on_failure 460 | ok \- test_assert_status_code_fails 461 | ok \- test_assert_status_code_succeeds 462 | ok \- test_assert_succeeds 463 | ok \- test_assert_within_delta_fails 464 | ok \- test_assert_within_delta_succeeds 465 | ok \- test_fail_fails 466 | ok \- test_fail_prints_failure_message 467 | ok \- test_fail_prints_where_is_error 468 | ok \- test_fake_actually_fakes_the_command 469 | ok \- test_fake_can_fake_inline 470 | ok \- test_fake_echo_stdin_when_no_params 471 | ok \- test_fake_exports_faked_in_subshells 472 | ok \- test_fake_transmits_params_to_fake_code 473 | ok \- test_fake_transmits_params_to_fake_code_as_array 474 | ok \- test_should_pretty_format_even_when_LANG_is_unset 475 | 1..30 476 | .fam 477 | .fi 478 | .if n .RE 479 | .SH "HOW TO WRITE TESTS" 480 | .sp 481 | Write your test functions in a file. The name of a test function has to start with \fBtest\fP. Only functions starting with \fBtest\fP will be tested. 482 | .sp 483 | Use the \fBbash_unit\fP assertion functions in your test functions, see below. 484 | .sp 485 | You may write a \fBsetup\fP function that will be executed before each test is run. 486 | .sp 487 | You may write a \fBteardown\fP function that will be executed after each test is run. 488 | .sp 489 | You may write a \fBsetup_suite\fP function that will be executed only once before all the tests of your test file. 490 | .sp 491 | You may write a \fBteardown_suite\fP function that will be executed only once after all the tests of your test file. 492 | .sp 493 | If you write code outside of any bash function, this code will be executed once at test file loading time since 494 | your file is a bash script and \fBbash_unit\fP sources it before running your tests. It is suggested to write a 495 | \fBsetup_suite\fP function and avoid any code outside a bash function. you must not use any bash_unit assertion 496 | in setup_suite or use exit in setup_suite for teardown_suite to be run. 497 | See \c 498 | .URL "https://github.com/pgrange/bash_unit/issues/43" "issue 43" "" 499 | for more details. 500 | .sp 501 | If you want to keep an eye on a test not yet implemented, prefix the name of the function by \fBtodo\fP instead of test. 502 | Test to do are not executed and do not impact the global status of your test suite but are displayed in \fBbash_unit\fP output. 503 | .sp 504 | \fBbash_unit\fP changes the current working directory to the one of the running test file. If you need to access files from your test code, for instance the script under test, use path relative to the test file. 505 | .sp 506 | You may need to change the behavior of some commands to create conditions for your code under test to behave as expected. The \fBfake\fP function may help you to do that, see below. 507 | .SH "TEST FUNCTIONS" 508 | .sp 509 | \fBbash_unit\fP supports several shell oriented assertion functions. 510 | .SS "\fBfail\fP" 511 | .sp 512 | .if n .RS 4 513 | .nf 514 | .fam C 515 | fail [message] 516 | .fam 517 | .fi 518 | .if n .RE 519 | .sp 520 | Fails the test and displays an optional message. 521 | .sp 522 | .if n .RS 4 523 | .nf 524 | .fam C 525 | test_can_fail() { 526 | fail "this test failed on purpose" 527 | } 528 | .fam 529 | .fi 530 | .if n .RE 531 | .sp 532 | .if n .RS 4 533 | .nf 534 | .fam C 535 | Running test_can_fail ... FAILURE 536 | this test failed on purpose 537 | doc:2:test_can_fail() 538 | .fam 539 | .fi 540 | .if n .RE 541 | .SS "\fBassert\fP" 542 | .sp 543 | .if n .RS 4 544 | .nf 545 | .fam C 546 | assert [message] 547 | .fam 548 | .fi 549 | .if n .RE 550 | .sp 551 | Evaluates \fIassertion\fP and fails if \fIassertion\fP fails. 552 | .sp 553 | \fIassertion\fP fails if its evaluation returns a status code different from 0. 554 | .sp 555 | In case of failure, the standard output and error of the evaluated \fIassertion\fP is displayed. The optional message is also displayed. 556 | .sp 557 | .if n .RS 4 558 | .nf 559 | .fam C 560 | test_assert_fails() { 561 | assert false "this test failed, obviously" 562 | } 563 | test_assert_succeed() { 564 | assert true 565 | } 566 | .fam 567 | .fi 568 | .if n .RE 569 | .sp 570 | .if n .RS 4 571 | .nf 572 | .fam C 573 | Running test_assert_fails ... FAILURE 574 | this test failed, obviously 575 | doc:2:test_assert_fails() 576 | Running test_assert_succeed ... SUCCESS 577 | .fam 578 | .fi 579 | .if n .RE 580 | .sp 581 | But you probably want to assert less obvious facts. 582 | .sp 583 | .if n .RS 4 584 | .nf 585 | .fam C 586 | code() { 587 | touch /tmp/the_file 588 | } 589 | 590 | test_code_creates_the_file() { 591 | code 592 | 593 | assert "test \-e /tmp/the_file" 594 | } 595 | 596 | test_code_makes_the_file_executable() { 597 | code 598 | 599 | assert "test \-x /tmp/the_file" "/tmp/the_file should be executable" 600 | } 601 | .fam 602 | .fi 603 | .if n .RE 604 | .sp 605 | .if n .RS 4 606 | .nf 607 | .fam C 608 | Running test_code_creates_the_file ... SUCCESS 609 | Running test_code_makes_the_file_executable ... FAILURE 610 | /tmp/the_file should be executable 611 | doc:14:test_code_makes_the_file_executable() 612 | .fam 613 | .fi 614 | .if n .RE 615 | .sp 616 | It may also be fun to use assert to check for the expected content of a file. 617 | .sp 618 | .if n .RS 4 619 | .nf 620 | .fam C 621 | code() { 622 | echo \*(Aqnot so cool\*(Aq > /tmp/the_file 623 | } 624 | 625 | test_code_write_appropriate_content_in_the_file() { 626 | code 627 | 628 | assert "diff <(echo \*(Aqthis is cool\*(Aq) /tmp/the_file" 629 | } 630 | .fam 631 | .fi 632 | .if n .RE 633 | .sp 634 | .if n .RS 4 635 | .nf 636 | .fam C 637 | Running test_code_write_appropriate_content_in_the_file ... FAILURE 638 | out> 1c1 639 | out> < this is cool 640 | out> \-\-\- 641 | out> > not so cool 642 | doc:8:test_code_write_appropriate_content_in_the_file() 643 | .fam 644 | .fi 645 | .if n .RE 646 | .SS "\fBassert_fail\fP" 647 | .sp 648 | .if n .RS 4 649 | .nf 650 | .fam C 651 | assert_fail [message] 652 | .fam 653 | .fi 654 | .if n .RE 655 | .sp 656 | Asserts that \fIassertion\fP fails. This is the opposite of \fBassert\fP. 657 | .sp 658 | \fIassertion\fP fails if its evaluation returns a status code different from 0. 659 | .sp 660 | If the evaluated expression does not fail, then \fBassert_fail\fP will fail and display the standard output and error of the evaluated \fIassertion\fP. The optional message is also displayed. 661 | .sp 662 | .if n .RS 4 663 | .nf 664 | .fam C 665 | code() { 666 | echo \*(Aqnot so cool\*(Aq > /tmp/the_file 667 | } 668 | 669 | test_code_does_not_write_cool_in_the_file() { 670 | code 671 | 672 | assert_fails "grep cool /tmp/the_file" "should not write \*(Aqcool\*(Aq in /tmp/the_file" 673 | } 674 | 675 | test_code_does_not_write_this_in_the_file() { 676 | code 677 | 678 | assert_fails "grep this /tmp/the_file" "should not write \*(Aqthis\*(Aq in /tmp/the_file" 679 | } 680 | .fam 681 | .fi 682 | .if n .RE 683 | .sp 684 | .if n .RS 4 685 | .nf 686 | .fam C 687 | Running test_code_does_not_write_cool_in_the_file ... FAILURE 688 | should not write \*(Aqcool\*(Aq in /tmp/the_file 689 | out> not so cool 690 | doc:8:test_code_does_not_write_cool_in_the_file() 691 | Running test_code_does_not_write_this_in_the_file ... SUCCESS 692 | .fam 693 | .fi 694 | .if n .RE 695 | .SS "\fBassert_status_code\fP" 696 | .sp 697 | .if n .RS 4 698 | .nf 699 | .fam C 700 | assert_status_code [message] 701 | .fam 702 | .fi 703 | .if n .RE 704 | .sp 705 | Checks for a precise status code of the evaluation of \fIassertion\fP. 706 | .sp 707 | It may be useful if you want to distinguish between several error conditions in your code. 708 | .sp 709 | In case of failure, the standard output and error of the evaluated \fIassertion\fP is displayed. The optional message is also displayed. 710 | .sp 711 | .if n .RS 4 712 | .nf 713 | .fam C 714 | code() { 715 | exit 23 716 | } 717 | 718 | test_code_should_fail_with_code_25() { 719 | assert_status_code 25 code 720 | } 721 | .fam 722 | .fi 723 | .if n .RE 724 | .sp 725 | .if n .RS 4 726 | .nf 727 | .fam C 728 | Running test_code_should_fail_with_code_25 ... FAILURE 729 | expected status code 25 but was 23 730 | doc:6:test_code_should_fail_with_code_25() 731 | .fam 732 | .fi 733 | .if n .RE 734 | .SS "\fBassert_equals\fP" 735 | .sp 736 | .if n .RS 4 737 | .nf 738 | .fam C 739 | assert_equals [message] 740 | .fam 741 | .fi 742 | .if n .RE 743 | .sp 744 | Asserts for equality of the two strings \fIexpected\fP and \fIactual\fP. 745 | .sp 746 | .if n .RS 4 747 | .nf 748 | .fam C 749 | test_obvious_inequality_with_assert_equals(){ 750 | assert_equals "a string" "another string" "a string should be another string" 751 | } 752 | test_obvious_equality_with_assert_equals(){ 753 | assert_equals a a 754 | } 755 | .fam 756 | .fi 757 | .if n .RE 758 | .sp 759 | .if n .RS 4 760 | .nf 761 | .fam C 762 | Running test_obvious_equality_with_assert_equals ... SUCCESS 763 | Running test_obvious_inequality_with_assert_equals ... FAILURE 764 | a string should be another string 765 | expected [a string] but was [another string] 766 | doc:2:test_obvious_inequality_with_assert_equals() 767 | .fam 768 | .fi 769 | .if n .RE 770 | .SS "\fBassert_not_equals\fP" 771 | .sp 772 | .if n .RS 4 773 | .nf 774 | .fam C 775 | assert_not_equals [message] 776 | .fam 777 | .fi 778 | .if n .RE 779 | .sp 780 | Asserts for inequality of the two strings \fIunexpected\fP and \fIactual\fP. 781 | .sp 782 | .if n .RS 4 783 | .nf 784 | .fam C 785 | test_obvious_equality_with_assert_not_equals(){ 786 | assert_not_equals "a string" "a string" "a string should be different from another string" 787 | } 788 | test_obvious_inequality_with_assert_not_equals(){ 789 | assert_not_equals a b 790 | } 791 | .fam 792 | .fi 793 | .if n .RE 794 | .sp 795 | .if n .RS 4 796 | .nf 797 | .fam C 798 | Running test_obvious_equality_with_assert_not_equals ... FAILURE 799 | a string should be different from another string 800 | expected different value than [a string] but was the same 801 | doc:2:test_obvious_equality_with_assert_not_equals() 802 | Running test_obvious_inequality_with_assert_not_equals ... SUCCESS 803 | .fam 804 | .fi 805 | .if n .RE 806 | .SS "\fBassert_matches\fP" 807 | .sp 808 | .if n .RS 4 809 | .nf 810 | .fam C 811 | assert_matches [message] 812 | .fam 813 | .fi 814 | .if n .RE 815 | .sp 816 | Asserts that the string \fIactual\fP matches the regex pattern \fIexpected\-regex\fP. 817 | .sp 818 | .if n .RS 4 819 | .nf 820 | .fam C 821 | test_obvious_notmatching_with_assert_matches(){ 822 | assert_matches "a str.*" "another string" "\*(Aqanother string\*(Aq should not match \*(Aqa str.*\*(Aq" 823 | } 824 | test_obvious_matching_with_assert_matches(){ 825 | assert_matches "a[nN].t{0,1}.*r str.*" "another string" 826 | } 827 | .fam 828 | .fi 829 | .if n .RE 830 | .sp 831 | .if n .RS 4 832 | .nf 833 | .fam C 834 | Running test_obvious_matching_with_assert_matches ... SUCCESS 835 | Running test_obvious_notmatching_with_assert_matches ... FAILURE 836 | \*(Aqanother string\*(Aq should not match \*(Aqa str.*\*(Aq 837 | expected regex [a str.*] to match [another string] 838 | doc:2:test_obvious_notmatching_with_assert_matches() 839 | .fam 840 | .fi 841 | .if n .RE 842 | .SS "\fBassert_not_matches\fP" 843 | .sp 844 | .if n .RS 4 845 | .nf 846 | .fam C 847 | assert_not_matches [message] 848 | .fam 849 | .fi 850 | .if n .RE 851 | .sp 852 | Asserts that the string \fIactual\fP does not match the regex pattern \fIunexpected\-regex\fP. 853 | .sp 854 | .if n .RS 4 855 | .nf 856 | .fam C 857 | test_obvious_matching_with_assert_not_matches(){ 858 | assert_not_matches "a str.*" "a string" "\*(Aqa string\*(Aq should not match \*(Aqa str.*\*(Aq" 859 | } 860 | test_obvious_notmatching_with_assert_not_matches(){ 861 | assert_not_matches "a str.*" "another string" 862 | } 863 | .fam 864 | .fi 865 | .if n .RE 866 | .sp 867 | .if n .RS 4 868 | .nf 869 | .fam C 870 | Running test_obvious_matching_with_assert_not_matches ... FAILURE 871 | \*(Aqa string\*(Aq should not match \*(Aqa str.*\*(Aq 872 | expected regex [a str.*] should not match but matched [a string] 873 | doc:2:test_obvious_matching_with_assert_not_matches() 874 | Running test_obvious_notmatching_with_assert_not_matches ... SUCCESS 875 | .fam 876 | .fi 877 | .if n .RE 878 | .SS "\fBassert_within_delta\fP" 879 | .sp 880 | .if n .RS 4 881 | .nf 882 | .fam C 883 | assert_within_delta [message] 884 | .fam 885 | .fi 886 | .if n .RE 887 | .sp 888 | Asserts that the expected num matches the actual num up to a given max delta. 889 | This function only support integers. 890 | Given an expectation of 5 and a delta of 2 this would match 3, 4, 5, 6, and 7: 891 | .sp 892 | .if n .RS 4 893 | .nf 894 | .fam C 895 | test_matches_within_delta(){ 896 | assert_within_delta 5 3 2 897 | assert_within_delta 5 4 2 898 | assert_within_delta 5 5 2 899 | assert_within_delta 5 6 2 900 | assert_within_delta 5 7 2 901 | } 902 | test_does_not_match_within_delta(){ 903 | assert_within_delta 5 2 2 904 | } 905 | .fam 906 | .fi 907 | .if n .RE 908 | .sp 909 | .if n .RS 4 910 | .nf 911 | .fam C 912 | Running test_does_not_match_within_delta ... FAILURE 913 | expected value [5] to match [2] with a maximum delta of [2] 914 | doc:9:test_does_not_match_within_delta() 915 | Running test_matches_within_delta ... SUCCESS 916 | .fam 917 | .fi 918 | .if n .RE 919 | .SS "\fBassert_no_diff\fP" 920 | .sp 921 | .if n .RS 4 922 | .nf 923 | .fam C 924 | assert_no_diff [message] 925 | .fam 926 | .fi 927 | .if n .RE 928 | .sp 929 | Asserts that the content of the file \fIactual\fP does not have any differences to the one \fIexpected\fP. 930 | .sp 931 | .if n .RS 4 932 | .nf 933 | .fam C 934 | test_obvious_notmatching_with_assert_no_diff(){ 935 | assert_no_diff <(echo foo) <(echo bar) 936 | } 937 | test_obvious_matching_with_assert_assert_no_diff(){ 938 | assert_no_diff bash_unit bash_unit 939 | } 940 | .fam 941 | .fi 942 | .if n .RE 943 | .sp 944 | .if n .RS 4 945 | .nf 946 | .fam C 947 | Running test_obvious_matching_with_assert_assert_no_diff ... SUCCESS 948 | Running test_obvious_notmatching_with_assert_no_diff ... FAILURE 949 | expected \*(Aqdoc\*(Aq to be identical to \*(Aqdoc\*(Aq but was different 950 | out> 1c1 951 | out> < foo 952 | out> \-\-\- 953 | out> > bar 954 | doc:2:test_obvious_notmatching_with_assert_no_diff() 955 | .fam 956 | .fi 957 | .if n .RE 958 | .SH "\fBSKIP_IF\fP FUNCTION" 959 | .sp 960 | .if n .RS 4 961 | .nf 962 | .fam C 963 | skip_if 964 | .fam 965 | .fi 966 | .if n .RE 967 | .sp 968 | If \fIcondition\fP is true, will skip all the tests in the current file which match the given \fIpattern\fP. 969 | .sp 970 | This can be useful when one has tests that are dependent on system environment, for instance: 971 | .sp 972 | .if n .RS 4 973 | .nf 974 | .fam C 975 | skip_if "uname | grep Darwin" linux 976 | skip_if "uname | grep Linux" darwin 977 | 978 | test_linux_proc_exists() { 979 | assert "ls /proc/" "there should exist /proc on Linux" 980 | } 981 | test_darwin_proc_does_not_exist() { 982 | assert_fail "ls /proc/" "there should not exist /proc on Darwin" 983 | } 984 | .fam 985 | .fi 986 | .if n .RE 987 | .sp 988 | will output, on a Linux system: 989 | .sp 990 | .if n .RS 4 991 | .nf 992 | .fam C 993 | Running test_darwin_proc_does_not_exist ... SKIPPED 994 | Running test_linux_proc_exists ... SUCCESS 995 | .fam 996 | .fi 997 | .if n .RE 998 | .SH "\fBFAKE\fP FUNCTION" 999 | .sp 1000 | .if n .RS 4 1001 | .nf 1002 | .fam C 1003 | fake [replacement code] 1004 | .fam 1005 | .fi 1006 | .if n .RE 1007 | .sp 1008 | Fakes \fIcommand\fP and replaces it with \fIreplacement code\fP (if code is specified) for the rest of the execution of your test. If no replacement code is specified, then it replaces command by one that echoes stdin of fake. This may be useful if you need to simulate an environment for you code under test. 1009 | .sp 1010 | For instance: 1011 | .sp 1012 | .if n .RS 4 1013 | .nf 1014 | .fam C 1015 | fake ps echo hello world 1016 | ps 1017 | .fam 1018 | .fi 1019 | .if n .RE 1020 | .sp 1021 | will output: 1022 | .sp 1023 | .if n .RS 4 1024 | .nf 1025 | .fam C 1026 | hello world 1027 | .fam 1028 | .fi 1029 | .if n .RE 1030 | .sp 1031 | We can do the same using \fIstdin\fP of fake: 1032 | .sp 1033 | .if n .RS 4 1034 | .nf 1035 | .fam C 1036 | fake ps << EOF 1037 | hello world 1038 | EOF 1039 | ps 1040 | .fam 1041 | .fi 1042 | .if n .RE 1043 | .sp 1044 | .if n .RS 4 1045 | .nf 1046 | .fam C 1047 | hello world 1048 | .fam 1049 | .fi 1050 | .if n .RE 1051 | .SS "Using stdin" 1052 | .sp 1053 | Here is an example, parameterizing fake with its \fIstdin\fP to test that code fails when some process does not run and succeeds otherwise: 1054 | .sp 1055 | .if n .RS 4 1056 | .nf 1057 | .fam C 1058 | code() { 1059 | ps a | grep apache 1060 | } 1061 | 1062 | test_code_succeeds_if_apache_runs() { 1063 | fake ps </dev/null 1227 | } 1228 | .fam 1229 | .fi 1230 | .if n .RE 1231 | .sp 1232 | This test calls \fIcode\fP, which calls \fIps\fP, which is actually implemented by \fI_ps\fP. Since \fIcode\fP does not use \fIax\fP but only \fIa\fP as parameters, this test should fail. But ... 1233 | .sp 1234 | .if n .RS 4 1235 | .nf 1236 | .fam C 1237 | Running test_code_gives_ps_appropriate_parameters ... SUCCESS 1238 | .fam 1239 | .fi 1240 | .if n .RE 1241 | .sp 1242 | The problem here is that \fIps\fP fail (because of the failed \fBassert_equals\fP assertion). But \fIps\fP is piped with \fIgrep\fP: 1243 | .sp 1244 | .if n .RS 4 1245 | .nf 1246 | .fam C 1247 | code() { 1248 | ps a | grep apache 1249 | } 1250 | .fam 1251 | .fi 1252 | .if n .RE 1253 | .sp 1254 | With bash, the result code of a pipeline equals the result code of the last command of the pipeline. The last command is \fIgrep\fP and since grep succeeds, the failure of \fI_ps\fP is lost and our test succeeds. We have only succeeded in messing with the test output, nothing more. 1255 | .sp 1256 | An alternative may be to activate bash \fIpipefail\fP option but this may introduce unwanted side effects. We can also simply not output anything in \fI_ps\fP so that \fIgrep\fP fails: 1257 | .sp 1258 | .if n .RS 4 1259 | .nf 1260 | .fam C 1261 | code() { 1262 | ps a | grep apache 1263 | } 1264 | 1265 | test_code_gives_ps_appropriate_parameters() { 1266 | _ps() { 1267 | assert_equals ax "${FAKE_PARAMS[@]}" 1268 | } 1269 | export \-f _ps 1270 | fake ps _ps 1271 | 1272 | code >/dev/null 1273 | } 1274 | .fam 1275 | .fi 1276 | .if n .RE 1277 | .sp 1278 | The problem here is that we use a trick to make the code under test fail but the 1279 | failure has nothing to do with the actual \fBassert_equals\fP failure. This is really 1280 | bad, don\(cqt do that. 1281 | .sp 1282 | Moreover, \fBassert_equals\fP output is captured by \fIps\fP and this just messes with the display of our test results: 1283 | .sp 1284 | .if n .RS 4 1285 | .nf 1286 | .fam C 1287 | Running test_code_gives_ps_appropriate_parameters ... 1288 | .fam 1289 | .fi 1290 | .if n .RE 1291 | .sp 1292 | The only correct alternative is for the fake \fIps\fP to write \fIFAKE_PARAMS\fP in a file descriptor 1293 | so that your test can grab them after code execution and assert their value. For instance 1294 | by writing to a file: 1295 | .sp 1296 | .if n .RS 4 1297 | .nf 1298 | .fam C 1299 | code() { 1300 | ps a | grep apache 1301 | } 1302 | 1303 | test_code_gives_ps_appropriate_parameters() { 1304 | _ps() { 1305 | echo ${FAKE_PARAMS[@]} > /tmp/fake_params 1306 | } 1307 | export \-f _ps 1308 | fake ps _ps 1309 | 1310 | code || true 1311 | 1312 | assert_equals ax "$(head \-n1 /tmp/fake_params)" 1313 | } 1314 | 1315 | setup() { 1316 | rm \-f /tmp/fake_params 1317 | } 1318 | .fam 1319 | .fi 1320 | .if n .RE 1321 | .sp 1322 | Here our fake writes to \fI/tmp/fake\fP. We delete this file in \fBsetup\fP to be 1323 | sure that we do not get inappropriate data from a previous test. We assert 1324 | that the first line of \fI/tmp/fake\fP equals \fIax\fP. Also, note that we know 1325 | that \fIcode\fP will fail and write this to ignore the error: \f(CRcode || true\fP. 1326 | .sp 1327 | .if n .RS 4 1328 | .nf 1329 | .fam C 1330 | Running test_code_gives_ps_appropriate_parameters ... FAILURE 1331 | expected [ax] but was [a] 1332 | doc:14:test_code_gives_ps_appropriate_parameters() 1333 | .fam 1334 | .fi 1335 | .if n .RE 1336 | .sp 1337 | We can also compact the fake definition: 1338 | .sp 1339 | .if n .RS 4 1340 | .nf 1341 | .fam C 1342 | code() { 1343 | ps a | grep apache 1344 | } 1345 | 1346 | test_code_gives_ps_appropriate_parameters() { 1347 | fake ps \*(Aqecho ${FAKE_PARAMS[@]} >/tmp/fake_params\*(Aq 1348 | 1349 | code || true 1350 | 1351 | assert_equals ax "$(head \-n1 /tmp/fake_params)" 1352 | } 1353 | 1354 | setup() { 1355 | rm \-f /tmp/fake_params 1356 | } 1357 | .fam 1358 | .fi 1359 | .if n .RE 1360 | .sp 1361 | .if n .RS 4 1362 | .nf 1363 | .fam C 1364 | Running test_code_gives_ps_appropriate_parameters ... FAILURE 1365 | expected [ax] but was [a] 1366 | doc:10:test_code_gives_ps_appropriate_parameters() 1367 | .fam 1368 | .fi 1369 | .if n .RE 1370 | .sp 1371 | Finally, we can avoid the \fI/tmp/fake_params\fP temporary file by using \fIcoproc\fP: 1372 | .sp 1373 | .if n .RS 4 1374 | .nf 1375 | .fam C 1376 | code() { 1377 | ps a | grep apache 1378 | } 1379 | 1380 | test_get_data_from_fake() { 1381 | #Fasten you seat belt ... 1382 | coproc cat 1383 | exec {test_channel}>&${COPROC[1]} 1384 | fake ps \*(Aqecho ${FAKE_PARAMS[@]} >&$test_channel\*(Aq 1385 | 1386 | code || true 1387 | 1388 | assert_equals ax "$(head \-n1 <&${COPROC[0]})" 1389 | } 1390 | .fam 1391 | .fi 1392 | .if n .RE 1393 | .sp 1394 | .if n .RS 4 1395 | .nf 1396 | .fam C 1397 | Running test_get_data_from_fake ... FAILURE 1398 | expected [ax] but was [a] 1399 | doc:13:test_get_data_from_fake() 1400 | .fam 1401 | .fi 1402 | .if n .RE -------------------------------------------------------------------------------- /img/bash_unit_100.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bash-unit/bash_unit/f4ee06f98d1de11cc5249d54913a1d57938a8012/img/bash_unit_100.png -------------------------------------------------------------------------------- /img/bash_unit_300.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bash-unit/bash_unit/f4ee06f98d1de11cc5249d54913a1d57938a8012/img/bash_unit_300.png -------------------------------------------------------------------------------- /img/bu_100.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bash-unit/bash_unit/f4ee06f98d1de11cc5249d54913a1d57938a8012/img/bu_100.png -------------------------------------------------------------------------------- /img/bu_50.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bash-unit/bash_unit/f4ee06f98d1de11cc5249d54913a1d57938a8012/img/bu_50.png -------------------------------------------------------------------------------- /img/demo.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/bash-unit/bash_unit/f4ee06f98d1de11cc5249d54913a1d57938a8012/img/demo.gif -------------------------------------------------------------------------------- /install.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | set -euo pipefail 4 | 5 | CURL="curl --show-error --silent --location" 6 | 7 | echo "downloading bash_unit" 8 | current_working_dir=$PWD 9 | tarball_urls=$($CURL https://api.github.com/repos/pgrange/bash_unit/releases | grep tarball_url) 10 | tarball_url=$(echo "$tarball_urls" | head -n 1 | cut -d '"' -f 4) 11 | tmp_dir=$(mktemp -d 2>/dev/null || mktemp -d -t 'tmpdir') 12 | cd "$tmp_dir" || exit 13 | $CURL "$tarball_url" | tar -xz -f - 14 | find "${tmp_dir}" -maxdepth 2 -type f -name "bash_unit" -exec cp {} "${current_working_dir}" \; 15 | rm -rf "$tmp_dir" 16 | echo "thank you for downloading bash_unit, you can now run ./bash_unit" 17 | -------------------------------------------------------------------------------- /release: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env -S bash -e 2 | 3 | token_file=token 4 | 5 | cd "$(dirname "$0")" || exit 6 | 7 | check_can_release() { 8 | local version="$1" 9 | 10 | type -P asciidoctor >/dev/null || usage "You need asciidoctor to make releases" 11 | 12 | if [ -n "$(git status --porcelain)" ] 13 | then 14 | git status >&2 15 | echo >&2 16 | usage "Please commit your pending changes first" 17 | fi 18 | 19 | [[ -z "$version" ]] && usage "No version specified on command line" 20 | echo "$version" | grep -E '^v[0-9]*\.[0-9]*\.[0-9]*$' >/dev/null || usage "Invalid format for version: $version" 21 | [ -r "$token_file" ] || usage "'$token_file' file does not exist" 22 | 23 | # shellcheck disable=1090 24 | source "$token_file" 25 | 26 | [[ -z "$user" ]] && usage "user not found in file '$token_file'" 27 | [[ -z "$token" ]] && usage "token not found in file '$token_file'" 28 | true #avoid error on last instruction of function (see bash -e) 29 | } 30 | 31 | prepare_release() { 32 | set -x 33 | local version="$1" 34 | shift 35 | 36 | update_version "$version" 37 | update_man 38 | 39 | git add bash_unit 40 | git add docs/man/man1/bash_unit.1 41 | 42 | if [ $# -eq 0 ] 43 | then 44 | git commit -m "prepare release $version" 45 | else 46 | git commit -m "$*" 47 | fi 48 | 49 | git tag "$version" 50 | } 51 | 52 | update_version() { 53 | local version="$1" 54 | sed -i -e "s:^VERSION=.*:VERSION=$version:" bash_unit 55 | } 56 | 57 | update_man() { 58 | asciidoctor -D docs/man/man1 -d manpage -b manpage README.adoc 59 | } 60 | 61 | publish_release() { 62 | local version="$1" 63 | 64 | git push 65 | git push --tags 66 | curl -u "$user:$token" -XPOST https://api.github.com/repos/pgrange/bash_unit/releases -d " 67 | { 68 | \"tag_name\": \"$version\" 69 | }" 70 | } 71 | 72 | usage() { 73 | local message=$1 74 | 75 | cat >&2 < [release note] 79 | Publishes a new release of bash_unit on github. 80 | Version must respect [Semantic Versioning](http://semver.org/) 81 | A token file must exist in the current working directory. This file must be of the form: 82 | user= 83 | token= 84 | 85 | is your github account. 86 | is your github api token. See https://github.com/settings/tokens 87 | EOF 88 | 89 | exit 1 90 | } 91 | 92 | [[ $# -ge 1 ]] || usage "You must specify version on command line" 93 | version=$1 94 | shift 95 | 96 | check_can_release "$version" 97 | 98 | prepare_release "$version" "$@" 99 | publish_release "$version" 100 | -------------------------------------------------------------------------------- /tests/test_cli.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | test_run_all_tests_even_in_case_of_failure() { 4 | assert_equals \ 5 | "\ 6 | Running tests in code 7 | Running test_fails ... FAILURE 8 | code:2:test_fails() 9 | Running test_succeed ... SUCCESS 10 | Overall resultcode: FAILURE\ 11 | " \ 12 | "$(bash_unit_out_for_code << EOF 13 | function test_succeed() { assert true ; } 14 | function test_fails() { assert false ; } 15 | EOF 16 | )" 17 | } 18 | 19 | test_exit_code_not_0_in_case_of_failure() { 20 | assert_fails "$BASH_UNIT <($CAT << EOF 21 | function test_succeed() { assert true ; } 22 | function test_fails() { assert false ; } 23 | EOF 24 | )" 25 | } 26 | 27 | test_exit_code_not_0_in_case_of_syntax_error() { 28 | assert_fails "$BASH_UNIT <($CAT << EOF 29 | function test_fails() { while true ; done ; } 30 | EOF 31 | )" 32 | } 33 | 34 | test_run_all_file_parameters() { 35 | bash_unit_output=$($BASH_UNIT \ 36 | <(echo "test_one() { echo -n ; }") \ 37 | <(echo "test_two() { echo -n ; }") \ 38 | | "$SED" -e 's:/dev/fd/[0-9]*:test_file:' \ 39 | ) 40 | 41 | assert_equals \ 42 | "\ 43 | Running tests in test_file 44 | Running test_one ... SUCCESS 45 | Running tests in test_file 46 | Running test_two ... SUCCESS 47 | Overall result: SUCCESS\ 48 | " \ 49 | "$bash_unit_output" 50 | } 51 | 52 | test_run_only_tests_that_match_pattern() { 53 | bash_unit_output=$($BASH_UNIT -p one \ 54 | <(echo "test_one() { echo -n ; }") \ 55 | <(echo "test_two() { echo -n ; }") \ 56 | | "$SED" -e 's:/dev/fd/[0-9]*:test_file:' \ 57 | ) 58 | 59 | assert_equals "\ 60 | Running tests in test_file 61 | Running test_one ... SUCCESS 62 | Running tests in test_file 63 | Overall result: SUCCESS" "$bash_unit_output" 64 | } 65 | 66 | test_do_not_run_pending_tests() { 67 | assert "$BASH_UNIT \ 68 | <(echo 'pending_should_not_run() { fail ; } 69 | todo_should_not_run() { fail ; }') \ 70 | " 71 | } 72 | 73 | test_pending_tests_appear_in_output() { 74 | bash_unit_output=$($BASH_UNIT \ 75 | <(echo 'pending_should_not_run() { fail ; } 76 | todo_should_not_run() { fail ; }') \ 77 | | "$SED" -e 's:/dev/fd/[0-9]*:test_file:' \ 78 | ) 79 | 80 | assert_equals "\ 81 | Running tests in test_file 82 | Running pending_should_not_run ... PENDING 83 | Running todo_should_not_run ... PENDING 84 | Overall result: SUCCESS" \ 85 | "$bash_unit_output" 86 | } 87 | 88 | test_do_not_run_skipped_tests() { 89 | assert "$BASH_UNIT -s two \ 90 | <(echo 'test_one() { echo -n ; } 91 | test_two() { fail ; } 92 | test_three() { fail ; } 93 | skip_if true three 94 | ') \ 95 | " 96 | } 97 | 98 | test_skipped_tests_appear_in_output() { 99 | bash_unit_output=$($BASH_UNIT -s two \ 100 | <(echo 'test_one() { echo -n ; } 101 | test_two() { fail ; } 102 | test_three() { fail ; } 103 | skip_if true three 104 | ') \ 105 | | "$SED" -e 's:/dev/fd/[0-9]*:test_file:' \ 106 | ) 107 | 108 | assert_equals "\ 109 | Running tests in test_file 110 | Running test_three ... SKIPPED 111 | Running test_two ... SKIPPED 112 | Running test_one ... SUCCESS 113 | Overall result: SUCCESS" \ 114 | "$bash_unit_output" 115 | } 116 | 117 | test_can_have_a_quiet_output() { 118 | bash_unit_output=$($BASH_UNIT -q \ 119 | <(echo 'test_one() { echo -n ; } 120 | test_two() { fail "this test fails" ; } 121 | test_three() { fail ; } 122 | ') \ 123 | | "$SED" -e 's:/dev/fd/[0-9]*:test_file:' \ 124 | ) 125 | 126 | assert_equals "\ 127 | Running tests in test_file 128 | Running test_one ... SUCCESS 129 | Running test_three ... FAILURE 130 | Running test_two ... FAILURE 131 | Overall result: FAILURE" \ 132 | "$bash_unit_output" 133 | } 134 | 135 | test_fails_when_test_file_does_not_exist() { 136 | assert_fails "$BASH_UNIT /not_exist/not_exist" 137 | } 138 | 139 | test_display_usage_when_test_file_does_not_exist() { 140 | bash_unit_output=$($BASH_UNIT /not_exist/not_exist 2>&1 >/dev/null | line 1) 141 | 142 | assert_equals "file does not exist: /not_exist/not_exist"\ 143 | "$bash_unit_output" 144 | } 145 | 146 | test_bash_unit_succeed_when_no_failure_even_if_no_teardown() { 147 | #FIX https://github.com/pgrange/bash_unit/issues/8 148 | assert "$BASH_UNIT <(echo 'test_success() { echo -n ; }')" 149 | } 150 | 151 | test_bash_unit_runs_teardown_even_in_case_of_failure() { 152 | #FIX https://github.com/pgrange/bash_unit/issues/10 153 | assert_equals "ran teardown" \ 154 | "$($BASH_UNIT <(echo 'test_fail() { fail ; } ; teardown() { echo "ran teardown" >&2 ; }') 2>&1 >/dev/null)" 155 | } 156 | 157 | test_bash_unit_runs_teardown_suite_even_in_case_of_failure() { 158 | assert_equals "ran teardown_suite" \ 159 | "$($BASH_UNIT <(echo 'test_fail() { fail ; } ; teardown_suite() { echo "ran teardown_suite" >&2 ; }') 2>&1 >/dev/null)" 160 | } 161 | 162 | test_bash_unit_runs_teardown_suite_even_in_case_of_failure_setup_suite() { 163 | #FIX https://github.com/pgrange/bash_unit/issues/43 164 | assert_equals "ran teardown_suite" \ 165 | "$($BASH_UNIT <(echo 'setup_suite() { return 1 ; } ; teardown_suite() { echo "ran teardown_suite" >&2 ; }') 2>&1 >/dev/null)" 166 | } 167 | 168 | test_one_test_should_stop_after_first_assertion_failure() { 169 | #FIX https://github.com/pgrange/bash_unit/issues/10 170 | assert_equals "before failure" \ 171 | "$($BASH_UNIT <(echo 'test_fail() { echo "before failure" >&2 ; fail ; echo "after failure" >&2 ; }') 2>&1 >/dev/null)" 172 | } 173 | 174 | test_one_test_should_stop_when_assert_fails() { 175 | #FIX https://github.com/pgrange/bash_unit/issues/26 176 | assert_equals "before failure" \ 177 | "$($BASH_UNIT <(echo 'test_fail() { echo "before failure" >&2 ; assert false ; echo "after failure" >&2 ; }') 2>&1 >/dev/null)" 178 | } 179 | 180 | setup() { 181 | # fake basic unix commands bash_unit relies on so that 182 | # we ensure bash_unit keeps working when people fake 183 | # this commands in their tests (and make what is necessary 184 | # so that code in these tests is immune to that fake by 185 | # using $SED or $CAT in the tests) 186 | fake cat : 187 | fake sed : 188 | } 189 | 190 | line() { 191 | line_nb=$1 192 | tail -n +"$line_nb" | head -1 193 | } 194 | 195 | bash_unit_out_for_code() { 196 | $BASH_UNIT <("$CAT") | "$SED" -e 's:/dev/fd/[0-9]*:code:' -e 's/[0-9]*:/code:/' 197 | } 198 | 199 | BASH_UNIT="eval FORCE_COLOR=false ../bash_unit" 200 | -------------------------------------------------------------------------------- /tests/test_core.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | # shellcheck disable=2317 # Unreachable code 4 | 5 | test_fail_fails() { 6 | with_bash_unit_muted fail && \ 7 | ( 8 | echo "FAILURE: fail must fail !!!" 9 | exit 1 10 | ) || \ 11 | echo "OK" > /dev/null 12 | } 13 | 14 | #fail can now be used in the following tests 15 | 16 | test_assert_fails_succeeds() { 17 | (assert_fails false) || fail 'assert_fails should succeed' 18 | } 19 | 20 | test_assert_fails_fails() { 21 | # shellcheck disable=2015 # Note about A && B || C - C may run if A is true 22 | with_bash_unit_muted assert_fails true && fail 'assert_fails should fail' || true 23 | } 24 | 25 | #assert_fails can now be used in the following tests 26 | 27 | test_assert_succeeds() { 28 | assert true || fail 'assert should succeed' 29 | } 30 | 31 | test_assert_fails() { 32 | assert_fails "with_bash_unit_muted assert false" "assert should fail" 33 | } 34 | 35 | #assert can now be used in the following tests 36 | 37 | test_assert_equals_fails_when_not_equal() { 38 | assert_fails \ 39 | "with_bash_unit_muted assert_equals toto tutu" \ 40 | "assert_equals should fail" 41 | } 42 | 43 | test_assert_equals_succeed_when_equal() { 44 | assert \ 45 | "assert_equals 'toto tata' 'toto tata'"\ 46 | 'assert_equals should succeed' 47 | } 48 | 49 | #assert_equals can now be used in the following tests 50 | 51 | test_assert_matches_fails_when_not_matching() { 52 | assert_fails \ 53 | "with_bash_unit_muted assert_matches to.*to tutu" \ 54 | "assert_matches should fail" 55 | } 56 | 57 | test_assert_matches_succeed_when_matching() { 58 | assert \ 59 | "assert_matches 't.to{0,1} t[Aa].*ta$' 'toto tata'"\ 60 | 'assert_matches should succeed' 61 | } 62 | 63 | test_assert_not_matches_fails_when_matching() { 64 | assert_fails \ 65 | "with_bash_unit_muted assert_not_matches 't.to{0,1} t[Aa].*ta$' 'toto tata'" \ 66 | "assert_not_matches should fail" 67 | } 68 | 69 | test_assert_not_matches_succeed_when_not_matching() { 70 | assert \ 71 | "assert_not_matches 'toto' 'tata'"\ 72 | 'assert_not_matches should succeed' 73 | } 74 | 75 | test_assert_not_equals_fails_when_equal() { 76 | assert_fails \ 77 | "with_bash_unit_muted assert_not_equals toto toto" \ 78 | "assert_not_equals should fail" 79 | } 80 | 81 | test_assert_not_equals_succeeds_when_not_equal() { 82 | assert \ 83 | "assert_not_equals 'toto tata' 'toto tutu'"\ 84 | 'assert_not_equals should succeed' 85 | } 86 | 87 | test_assert_within_delta_succeeds() { 88 | assert \ 89 | "assert_within_delta 12 10 3"\ 90 | 'assert_within_delta should succeed' 91 | assert \ 92 | "assert_within_delta 10 12 3"\ 93 | 'assert_within_delta should succeed' 94 | assert \ 95 | "assert_within_delta 10 12 2"\ 96 | 'assert_within_delta should succeed' 97 | assert \ 98 | "assert_within_delta 10 10 0"\ 99 | 'assert_within_delta should succeed' 100 | } 101 | 102 | test_assert_within_delta_fails() { 103 | assert_fails \ 104 | "assert_within_delta 13 10 2"\ 105 | 'assert_within_delta should fail' 106 | assert_fails \ 107 | "assert_within_delta 10 13 2"\ 108 | 'assert_within_delta should fail' 109 | assert_fails \ 110 | "assert_within_delta 11 10 0"\ 111 | 'assert_within_delta should fail' 112 | assert_fails \ 113 | "assert_within_delta eleven 10 0"\ 114 | 'assert_within_delta should fail' 115 | } 116 | 117 | test_assert_no_diff_succeeds_when_no_diff() { 118 | assert \ 119 | "assert_no_diff <(echo foo) <(echo foo)" \ 120 | "assert_no_diff should succeed" 121 | } 122 | 123 | test_assert_no_diff_fails_when_diff() { 124 | assert_fails \ 125 | "assert_no_diff <(echo foo) <(echo bar)" \ 126 | "assert_no_diff should fail" 127 | } 128 | 129 | test_fail_prints_failure_message() { 130 | message=$(with_bash_unit_log fail 'failure message' | line 2) 131 | 132 | assert_equals 'failure message' "$message" \ 133 | "unexpected error message" 134 | } 135 | 136 | test_fail_prints_where_is_error() { 137 | assert_equals "${BASH_SOURCE[0]}:${LINENO}:${FUNCNAME[0]}()" \ 138 | "$(with_bash_unit_stack fail | last_line)" 139 | } 140 | 141 | test_assert_status_code_succeeds() { 142 | assert "assert_status_code 3 'exit 3'" \ 143 | "assert_status_code should succeed" 144 | } 145 | 146 | test_assert_status_code_fails() { 147 | assert_fails "with_bash_unit_muted assert_status_code 3 true" \ 148 | "assert_status_code should fail" 149 | } 150 | 151 | test_assert_shows_stderr_on_failure() { 152 | message="$(with_bash_unit_err \ 153 | assert 'echo some error message >&2; echo some ok message; echo another ok message; exit 2' 154 | )" 155 | 156 | assert_equals "\ 157 | some error message" \ 158 | "$message" 159 | } 160 | 161 | test_assert_shows_stdout_on_failure() { 162 | message="$(with_bash_unit_out \ 163 | assert 'echo some error message >&2; echo some ok message; echo another ok message; exit 2' 164 | )" 165 | 166 | assert_equals "\ 167 | some ok message 168 | another ok message" \ 169 | "$message" 170 | } 171 | 172 | test_fake_actually_fakes_the_command() { 173 | fake ps echo expected 174 | assert_equals "expected" "$(ps)" 175 | } 176 | 177 | test_fake_can_fake_inline() { 178 | assert_equals \ 179 | "expected" \ 180 | "$(fake ps echo expected ; ps)" 181 | } 182 | 183 | test_fake_exports_faked_in_subshells() { 184 | fake ps echo expected 185 | assert_equals \ 186 | expected \ 187 | "$( bash -c ps )" 188 | } 189 | 190 | test_fake_transmits_params_to_fake_code() { 191 | function _ps() { 192 | assert_equals "aux" "$FAKE_PARAMS" 193 | } 194 | export -f _ps 195 | fake ps _ps 196 | 197 | ps aux 198 | } 199 | 200 | test_fake_transmits_params_to_fake_code_as_array() { 201 | function _ps() { 202 | assert_equals "1" "${#FAKE_PARAMS[@]}" 203 | } 204 | export -f _ps 205 | fake ps _ps 206 | 207 | ps "hello world" 208 | } 209 | 210 | test_fake_echo_stdin_when_no_params() { 211 | fake ps << EOF 212 | PID TTY TIME CMD 213 | 7801 pts/9 00:00:00 bash 214 | 7818 pts/9 00:00:00 ps 215 | EOF 216 | 217 | assert_equals 2 "$(ps | "$GREP" pts | wc -l| tr -d ' ')" 218 | } 219 | 220 | test_should_pretty_format_even_when_LANG_is_unset() { 221 | # See https://github.com/pgrange/bash_unit/pull/81 222 | unset LANG 223 | assert "echo foo | pretty_format GREEN I" 224 | } 225 | 226 | if [[ "${STICK_TO_CWD:-}" != true ]] 227 | then 228 | # do not test for cwd if STICK_TO_CWD is true 229 | test_bash_unit_changes_cwd_to_current_test_file_directory() { 230 | assert "ls ../tests/$(basename "${BASH_SOURCE[0]}")" \ 231 | "bash_unit should change current working directory to match the directory of the currently running test" 232 | } 233 | 234 | #the following assertion is out of any test on purpose 235 | assert "ls ../tests/$(basename "${BASH_SOURCE[0]}")" \ 236 | "bash_unit should change current working directory to match the directory of the currently running test before sourcing test file" 237 | fi 238 | 239 | setup() { 240 | # enforce bash variable controls during core tests 241 | # this way we know that people using this enforcement 242 | # in their own code can still rely on bash_unit 243 | set -u 244 | # fake basic unix commands bash_unit relies on so that 245 | # we ensure bash_unit keeps working when people fake 246 | # this commands in their tests 247 | fake cat : 248 | fake sed : 249 | fake grep : 250 | fake printf : 251 | } 252 | 253 | line() { 254 | line_nb=$1 255 | tail -n +"$line_nb" | head -1 256 | } 257 | 258 | last_line() { 259 | tail -1 260 | } 261 | 262 | with_bash_unit_muted() { 263 | with_bash_unit_notifications_muted "$@" 264 | } 265 | 266 | with_bash_unit_err() { 267 | with_bash_unit_notifications_muted -e "$@" 268 | } 269 | 270 | with_bash_unit_out() { 271 | with_bash_unit_notifications_muted -o "$@" 272 | } 273 | 274 | with_bash_unit_log() { 275 | with_bash_unit_notifications_muted -l "$@" 276 | } 277 | 278 | with_bash_unit_stack() { 279 | with_bash_unit_notifications_muted -s "$@" 280 | } 281 | 282 | with_bash_unit_notifications_muted() { 283 | ( 284 | mute 285 | unset OPTIND 286 | while getopts "lsoe" option 287 | do 288 | case "$option" in 289 | l) 290 | unmute_logs 291 | ;; 292 | s) 293 | unmute_stack 294 | ;; 295 | o) 296 | unmute_out 297 | ;; 298 | e) 299 | unmute_err 300 | ;; 301 | *) 302 | # Ignore invalid flags 303 | ;; 304 | esac 305 | done 306 | shift $((OPTIND-1)) 307 | 308 | "$@" 309 | ) 310 | } 311 | 312 | unmute_logs() { 313 | notify_suite_starting() { echo "Running tests in $1" ; } 314 | notify_test_starting () { echo -e -n "\tRunning $1... " ; } 315 | notify_test_succeeded() { echo "SUCCESS" ; } 316 | notify_test_failed () { echo "FAILURE" ; } 317 | notify_message () { echo "$1" ; } 318 | } 319 | 320 | unmute_stack() { 321 | notify_stack() { "$CAT" ; } 322 | } 323 | 324 | unmute_out() { 325 | notify_stdout() { "$CAT" ; } 326 | } 327 | 328 | unmute_err() { 329 | notify_stderr() { "$CAT" ; } 330 | } 331 | 332 | mute() { 333 | notify_suite_starting() { : ; } 334 | notify_test_starting () { : ; } 335 | notify_test_succeeded() { : ; } 336 | notify_test_failed () { : ; } 337 | notify_message () { : ; } 338 | notify_stack () { $CAT >/dev/null ; } 339 | notify_stdout () { : ; } 340 | notify_stderr () { : ; } 341 | notify_suites_succeded () { : ; } 342 | notify_suites_failed () { : ; } 343 | } 344 | -------------------------------------------------------------------------------- /tests/test_doc.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | TEST_PATTERN='```test' 4 | OUTPUT_PATTERN='```output' 5 | LANG=C.UTF-8 6 | unset LC_ALL LANGUAGE 7 | 8 | export STICK_TO_CWD=true 9 | BASH_UNIT="eval FORCE_COLOR=false ./bash_unit" 10 | 11 | prepare_tests() { 12 | mkdir /tmp/$$ 13 | local block=0 14 | local remaining=/tmp/$$/remaining 15 | local swap=/tmp/$$/swap 16 | local test_output=/tmp/$$/test_output 17 | local expected_output=/tmp/$$/expected_output 18 | cat README.adoc > "$remaining" 19 | 20 | while grep -E "^${TEST_PATTERN}$" "$remaining" >/dev/null 21 | do 22 | ((++block)) 23 | run_doc_test "$remaining" "$swap" |& sed "\$a\\" > "$test_output$block" 24 | doc_to_output "$remaining" "$swap" > "$expected_output$block" 25 | eval 'function test_block_'"$(printf "%02d" "$block")"'() { 26 | assert "diff -u '"$expected_output$block"' '"$test_output$block"'" 27 | }' 28 | done 29 | } 30 | 31 | function run_doc_test() { 32 | local remaining="$1" 33 | local swap="$2" 34 | $BASH_UNIT <(< "$remaining" _next_code "$swap") \ 35 | | clean_bash_unit_running_header \ 36 | | clean_bash_pseudo_files_name \ 37 | | clean_bash_unit_overall_result 38 | cat "$swap" > "$remaining" 39 | } 40 | 41 | function clean_bash_unit_running_header() { 42 | tail -n +2 43 | } 44 | 45 | function clean_bash_pseudo_files_name() { 46 | sed -e 's:/dev/fd/[0-9]*:doc:g' 47 | } 48 | 49 | function clean_bash_unit_overall_result() { 50 | sed '$d' 51 | } 52 | 53 | function doc_to_output() { 54 | local remaining="$1" 55 | local swap="$2" 56 | < "$remaining" _next_output "$swap" 57 | cat "$swap" > "$remaining" 58 | } 59 | 60 | function _next_code() { 61 | local remaining="$1" 62 | _next_quote_section "$TEST_PATTERN" "$remaining" 63 | } 64 | 65 | function _next_output() { 66 | local remaining="$1" 67 | _next_quote_section "$OUTPUT_PATTERN" "$remaining" 68 | } 69 | 70 | function _next_quote_section() { 71 | local quote_pattern=$1 72 | local remaining=$2 73 | sed -E '1 , /^'"$quote_pattern"'$/ d' |\ 74 | sed -E ' 75 | /^```$/ , $ w '"$remaining"' 76 | 1,/^```$/ !d;//d 77 | ' 78 | } 79 | 80 | # change to bash_unit source directory since we should be in 81 | # test subdirectory 82 | cd .. 83 | prepare_tests 84 | -------------------------------------------------------------------------------- /tests/test_tap_format: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | test_bash_unit_accepts_tap_format_option() { 4 | assert "$BASH_UNIT -f tap" 5 | } 6 | 7 | test_bash_unit_rejects_invalid_format() { 8 | assert_fails "$BASH_UNIT -f invalid_format" 9 | } 10 | 11 | test_tap_format_for_one_succesfull_test() { 12 | assert_equals \ 13 | "\ 14 | # Running tests in code 15 | ok - test_ok 16 | 1..1" \ 17 | "$(bash_unit_out_for_code < message on stdout 76 | # err> message on stderr 77 | # code:2:test_not_ok() 78 | 1..1" \ 79 | "$(bash_unit_out_for_code <&2" 82 | } 83 | EOF 84 | )" 85 | } 86 | 87 | test_assertion_message_is_tap_formatted() { 88 | assert_equals \ 89 | "\ 90 | # Running tests in code 91 | not ok - test_not_ok 92 | # obvious failure 93 | # code:2:test_not_ok() 94 | 1..1" \ 95 | "$(bash_unit_out_for_code <