[
  {
    "path": "CONTRIBUTING.md",
    "content": "Thanks for taking the time to provide corrections and additions!\n\nTo add an example with a new framework please follow the structure used for the other examples:\n* Check the `example-<testlib>` folders and create a similar folder for the new library to showcase its capabilities.\n* Add a new column to the main [readme.md](https://github.com/dodie/testing-in-bash/blob/master/readme.md) file for the new library.\n  * To keep sanity while editing the huge table I disable line wrapping and use a [markdown formatter](http://markdowntable.com/).\n"
  },
  {
    "path": "LICENSE",
    "content": "                    GNU GENERAL PUBLIC LICENSE\n                       Version 2, June 1991\n\n Copyright (C) 1989, 1991 Free Software Foundation, Inc.,\n 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA\n Everyone is permitted to copy and distribute verbatim copies\n of this license document, but changing it is not allowed.\n\n                            Preamble\n\n  The licenses for most software are designed to take away your\nfreedom to share and change it.  By contrast, the GNU General Public\nLicense is intended to guarantee your freedom to share and change free\nsoftware--to make sure the software is free for all its users.  This\nGeneral Public License applies to most of the Free Software\nFoundation's software and to any other program whose authors commit to\nusing it.  (Some other Free Software Foundation software is covered by\nthe GNU Lesser General Public License instead.)  You can apply it to\nyour programs, too.\n\n  When we speak of free software, we are referring to freedom, not\nprice.  Our General Public Licenses are designed to make sure that you\nhave the freedom to distribute copies of free software (and charge for\nthis service if you wish), that you receive source code or can get it\nif you want it, that you can change the software or use pieces of it\nin new free programs; and that you know you can do these things.\n\n  To protect your rights, we need to make restrictions that forbid\nanyone to deny you these rights or to ask you to surrender the rights.\nThese restrictions translate to certain responsibilities for you if you\ndistribute copies of the software, or if you modify it.\n\n  For example, if you distribute copies of such a program, whether\ngratis or for a fee, you must give the recipients all the rights that\nyou have.  You must make sure that they, too, receive or can get the\nsource code.  And you must show them these terms so they know their\nrights.\n\n  We protect your rights with two steps: (1) copyright the software, and\n(2) offer you this license which gives you legal permission to copy,\ndistribute and/or modify the software.\n\n  Also, for each author's protection and ours, we want to make certain\nthat everyone understands that there is no warranty for this free\nsoftware.  If the software is modified by someone else and passed on, we\nwant its recipients to know that what they have is not the original, so\nthat any problems introduced by others will not reflect on the original\nauthors' reputations.\n\n  Finally, any free program is threatened constantly by software\npatents.  We wish to avoid the danger that redistributors of a free\nprogram will individually obtain patent licenses, in effect making the\nprogram proprietary.  To prevent this, we have made it clear that any\npatent must be licensed for everyone's free use or not licensed at all.\n\n  The precise terms and conditions for copying, distribution and\nmodification follow.\n\n                    GNU GENERAL PUBLIC LICENSE\n   TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION\n\n  0. This License applies to any program or other work which contains\na notice placed by the copyright holder saying it may be distributed\nunder the terms of this General Public License.  The \"Program\", below,\nrefers to any such program or work, and a \"work based on the Program\"\nmeans either the Program or any derivative work under copyright law:\nthat is to say, a work containing the Program or a portion of it,\neither verbatim or with modifications and/or translated into another\nlanguage.  (Hereinafter, translation is included without limitation in\nthe term \"modification\".)  Each licensee is addressed as \"you\".\n\nActivities other than copying, distribution and modification are not\ncovered by this License; they are outside its scope.  The act of\nrunning the Program is not restricted, and the output from the Program\nis covered only if its contents constitute a work based on the\nProgram (independent of having been made by running the Program).\nWhether that is true depends on what the Program does.\n\n  1. You may copy and distribute verbatim copies of the Program's\nsource code as you receive it, in any medium, provided that you\nconspicuously and appropriately publish on each copy an appropriate\ncopyright notice and disclaimer of warranty; keep intact all the\nnotices that refer to this License and to the absence of any warranty;\nand give any other recipients of the Program a copy of this License\nalong with the Program.\n\nYou may charge a fee for the physical act of transferring a copy, and\nyou may at your option offer warranty protection in exchange for a fee.\n\n  2. You may modify your copy or copies of the Program or any portion\nof it, thus forming a work based on the Program, and copy and\ndistribute such modifications or work under the terms of Section 1\nabove, provided that you also meet all of these conditions:\n\n    a) You must cause the modified files to carry prominent notices\n    stating that you changed the files and the date of any change.\n\n    b) You must cause any work that you distribute or publish, that in\n    whole or in part contains or is derived from the Program or any\n    part thereof, to be licensed as a whole at no charge to all third\n    parties under the terms of this License.\n\n    c) If the modified program normally reads commands interactively\n    when run, you must cause it, when started running for such\n    interactive use in the most ordinary way, to print or display an\n    announcement including an appropriate copyright notice and a\n    notice that there is no warranty (or else, saying that you provide\n    a warranty) and that users may redistribute the program under\n    these conditions, and telling the user how to view a copy of this\n    License.  (Exception: if the Program itself is interactive but\n    does not normally print such an announcement, your work based on\n    the Program is not required to print an announcement.)\n\nThese requirements apply to the modified work as a whole.  If\nidentifiable sections of that work are not derived from the Program,\nand can be reasonably considered independent and separate works in\nthemselves, then this License, and its terms, do not apply to those\nsections when you distribute them as separate works.  But when you\ndistribute the same sections as part of a whole which is a work based\non the Program, the distribution of the whole must be on the terms of\nthis License, whose permissions for other licensees extend to the\nentire whole, and thus to each and every part regardless of who wrote it.\n\nThus, it is not the intent of this section to claim rights or contest\nyour rights to work written entirely by you; rather, the intent is to\nexercise the right to control the distribution of derivative or\ncollective works based on the Program.\n\nIn addition, mere aggregation of another work not based on the Program\nwith the Program (or with a work based on the Program) on a volume of\na storage or distribution medium does not bring the other work under\nthe scope of this License.\n\n  3. You may copy and distribute the Program (or a work based on it,\nunder Section 2) in object code or executable form under the terms of\nSections 1 and 2 above provided that you also do one of the following:\n\n    a) Accompany it with the complete corresponding machine-readable\n    source code, which must be distributed under the terms of Sections\n    1 and 2 above on a medium customarily used for software interchange; or,\n\n    b) Accompany it with a written offer, valid for at least three\n    years, to give any third party, for a charge no more than your\n    cost of physically performing source distribution, a complete\n    machine-readable copy of the corresponding source code, to be\n    distributed under the terms of Sections 1 and 2 above on a medium\n    customarily used for software interchange; or,\n\n    c) Accompany it with the information you received as to the offer\n    to distribute corresponding source code.  (This alternative is\n    allowed only for noncommercial distribution and only if you\n    received the program in object code or executable form with such\n    an offer, in accord with Subsection b above.)\n\nThe source code for a work means the preferred form of the work for\nmaking modifications to it.  For an executable work, complete source\ncode means all the source code for all modules it contains, plus any\nassociated interface definition files, plus the scripts used to\ncontrol compilation and installation of the executable.  However, as a\nspecial exception, the source code distributed need not include\nanything that is normally distributed (in either source or binary\nform) with the major components (compiler, kernel, and so on) of the\noperating system on which the executable runs, unless that component\nitself accompanies the executable.\n\nIf distribution of executable or object code is made by offering\naccess to copy from a designated place, then offering equivalent\naccess to copy the source code from the same place counts as\ndistribution of the source code, even though third parties are not\ncompelled to copy the source along with the object code.\n\n  4. You may not copy, modify, sublicense, or distribute the Program\nexcept as expressly provided under this License.  Any attempt\notherwise to copy, modify, sublicense or distribute the Program is\nvoid, and will automatically terminate your rights under this License.\nHowever, parties who have received copies, or rights, from you under\nthis License will not have their licenses terminated so long as such\nparties remain in full compliance.\n\n  5. You are not required to accept this License, since you have not\nsigned it.  However, nothing else grants you permission to modify or\ndistribute the Program or its derivative works.  These actions are\nprohibited by law if you do not accept this License.  Therefore, by\nmodifying or distributing the Program (or any work based on the\nProgram), you indicate your acceptance of this License to do so, and\nall its terms and conditions for copying, distributing or modifying\nthe Program or works based on it.\n\n  6. Each time you redistribute the Program (or any work based on the\nProgram), the recipient automatically receives a license from the\noriginal licensor to copy, distribute or modify the Program subject to\nthese terms and conditions.  You may not impose any further\nrestrictions on the recipients' exercise of the rights granted herein.\nYou are not responsible for enforcing compliance by third parties to\nthis License.\n\n  7. If, as a consequence of a court judgment or allegation of patent\ninfringement or for any other reason (not limited to patent issues),\nconditions are imposed on you (whether by court order, agreement or\notherwise) that contradict the conditions of this License, they do not\nexcuse you from the conditions of this License.  If you cannot\ndistribute so as to satisfy simultaneously your obligations under this\nLicense and any other pertinent obligations, then as a consequence you\nmay not distribute the Program at all.  For example, if a patent\nlicense would not permit royalty-free redistribution of the Program by\nall those who receive copies directly or indirectly through you, then\nthe only way you could satisfy both it and this License would be to\nrefrain entirely from distribution of the Program.\n\nIf any portion of this section is held invalid or unenforceable under\nany particular circumstance, the balance of the section is intended to\napply and the section as a whole is intended to apply in other\ncircumstances.\n\nIt is not the purpose of this section to induce you to infringe any\npatents or other property right claims or to contest validity of any\nsuch claims; this section has the sole purpose of protecting the\nintegrity of the free software distribution system, which is\nimplemented by public license practices.  Many people have made\ngenerous contributions to the wide range of software distributed\nthrough that system in reliance on consistent application of that\nsystem; it is up to the author/donor to decide if he or she is willing\nto distribute software through any other system and a licensee cannot\nimpose that choice.\n\nThis section is intended to make thoroughly clear what is believed to\nbe a consequence of the rest of this License.\n\n  8. If the distribution and/or use of the Program is restricted in\ncertain countries either by patents or by copyrighted interfaces, the\noriginal copyright holder who places the Program under this License\nmay add an explicit geographical distribution limitation excluding\nthose countries, so that distribution is permitted only in or among\ncountries not thus excluded.  In such case, this License incorporates\nthe limitation as if written in the body of this License.\n\n  9. The Free Software Foundation may publish revised and/or new versions\nof the General Public License from time to time.  Such new versions will\nbe similar in spirit to the present version, but may differ in detail to\naddress new problems or concerns.\n\nEach version is given a distinguishing version number.  If the Program\nspecifies a version number of this License which applies to it and \"any\nlater version\", you have the option of following the terms and conditions\neither of that version or of any later version published by the Free\nSoftware Foundation.  If the Program does not specify a version number of\nthis License, you may choose any version ever published by the Free Software\nFoundation.\n\n  10. If you wish to incorporate parts of the Program into other free\nprograms whose distribution conditions are different, write to the author\nto ask for permission.  For software which is copyrighted by the Free\nSoftware Foundation, write to the Free Software Foundation; we sometimes\nmake exceptions for this.  Our decision will be guided by the two goals\nof preserving the free status of all derivatives of our free software and\nof promoting the sharing and reuse of software generally.\n\n                            NO WARRANTY\n\n  11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY\nFOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW.  EXCEPT WHEN\nOTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES\nPROVIDE THE PROGRAM \"AS IS\" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED\nOR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\nMERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.  THE ENTIRE RISK AS\nTO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU.  SHOULD THE\nPROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,\nREPAIR OR CORRECTION.\n\n  12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING\nWILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR\nREDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,\nINCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING\nOUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED\nTO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY\nYOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER\nPROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE\nPOSSIBILITY OF SUCH DAMAGES.\n\n                     END OF TERMS AND CONDITIONS\n\n            How to Apply These Terms to Your New Programs\n\n  If you develop a new program, and you want it to be of the greatest\npossible use to the public, the best way to achieve this is to make it\nfree software which everyone can redistribute and change under these terms.\n\n  To do so, attach the following notices to the program.  It is safest\nto attach them to the start of each source file to most effectively\nconvey the exclusion of warranty; and each file should have at least\nthe \"copyright\" line and a pointer to where the full notice is found.\n\n    <one line to give the program's name and a brief idea of what it does.>\n    Copyright (C) <year>  <name of author>\n\n    This program is free software; you can redistribute it and/or modify\n    it under the terms of the GNU General Public License as published by\n    the Free Software Foundation; either version 2 of the License, or\n    (at your option) any later version.\n\n    This program is distributed in the hope that it will be useful,\n    but WITHOUT ANY WARRANTY; without even the implied warranty of\n    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n    GNU General Public License for more details.\n\n    You should have received a copy of the GNU General Public License along\n    with this program; if not, write to the Free Software Foundation, Inc.,\n    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.\n\nAlso add information on how to contact you by electronic and paper mail.\n\nIf the program is interactive, make it output a short notice like this\nwhen it starts in an interactive mode:\n\n    Gnomovision version 69, Copyright (C) year name of author\n    Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'.\n    This is free software, and you are welcome to redistribute it\n    under certain conditions; type `show c' for details.\n\nThe hypothetical commands `show w' and `show c' should show the appropriate\nparts of the General Public License.  Of course, the commands you use may\nbe called something other than `show w' and `show c'; they could even be\nmouse-clicks or menu items--whatever suits your program.\n\nYou should also get your employer (if you work as a programmer) or your\nschool, if any, to sign a \"copyright disclaimer\" for the program, if\nnecessary.  Here is a sample; alter the names:\n\n  Yoyodyne, Inc., hereby disclaims all copyright interest in the program\n  `Gnomovision' (which makes passes at compilers) written by James Hacker.\n\n  <signature of Ty Coon>, 1 April 1989\n  Ty Coon, President of Vice\n\nThis General Public License does not permit incorporating your program into\nproprietary programs.  If your program is a subroutine library, you may\nconsider it more useful to permit linking proprietary applications with the\nlibrary.  If this is what you want to do, use the GNU Lesser General\nPublic License instead of this License.\n"
  },
  {
    "path": "example-bash_unit/.gitignore",
    "content": "lib\nbuild\n"
  },
  {
    "path": "example-bash_unit/install.sh",
    "content": "#!/bin/bash\n\ncd \"$(dirname \"$0\")\"\nrm -rf lib\nmkdir lib\ncd lib\n\ncurl -L -O -J https://github.com/pgrange/bash_unit/archive/v1.7.1.tar.gz\ntar -zxvf bash_unit-1.7.1.tar.gz\nmv bash_unit-1.7.1 bash_unit\n\n"
  },
  {
    "path": "example-bash_unit/readme.md",
    "content": "# bash_unit test drive\n\nThis framework allows to write test files in pure Bash, without custom syntax, yet provide concise syntax and many features.\nMy only concern is that it has only a few built-in assertions, but that can be solved by writing custom ones.\n\n\n## Test format\n\n✔️ Test files are simple shell scripts, executed by the test framework. There's no need for any extra noise besides the test\nfunctions in a test file: no custom shebang line, no framework initialization code, and no custom imports.\n\n```bash\ntest_example(){\n  assert_equals \"hello\" \"world\"\n}\n```\n\n✔️ Test functions are simple bash functions without custom syntax. All functions which name is starting with `test_`\nis considered a test function\n\n\n## Test Discovery\n\n✔️ bash_unit is parameterized with the list of test files to be executed. This means that it's trivial to focus on a single\ntest file to execute. Moreover, it can also take a pattern to narrow test cases to be executed to the matching ones.\n\n⚠️ Because it explicitly has to be parameterized with the list of test files, it can't discover directories. For this purpose\none might wish to introduce a [lightweight wrapper](https://github.com/dodie/testing-in-bash/blob/master/example-bash_unit/test.sh).\n\n\n## Assertions\n\n⚠️ It has some assertions available like `assert_equals` but lacks other essential ones, like `assert_contains`.\n\n✔️ All assertions provide a nice, easy to read report including the context and line number.\n\n\n## Custom assertions\n\n✔️ It’s possible to create new assertions by building functions on top of the existing assertions, although there's no dedicated lower-level API to support it. Custom assertions can be sourced from an external file.\n\n\n## Skip tests\n\n✔️ If a test function name is starting with `pending` or `todo` it will be skipped which will be properly reflected in the report.\n\n\n## Mocking\n\n✔️ Supports all [common techniques](https://github.com/dodie/testing-in-bash/tree/master/mocking) for mocking.\n\n✔️ Additionally, it even has the\n[built-in `fake` command](https://github.com/pgrange/bash_unit/blob/a10751626ef0caa2bc5988a574e9a546426a2791/bash_unit#L123-L133)\nwhich is a shorthand for the function-export based mock technique,\nmaking the tests even more clear and concise.\n\n\n## Activity\n\n✔️ The project is around since 2016 and has regular releases. Overall much less popular than Bats or shUnit2, and has activity\non the GitHub repository, but there's also much less forgotten issues and PRs.\n\n\n## Documentation\n\n✔️ Based on the [getting started guide](https://github.com/pgrange/bash_unit) it was really easy to get started.\n"
  },
  {
    "path": "example-bash_unit/src/file-report.sh",
    "content": "#!/bin/bash\n\nmode=$1\npath=$2\n\nif [ -z $path  ]; then\n    echo \"Usage: ./file-report.sh [size|number-of-files] [path]\"; >&2\n    exit 1\nfi\n\ncd \"${path}\"\n\ncase \"${mode}\" in\n    number-of-files)\n        ls -1 | wc -l ;;\n    size)\n        du -h | cut -f1 ;;\n    *)\n        >&2 echo \"Usage: ./file-report.sh [size|number-of-files] [path]\"; exit 1 ;;\nesac\n\n"
  },
  {
    "path": "example-bash_unit/src/ice_cream_price.sh",
    "content": "#!/bin/bash\n\nfunction main() {\n    numberOfPortions=$1\n    pricePerPortion=$(calculatePrice $numberOfPortions)\n    totalPrice=$(( $numberOfPortions * $pricePerPortion ))\n\n    echo \"Total $totalPrice\"\n}\n\nfunction calculatePrice() {\n    if [[ $numberOfPortions -lt 3 ]]; then\n        echo \"100\"\n    else\n        day=$(getDay)\n        if (( $day % 2 )); then\n            echo \"80\"\n        else\n            echo \"100\"\n        fi\n    fi\n}\n\nfunction getDay() {\n    date -d '+%d'\n}\n\nif [[ \"${BASH_SOURCE[0]}\" == \"${0}\" ]]\nthen\n  main $@\nfi\n"
  },
  {
    "path": "example-bash_unit/src/myfun.sh",
    "content": "#!/bin/bash\n\nfunction myfun() {\n    echo \"Hello!\"\n}\n"
  },
  {
    "path": "example-bash_unit/test/assertion_showcase.sh",
    "content": "# include custom assertions\nsource \"${TEST}/util/customasserts.sh\"\n\ntest_with_assert_equals() {\n  result=$(${SRC}/ice_cream_price.sh 1)\n  assert_equals 0 $?\n  assert_equals \"Total 100\" \"${result}\"\n}\n\npending_test_this_is_broken() {\n  #...\n  assert_equals \"1\" \"2\"\n}\n\ntest_custom_assertion() {\n  assert_contains \"Hello\" \"Hello World\"\n}\n"
  },
  {
    "path": "example-bash_unit/test/e2e_test.sh",
    "content": "function test_ice_cream_price_should_be_100_per_portion_for_low_quantities() {\n  result=$(${SRC}/ice_cream_price.sh 1)\n  assert_equals $? 0 # assert success\n  assert_equals \"${result}\" \"Total 100\"\n}\n\nfunction test_there_should_be_20_percent_discount_for_large_quantities_on_odd_days() {\n  # Mocking date by adding a custom executable to the PATH\n  export DATE_MOCK_REPLY=\"1\"\n  export PATH=${TEST}/mocks:$PATH\n\n  result=$(${SRC}/ice_cream_price.sh 4)\n  assert_equals $? 0 # assert success\n  assert_equals \"${result}\" \"Total 320\"\n}\n\nfunction test_there_should_be_no_discount_even_for_large_quantities_on_even_days() {\n  # Mocking date by adding a custom executable to the PATH\n  export DATE_MOCK_REPLY=\"2\"\n  export PATH=${TEST}/mocks:$PATH\n\n  result=$(${SRC}/ice_cream_price.sh 4)\n  assert_equals $? 0 # assert success\n  assert_equals \"${result}\" \"Total 400\"\n}\n\n"
  },
  {
    "path": "example-bash_unit/test/mocks/date",
    "content": "#!/bin/bash\n\necho \"${DATE_MOCK_REPLY}\"\n\n"
  },
  {
    "path": "example-bash_unit/test/setup_and_teardown.sh",
    "content": "source \"${TEST}/util/customasserts.sh\"\n\n# Create test data directories\nfunction setup_suite() {\n  mkdir -p ${BUILD}/testdata\n}\n\n# Remove test data directories\nfunction teardown_suite() {\n  rm -rf ${BUILD}/testdata\n}\n\n# Generate fresh example files for the tests\nfunction setup() {\n  rm -rf ${BUILD}/testdata/*\n\n  for i in {1..3}\n  do\n    echo \"Hello $i\" > \"${BUILD}/testdata/${i}.txt\"\n  done\n}\n\n# Assert that the tests did not remove the example files\nfunction teardown() {\n  result=$(ls ${BUILD}/testdata -1)\n  assert_equals 0 $? # assert success\n\n  assert_contains \"1.txt\" \"${result}\"\n  assert_contains \"2.txt\" \"${result}\"\n  assert_contains \"3.txt\" \"${result}\"\n}\n\nfunction test_number-of-files_should_return_the_number_of_files_in_the_directory() {\n  result=$(${SRC}/file-report.sh number-of-files ${BUILD}/testdata)\n  assert_equals 0 $?\n  assert_equals 3 \"${result}\"\n}\n\nfunction test_size_should_return_the_total_size_of_the_files_in_the_directory() {\n  result=$(${SRC}/file-report.sh size ${BUILD}/testdata)\n  assert_equals 0 $?\n  assert_equals \"16K\" \"${result}\"\n}\n\n"
  },
  {
    "path": "example-bash_unit/test/unit_test.sh",
    "content": "function test_ice_cream_price_should_be_100_per_portion_for_low_quantities() {\n  source ${SRC}/ice_cream_price.sh\n\n  result=$(main 1)\n\n  assert_equals 0 $?\n  assert_equals \"${result}\" \"Total 100\"\n}\n\nfunction test_there_should_be_20_percent_discount_for_large_quantities_on_odd_days() {\n  # Mocking a function by defining an alias\n  shopt -s expand_aliases\n  alias \"getDay\"=\"echo '1'\"\n\n  source ${SRC}/ice_cream_price.sh\n\n  result=$(main 4)\n\n  assert_equals 0 $?\n  assert_equals \"${result}\" \"Total 320\"\n}\n\nfunction test_there_should_be_no_discount_even_for_large_quantities_on_even_days() {\n  source ${SRC}/ice_cream_price.sh\n\n  # Mocking a function by exporting a function\n  function getDay() { echo \"2\"; }\n  export -f getDay\n  result=$(main 4)\n\n  assert_equals 0 $?\n  assert_equals \"${result}\" \"Total 400\"\n}\n\nfunction test_mock_alternative_using_fake() {\n  source ${SRC}/ice_cream_price.sh\n\n  # Mocking a function by using bash_unit's fake\n  fake getDay echo \"2\"\n  result=$(main 4)\n\n  assert_equals 0 $?\n  assert_equals \"${result}\" \"Total 400\"\n}\n\n"
  },
  {
    "path": "example-bash_unit/test/util/customasserts.sh",
    "content": "function assert_contains() {\n  local expected=$1\n  local actual=$2\n\n  if [[ \"${actual}\" =~ *\"${expected}\"* ]]; then\n    fail \"${actual} does not contain ${expected}\"\n  fi\n}\n"
  },
  {
    "path": "example-bash_unit/test.sh",
    "content": "#!/bin/bash\n\n# Set project root as the working directory\ncd \"$(dirname \"$0\")\"\n\nexport PROJECT_DIR=\"$(realpath \".\")\"\nexport SRC=\"${PROJECT_DIR}/src\"\nexport TEST=\"${PROJECT_DIR}/test\"\nexport BUILD=\"${PROJECT_DIR}/build\"\n\nlib/bash_unit/bash_unit test/*.sh\n\n"
  },
  {
    "path": "example-bashunit/.gitignore",
    "content": "lib/\n"
  },
  {
    "path": "example-bashunit/README.md",
    "content": "# bashunit\n\n1. Install the library: `./install.sh`\n2. Run the test script: `./test.sh`"
  },
  {
    "path": "example-bashunit/install.sh",
    "content": "#!/bin/bash\n\n####################################################\n# More details about bashunit installation         #\n# see: https://bashunit.typeddevs.com/installation #\n####################################################\ncurl -s https://bashunit.typeddevs.com/install.sh | bash -s lib 0.23.0\n"
  },
  {
    "path": "example-bashunit/src/file-report.sh",
    "content": "#!/bin/bash\n\nmode=$1\npath=$2\n\nif [ -z $path  ]; then\n  echo \"Usage: ./file-report.sh [size|number-of-files] [path]\"; >&2\n  exit 1\nfi\n\ncd \"${path}\"\n\ncase \"${mode}\" in\n  number-of-files)\n    ls -1 | wc -l ;;\n  size)\n    du -h | cut -f1 ;;\n  *)\n    >&2 echo \"Usage: ./file-report.sh [size|number-of-files] [path]\"; exit 1 ;;\nesac\n\n"
  },
  {
    "path": "example-bashunit/src/ice_cream_price.sh",
    "content": "#!/bin/bash\n\nfunction main() {\n  numberOfPortions=$1\n  pricePerPortion=$(calculatePrice $numberOfPortions)\n  totalPrice=$(( numberOfPortions * pricePerPortion ))\n\n  echo \"Total $totalPrice\"\n}\n\nfunction calculatePrice() {\n  if [[ $numberOfPortions -lt 3 ]]; then\n    echo \"100\"\n  else\n    day=$(getDay)\n    if (( day % 2 )); then\n      echo \"80\"\n    else\n      echo \"100\"\n    fi\n  fi\n}\n\nfunction getDay() {\n  date -d '+%d'\n}\n\nif [[ \"${BASH_SOURCE[0]}\" == \"${0}\" ]]; then\n  main \"$@\"\nfi\n"
  },
  {
    "path": "example-bashunit/src/myfun.sh",
    "content": "#!/bin/bash\n\nfunction myfun() {\n  echo \"Hello!\"\n}\n"
  },
  {
    "path": "example-bashunit/test.sh",
    "content": "#!/bin/bash\n\nlib/bashunit tests\n"
  },
  {
    "path": "example-bashunit/tests/unit_test.sh",
    "content": "#!/bin/bash\n\n# set_up is called before executing each test function\nfunction set_up() {\n  source \"$(realpath \".\")/src/ice_cream_price.sh\"\n}\n\nfunction test_ice_cream_price_should_be_100_per_portion_for_low_quantities() {\n  result=$(main 1)\n\n  assert_successful_code\n  assert_equals \"${result}\" \"Total 100\"\n}\n\nfunction test_there_should_be_20_percent_discount_for_large_quantities_on_odd_days() {\n  function getDay() {\n    echo \"1\"\n  }\n\n  result=$(main 4)\n\n  assert_successful_code\n  assert_equals \"${result}\" \"Total 320\"\n}\n\nfunction test_there_should_be_no_discount_even_for_large_quantities_on_even_days() {\n  function getDay() {\n    echo \"2\"\n  }\n\n  result=$(main 4)\n\n  assert_successful_code\n  assert_equals \"${result}\" \"Total 400\"\n}\n"
  },
  {
    "path": "example-bats/.gitignore",
    "content": "lib\nbuild\n"
  },
  {
    "path": "example-bats/install.sh",
    "content": "#!/bin/bash\n\ncd \"$(dirname \"$0\")\"\nrm -rf lib\nmkdir lib\ncd lib\n\ncurl -L -O -J https://github.com/bats-core/bats-core/archive/v1.1.0.tar.gz\ntar -zxvf bats-core-1.1.0.tar.gz\nmv bats-core-1.1.0 bats-core\n\ncurl -L -O -J https://github.com/bats-core/bats-support/archive/v0.3.0.tar.gz\ntar -zxvf bats-support-0.3.0.tar.gz\nmv bats-support-0.3.0 bats-support\n\ncurl -L -O -J https://github.com/bats-core/bats-assert/archive/v2.0.0.tar.gz\ntar -zxvf bats-assert-2.0.0.tar.gz\nmv bats-assert-2.0.0 bats-assert\n\n"
  },
  {
    "path": "example-bats/readme.md",
    "content": "# Bats test drive\n\nBats is a well established contender in the scene of Bash testing frameworks. It has many features and provides a custom `@test` annotation to make tests easier to write.\n\n\n## Test format\n\n✔️ Test files are \"almost Bash\".\n\n⚠️ Bats only considers files with the `.bats` extension. This is not a huge problem, but the editor has to be adapted\na bit to offer syntax highlight and the usual features for these files.\n\n✔️ Also, tests are marked with a special `@test` annotation, which is not Bash syntax. It's is picked up by BATS which\ntranslates it into a test wrapper. Although it's special syntax, it makes really clean how tests can be defined:\n\n```bash\n@test \"ice cream price should be 100 per portion for low quantities\" {\n  run src/ice_cream_price.sh 1\n  assert_success\n  assert_output -p \"Total 100\"\n}\n```\n\nAs can be seen in this example, it also offers a special `run` construct to run the script under test and capture the results.\n\nFinally, although it's optional, BATS test files can have their own shebang line as well: `#!/usr/bin/env bats`.\n\n\n## Test Discovery\n\n✔️ By default it runs all test files in the given directory, or just a given file. It can also consider subdirectories\nwith the `--recursive flag`. With `--filter` one can specify a regex, and only tests with a matching name will be executed.\n\n\n## Assertions\n\n⚠️ It provides no assertions by default. Instead, simply if a command exits with a non-zero code, the test will fail:\n\n```bash\n@test \"addition using bc\" {\n  result=\"$(echo 2+2 | bc)\"\n  [ \"$result\" -eq 4 ]\n}\n```\n\nAll the examples promote this approach. In my opinion, it has two serious problems:\n\n- one have to be fluent in [Bash conditionals](https://tldp.org/LDP/abs/html/comparison-ops.html)\n  in order to work with it\n- the report lacks important information, like the expected and actual values\n\n```bash\n✗ addition using bc\n   (in test file test/test2.bats, line 8)\n   `[ \"$result\" -eq 4 ]' failed\n\n# what was the $result? :(\n```\n\n✔️ It has an extension called [bats-assert](https://github.com/bats-core/bats-assert) that provide assertions\nlike equals or partial matching on the output. They provide nice error messages too:\n\n```bash\nload '../lib/bats-support/load'\nload '../lib/bats-assert/load'\n\n@test \"using asserts\" {\n  run src/ice_cream_price.sh 4\n  assert_success\n  assert_output -p \"Total 500\"\n}\n\n ✗ using asserts\n   (from function `assert_output' in file test/../lib/bats-assert/src/assert_output.bash, line 186,\n    in test file test/asserts.bats, line 7)\n     `assert_output -p \"Total 500\"' failed\n\n   -- output does not contain substring --\n   substring : Total 500\n   output    : Total 400\n   --\n```\n\n⚠️ Most of the asserts provided by `bats-assert` are working directly with the return code or the output\nof the command executed with run. There are not too many general assertions besides `assert_equal`.\n\n\n## Custom assertions\n\n✔️ It's possible to create custom assertions, and also there are some dedicated constructs in the framework\nto support creating them:\n\n- With `load` shared test code (e.g. assertion functions) can be imported ([doc](https://github.com/bats-core/bats-core#load-share-common-code))\n- The [bats-support](https://github.com/bats-core/bats-support) library provides common functions for error reporting and output formatting.\n\n\n## Report\n\n⚠️ Report for test cases are not separated by test files. This is not a problem for small projects,\nbut with many test files it's a bit harder to relate a failing test to the test file where it's\ndefined:\n\n```\n ✓ this is the first test from the first test suite\n ✓ this is the second test from the first test suite\n ✓ this is the first test from the second test suite\n ✓ this is the second test from the second test suite\n```\n\n\n## Mocking\n\n✔️ Mocking is possible with all [three common techniques](https://github.com/dodie/testing-in-bash/tree/master/mocking):\n\n- alias\n- function export\n- PATH override\n\n\n## Unit testing scripts that set Bash options\n\n✔️ Bats itself can `source` scripts that set custom Bash options, like `-e` or `-u` without breaking test\nexecution or causing problems for the test framework.\n[[1](https://github.com/bats-core/bats-core/blob/15686b3e4cbcc00dd589e4a87e42975a0501e1f8/test/bats.bats#L325)],\n[[2](https://github.com/bats-core/bats-core/pull/26)].\n\n✔️ Because a non-zero exit code for any test command results in a failed test, I thought that all unit tested functions\nare inherently run with the `errexit` mode set. Luckily this is not the case. The `run` construct\nprovides a sandbox to the function under test.\n\n⚠️ Generally, this means that unit testing functions that does not use custom Bash options, or use\n`nounset` will work normally. However, for some reason when a function is exercised with the `run` construct the\n`-e` option is always forcefully is disabled. So, if you design a script that use\n`set -e` and unit test its functions with Bats, in the unit tests the `-e` will not apply and the function\nmight behave differently.\n\nFor more details, see the\n[example tests that exercise scripts that use custom Bash options](https://github.com/dodie/testing-in-bash/tree/master/example-bats/test/unit_test_strict.bats).\n\n\n## Activity\n\n⚠️ According to the project’s GitHub repository it's been around since 2011. The [original repository](https://github.com/sstephenson/bats)\nwas discontinued in 2016, but it was forked to the [bats-core organization](https://github.com/bats-core) to ensure the project's\nmaintenance and collect useful thirdparty libraries. Despite this transition, there's a huge number of old open issues and PRs.\n\n\n## Documentation\n\n⚠️ The bats-core project has good documentation, and seems to be up to date, but the thirdparty libraries, including bats-assert\nseems to lag behind. In many cases they link to old repos from before the transition to the bats-core organization, and documents that\nare not updated in the last couple of years. For example the documentation recommended installing bats-assert and its dependency\ninto the test folder, but that [did not work](https://github.com/bats-core/bats-core/issues/273) because BATS considered their files\nas test cases and tried to execute them.\n\n"
  },
  {
    "path": "example-bats/src/file-report.sh",
    "content": "#!/bin/bash\n\nmode=$1\npath=$2\n\nif [ -z $path  ]; then\n    echo \"Usage: ./file-report.sh [size|number-of-files] [path]\"; >&2\n    exit 1\nfi\n\ncd \"${path}\"\n\ncase \"${mode}\" in\n    number-of-files)\n        ls -1 | wc -l ;;\n    size)\n        du -h | cut -f1 ;;\n    *)\n        >&2 echo \"Usage: ./file-report.sh [size|number-of-files] [path]\"; exit 1 ;;\nesac\n\n"
  },
  {
    "path": "example-bats/src/ice_cream_price.sh",
    "content": "#!/bin/bash\n\nfunction main() {\n    numberOfPortions=$1\n    pricePerPortion=$(calculatePrice $numberOfPortions)\n    totalPrice=$(( $numberOfPortions * $pricePerPortion ))\n\n    echo \"Total $totalPrice\"\n}\n\nfunction calculatePrice() {\n    if [[ $numberOfPortions -lt 3 ]]; then\n        echo \"100\"\n    else\n        day=$(getDay)\n        if (( $day % 2 )); then\n            echo \"80\"\n        else\n            echo \"100\"\n        fi\n    fi\n}\n\nfunction getDay() {\n    date -d '+%d'\n}\n\nif [[ \"${BASH_SOURCE[0]}\" == \"${0}\" ]]\nthen\n  main $@\nfi\n"
  },
  {
    "path": "example-bats/src/myfun.sh",
    "content": "#!/bin/bash\nfunction myfun() {\n    echo \"Hello!\"\n}\n"
  },
  {
    "path": "example-bats/test/assertion_showcase.bats",
    "content": "\n# required for tests using bats-assert\nload '../lib/bats-support/load'\nload '../lib/bats-assert/load'\n\n# required for tests using a custom assert\nload util/customasserts\n\n@test \"skipped test\" {\n  skip \"This is broken\"\n  #...\n}\n\n@test \"asserting with conditionals\" {\n  run src/ice_cream_price.sh 1\n  [ \"$status\" -eq 0 ]\n  [ \"${lines[0]}\" = \"Total 100\" ]\n}\n\n@test \"asserting with bats-assert\" {\n  run src/ice_cream_price.sh 1\n  assert_success\n  assert_output -p \"Total 100\"\n}\n\n@test \"asserting with custom assertion\" {\n  run src/ice_cream_price.sh 1\n  custom_assert_equal \"${lines[0]}\" \"Total 100\"\n}\n\n"
  },
  {
    "path": "example-bats/test/e2e_test.bats",
    "content": "load '../lib/bats-support/load'\nload '../lib/bats-assert/load'\n\n@test \"e2e - ice cream price should be 100 per portion for low quantities\" {\n  run src/ice_cream_price.sh 1\n  assert_success\n  assert_output -p \"Total 100\"\n}\n\n@test \"e2e - there should be 20% discount for large quantities on odd days\" {\n  # Mocking date by adding a custom executable to the PATH\n  export DATE_MOCK_REPLY=\"1\"\n  export PATH=test/mocks:$PATH\n\n  run src/ice_cream_price.sh 4\n  assert_success\n  assert_output -p \"Total 320\"\n}\n\n@test \"e2e - there should be no discount even for large quantities on even days\" {\n  # Mocking date by adding a custom executable to the PATH\n  export DATE_MOCK_REPLY=\"2\"\n  export PATH=test/mocks:$PATH\n\n  run src/ice_cream_price.sh 4\n  assert_success\n  assert_output -p \"Total 400\"\n}\n\n"
  },
  {
    "path": "example-bats/test/mocks/date",
    "content": "#!/bin/bash\n\necho \"${DATE_MOCK_REPLY}\"\n\n"
  },
  {
    "path": "example-bats/test/setup_and_teardown.bats",
    "content": "load '../lib/bats-support/load'\nload '../lib/bats-assert/load'\n\n# Generate fresh example files for the tests\nsetup() {\n  rm -rf build/testdata\n  mkdir -p build/testdata\n\n  for i in {1..3}\n  do\n    echo \"Hello $i\" > \"build/testdata/${i}.txt\"\n  done\n}\n\n# Assert that the tests did not remove the example files\nteardown() {\n  run ls build/testdata -1\n  assert_success\n  assert_output --partial \"1.txt\"\n  assert_output --partial \"2.txt\"\n  assert_output --partial \"3.txt\"\n}\n\n@test \"number-of-files should return the number of files in the directory\" {\n  run src/file-report.sh number-of-files build/testdata\n  assert_success\n  assert_output \"3\"\n}\n\n@test \"size should return the total size of the files in the directory\" {\n  run src/file-report.sh size build/testdata\n  assert_success\n  assert_output \"16K\"\n}\n\n"
  },
  {
    "path": "example-bats/test/unit_test.bats",
    "content": "load '../lib/bats-support/load'\nload '../lib/bats-assert/load'\n\n@test \"ice cream price should be 100 per portion for low quantities\" {\n  source src/ice_cream_price.sh\n\n  run main 1\n\n  assert_success\n  assert_output -p \"Total 100\"\n}\n\n@test \"there should be 20% discount for large quantities on odd days\" {\n  # Mocking a function by defining an alias\n  shopt -s expand_aliases\n  alias \"getDay\"=\"echo '1'\"\n\n  source src/ice_cream_price.sh\n\n  run main 4\n\n  assert_success\n  assert_output -p \"Total 320\"\n}\n\n@test \"there should be no discount even for large quantities on even days\" {\n  source src/ice_cream_price.sh\n\n  # Mocking a function by exporting a function\n  function getDay() { echo \"2\"; }\n  export -f getDay\n  run main 4\n\n  assert_success\n  assert_output -p \"Total 400\"\n}\n"
  },
  {
    "path": "example-bats/test/unit_test_strict.bats",
    "content": "load '../lib/bats-support/load'\nload '../lib/bats-assert/load'\n\n@test \"e2e testing a script that relies on errexit\" {\n  # This is how the script behaves when executed directly\n\n  run ../strictmode/src/using_e.sh \"https://www.google.com\"\n  assert_success\n  assert_output -p \"200\"\n\n  run ../strictmode/src/using_e.sh \"https://www.google.com/nonexistent\"\n  assert_failure\n  assert_output -p \"\"\n}\n\n@test \"unit testing a script that relies on errexit should work the same - fails because -e is not applied\" {\n  # And this is how the script behaves when it is sourced\n  source ../strictmode/src/using_e.sh\n\n  run getResponseCode \"https://www.google.com\"\n  assert_success\n  assert_output -p \"200\"\n\n  run getResponseCode \"https://www.google.com/nonexistent\"\n  assert_failure\n  assert_output -p \"\"\n}\n\n@test \"e2e testing a script that relies on no errexit\" {\n  # This is how the script behaves when executed directly\n\n  run ../strictmode/src/using_no_e.sh \"https://www.google.com\"\n  assert_success\n  assert_output -p \"200\"\n\n  run ../strictmode/src/using_no_e.sh \"https://www.google.com/nonexistent\"\n  assert_success\n  assert_output -p \"404\"\n}\n\n@test \"unit testing a script that relies on no errexit should work the same\" {\n  # And this is how the script behaves when it is sourced\n\n  source ../strictmode/src/using_no_e.sh\n\n  run getResponseCode \"https://www.google.com\"\n  assert_success\n  assert_output -p \"200\"\n\n  run getResponseCode \"https://www.google.com/nonexistent\"\n  assert_success\n  assert_output -p \"404\"\n}\n\n@test \"e2e testing a script that relies on nounset\" {\n  # This is how the script behaves when executed directly\n\n  run ../strictmode/src/using_u.sh Hello\n  assert_success\n  assert_output -p \"The parameter is: Hello\"\n\n  run ../strictmode/src/using_u.sh\n  assert_failure\n  assert_output -p \"\"\n}\n\n@test \"unit testing a script that relies on nounset should work the same\" {\n  # And this is how the script behaves when it is sourced\n\n  source ../strictmode/src/using_u.sh\n\n  run main Hello\n  assert_success\n  assert_output -p \"The parameter is: Hello\"\n\n  run main\n  assert_failure\n  assert_output -p \"\"\n}\n\n@test \"e2e testing a script that relies on no nounset\" {\n  # This is how the script behaves when executed directly\n\n  run ../strictmode/src/using_no_u.sh Hello\n  assert_success\n  assert_output -p \"YES\"\n\n  run ../strictmode/src/using_no_u.sh\n  assert_success\n  assert_output -p \"NO\"\n}\n\n@test \"unit testing a script that relies on no nounset should work the same\" {\n  # And this is how the script behaves when it is sourced\n\n  source ../strictmode/src/using_no_u.sh\n\n  run main Hello\n  assert_success\n  assert_output -p \"YES\"\n\n  run main\n  assert_success\n  assert_output -p \"NO\"\n}\n\n"
  },
  {
    "path": "example-bats/test/util/customasserts.bash",
    "content": "# based on https://github.com/bats-core/bats-assert/blob/master/src/assert_equal.bash\nfunction custom_assert_equal() {\n  if [[ $1 != \"$2\" ]]; then\n    batslib_print_kv_single_or_multi 8 \\\n    'expected' \"$2\" \\\n    'actual'   \"$1\" \\\n    | batslib_decorate 'values are really not the same' \\\n    | fail\n  fi\n}\n"
  },
  {
    "path": "example-bats/test.sh",
    "content": "#!/bin/bash\n\n# Set project root as the working directory\ncd \"$(dirname \"$0\")\"\n\n# Run tests\nlib/bats-core/bin/bats -r test\n\n"
  },
  {
    "path": "example-shellspec/.gitignore",
    "content": "lib\nbuild\n"
  },
  {
    "path": "example-shellspec/.shellspec",
    "content": "--require spec_helper\n\n## Default kcov (coverage) options\n# --kcov-options \"--include-path=. --path-strip-level=1\"\n# --kcov-options \"--include-pattern=.sh\"\n# --kcov-options \"--exclude-pattern=/.shellspec,/spec/,/coverage/,/report/\"\n\n## Example: Include script \"myprog\" with no extension\n# --kcov-options \"--include-pattern=.sh,myprog\"\n\n## Example: Only specified files/directories\n# --kcov-options \"--include-pattern=myprog,/lib/\"\n"
  },
  {
    "path": "example-shellspec/install.sh",
    "content": "#!/bin/bash\n\ncd \"$(dirname \"$0\")\"\nrm -rf lib\nmkdir lib\ncd lib\n\ncurl -L -O -J https://github.com/shellspec/shellspec/archive/0.23.0.tar.gz\ntar -zxvf shellspec-0.23.0.tar.gz\nmv shellspec-0.23.0 shellspec\n\n"
  },
  {
    "path": "example-shellspec/readme.md",
    "content": "# ShellSpec test drive\n\nShellSpec is the latest and the most featureful test framework I've found for Bash. It has sexy syntax and new releases are coming up\nfrequently.\n\nHowever, it's also pretty new, first released in 2019. I expect some rough edges to be polished and some breaking API changes here.\nAlso, while I really like its DSL, one has to decide if this is really his thing or not. I for one prefer BDD style for more end-user facing tests only, and I like it better if the unit tests are more similar to production code, so they can be used as concrete examples.\n\n\n## Test format\n\nThere's a specific DSL built into the framework to support BDD style tests:\n\n```bash\nDescribe 'lib.sh'\n  Include ./lib.sh # include other script\n\n  Describe 'calc()'\n    It 'calculates'\n      When call calc 1 + 1\n      The output should eq 2\n    End\n  End\nEnd\n```\n\n✔️ What's really interesting is that despite this DSL, files are simple Bash files. It's possible mix simple\nBash statements and functions with the DSL, so even if the DSL lacks certain features, it's usually not a limitation.\n\n⚠️ This language looks really neat and extensible, but on the other hand it's something that one have to get used to\nin order to write tests efficiently.\n\n⚠️ Also, some features of this DSL are not on par what's achievable with Bash. For example, [currently it's only possible\nto have beforeAll and afterAll functions with Bash snippets](https://github.com/shellspec/shellspec/issues/7).\n\n\n## Test Discovery\n\n✔️ The test runner considers all files as tests that are in the spec folder and their name contains \"spec\".\nA subset of the tests can also be executed with filters, or it’s even possible to focus on a single test case.\n\n✔️ It also expects a specific project structure, and comes with an initializer to generate that.\n\n\n## Assertions\n\n✔️ The framework provide many customizable/combinable assertions via it's DSL, and the failures generated by these assertions\ncontain all necessary context information.\n\n⚠️ Assertions can not be used in all places. For example I couldn't use them in after hooks when I wanted to check a post condition\nfor each test. Althoug I could work around the issue with Bash snippets, it made the tests a bit harder to read.\n\n\n## Custom assertions\n\n✔️ The DSL itself is extensible with custom matchers.\n\n⚠️ Compared with other frameworks where it's simply done by defining a function it's less straightforward.\n\nIf all else fails Bash functions can be directly called from the tests, and with that custom assertions can be implemented.\nHowever it might have the downside that the basy code has to interface with the DSL, which might be not so convenient,\nand the test code will be a mix of DSL and bash. \n\n\n## Mocking\n\n✔️ Mocking is possible with all [three common techniques](https://github.com/dodie/testing-in-bash/tree/master/mocking):\n\n- alias\n- function export\n- PATH override\n\n\n## Activity\n\n⚠️ The project is really new, it's been around since 2019. This means that the project might have some rough edges,\nas some features are not polished yet. (E.g. the beforeAll functionality [was removed at one point](https://github.com/shellspec/shellspec/issues/7) because it was not ready yet.)\n\n✔️ The project is very active, and had quite some releases over the past year. The author is active answering issues and\ntaking care of PRs.\n\n\n## Documentation\n\n✔️ The project has [detailed examples](https://github.com/shellspec/shellspec/tree/master/sample/spec) on how to use the framework.\nAlso, it has a [getting started guide](https://github.com/shellspec/shellspec/) and [website](https://shellspec.info/) covering the\nbasics.\n\n✔️ The thing I like the most is the interactive help. Although it's a bit confusing at first what assertions are available in the DSL, the tool helps with interactive help when it encounters an invalid expression.\n\nFor example, I got the following error message when I tried to use the non-existent \"contain\" keyword:\n\n```bash\n#\"The output should contain 'Total'\"\n\nUnknown word 'contain' after should verb. The correct word is one of the following.\n            \n     be, end, equal, eq, has, include, match, start, satisfy\n```\n\n"
  },
  {
    "path": "example-shellspec/spec/assertion_showcase_spec.sh",
    "content": "Describe 'assertion showcase'\n\n  # Example for Skip\n  It 'should display 100 for the price of a portion for low quantities'\n    Skip 'this test is broken'\n    When call src/ice_cream_price.sh 1\n    The output should equal 'Total 101'\n  End\n\n  # Example for a simple 'equals' assertion\n  It 'should display 100 for the price of a portion for low quantities'\n    When call src/ice_cream_price.sh 1\n    The output should equal 'Total 100'\n  End\n\n  # Example for a different assertion\n  It 'should display \"total\"'\n    When call src/ice_cream_price.sh 1\n    The output should start with 'Total'\n  End\n\n  # Example for a custom assertion\n  It 'should use my custom assertion'\n    When call src/ice_cream_price.sh 1\n    The output should regexp '.*Total.*'\n  End\nEnd\n"
  },
  {
    "path": "example-shellspec/spec/e2e_test_spec.sh",
    "content": "Describe 'ice_cream_price.sh'\n\n  It 'should display 100 for the price of a portion for low quantities'\n    When call src/ice_cream_price.sh 1\n    The status should be success\n    The output should equal 'Total 100'\n  End\n\n  It 'should give 20% discount for large quantities on odd days'\n    # Mocking date by adding a custom executable to the PATH\n    export DATE_MOCK_REPLY=\"1\"\n    export PATH=spec/mocks:$PATH\n\n    When call src/ice_cream_price.sh 4\n    The status should be success\n    The output should equal 'Total 320'\n  End\n\n  It 'should give no discount even for large quantities on even days'\n    # Mocking date by adding a custom executable to the PATH\n    export DATE_MOCK_REPLY=\"2\"\n    export PATH=spec/mocks:$PATH\n\n    When call src/ice_cream_price.sh 4\n    The status should be success\n    The output should equal 'Total 400'\n  End\nEnd\n\n"
  },
  {
    "path": "example-shellspec/spec/mocks/date",
    "content": "#!/bin/bash\n\necho \"${DATE_MOCK_REPLY}\"\n\n"
  },
  {
    "path": "example-shellspec/spec/setup_and_teardown_spec.sh",
    "content": "# Create test data directories\nfunction setup_suite() {\n  mkdir -p build/testdata\n}\nsetup_suite\n\nDescribe 'file-report.sh'\n  # Generate fresh example files for the tests\n  setup() {\n    rm -rf build/testdata\n    mkdir -p build/testdata\n\n    for i in {1..3}\n    do\n      echo \"Hello $i\" > \"build/testdata/${i}.txt\"\n    done\n  }\n\n  # Assert that the tests did not remove the example files\n  teardown() {\n    local result\n    result=$(ls build/testdata -1)\n    [[ 0 -eq $? ]] # assert success\n    [[ \"${result}\" == *\"1.txt\"* ]]\n    [[ \"${result}\" == *\"2.txt\"* ]]\n    [[ \"${result}\" == *\"3.txt\"* ]]\n  }\n\n  Before 'setup'\n  After 'teardown'\n\n  It 'should return the number of files in the directory'\n    When call src/file-report.sh number-of-files build/testdata\n    The status should be success\n    The output should equal '3'\n  End\n\n  It 'should return the total size of the files in the directory'\n    When call src/file-report.sh size build/testdata\n    The status should be success\n    The output should equal '16K'\n  End\nEnd\n\n# Remove test data directories\nfunction teardown_suite() {\n  rm -rf build/testdata\n}\nteardown_suite\n\n"
  },
  {
    "path": "example-shellspec/spec/spec_helper.sh",
    "content": "#shellcheck shell=sh\n\n# set -eu\n\nshellspec_spec_helper_configure() {\n  shellspec_import 'support/custom_matcher'\n}\n"
  },
  {
    "path": "example-shellspec/spec/support/custom_matcher.sh",
    "content": "#shellcheck shell=sh\n\n# imported by \"spec_helper.sh\"\n\nshellspec_syntax 'shellspec_matcher_regexp'\n\nshellspec_matcher_regexp() {\n  shellspec_matcher__match() {\n    SHELLSPEC_EXPECT=\"$1\"\n    [ \"${SHELLSPEC_SUBJECT+x}\" ] || return 1\n    expr \"$SHELLSPEC_SUBJECT\" : \"$SHELLSPEC_EXPECT\" > /dev/null || return 1\n    return 0\n  }\n\n  # Message when the matcher fails with \"should\"\n  shellspec_matcher__failure_message() {\n    shellspec_putsn \"expected: $1 match $2\"\n  }\n\n  # Message when the matcher fails with \"should not\"\n  shellspec_matcher__failure_message_when_negated() {\n    shellspec_putsn \"expected: $1 not match $2\"\n  }\n\n  # checking for parameter count\n  shellspec_syntax_param count [ $# -eq 1 ] || return 0\n  shellspec_matcher_do_match \"$@\"\n}\n"
  },
  {
    "path": "example-shellspec/spec/unit_test_spec.sh",
    "content": "Describe 'ice_cream_price.sh'\n  Include src/ice_cream_price.sh\n\n  It 'should display 100 for the price of a portion for low quantities'\n    When call main 1\n    The output should equal 'Total 100'\n  End\n\n  It 'should give 20% discount for large quantities on odd days'\n    # Mocking a function by defining an alias\n    shopt -s expand_aliases\n    alias \"getDay\"=\"echo '1'\"\n\n    When call main 4\n    The output should equal 'Total 320'\n  End\n\n  It 'should give no discount even for large quantities on even days'\n    # Mocking a function by exporting a function\n    function getDay() { echo \"2\"; }\n    export -f getDay\n\n    When call main 4\n    The output should equal 'Total 400'\n  End\nEnd\n\n"
  },
  {
    "path": "example-shellspec/src/file-report.sh",
    "content": "#!/bin/bash\n\nmode=$1\npath=$2\n\nif [ -z $path  ]; then\n    echo \"Usage: ./file-report.sh [size|number-of-files] [path]\"; >&2\n    exit 1\nfi\n\ncd \"${path}\"\n\ncase \"${mode}\" in\n    number-of-files)\n        ls -1 | wc -l ;;\n    size)\n        du -h | cut -f1 ;;\n    *)\n        >&2 echo \"Usage: ./file-report.sh [size|number-of-files] [path]\"; exit 1 ;;\nesac\n\n"
  },
  {
    "path": "example-shellspec/src/ice_cream_price.sh",
    "content": "#!/bin/bash\n\nfunction main() {\n    numberOfPortions=$1\n    pricePerPortion=$(calculatePrice $numberOfPortions)\n    totalPrice=$(( $numberOfPortions * $pricePerPortion ))\n\n    echo \"Total $totalPrice\"\n}\n\nfunction calculatePrice() {\n    if [[ $numberOfPortions -lt 3 ]]; then\n        echo \"100\"\n    else\n        day=$(getDay)\n        if (( $day % 2 )); then\n            echo \"80\"\n        else\n            echo \"100\"\n        fi\n    fi\n}\n\nfunction getDay() {\n    date -d '+%d'\n}\n\nif [[ \"${BASH_SOURCE[0]}\" == \"${0}\" ]]\nthen\n  main $@\nfi\n"
  },
  {
    "path": "example-shellspec/src/myfun.sh",
    "content": "#!/bin/bash\nfunction myfun() {\n    echo \"Hello!\"\n}\n"
  },
  {
    "path": "example-shellspec/test.sh",
    "content": "#!/bin/bash\n\n# Set project root as the working directory\ncd \"$(dirname \"$0\")\"\n\n# Run tests\nlib/shellspec/shellspec\n\n"
  },
  {
    "path": "example-shpec/.gitignore",
    "content": "lib"
  },
  {
    "path": "example-shpec/install.sh",
    "content": "#!/bin/bash\n\ncd \"$(dirname \"$0\")\"\nrm -rf lib\nmkdir lib\ncd lib\nVERSION=0.3.1\ncurl -sL https://github.com/rylnd/shpec/archive/${VERSION}.tar.gz | tar zx --strip-components=1\n\n"
  },
  {
    "path": "example-shpec/readme.md",
    "content": "# Shpec test drive\n\nShpec is a quite mature testing framework with it's first release from 2013.\nIts DSL is looks like a simpler version of\n[shellspec](https://github.com/dodie/testing-in-bash/tree/master/example-shellspec)'s DSL.\n\nShpec tries to not implement things that can be done as easily in bash it self.\n\n\n## Test format\n\nThere's a specific DSL built into the framework to support BDD style tests:\n\n```bash\ndescribe \"ice_cream_price.sh (e2e)\"\n  it \"should display 100 for the price of a portion for low quantities\"\n    assert equal \"$(src/ice_cream_price.sh 1)\" \"Total 100\"\n  end\nend\n```\n\n✔️ What's really interesting is that despite this DSL, files are simple Bash files. It's possible mix simple\nBash statements and functions with the DSL, so even if the DSL lacks certain features, it's usually not a limitation.\n\n⚠️ This language looks really neat and extensible, but on the other hand it's something that one have to get used to\nin order to write tests efficiently.\n\n\n## Test Discovery\n\n✔️ The test runner considers all files as tests that are in the shpec folder and their name ends in `_spec.sh`.\nBy default all test cases are executed, by calling shpec with a shpec-file as the first parameter, only this case is tested.\n\n✔️ It also expects a specific project structure, test files must be in the schema of `./shpec/**/*_shpec.sh`, \ncustom matcher should be in `./shpec/matchers/` every thing else is up to you.\n\n\n## Assertions\n\n✔️ The framework provide [many](https://github.com/rylnd/shpec#matchers) customizable/combinable assertions\nvia it's DSL, and the failures generated by these assertions contain all necessary context information.\n\n## Custom assertions\n\n✔️ The DSL itself is extensible with custom matchers.\n\nYou can define custom matchers simply as functions, but in order to make them reusable, you have to put them in an extra file under `shpec/matchers/`.\n\n⚠️ If all else fails Bash functions can be directly called from the tests, and with that custom assertions can be implemented.\nHowever it might have the downside that the bashy code has to interface with the DSL, which might be not so convenient,\nand the test code will be a mix of DSL and bash. \n\n\n## Mocking\n\n✔️ Mocking is possible with all [three common techniques](https://github.com/dodie/testing-in-bash/tree/master/mocking):\n\n- alias\n- function export\n- PATH override\n\n✔️ Also there is [stubbing](https://github.com/rylnd/shpec#stubbing) build in.\n\n## Activity\n\n⚠️ The project is not in development right now, is seems to be in a \"done enough\" state.\n\n\n✔️ The project had quite some releases over the past year. The author is active answering issues and\ntaking care of PRs.\n\n\n## Documentation\n\n✔️ The project test's itself, the tests are not only functional but are written to be good [examples](https://github.com/rylnd/shpec/blob/master/shpec/shpec_shpec.sh).\nAlso, the [Readme](https://github.com/rylnd/shpec/) gives a good overview on how to use and where to read more.\n"
  },
  {
    "path": "example-shpec/shpec/e2e_test_shpec.sh",
    "content": "#!/usr/bin/env bash\nexport DATE_MOCK_REPLY\nPATH=\"$PWD/shpec/mocks:$PATH\"\nDATE_MOCK_REPLY=1\n\ndescribe \"ice_cream_price.sh (e2e)\"\n  it \"should display 100 for the price of a portion for low quantities\"\n    assert equal \"$(src/ice_cream_price.sh 1)\" \"Total 100\"\n  end\n\n  it \"should not fail calculating\"\n    src/ice_cream_price.sh 1 >/dev/null\n    assert equal $? 0\n  end\n\n  it 'should give 20% discount for large quantities on odd days'\n    DATE_MOCK_REPLY=1\n    assert equal \"$(src/ice_cream_price.sh 5)\" \"Total 400\"\n  end\n  it 'should give no discount even for large quantities on even days'\n    DATE_MOCK_REPLY=2\n    assert equal \"$(src/ice_cream_price.sh 5)\" \"Total 500\"\n  end\nend\n"
  },
  {
    "path": "example-shpec/shpec/matchers/ice_price.sh",
    "content": "#!/usr/bin/env bash\nice_price(){\n    actual=\"$1\"\n    expected_num=\"$2\"\n    assert equal \"$actual\" \"Total $expected_num\"\n}"
  },
  {
    "path": "example-shpec/shpec/mocks/date",
    "content": "#!/bin/bash\n\necho \"${DATE_MOCK_REPLY}\"\n\n"
  },
  {
    "path": "example-shpec/shpec/showcase_shpec.sh",
    "content": "#!/usr/bin/env bash\ndescribe 'assertion showcase'\n\n # Example for a simple 'equals' assertion\n it 'should display 100 for the price of a portion for low quantities'\n   assert equal \"$(src/ice_cream_price.sh 1)\" 'Total 100'\n end\n\n # Example for a different assertion\n it 'should display \"Total\"'\n   assert glob \"$(src/ice_cream_price.sh 1)\" 'Total*'\n end\n\n # Example for a custom assertion\n it 'should use my custom assertion'\n   assert ice_price \"$(src/ice_cream_price.sh 1)\" 100\n end\nend\n"
  },
  {
    "path": "example-shpec/shpec/unit_test_shpec.sh",
    "content": "#!/usr/bin/env bash\n. ./src/ice_cream_price.sh\n\ndescribe \"ice_cream_price.sh (unit)\"\n  it \"should display 100 for the price of a portion for low quantities\"\n    assert equal \"$(main 1)\" \"Total 100\"\n  end\n\n  it \"should not fail calculating\"\n    main 1 >/dev/null\n    assert equal $? 0\n  end\n\n  it 'should give 20% discount for large quantities on odd days'\n    stub_command getDay \"echo 1\"\n    assert equal \"$(main 5)\" \"Total 400\"\n  end\n  \n  it 'should give no discount even for large quantities on even days'\n    stub_command getDay \"echo 2\"\n   assert equal \"$(main 5)\" \"Total 500\"\n  end\nend\n"
  },
  {
    "path": "example-shpec/src/file-report.sh",
    "content": "#!/bin/bash\n\nmode=$1\npath=$2\n\nif [ -z $path  ]; then\n    echo \"Usage: ./file-report.sh [size|number-of-files] [path]\"; >&2\n    exit 1\nfi\n\ncd \"${path}\"\n\ncase \"${mode}\" in\n    number-of-files)\n        ls -1 | wc -l ;;\n    size)\n        du -h | cut -f1 ;;\n    *)\n        >&2 echo \"Usage: ./file-report.sh [size|number-of-files] [path]\"; exit 1 ;;\nesac\n\n"
  },
  {
    "path": "example-shpec/src/ice_cream_price.sh",
    "content": "#!/bin/bash\n\nfunction main() {\n    numberOfPortions=$1\n    pricePerPortion=$(calculatePrice $numberOfPortions)\n    totalPrice=$(( $numberOfPortions * $pricePerPortion ))\n\n    echo \"Total $totalPrice\"\n}\n\nfunction calculatePrice() {\n    if [[ $numberOfPortions -lt 3 ]]; then\n        echo \"100\"\n    else\n        day=$(getDay)\n        if (( $day % 2 )); then\n            echo \"80\"\n        else\n            echo \"100\"\n        fi\n    fi\n}\n\nfunction getDay() {\n    date '+%d'\n}\n\nif [[ \"${BASH_SOURCE[0]}\" == \"${0}\" ]]\nthen\n  main $@\nfi\n"
  },
  {
    "path": "example-shpec/src/myfun.sh",
    "content": "#!/bin/bash\nfunction myfun() {\n    echo \"Hello!\"\n}\n"
  },
  {
    "path": "example-shpec/test.sh",
    "content": "#!/bin/bash\n\n# Set project root as the working directory\ncd \"$(dirname \"$0\")\"\n\n# Run tests\nlib/bin/shpec\n\n"
  },
  {
    "path": "example-shunit2/.gitignore",
    "content": "lib\nbuild\n"
  },
  {
    "path": "example-shunit2/install.sh",
    "content": "#!/bin/bash\n\ncd \"$(dirname \"$0\")\"\nrm -rf lib\nmkdir lib\ncd lib\n\ncurl -L -O -J https://github.com/kward/shunit2/archive/v2.1.8.tar.gz\ntar -zxvf shunit2-2.1.8.tar.gz\nmv shunit2-2.1.8 shunit2\n\n"
  },
  {
    "path": "example-shunit2/readme.md",
    "content": "# shUnit2 test drive\n\nshUnit2 is the great-grandmother of all bash test frameworks. I really like that the test files are plain old Bash files,\nand that it has lot's of assertions by default. However, regarding other features it seems to be lagging behind it's competitors.\n\nIf you are after a testing framework where you can write tests in pure Bash scripts, make sure to also check [bash_unit](https://github.com/dodie/testing-in-bash/blob/master/example-bash_unit/).\n\n\n## Test format\n\n✔️ Test files are simple Bash scripts that one can execute. Functions are picked up by the test framework based on\na naming convention, and overall the look and feel of a test file is very similar to JUnit.\n\n⚠️ What I personally don't like is that the end of each test file shunit2 has to be sourced to do it's thing.\nThis forces you to either put its executable to the PATH or some environment variable, or hardcode its path into\neach of the tests.\n\n```bash\ntestEquality() {\n  assertEquals 1 1\n}\n\n. ./shunit2\n```\n\n\n## Test Discovery\n\n⚠️ Because the framework is invoked from each individual test file, it also means that there's no such thing as test\ndiscovery in shunit2. If you'd like to run your whole test suite, constituting multiple test files at once, you have\nto create some sort of custom launch mechanism that discovers and starts the test files.\n\nIt's easy to get up and running with a [simple wrapper](https://github.com/dodie/testing-in-bash/blob/master/example-shunit2/test.sh),\nbut things can easily get more complicated if you'd like to support proper test file discovery including subdirectories, partial execution, and executing all test files regardless of failures but still emit appropriate status code to indicate failures.\n\nRelated: [kward/shunit/issues/52](https://github.com/kward/shunit2/issues/52)\n\nIt has the concept of a test suite, but only in a single file. It can\n[take test function names as an argument](https://github.com/kward/shunit2#-running-specific-tests-from-the-command-line)\nto focus on some dedicated tests and only execute those. It does not support wildcard matching. \n\n\n## Assertions\n\n✔️ Among the frameworks I've checked, shUnit2 has the most assertions available by default:\n\n- `assertEquals`\n- `assertNotEquals`\n- `assertContains`\n- `assertNotContains`\n- `assertNull`\n- `assertNotNull`\n- `assertTrue`\n- `assertFalse`\n\n(In fact, there might be a bit too many of them, some providing similar functionalities like `assertEquals` and `assertSame`.)\n\n✔️ They can also take optional messages that will be displayed upon failures, which is quite nice.\n\n⚠️ One problem I found with these assertions is that by default they don't report the line number where the failure happened.\nTo include line numbers, the framework provide [macros](https://github.com/kward/shunit2#-including-line-numbers-in-asserts-macros)\ninstead of these assertions:\n\n```bash\n${_ASSERT_EQUALS_} '\"not equal\"' 2 2\n```\n\nUsing macros involve quite some line noise, and it's also a bit problematic that with this test files might become mixed,\nusing both assertion functions and macros for the same reason.\n\n\n## Custom assertions\n\n✔️ It’s possible to [create new assertions](https://github.com/kward/shunit2/wiki/Cookbook) by building functions on top of\nthe existing assertions, although there's no dedicated no dedicated lower-level API to support it.\nCustom assertions can be sourced from an external file.\n\n\n## Skip tests\n\n✔️ shUnit2 has an unique approach to skipping tests: instead of skipping whole test cases,\nhere it's possible to skip certain assertions:\n\n```bash\nassertEquals 1 1 # This assertion will run\nstartSkipping\nassertEquals 1 1 # This assertion will be skipped\nendSkipping\nassertEquals 1 1 # This assertion will also run\n```\n\nIf the test starts with `startsSkipping`, and there are no `stopSkipping` the whole test will be skipped.\n\nThis can be handy to temporarily disable some faulty assertions in a test.\n\n⚠️ From a reporting point of view however, things are a bit lacking compared to other frameworks.\nSkipping can't take an optional argument, so it's not easy to tell why it's skipped. Also, in the report\nthere's no distinction between passed and skipped tests, the report will only mention the number of\nskipped assertions at the end.\n\n```bash\ntestEquality1\ntestEquality2\n\nRan 2 tests.\n\nOK (skipped=1)\n\n# Which one was skipped? :(\n```\n\n\n## Mocking\n\n⚠️ From the [three common techniques](https://github.com/dodie/testing-in-bash/tree/master/mocking), mocking is only working\nby using aliases or PATH override, but [not with function exports](https://github.com/dodie/testing-in-bash/blob/master/example-shunit2/test/unit_test.sh#L25-L38). This is a bit unfortunate since this technique seems to be the most versatile.\n\n\n## Activity\n\n⚠️ The project is around since 2007. Between 2011 and 2018 there were no releases, but the project seems to be awakening since then.\naccept PRs again since 2018. There's a huge number of old open issues and PRs.\n\n\n## Documentation\n\n✔️ The [example tests](https://github.com/kward/shunit2/tree/master/examples) make it very clear how to use the framework,\nand the project's [readme](https://github.com/kward/shunit2/) does not leave out a single feature.\n"
  },
  {
    "path": "example-shunit2/src/file-report.sh",
    "content": "#!/bin/bash\n\nmode=$1\npath=$2\n\nif [ -z $path  ]; then\n    echo \"Usage: ./file-report.sh [size|number-of-files] [path]\"; >&2\n    exit 1\nfi\n\ncd \"${path}\"\n\ncase \"${mode}\" in\n    number-of-files)\n        ls -1 | wc -l ;;\n    size)\n        du -h | cut -f1 ;;\n    *)\n        >&2 echo \"Usage: ./file-report.sh [size|number-of-files] [path]\"; exit 1 ;;\nesac\n\n"
  },
  {
    "path": "example-shunit2/src/ice_cream_price.sh",
    "content": "#!/bin/bash\n\nfunction main() {\n    numberOfPortions=$1\n    pricePerPortion=$(calculatePrice $numberOfPortions)\n    totalPrice=$(( $numberOfPortions * $pricePerPortion ))\n\n    echo \"Total $totalPrice\"\n}\n\nfunction calculatePrice() {\n    if [[ $numberOfPortions -lt 3 ]]; then\n        echo \"100\"\n    else\n        day=$(getDay)\n        if (( $day % 2 )); then\n            echo \"80\"\n        else\n            echo \"100\"\n        fi\n    fi\n}\n\nfunction getDay() {\n    date -d '+%d'\n}\n\nif [[ \"${BASH_SOURCE[0]}\" == \"${0}\" ]]\nthen\n  main $@\nfi\n"
  },
  {
    "path": "example-shunit2/src/myfun.sh",
    "content": "#!/bin/bash\nfunction myfun() {\n    echo \"Hello!\"\n}\n"
  },
  {
    "path": "example-shunit2/test/assertion_showcase.sh",
    "content": "#!/bin/bash\n\n# This test has a nice assertion...\ntest_equality1() {\n  assertEquals \"not equal\" 2 2\n}\n\n# ... but only this one reports line numbers\ntest_equality_2() {\n  ${_ASSERT_EQUALS_} '\"not equal\"' 2 2\n}\n\nfunction test_skipped() {\n  startSkipping\n  # This is broken\n  #...\n}\n\nfunction test_partially_skipped() {\n  # this is okay\n  assertEquals \"not equal\" 2 2\n  startSkipping\n  # But this is broken\n  assertEquals \"not equal\" 1 2\n}\n\nassertCustomEquals() {\n  msg=''\n  if [ $# -eq 3 ]; then\n    msg=\"$1 \"\n    shift\n  fi\n  expected=$1\n  actual=$2\n\n  if [[ \"${expected}\" != \"${actual}\" ]]; then\n    fail \"${msg}expected '${expected}' but got '${actual}'\"\n  fi\n}\n\ntestWithCustomAssert() {\n  expected='shell'\n  actual='shell'\n  msg=\"This should be equal\"\n  assertCustomEquals \"${msg}\" \"${expected}\" \"${actual}\"\n}\n\n# Load shUnit2.\n. lib/shunit2/shunit2\n"
  },
  {
    "path": "example-shunit2/test/e2e_test.sh",
    "content": "#!/bin/bash\n \n\nfunction test_ice_cream_price_should_be_100_per_portion_for_low_quantities() {\n  result=$(src/ice_cream_price.sh 1)\n  assertEquals $? 0 # assert success\n  assertEquals \"${result}\" \"Total 100\"\n}\n\nfunction test_there_should_be_20_percent_discount_for_large_quantities_on_odd_days() {\n  # Mocking date by adding a custom executable to the PATH\n  export DATE_MOCK_REPLY=\"1\"\n  export PATH=test/mocks:$PATH\n\n  result=$(src/ice_cream_price.sh 4)\n  assertEquals $? 0 # assert success\n  assertEquals \"${result}\" \"Total 320\"\n}\n\nfunction test_there_should_be_no_discount_even_for_large_quantities_on_even_days() {\n  # Mocking date by adding a custom executable to the PATH\n  export DATE_MOCK_REPLY=\"2\"\n  export PATH=test/mocks:$PATH\n\n  result=$(src/ice_cream_price.sh 4)\n  assertEquals $? 0 # assert success\n  assertEquals \"${result}\" \"Total 400\"\n}\n\n. lib/shunit2/shunit2\n"
  },
  {
    "path": "example-shunit2/test/mocks/date",
    "content": "#!/bin/bash\n\necho \"${DATE_MOCK_REPLY}\"\n\n"
  },
  {
    "path": "example-shunit2/test/setup_and_teardown.sh",
    "content": "#!/bin/bash\n\n# Create test data directories\nfunction oneTimeSetUp() {\n  mkdir -p build/testdata\n}\n\n# Remove test data directories\nfunction oneTimeTearDown() {\n  rm -rf build/testdata\n}\n\n# Generate fresh example files for the tests\nfunction setUp() {\n  rm -rf build/testdata/*\n\n  for i in {1..3}\n  do\n    echo \"Hello $i\" > \"build/testdata/${i}.txt\"\n  done\n}\n\n# Assert that the tests did not remove the example files\nfunction tearDown() {\n  # Here I've tried to assert on some post conditions for each\n  # test, but I've bumped into a bug that prevented it.\n  # https://github.com/kward/shunit2/issues/112\n  # oneTimeTearDown()/tearDown() is being called an additional time at the end of execution\n\n  startSkipping\n  result=$(ls build/testdata -1)\n  assertEquals 0 $? # assert success\n  assertContains \"1.txt\" \"${result}\"\n  assertContains \"2.txt\" \"${result}\"\n  assertContains \"3.txt\" \"${result}\"\n}\n\nfunction test_number-of-files_should_return_the_number_of_files_in_the_directory() {\n  result=$(src/file-report.sh number-of-files build/testdata)\n  assertEquals 0 $?\n  assertEquals 3 \"${result}\"\n}\n\nfunction test_size_should_return_the_total_size_of_the_files_in_the_directory() {\n  result=$(src/file-report.sh size build/testdata)\n  assertEquals 0 $?\n  assertEquals \"16K\" \"${result}\"\n}\n\n. lib/shunit2/shunit2\n\n"
  },
  {
    "path": "example-shunit2/test/unit_test.sh",
    "content": "#!/bin/bash\n\nfunction test_ice_cream_price_should_be_100_per_portion_for_low_quantities() {\n  source src/ice_cream_price.sh\n\n  result=$(main 1)\n\n  assertEquals 0 $?\n  assertContains \"${result}\" \"Total 100\"\n}\n\nfunction test_there_should_be_20_percent_discount_for_large_quantities_on_odd_days() {\n  # Mocking a function by defining an alias\n  shopt -s expand_aliases\n  alias \"getDay\"=\"echo '1'\"\n\n  source src/ice_cream_price.sh\n\n  result=$(main 4)\n\n  assertEquals 0 $?\n  assertContains \"${result}\" \"Total 320\"\n}\n\nfunction test_there_should_be_no_discount_even_for_large_quantities_on_even_days() {\n  # Mocking by exporting functions does not work for shunit2\n  startSkipping\n\n  source src/ice_cream_price.sh\n\n  # Mocking a function by exporting a function\n  function getDay() { echo \"2\"; }\n  export -f getDay\n  result=$(main 4)\n\n  assertEquals 0 $?\n  assertContains \"${result}\" \"Total 400\"\n}\n\n. lib/shunit2/shunit2\n\n"
  },
  {
    "path": "example-shunit2/test.sh",
    "content": "#!/bin/bash\n\n# Set project root as the working directory\ncd \"$(dirname \"$0\")\"\n\nfor testfile in test/*.sh\ndo\n\techo \"Running tests in ${testfile}:\"\n\t$testfile\ndone\n\n\n"
  },
  {
    "path": "mocking/src/greeting.sh",
    "content": "#!/bin/bash\n\nfunction hello() {\n  local username=$1\n\n  if [[ \"$(day_of_week)\" == \"Friday\" ]]; then\n    echo \"What a wonderful day, $username!\"\n  else\n    echo \"Hello, $username\"\n  fi\n}\n\nfunction day_of_week() {\n  date +%A\n}\n\nif [[ \"${BASH_SOURCE[0]}\" == \"${0}\" ]]; then\n  hello \"John\"\nfi\n"
  },
  {
    "path": "mocking/test/mocks/date",
    "content": "#!/bin/bash\n\necho \"Friday\"\n\n"
  },
  {
    "path": "mocking/test/test-alias-date.sh",
    "content": "#!/bin/bash\n\nscript_dir=\"$( cd \"$( dirname \"${BASH_SOURCE[0]}\" )\" && pwd )\"\n\n# Set up mocks\nshopt -s expand_aliases\nalias \"date\"=\"echo 'Friday';true\"\n\n# Include the function to be tested\nSRC=\"${script_dir}/../src\"\nsource ${SRC}/greeting.sh\n\n# Execute test case\nresult=$(hello \"John\")\n\n# Verify results\nexpected=\"What a wonderful day, John!\"\n\nif [[ \"${result}\" == \"${expected}\" ]]; then\n  echo \"Test passed!\"\nelse\n  echo \"Test failed!\"\n  exit 1\nfi\n"
  },
  {
    "path": "mocking/test/test-alias-own.sh",
    "content": "#!/bin/bash\n\nscript_dir=\"$( cd \"$( dirname \"${BASH_SOURCE[0]}\" )\" && pwd )\"\n\n# Set up mocks\nshopt -s expand_aliases\nalias day_of_week=\"echo 'Friday'\"\n\n# Include the function to be tested\nSRC=\"${script_dir}/../src\"\nsource ${SRC}/greeting.sh\n\n# Execute test case\nresult=$(hello \"John\")\n\n# Verify results\nexpected=\"What a wonderful day, John!\"\n\nif [[ \"${result}\" == \"${expected}\" ]]; then\n  echo \"Test passed!\"\nelse\n  echo \"Test failed!\"\n  exit 1\nfi\n"
  },
  {
    "path": "mocking/test/test-export-date.sh",
    "content": "#!/bin/bash\n\nscript_dir=\"$( cd \"$( dirname \"${BASH_SOURCE[0]}\" )\" && pwd )\"\n\n# Include the function to be tested\nSRC=\"${script_dir}/../src\"\nsource ${SRC}/greeting.sh\n\n# Set up mocks\nfunction date() {\n  echo \"Friday\"\n}\nexport -f day_of_week\n\n# Execute test case\nresult=$(hello \"John\")\n\n# Verify results\nexpected=\"What a wonderful day, John!\"\n\nif [[ \"${result}\" == \"${expected}\" ]]; then\n  echo \"Test passed!\"\nelse\n  echo \"Test failed!\"\n  exit 1\nfi\n"
  },
  {
    "path": "mocking/test/test-export-own.sh",
    "content": "#!/bin/bash\n\nscript_dir=\"$( cd \"$( dirname \"${BASH_SOURCE[0]}\" )\" && pwd )\"\n\n# Include the function to be tested\nSRC=\"${script_dir}/../src\"\nsource ${SRC}/greeting.sh\n\n# Set up mocks\nfunction day_of_week() {\n  echo \"Friday\"\n}\nexport -f day_of_week\n\n# Execute test case\nresult=$(hello \"John\")\n\n# Verify results\nexpected=\"What a wonderful day, John!\"\n\nif [[ \"${result}\" == \"${expected}\" ]]; then\n  echo \"Test passed!\"\nelse\n  echo \"Test failed!\"\n  exit 1\nfi\n"
  },
  {
    "path": "mocking/test/test-pathoverride-date.sh",
    "content": "#!/bin/bash\n\nscript_dir=\"$( cd \"$( dirname \"${BASH_SOURCE[0]}\" )\" && pwd )\"\n\n# Add mocks to the path\nexport PATH=mocks:$PATH\n\n# Execute test case\nSRC=\"${script_dir}/../src\"\nresult=$(${SRC}/greeting.sh \"John\")\n\n# Verify results\nexpected=\"What a wonderful day, John!\"\n\nif [[ \"${result}\" == \"${expected}\" ]]; then\n  echo \"Test passed!\"\nelse\n  echo \"Test failed!\"\n  exit 1\nfi\n"
  },
  {
    "path": "readme.md",
    "content": "# In search of the best test framework for Bash\n\nThere are [many testing frameworks for Bash](https://stackoverflow.com/questions/1339416/unit-testing-bash-scripts).\nHowever, many of them are not actively maintained, or are used only by a small group of people.\n\nIn this comparision I've included well-established and new testing frameworks that had at least recent release and have more than 50 Stars on GitHub:\n\n- [bashunit](https://github.com/TypedDevs/bashunit), a fast, simple and flexible testing library for your bash scripts \n- [Bats](https://github.com/bats-core/bats-core), a mature testing library with a bit of a custom syntax for more concise tests\n- [shUnit2](https://github.com/kward/shunit2), possibly the oldest Bash test framework around, supporting test files in pure Bash\n- [bash_unit](https://github.com/pgrange/bash_unit), a new and featureful test framework where test cases can be written in pure Bash\n- [ShellSpec](https://github.com/shellspec/shellspec), a BDD style testing framework with its own DSL\n- [shpec](https://github.com/rylnd/shpec), a BDD style testing framework similar to RSpec, Jasmine, and mocha\n\n\n## Test drive\n\nTo start fiddling with any of the test projects just run the `install.sh` to download and extract the particular\ntesting framework to a local directory, then execute `test.sh` to start the suite.\n\n- [bashunit example project and test drive](example-bashunit)  \n- [Bats example project and test drive](example-bats)  \n- [shUnit2 example project and test drive](example-shunit2)\n- [bash_unit example project and test drive](example-bash_unit)\n- [ShellSpec example project and test drive](example-shellspec)\n- [shpec example project and test drive](example-shpec)\n\n\n## Detailed comparision\n\n|                                                                                                                                          | [bashunit](https://github.com/TypedDevs/bashunit)                         | [Bats](https://github.com/bats-core/bats-core)                                                                                | [shUnit2](https://github.com/kward/shunit2)                               | [bash_unit](https://github.com/pgrange/bash_unit)                          | [ShellSpec](https://github.com/shellspec/shellspec)                 | [shpec](https://github.com/rylnd/shpec) |\n|------------------------------------------------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------|----------------------------------------------------------------------------|---------------------------------------------------------------------|-----------------------------------------|\n| Discover and run all tests<br/>in the project                                                                                            | ✔️                                                                        | ✔️                                                                                                                             | ❌ [<sup>?</sup>](example-shunit2#test-discovery)                                                                              | ⚠ [<sup>?</sup>](example-bash_unit#test-discovery)                        | ✔️ [<sup>?</sup>](example-shellspec#test-discovery)                        | ✔️                                                                  |\n| Run subset of tests or<br/>a specific test                                                                                               | ✔️                                                                        | ✔️ [<sup>?</sup>](example-bats#test-discovery)                                                                                 | ⚠ [<sup>?</sup>](example-shunit2#test-discovery)                                                                              | ✔️                                                                        | ✔️                                                                         | ❌ [<sup>issue</sup>](https://github.com/rylnd/shpec/issues/31)      |\n| Parallel test execution                                                                                                                  | ✔️                                                                         | ✔️                                                                                                                             | ❌                                                                                                                             | ❌                                                                         | ✔️                                                                         | ❌                                                                   |\n| Skip or ignore tests                                                                                                                     | ✔️                                                                        | ✔️                                                                                                                             | ⚠ [<sup>?</sup>](example-shunit2#skip-tests)                                                                                  | ✔️ [<sup>?</sup>](example-bash_unit#skip-tests)                           | ✔️                                                                         | ❌ [<sup>issue</sup>](https://github.com/rylnd/shpec/issues/57)      |\n| Rich assertion library                                                                                                                   | ✔️                                                                        | ⚠ [<sup>?</sup>](example-bats#assertions)                                                                                      | ⚠ [<sup>?</sup>](example-shunit2#assertions)                                                                                  | ⚠ [<sup>?</sup>](example-bash_unit#assertions)                            | ✔️ [<sup>?</sup>](example-shellspec#assertions)                            | ✔️ [<sup>?</sup>](example-shpec#assertions)                         |\n| Extensible with custom assertions                                                                                                        | ✔️                                                                        | ✔️ [<sup>?</sup>](example-bats#custom-assertions)                                                                              | ✔️ [<sup>?</sup>](example-shunit2#custom-assertions)                                                                          | ✔️ [<sup>?</sup>](example-bash_unit#custom-assertions)                    | ✔️ [<sup>?</sup>](example-shellspec#custom-assertions)                     | ✔️ [<sup>?</sup>](example-shpec#custom-assertions)                  |\n| Before / After                                                                                                                           | ✔️                                                                        | ✔️                                                                                                                             | ⚠ [<sup>issue</sup>](https://github.com/kward/shunit2/issues/112)                                                             | ✔️                                                                        | ✔️                                                                         | ❌ [<sup>issue</sup>](https://github.com/rylnd/shpec/issues/52)      |\n| BeforeAll / AfterAll                                                                                                                     | ✔️                                                                        | ❌ [<sup>issue</sup>](example-bats#test-discovery)                                                                              | ⚠ [<sup>issue</sup>](https://github.com/kward/shunit2/issues/112)                                                             | ✔️                                                                        | ✔️ [<sup>?</sup>](example-shellspec#test-format)                           | ❌ [<sup>issue</sup>](https://github.com/rylnd/shpec/issues/52)      |\n| Parameterized tests<br/>and test data                                                                                                    | ✔️                                                                        | ❌ [<sup>issue</sup>](https://github.com/sstephenson/bats/issues/136)                                                           | ❌                                                                                                                             | ❌                                                                         | ✔️                                                                         | ❌                                                                   |\n| Mocking \\*                                                                                                                               | ✔️                                                                        | ✔️ [<sup>?</sup>](example-bats#mocking)                                                                                        | ⚠ [<sup>?</sup>](example-shunit2#mocking)                                                                                     | ✔️ [<sup>?</sup>](example-bash_unit#mocking)                              | ✔️ [<sup>?</sup>](example-shellspec#mocking)                               | ✔️ [<sup>?</sup>](example-shpec#mocking)                            |\n| Isolation between tests \\*\\*                                                                                                             | ✔️                                                                        | ✔️ [<sup>doc</sup>](https://github.com/bats-core/bats-core/wiki/Bats-Evaluation-Process)                                       | ❌                                                                                                                             | ✔️                                                                        | ✔️                                                                         |                                                                     |\n| Nice local report                                                                                                                        | ✔️                                                                        | ⚠ [<sup>?</sup>](example-bats#report)                                                                                          | ✔️                                                                                                                            | ✔️                                                                        | ✔️                                                                         | ✔️                                                                  |\n| CI compatible report                                                                                                                     | ✔️                                                                          | ✔️ [<sup>TAP</sup>](http://testanything.org/)                                                                                  | ❌ [<sup>issue</sup>](https://github.com/kward/shunit2/issues/31)                                                              | ✔️ [<sup>TAP</sup>](http://testanything.org/)                             | ✔️ [<sup>TAP</sup>](http://testanything.org/)                              | ❌                                                                   |\n| Zero Dependency and<br/>implemented in Bash \\*\\*\\*                                                                                       | ✔️                                                                        | ✔️                                                                                                                             | ✔️                                                                                                                            | ✔️                                                                        | ✔️                                                                         | ✔️                                                                  |\n| Easy to install \\*\\*\\*\\*                                                                                                                 | ✔️                                                                        | ✔️                                                                                                                             | ✔️                                                                                                                            | ✔️                                                                        | ✔️                                                                         | ✔️                                                                  |\n| Tests written in Bash                                                                                                                    | ✔️                                                                        | ⚠ [<sup>almost</sup>](example-bats#test-format)                                                                                | ✔️                                                                                                                            | ✔️                                                                        | ❌ [<sup>?</sup>](example-shellspec#test-format)                            | ❌ [<sup>?</sup>](example-shpec#test-format)                         |\n| Compatible with the <br/>Unofficial Bash Strict Mode \\*\\*\\*\\*\\*     | ✔️                                                                         | ✔️| ⚠ [<sup>?</sup>](https://github.com/dodie/testing-in-bash/tree/master/example-bats#unit-testing-scripts-that-set-bash-options) | ✔️                                                                                                                            | ⚠ [<sup>issue</sup>](https://github.com/pgrange/bash_unit/issues/61)      | ✔️                                                                         |                                                                     |\n| Maintained                                                                                                                               | ✔️                                                                        | ⚠ [<sup>?</sup>](example-bats#activity)                                                                                        | ⚠ [<sup>?</sup>](example-shunit2#activity)                                                                                    | ✔️ [<sup>?</sup>](example-bash_unit#activity)                             | ✔️ [<sup>?</sup>](example-shellspec#activity)                              | ⚠ [<sup>?</sup>](example-shpec#activity)                            |\n| Mature                                                       | ❌ [<sup>since 2023</sup>](https://github.com/TypedDevs/bashunit/releases) | ✔️ [<sup>since 2011</sup>](https://github.com/bats-core/bats-core/releases)                                                    | ✔️ [<sup>since 2007</sup>](https://github.com/kward/shunit2/commits/master?after=0f0a77b6b257f24d2b3ef1b28096fdd154a19f22+10) | ✔️ [<sup>since 2016</sup>](https://github.com/pgrange/bash_unit/releases) | ❌ [<sup>since 2019</sup>](https://github.com/shellspec/shellspec/releases) | ✔️ [<sup>since 2013</sup>](https://github.com/rylnd/shpec/releases) |\n| Good documentation                             | ✔️ [<sup>?</sup>](https://bashunit.typeddevs.com)                         | ⚠ [<sup>?</sup>](example-bats#documentation)                                                                                   | ✔️ [<sup>?</sup>](example-shunit2#documentation)                                                                              | ✔️ [<sup>?</sup>](example-bash_unit#documentation)                        | ✔️ [<sup>?</sup>](example-shellspec#documentation)                         | ✔️ [<sup>?</sup>](example-shpec#documentation)                      |\n\n\\* **Mocking**:<br/>\nThe testing framework is compatible with the [common mocking techniques](https://github.com/dodie/testing-in-bash/tree/master/mocking).\n\n\\*\\* **Isolation between tests**:<br/>\nAliases, custom function definitions, Bash options and variables are not leaking from one test\nto another.\n\n\\*\\*\\* **Zero Dependency and implemented in Bash**:<br/>\nApart from a few common tools like `coreutils` and `grep`, you don't need anything to use the framework.<br/>\nThe source code of the framework is itself implemented as Bash scripts. If you are comfortable with Bash, you can read and even modify it.\n\n\\*\\*\\*\\* **Easy to install**:<br/>\nMany of the frameworks support more than one alternatives on how to set it up quickly. It's possible\nto install it with your favourite package manager and some of them even have an official Docker image.\nPersonally, I try to avoid installing project dependencies globally as much as I can,\nso I just decided to grab a tar.gz file with the latest release from GitHub.\n\n\\*\\*\\*\\*\\* **Compatible with the Unofficial Bash Strict Mode**:<br/>\nSourcing a script file that use the [Unofficial Bash Strict Mode](http://redsymbol.net/articles/unofficial-bash-strict-mode/) should\nnot break the test framework. Of course, custom assertions and the test code\n[should also work with strict mode](https://github.com/dodie/testing-in-bash/tree/master/strictmode) but that's up for the user.\nAlso, the test framework shouldn't set Bash options as well to ensure that executing sourced functions work similarly to when they are not sourced.\n\n\n## Related posts, other comparisions\n\n- https://medium.com/wemake-services/testing-bash-applications-85512e7fe2de\n- https://stackoverflow.com/questions/1339416/unit-testing-bash-scripts\n- https://www.leadingagile.com/2018/10/unit-testing-shell-scriptspart-one/\n\n\n## Contribution guide\n\nIf you know a testing framework that should be mentioned here or find that there's room for improvement feel free to\nopen an issue or Pull Request. (See [CONTRIBUTING.md](https://github.com/dodie/testing-in-bash/blob/master/CONTRIBUTING.md).)\n"
  },
  {
    "path": "stdio/src/interactive_greeter.sh",
    "content": "#!/bin/bash\n\necho \"Please enter your name:\" >&2\nread name\n\necho \"Hello, ${name}!\"\n\necho \"Please enter your age:\" >&2\nread age\n\necho \"You are ${age} years old\"\n\n"
  },
  {
    "path": "stdio/test/test_stdio.sh",
    "content": "#!/bin/bash\n\nscript_dir=\"$( cd \"$( dirname \"${BASH_SOURCE[0]}\" )\" && pwd )\"\nSRC=\"${script_dir}/../src\"\n\necho \"Test that the program takes user input and prints the result to the standard output (verifying multiple lines)\"\n\nresult=$(printf \"John\\n42\\n\" | ${SRC}/interactive_greeter.sh 2>/dev/null)\nexpected=$(printf \"Hello, John!\\nYou are 42 years old\")\n\nif [[ \"${result}\" == \"${expected}\" ]]; then\n  echo \"Test passed!\"\nelse\n  echo \"Test failed!\"\n  exit 1\nfi\n\necho \"Test that the program ask questions via standard error\"\n\nresult=$(printf \"Any\\n12\\n\" | ${SRC}/interactive_greeter.sh 2>&1 >/dev/null)\nexpected=$(printf \"Please enter your name:\\nPlease enter your age:\")\n\nif [[ \"${result}\" == \"${expected}\" ]]; then\n  echo \"Test passed!\"\nelse\n  echo \"Test failed!\"\n  exit 1\nfi\n"
  },
  {
    "path": "strictmode/readme.md",
    "content": "# Unit testing scripts that use the [Unofficial Bash Strict Mode](http://redsymbol.net/articles/unofficial-bash-strict-mode/)\n\nIn this module there are two scripts, one that sets `-e` and `pipefail`, and one that sets `-u`. There's a test for each\nof these files that first executes the corresponding script to demonstrate it's behavior on edge cases, then it sources\nthe script to exercise its functions directly.\n\nThe test files demonstrate two common problems that might come up in this setup:\n\n- Bash options \"leak\" from the sourced file to the test file, changing its behavior.\n- By default, `-e` does not apply to functions executed in a subshell. This can be problematic for unit tests where the\n  output of a function is captured by the following construct: `result=$(myfunction)`.\n\n"
  },
  {
    "path": "strictmode/src/using_e.sh",
    "content": "#!/bin/bash\n\n# The script makes a HTTP GET to the URL passed in the argument.\n# For successful queries it prints the result code (e.g. 200).\n# For failures the script prints nothing and exits with a non-zero code.\n#\n# This achieved by setting bash opt errexit to make the script fail early\n# and passig the --fail parameter to curl to fail on errors.\n\nset -e\n\nfunction getResponseCode() {\n  local url=$1\n  local result\n  result=$(curl --fail -o /dev/null -s -w \"%{http_code}\\n\" \"${url}\")\n  echo $result\n}\n\nif [[ \"${BASH_SOURCE[0]}\" == \"${0}\" ]]; then\n  getResponseCode \"$1\"\nfi\n\n"
  },
  {
    "path": "strictmode/src/using_no_e.sh",
    "content": "#!/bin/bash\n\n# The script makes a HTTP GET to the URL passed in the argument.\n# It prints the result code for  (e.g. 200).\n# For failures the script prints ERROR.\n# In both cases the script exists with 0 status code.\n#\n# This achieved by not setting bash opt errexit, so the script continues even if there's an error.\n# However, curl uses --fail which makes it exit with a non-zero code for failures (e.g. HTTP 404).\n# This has no effect unless the script is executed in an environment where errexit is set.\n\nfunction getResponseCode() {\n  local url=$1\n  local result\n  result=$(curl --fail -o /dev/null -s -w \"%{http_code}\\n\" \"${url}\")\n  echo $result\n}\n\nif [[ \"${BASH_SOURCE[0]}\" == \"${0}\" ]]; then\n  getResponseCode \"$1\"\nfi\n\n"
  },
  {
    "path": "strictmode/src/using_no_u.sh",
    "content": "#!/bin/bash\n\n# When called with a parameter, the script will print YES.\n# Otherwise, it will print NO.\n# In either case, it will exit with 0.\n\n# | » ./using_no_u.sh\n# | NO\n# | » echo $?\n# | 0\n#\n# | » ./using_no_u.sh test\n# | YES\n# | » echo $?\n# | 0\n#\n# If we'd define set -u, it will make the script fail in the NO case.\n\n\nfunction main() {\n  if [ -n \"$1\" ]; then\n    echo \"YES\"\n  else\n    echo \"NO\"\n  fi\n}\n\nif [[ \"${BASH_SOURCE[0]}\" == \"${0}\" ]]; then\n  main $@\nfi\n\n"
  },
  {
    "path": "strictmode/src/using_u.sh",
    "content": "#!/bin/bash\n\n# When called without a parameter, this script will fail due to\n# an unbound variable in the 'main' function.\n# | » ./using_u.sh\n# | ../src/faulty_functions.sh: line 10: 1: unbound variable\n# | » echo $?\n# | 1\n#\n# When called with a parameter, it will finish without an error.\n# | » ./using_u.sh something\n# | The parameter is: something\n# | » echo $?\n# | 0\n#\n\n# This line enables the 'nounset' Bash option.\n# From the man page:\n# -u   Treat unset variables and parameters other than the special parameters \"@\" and \"*\"\n#      as an error when performing parameter expansion. If expansion is attempted on an\n#      unset variable or parameter, the shell prints an error message, and, if not interactive,\n#      exits with a non-zero status.\n\nset -u\n\nfunction main() {\n  # When called without a parameter it should cause the program to terminate due to -u\n  echo \"The parameter is: ${1}\"\n}\n\nif [[ \"${BASH_SOURCE[0]}\" == \"${0}\" ]]; then\n  main $@\nfi\n\n"
  },
  {
    "path": "strictmode/test/unit_test_script_using_e_o_pipefail.sh",
    "content": "#!/bin/bash\n\nscript_dir=\"$( cd \"$( dirname \"${BASH_SOURCE[0]}\" )\" && pwd )\"\nSRC=\"${script_dir}/../src\"\n\n\necho \"E2E Test case: early failure due to a non-zero exit status\"\necho \"==========================================================\"\necho \"Executing src/using_e_o_pipefail.sh without an argument does the following:\"\necho \" - output 'Start', but not 'End'\"\necho \" - terminate with a non-zero exit code\"\n\nresult=$(\"${SRC}/using_e_o_pipefail.sh\")\nsc=$?\n\necho \"\"\necho \"Output:\"\necho \"=======\"\necho \"${result}\"\necho \"\"\necho \"Exit code:\"\necho \"==========\"\necho \"${sc}\"\n\necho \"\"\necho \"\"\necho \"It works as expected.\"\necho \"Let's see what happens if we source the script file and to execute it's main\"\necho \"function directly in the same manner.\"\necho \"\"\necho \"\"\nsource \"${SRC}/using_e_o_pipefail.sh\"\n\necho \"\"\necho \"\"\necho \"Unit Test case: -e does not apply when the function is executed in a subshell\"\necho \"=============================================================================\"\necho \"When executing the main function directly, we might expect that the test suite\"\necho \"terminates early with a non-zero exit code, because we sourced a script with\"\necho \"a line set -e, and we execute a faulty function in a subshell.\"\necho \"However, the sad reality is the following:\"\n\nresult=$(main)\nsc=$?\n\necho \"\"\necho \"Output:\"\necho \"=======\"\necho \"${result}\"\necho \"\"\necho \"Exit code:\"\necho \"==========\"\necho \"${sc}\"\n\necho \"\"\necho \"The explanation is in the man page:\"\necho \"> Subshells spawned to execute command substitutions inherit the value of\"\necho \"> the -e option from the parent shell.  When  not  in  posix  mode,  bash\"\necho \"> clears the -e option in such subshells.\"\n\necho \"\"\necho \"So, although -e is leaked to the test from the sourced file, it has no effect on subshells\"\necho \"unless posix mode (or inherit_errexit) is enabled.\"\necho \"This can cause problems with unit testing functions where one might\"\necho \"capture the result of a function with subshells.\"\necho \"Not sure if it's a good idea, but enabling posix mode (or inherit_errexit) would fix this issue.\"\necho \" - set -o posix\"\necho \" - shopt -s inherit_errexit\"\n\n\necho \"\"\necho \"\"\necho \"Unit Test case: the leaked -e modifies the behavior of the test\"\necho \"===============================================================\"\necho \"Lets' conclude this demonstration with an unit test should work normally,\"\necho \"but fails because the -e mode leaks from the sourced file\"\n\n# Grep will exit with a 0 exit code when there's a match, and with anon-zero exitcode otherwise\n# The next tests try to use this to check whether or not the output contains a certain string\nmain special | grep special\nsc=$?\nif [ $? -eq 0 ]; then\n\techo \"Test passed, output contains 'special'\"\nfi\n\nmain special | grep somethingelse\nsc=$?\nif [ $? -ne 0 ]; then\n\techo \"Test passed, output does not contain 'somethingelse'\"\nfi\n\n"
  },
  {
    "path": "strictmode/test/unit_test_script_using_u.sh",
    "content": "#!/bin/bash\n\nscript_dir=\"$( cd \"$( dirname \"${BASH_SOURCE[0]}\" )\" && pwd )\"\nSRC=\"${script_dir}/../src\"\n\n\necho \"E2E Test case: early failure due to a non-zero exit status\"\necho \"==========================================================\"\necho \"Executing src/using_u.sh without an argument does the following:\"\necho \" - output 'Start' only\"\necho \" - terminate with a non-zero exit code\"\n\nresult=$(\"${SRC}/using_u.sh\")\nsc=$?\n\necho \"\"\necho \"Output:\"\necho \"=======\"\necho \"${result}\"\necho \"\"\necho \"Exit code:\"\necho \"==========\"\necho \"${sc}\"\n\necho \"\"\necho \"\"\necho \"It works as expected.\"\necho \"Let's see what happens if we source the script file and to execute it's main\"\necho \"function directly in the same manner.\"\necho \"\"\necho \"\"\nsource \"${SRC}/using_u.sh\"\n\necho \"\"\necho \"\"\necho \"Unit Test case: -u applies to functions invoked in a subshell\"\necho \"=============================================================\"\necho \"After the case of -e I expected that -u is also ignored in subshells,\"\necho \"but it seems that after all, they are considered. The following unit test\"\necho \"works similarly to the end-to-end test.\"\n\n\nresult=$(main)\nsc=$?\n\necho \"\"\necho \"Output:\"\necho \"=======\"\necho \"${result}\"\necho \"\"\necho \"Exit code:\"\necho \"==========\"\necho \"${sc}\"\n\necho \"\"\necho \"\"\necho \"Unit Test case: the leaked -u modifies the behavior of the test\"\necho \"===============================================================\"\necho \"Lets' conclude this demonstration with an unit test should work normally,\"\necho \"but fails because the -u mode leaks from the sourced file\"\n\n# The next test tries to check that a hello variable is not created during the execution of the function. However, it fails, because -u changes how undefined variables can be detected.\nmain hello\n\n\nif [ -n $HELLO ]; then\n  echo \"Test passed!\"\nelse\n  echo \"Test failed!\"\nfi\n\n"
  }
]