├── .gitignore ├── .rspec ├── .tool-versions ├── CHANGELOG.md ├── CODE_OF_CONDUCT.md ├── CONTRIBUTING.md ├── Dockerfile.dx ├── Gemfile ├── LICENSE.md ├── README.md ├── Rakefile ├── bin ├── ci ├── console ├── matrix ├── rake ├── rspec └── setup ├── docker-compose.dx.yml ├── dx ├── build ├── docker-compose.env ├── dx.sh.lib ├── exec ├── prune ├── setupkit.sh.lib ├── show-help-in-app-container-then-wait.sh ├── start └── stop ├── lib ├── with_clues.rb └── with_clues │ ├── browser_logs.rb │ ├── html.rb │ ├── method.rb │ ├── notifier.rb │ ├── private │ └── custom_clue_method_analysis.rb │ └── version.rb ├── spec ├── custom_clues_spec.rb ├── spec_helper.rb └── with_clues_spec.rb └── with_clues.gemspec /.gitignore: -------------------------------------------------------------------------------- 1 | /.bundle/ 2 | /.yardoc 3 | /_yardoc/ 4 | /coverage/ 5 | /doc/ 6 | /pkg/ 7 | /spec/reports/ 8 | /tmp/ 9 | Gemfile.lock 10 | -------------------------------------------------------------------------------- /.rspec: -------------------------------------------------------------------------------- 1 | --require spec_helper 2 | -------------------------------------------------------------------------------- /.tool-versions: -------------------------------------------------------------------------------- 1 | ruby 3.1.0 2 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | See [https://github.com/sustainable-rails/with\_clues/releases](https://github.com/sustainable-rails/with_clues/releases) 2 | -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Contributor Covenant Code of Conduct 2 | 3 | ## Our Pledge 4 | 5 | In the interest of fostering an open and welcoming environment, we as 6 | contributors and maintainers pledge to making participation in our project and 7 | our community a harassment-free experience for everyone, regardless of age, body 8 | size, disability, ethnicity, sex characteristics, gender identity and expression, 9 | level of experience, education, socio-economic status, nationality, personal 10 | appearance, race, religion, or sexual identity and orientation. 11 | 12 | ## Our Standards 13 | 14 | Examples of behavior that contributes to creating a positive environment 15 | include: 16 | 17 | * Using welcoming and inclusive language 18 | * Being respectful of differing viewpoints and experiences 19 | * Gracefully accepting constructive criticism 20 | * Focusing on what is best for the community 21 | * Showing empathy towards other community members 22 | 23 | Examples of unacceptable behavior by participants include: 24 | 25 | * The use of sexualized language or imagery and unwelcome sexual attention or 26 | advances 27 | * Trolling, insulting/derogatory comments, and personal or political attacks 28 | * Public or private harassment 29 | * Publishing others' private information, such as a physical or electronic 30 | address, without explicit permission 31 | * Other conduct which could reasonably be considered inappropriate in a 32 | professional setting 33 | 34 | ## Our Responsibilities 35 | 36 | Project maintainers are responsible for clarifying the standards of acceptable 37 | behavior and are expected to take appropriate and fair corrective action in 38 | response to any instances of unacceptable behavior. 39 | 40 | Project maintainers have the right and responsibility to remove, edit, or 41 | reject comments, commits, code, wiki edits, issues, and other contributions 42 | that are not aligned to this Code of Conduct, or to ban temporarily or 43 | permanently any contributor for other behaviors that they deem inappropriate, 44 | threatening, offensive, or harmful. 45 | 46 | ## Scope 47 | 48 | This Code of Conduct applies both within project spaces and in public spaces 49 | when an individual is representing the project or its community. Examples of 50 | representing a project or community include using an official project e-mail 51 | address, posting via an official social media account, or acting as an appointed 52 | representative at an online or offline event. Representation of a project may be 53 | further defined and clarified by project maintainers. 54 | 55 | ## Enforcement 56 | 57 | Instances of abusive, harassing, or otherwise unacceptable behavior may be 58 | reported by contacting the project team at davetron5000 (at) gmail.com. All 59 | complaints will be reviewed and investigated and will result in a response that 60 | is deemed necessary and appropriate to the circumstances. The project team is 61 | obligated to maintain confidentiality with regard to the reporter of an incident. 62 | Further details of specific enforcement policies may be posted separately. 63 | 64 | Project maintainers who do not follow or enforce the Code of Conduct in good 65 | faith may face temporary or permanent repercussions as determined by other 66 | members of the project's leadership. 67 | 68 | ## Attribution 69 | 70 | This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, 71 | available at https://www.contributor-covenant.org/version/1/4/code-of-conduct.html 72 | 73 | [homepage]: https://www.contributor-covenant.org 74 | 75 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing 2 | 3 | * Code changes for style, cleanliness, eleganance, or other aesthetic stuff will not be accepted. 4 | * All proposed changes must have a clear problem statement and be based on a real-world scenario. Imagined problems will not be solved. 5 | * Tests are appreciated with all pull requests. 6 | -------------------------------------------------------------------------------- /Dockerfile.dx: -------------------------------------------------------------------------------- 1 | ARG RUBY_VERSION 2 | FROM ruby:${RUBY_VERSION} 3 | 4 | ENV DEBIAN_FRONTEND noninteractive 5 | RUN apt-get -y update 6 | 7 | 8 | # dx.snippet.start=templates/snippets/bundler/latest__bundler.dockerfile-snippet 9 | # Based on documentation at https://guides.rubygems.org/command-reference/#gem-update 10 | # based on the vendor's documentation 11 | RUN echo "gem: --no-document" >> ~/.gemrc && \ 12 | gem update --system && \ 13 | gem install bundler 14 | 15 | # dx.snippet.end=templates/snippets/bundler/latest__bundler.dockerfile-snippet 16 | 17 | 18 | # dx.snippet.start=templates/snippets/vim/bullseye_vim.dockerfile-snippet 19 | # Based on documentation at https://packages.debian.org/search?keywords=vim 20 | # based on the vendor's documentation 21 | ENV EDITOR=vim 22 | RUN apt-get install -y vim && \ 23 | echo "set -o vi" >> /root/.bashrc 24 | # dx.snippet.end=templates/snippets/vim/bullseye_vim.dockerfile-snippet 25 | 26 | # This entrypoint produces a nice help message and waits around for you to do 27 | # something with the container. 28 | COPY dx/show-help-in-app-container-then-wait.sh /root 29 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source "https://rubygems.org" 2 | 3 | # Specify your gem's dependencies in with_clues.gemspec 4 | gemspec 5 | 6 | gem "rake", "~> 12.0" 7 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | [with_clues] Copyright (2021) (David Copeland)(“Licensor”) 2 | 3 | Hippocratic License Version Number: 2.1. 4 | 5 | Purpose. The purpose of this License is for the Licensor named above to permit the Licensee (as defined below) broad permission, if consistent with Human Rights Laws and Human Rights Principles (as each is defined below), to use and work with the Software (as defined below) within the full scope of Licensor’s copyright and patent rights, if any, in the Software, while ensuring attribution and protecting the Licensor from liability. 6 | 7 | Permission and Conditions. The Licensor grants permission by this license (“License”), free of charge, to the extent of Licensor’s rights under applicable copyright and patent law, to any person or entity (the “Licensee”) obtaining a copy of this software and associated documentation files (the “Software”), to do everything with the Software that would otherwise infringe (i) the Licensor’s copyright in the Software or (ii) any patent claims to the Software that the Licensor can license or becomes able to license, subject to all of the following terms and conditions: 8 | 9 | * Acceptance. This License is automatically offered to every person and entity subject to its terms and conditions. Licensee accepts this License and agrees to its terms and conditions by taking any action with the Software that, absent this License, would infringe any intellectual property right held by Licensor. 10 | 11 | * Notice. Licensee must ensure that everyone who gets a copy of any part of this Software from Licensee, with or without changes, also receives the License and the above copyright notice (and if included by the Licensor, patent, trademark and attribution notice). Licensee must cause any modified versions of the Software to carry prominent notices stating that Licensee changed the Software. For clarity, although Licensee is free to create modifications of the Software and distribute only the modified portion created by Licensee with additional or different terms, the portion of the Software not modified must be distributed pursuant to this License. If anyone notifies Licensee in writing that Licensee has not complied with this Notice section, Licensee can keep this License by taking all practical steps to comply within 30 days after the notice. If Licensee does not do so, Licensee’s License (and all rights licensed hereunder) shall end immediately. 12 | 13 | * Compliance with Human Rights Principles and Human Rights Laws. 14 | 15 | 1. Human Rights Principles. 16 | 17 | (a) Licensee is advised to consult the articles of the United Nations Universal Declaration of Human Rights and the United Nations Global Compact that define recognized principles of international human rights (the “Human Rights Principles”). Licensee shall use the Software in a manner consistent with Human Rights Principles. 18 | 19 | (b) Unless the Licensor and Licensee agree otherwise, any dispute, controversy, or claim arising out of or relating to (i) Section 1(a) regarding Human Rights Principles, including the breach of Section 1(a), termination of this License for breach of the Human Rights Principles, or invalidity of Section 1(a) or (ii) a determination of whether any Law is consistent or in conflict with Human Rights Principles pursuant to Section 2, below, shall be settled by arbitration in accordance with the Hague Rules on Business and Human Rights Arbitration (the “Rules”); provided, however, that Licensee may elect not to participate in such arbitration, in which event this License (and all rights licensed hereunder) shall end immediately. The number of arbitrators shall be one unless the Rules require otherwise. 20 | 21 | Unless both the Licensor and Licensee agree to the contrary: (1) All documents and information concerning the arbitration shall be public and may be disclosed by any party; (2) The repository referred to under Article 43 of the Rules shall make available to the public in a timely manner all documents concerning the arbitration which are communicated to it, including all submissions of the parties, all evidence admitted into the record of the proceedings, all transcripts or other recordings of hearings and all orders, decisions and awards of the arbitral tribunal, subject only to the arbitral tribunal's powers to take such measures as may be necessary to safeguard the integrity of the arbitral process pursuant to Articles 18, 33, 41 and 42 of the Rules; and (3) Article 26(6) of the Rules shall not apply. 22 | 23 | 2. Human Rights Laws. The Software shall not be used by any person or entity for any systems, activities, or other uses that violate any Human Rights Laws. “Human Rights Laws” means any applicable laws, regulations, or rules (collectively, “Laws”) that protect human, civil, labor, privacy, political, environmental, security, economic, due process, or similar rights; provided, however, that such Laws are consistent and not in conflict with Human Rights Principles (a dispute over the consistency or a conflict between Laws and Human Rights Principles shall be determined by arbitration as stated above). Where the Human Rights Laws of more than one jurisdiction are applicable or in conflict with respect to the use of the Software, the Human Rights Laws that are most protective of the individuals or groups harmed shall apply. 24 | 25 | 3. Indemnity. Licensee shall hold harmless and indemnify Licensor (and any other contributor) against all losses, damages, liabilities, deficiencies, claims, actions, judgments, settlements, interest, awards, penalties, fines, costs, or expenses of whatever kind, including Licensor’s reasonable attorneys’ fees, arising out of or relating to Licensee’s use of the Software in violation of Human Rights Laws or Human Rights Principles. 26 | 27 | * Failure to Comply. Any failure of Licensee to act according to the terms and conditions of this License is both a breach of the License and an infringement of the intellectual property rights of the Licensor (subject to exceptions under Laws, e.g., fair use). In the event of a breach or infringement, the terms and conditions of this License may be enforced by Licensor under the Laws of any jurisdiction to which Licensee is subject. Licensee also agrees that the Licensor may enforce the terms and conditions of this License against Licensee through specific performance (or similar remedy under Laws) to the extent permitted by Laws. For clarity, except in the event of a breach of this License, infringement, or as otherwise stated in this License, Licensor may not terminate this License with Licensee. 28 | 29 | * Enforceability and Interpretation. If any term or provision of this License is determined to be invalid, illegal, or unenforceable by a court of competent jurisdiction, then such invalidity, illegality, or unenforceability shall not affect any other term or provision of this License or invalidate or render unenforceable such term or provision in any other jurisdiction; provided, however, subject to a court modification pursuant to the immediately following sentence, if any term or provision of this License pertaining to Human Rights Laws or Human Rights Principles is deemed invalid, illegal, or unenforceable against Licensee by a court of competent jurisdiction, all rights in the Software granted to Licensee shall be deemed null and void as between Licensor and Licensee. Upon a determination that any term or provision is invalid, illegal, or unenforceable, to the extent permitted by Laws, the court may modify this License to affect the original purpose that the Software be used in compliance with Human Rights Principles and Human Rights Laws as closely as possible. The language in this License shall be interpreted as to its fair meaning and not strictly for or against any party. 30 | 31 | * Disclaimer. TO THE FULL EXTENT ALLOWED BY LAW, THIS SOFTWARE COMES “AS IS,” WITHOUT ANY WARRANTY, EXPRESS OR IMPLIED, AND LICENSOR AND ANY OTHER CONTRIBUTOR SHALL NOT BE LIABLE TO ANYONE FOR ANY DAMAGES OR OTHER LIABILITY ARISING FROM, OUT OF, OR IN CONNECTION WITH THE SOFTWARE OR THIS LICENSE, UNDER ANY KIND OF LEGAL CLAIM. 32 | 33 | This Hippocratic License is an Ethical Source license (https://ethicalsource.dev) and is offered for use by licensors and licensees at their own risk, on an “AS IS” basis, and with no warranties express or implied, to the maximum extent permitted by Laws. 34 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # `with_clues` - temporarily provide more context when tests fail. Beats `puts` 2 | 3 | Suppose you have this: 4 | 5 | ```ruby 6 | expect(page).to have_content("My Awesome Site") 7 | ``` 8 | 9 | And Capybara says that that content is not there and that is all it says. You might slap in a `puts page.html` and try again. Instead, what if you could not do that and do this? 10 | 11 | ```ruby 12 | with_clues do 13 | expect(page).to have_content("My Awesome Site") 14 | end 15 | ``` 16 | 17 | And *that* would print out your HTML? Or your JavaScript console? Or whatever else? Neat, right? 18 | 19 | ## Install 20 | 21 | ``` 22 | gem install with_clues 23 | ``` 24 | 25 | Or, in `Gemfile`: 26 | 27 | ```ruby 28 | gem "with_clues" 29 | ``` 30 | 31 | For Rails, you might want to do this: 32 | 33 | ```ruby 34 | gem "with_clues", group: :test 35 | ``` 36 | 37 | Then `bundle install` 38 | 39 | ## Setup 40 | 41 | Best thing to do is mix into your base test class. 42 | 43 | ### For Minitest 44 | 45 | If you are using Rails, probably something like this: 46 | 47 | ```ruby 48 | # test/test_helper.rb 49 | require "with_clues" 50 | 51 | class ActiveSupport::TestCase 52 | include WithClues::Method 53 | 54 | # ... 55 | end 56 | ``` 57 | 58 | If you aren't using Rails, add the `require` and `include` wherever you configure your base test case (or just put it in each test individually). 59 | 60 | ### For RSpec 61 | 62 | You'll want to put this in your `spec/spec_helper.rb` file: 63 | 64 | ```ruby 65 | require "with_clues" 66 | RSpec.configure do |c| 67 | c.include WithClues::Method 68 | end 69 | ``` 70 | 71 | ## Use 72 | 73 | In general, you would not want to wrap all tests with `with_clues`. This is a diagnostic tool to allow you to get more information on a test that is failing. As such, your workflow might be: 74 | 75 | 1. Notice a test failing that you cannot easily diagnose 76 | 1. Wrap the failing assertion in `with_clues`: 77 | 78 | ```ruby 79 | with_clues do 80 | expect(page).to have_selector("div.foo.bar") 81 | end 82 | ``` 83 | 1. Run the test again, and see the additional info. 84 | 1. Once you've made the test pass, remove `with_clues` 85 | 86 | ## Included Clues 87 | 88 | There are three clues included: 89 | 90 | * Dumping HTML - when `page` exists, it will dump the contents of `page.html` (for Selenium) or `page.content` 91 | (for Playwright) when the test fails 92 | * Dumping Browser logs - for a browser-based test, it will dump anything that was `console.log`'ed. This should 93 | work with Selenium and Playwright 94 | * Arbitrary context you pass in, for example when testing an Active Record 95 | 96 | ```ruby 97 | person = Person.where(name: "Pat") 98 | with_clues(person.inspect) do 99 | expect(person.valid?).to eq(true) 100 | end 101 | ``` 102 | 103 | If the test fails, `person.inspect` is included in the output 104 | 105 | ## Adding Your Own Clues 106 | 107 | `with_clues` is intended as a diagnostic tool you can develop and enhance over time. As your team writes more code or develops 108 | more conventions, you can develop diagnostics as well. 109 | 110 | To add one, create a class that implements `dump(notifier, context:)` or `dump(notifier, context:, page:)` or 111 | `dump(notifier, context:, page:, captured_logs)`: 112 | 113 | * `notifier` is a `WithClues::Notifier` that you should use to produce output via the following methods: 114 | * `notifier.notify` - output text, preceded with `[ with_clues ]` (this is so you can tell output from your code vs from `with_clues`) 115 | * `notifier.blank_line` - a blank line (no prefix) 116 | * `notifier.notify_raw` - output text without a prefix, useful for removing ambiguity about what is being output 117 | * `context:` the context passed into `with_clues` (nil if it was omitted) 118 | * `page:` will be given the Selenium or Playwright page object 119 | * `captured_logs:` for Playwright, this will be the browser console logs captured inside the block 120 | 121 | For example, suppose you want to output information about an Active Record like so: 122 | 123 | ```ruby 124 | with_clues(person) do 125 | # some test 126 | end 127 | ``` 128 | 129 | If this test fails, you output the person's ID and any `errors`. 130 | 131 | Create this class, e.g. in `spec/support/active_record_clues.rb`: 132 | 133 | ```ruby 134 | class ActiveRecordClues 135 | def dump(notifier, context:) 136 | if context.kind_of?(ActiveRecord::Base) 137 | notifier.notify "#{context.class}: id: #{context.id}" 138 | notifier.notify "#{context.class}: errors: #{context.errors.inspect}" 139 | end 140 | end 141 | end 142 | ``` 143 | 144 | To use it, call `WithClues::Method.use_custom_clue`, for example, in your `spec_helper.rb`: 145 | 146 | ```ruby 147 | require "with_clues" 148 | require_relative "support/active_record_clues" 149 | 150 | RSpec.configure do |c| 151 | c.include WithClues::Method 152 | end 153 | 154 | WithClues::Method.use_custom_clue ActiveRecordClues 155 | ``` 156 | 157 | You can use multiple clues by repeatedly calling `use_custom_clue` 158 | 159 | Note that if your clue implements the three-arg version of `dump` ( `dump(notifier, context:, page:)` ), it will *only* be used when in 160 | a context where Capybara's `page` element in in play. 161 | 162 | ## Developing 163 | 164 | This uses a Docker-based setup to allow running tests and doing development across all supported Rubies. 165 | 166 | 1. Install Docker 167 | 2. `dx/build` 168 | 3. `dx/start` 169 | 4. `dx/exec -v «version» bash` - runs bash "inside" the container for Ruby `«version»` 170 | 5. `dx/exec -v «version» bin/setup` - install dependencies for Ruby `«version»` 171 | 6. `dx/exec -v «version» bin/ci` - after `bin/setup` will run all tests. 172 | 7. `bin/matrix` - runs all tests on all supported Rubies 173 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | require "bundler/gem_tasks" 2 | task :default => :spec 3 | -------------------------------------------------------------------------------- /bin/ci: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | set -e 4 | echo "[bin/ci] Running tests" 5 | if [ -z $1 ]; then 6 | bin/rspec --format=doc 7 | else 8 | echo "[bin/ci] Generating JUnit XML output to $1" 9 | bin/rspec --format RspecJunitFormatter --out $1 --format=doc 10 | fi 11 | -------------------------------------------------------------------------------- /bin/console: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | 3 | require "bundler/setup" 4 | require "with_clues" 5 | 6 | # You can add fixtures and/or initialization code here to make experimenting 7 | # with your gem easier. You can also use a different console, if you like. 8 | 9 | # (If you use this, don't forget to add pry to your Gemfile!) 10 | # require "pry" 11 | # Pry.start 12 | 13 | require "irb" 14 | IRB.start(__FILE__) 15 | -------------------------------------------------------------------------------- /bin/matrix: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | . dx/docker-compose.env 4 | for version in ${RUBY_VERSIONS[@]}; do 5 | dx/exec -v $version bin/setup && dx/exec -v $version bin/ci 6 | done 7 | -------------------------------------------------------------------------------- /bin/rake: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | # frozen_string_literal: true 3 | 4 | # 5 | # This file was generated by Bundler. 6 | # 7 | # The application 'rake' is installed as part of a gem, and 8 | # this file is here to facilitate running it. 9 | # 10 | 11 | require "pathname" 12 | ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile", 13 | Pathname.new(__FILE__).realpath) 14 | 15 | bundle_binstub = File.expand_path("../bundle", __FILE__) 16 | 17 | if File.file?(bundle_binstub) 18 | if File.read(bundle_binstub, 300) =~ /This file was generated by Bundler/ 19 | load(bundle_binstub) 20 | else 21 | abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run. 22 | Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.") 23 | end 24 | end 25 | 26 | require "rubygems" 27 | require "bundler/setup" 28 | 29 | load Gem.bin_path("rake", "rake") 30 | -------------------------------------------------------------------------------- /bin/rspec: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | # frozen_string_literal: true 3 | 4 | # 5 | # This file was generated by Bundler. 6 | # 7 | # The application 'rspec' is installed as part of a gem, and 8 | # this file is here to facilitate running it. 9 | # 10 | 11 | require "pathname" 12 | ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile", 13 | Pathname.new(__FILE__).realpath) 14 | 15 | bundle_binstub = File.expand_path("../bundle", __FILE__) 16 | 17 | if File.file?(bundle_binstub) 18 | if File.read(bundle_binstub, 300) =~ /This file was generated by Bundler/ 19 | load(bundle_binstub) 20 | else 21 | abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run. 22 | Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.") 23 | end 24 | end 25 | 26 | require "rubygems" 27 | require "bundler/setup" 28 | 29 | load Gem.bin_path("rspec-core", "rspec") 30 | -------------------------------------------------------------------------------- /bin/setup: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | set -euo pipefail 3 | IFS=$'\n\t' 4 | set -vx 5 | 6 | bundle check || bundle install 7 | -------------------------------------------------------------------------------- /docker-compose.dx.yml: -------------------------------------------------------------------------------- 1 | # THIS IS GENERATED - DO NOT EDIT 2 | 3 | services: 4 | with-clues-3.2: 5 | image: sustainable-rails/with-clues-dev:ruby-3.2 6 | init: true 7 | volumes: 8 | - type: bind 9 | source: "./" 10 | target: "/root/work" 11 | consistency: "consistent" 12 | entrypoint: /root/show-help-in-app-container-then-wait.sh 13 | working_dir: /root/work 14 | with-clues-3.3: 15 | image: sustainable-rails/with-clues-dev:ruby-3.3 16 | init: true 17 | volumes: 18 | - type: bind 19 | source: "./" 20 | target: "/root/work" 21 | consistency: "consistent" 22 | entrypoint: /root/show-help-in-app-container-then-wait.sh 23 | working_dir: /root/work 24 | with-clues-3.4: 25 | image: sustainable-rails/with-clues-dev:ruby-3.4 26 | init: true 27 | volumes: 28 | - type: bind 29 | source: "./" 30 | target: "/root/work" 31 | consistency: "consistent" 32 | entrypoint: /root/show-help-in-app-container-then-wait.sh 33 | working_dir: /root/work 34 | -------------------------------------------------------------------------------- /dx/build: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | set -e 4 | 5 | SCRIPT_DIR=$( cd -- "$( dirname -- "${0}" )" > /dev/null 2>&1 && pwd ) 6 | 7 | . "${SCRIPT_DIR}/dx.sh.lib" 8 | 9 | require_command "docker" 10 | load_docker_compose_env 11 | 12 | usage_on_help "Builds the Docker image based on the Dockerfile" "" "build.pre" "build.post" "${@}" 13 | 14 | for ruby_version in ${RUBY_VERSIONS[@]}; do 15 | dockerfile="Dockerfile.dx" 16 | docker_image_name="${IMAGE}:ruby-${ruby_version}" 17 | 18 | log "Building for Ruby '${ruby_version}' using Docker image name '${docker_image_name}'" 19 | 20 | exec_hook_if_exists "build.pre" "${dockerfile}" "${docker_image_name}" 21 | 22 | docker build \ 23 | --file "${dockerfile}" \ 24 | --build-arg="RUBY_VERSION=${ruby_version}" \ 25 | --tag "${docker_image_name}" \ 26 | ./ 27 | 28 | exec_hook_if_exists "build.post" "${dockerfile}" "${docker_image_name}" 29 | log "🌈" "Your Docker image has been built tagged '${docker_image_name}'" 30 | done 31 | 32 | log "✅" "All images built" 33 | 34 | log "✨" "Creating docker-compose.dx.yml" 35 | compose_file="docker-compose.dx.yml" 36 | log "🗑️" "Deleting previous ${compose_file}" 37 | 38 | rm -f "${compose_file}" 39 | echo "# THIS IS GENERATED - DO NOT EDIT" > "${compose_file}" 40 | echo "" >> "${compose_file}" 41 | echo "services:" >> "${compose_file}" 42 | 43 | for ruby_version in ${RUBY_VERSIONS[@]}; do 44 | log "Generating stanza for version '${ruby_version}'" 45 | docker_image_name="${IMAGE}:ruby-${ruby_version}" 46 | echo " with-clues-${ruby_version}:" >> "${compose_file}" 47 | echo " image: ${docker_image_name}" >> "${compose_file}" 48 | echo " init: true" >> "${compose_file}" 49 | echo " volumes:" >> "${compose_file}" 50 | echo " - type: bind" >> "${compose_file}" 51 | echo " source: \"./\"" >> "${compose_file}" 52 | echo " target: \"/root/work\"" >> "${compose_file}" 53 | echo " consistency: \"consistent\"" >> "${compose_file}" 54 | echo " entrypoint: /root/show-help-in-app-container-then-wait.sh" >> "${compose_file}" 55 | echo " working_dir: /root/work" >> "${compose_file}" 56 | done 57 | log "🎼" "${compose_file} is now created" 58 | log "🔄" "You can run dx/start to start it up, though you may need to stop it first with Ctrl-C" 59 | 60 | # vim: ft=bash 61 | -------------------------------------------------------------------------------- /dx/docker-compose.env: -------------------------------------------------------------------------------- 1 | # This array must include the oldest Ruby first! 2 | RUBY_VERSIONS=("3.2" "3.3" "3.4") 3 | IMAGE=sustainable-rails/with-clues-dev 4 | PROJECT_NAME=with-clues 5 | # vim: ft=bash 6 | -------------------------------------------------------------------------------- /dx/dx.sh.lib: -------------------------------------------------------------------------------- 1 | # shellcheck shell=bash 2 | 3 | . "${SCRIPT_DIR}/setupkit.sh.lib" 4 | 5 | require_command "realpath" 6 | require_command "cat" 7 | 8 | ENV_FILE=$(realpath "${SCRIPT_DIR}")/docker-compose.env 9 | 10 | load_docker_compose_env() { 11 | . "${ENV_FILE}" 12 | } 13 | 14 | exec_hook_if_exists() { 15 | script_name=$1 16 | shift 17 | if [ -x "${SCRIPT_DIR}"/"${script_name}" ]; then 18 | log "🪝" "${script_name} exists - executing" 19 | "${SCRIPT_DIR}"/"${script_name}" "${@}" 20 | else 21 | debug "${script_name} does not exist" 22 | fi 23 | } 24 | # vim: ft=bash 25 | -------------------------------------------------------------------------------- /dx/exec: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | set -e 4 | 5 | SCRIPT_DIR=$( cd -- "$( dirname -- "${0}" )" > /dev/null 2>&1 && pwd ) 6 | 7 | . "${SCRIPT_DIR}/dx.sh.lib" 8 | 9 | require_command "docker" 10 | load_docker_compose_env 11 | 12 | usage_description="Execute a command inside the app's container." 13 | usage_args="[-s service] [-v ruby_version] command" 14 | usage_pre="exec.pre" 15 | usage_on_help "${usage_description}" "${usage_args}" "${usage_pre}" "" "${@}" 16 | 17 | LATEST_RUBY=${RUBY_VERSIONS[0]} 18 | DEFAULT_SERVICE=with-clues-${LATEST_RUBY} 19 | SERVICE="${SERVICE_NAME:-${DEFAULT_SERVICE}}" 20 | while getopts "v:s:" opt "${@}"; do 21 | case ${opt} in 22 | v ) 23 | SERVICE="with-clues-${OPTARG}" 24 | ;; 25 | s ) 26 | SERVICE="${OPTARG}" 27 | ;; 28 | \? ) 29 | log "🛑" "Unknown option: ${opt}" 30 | usage "${description}" "${usage_args}" "${usage_pre}" 31 | ;; 32 | : ) 33 | log "🛑" "Invalid option: ${opt} requires an argument" 34 | usage "${description}" "${usage_args}" "${usage_pre}" 35 | ;; 36 | esac 37 | done 38 | shift $((OPTIND -1)) 39 | 40 | if [ $# -eq 0 ]; then 41 | log "🛑" "You must provide a command e.g. bash or ls -l" 42 | usage "${description}" "${usage_args}" "${usage_pre}" 43 | fi 44 | 45 | 46 | exec_hook_if_exists "exec.pre" 47 | 48 | log "🚂" "Running '${*}' inside container with service name '${SERVICE}'" 49 | 50 | docker \ 51 | compose \ 52 | --file docker-compose.dx.yaml \ 53 | --project-name "${PROJECT_NAME}" \ 54 | --env-file "${ENV_FILE}" \ 55 | exec \ 56 | "${SERVICE}" \ 57 | "${@}" 58 | 59 | # vim: ft=bash 60 | -------------------------------------------------------------------------------- /dx/prune: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | set -e 4 | 5 | SCRIPT_DIR=$( cd -- "$( dirname -- "${0}" )" > /dev/null 2>&1 && pwd ) 6 | 7 | . "${SCRIPT_DIR}/dx.sh.lib" 8 | require_command "docker" 9 | load_docker_compose_env 10 | 11 | usage_on_help "Prune containers for this repo" "" "" "" "${@}" 12 | 13 | for container_id in $(docker container ls -a -f "name=^${PROJECT_NAME}-.*-1$" --format="{{.ID}}"); do 14 | log "🗑" "Removing container with id '${container_id}'" 15 | docker container rm "${container_id}" 16 | done 17 | echo "🧼" "Containers removed" 18 | 19 | # vim: ft=bash 20 | -------------------------------------------------------------------------------- /dx/setupkit.sh.lib: -------------------------------------------------------------------------------- 1 | # shellcheck shell=bash 2 | 3 | fatal() { 4 | remainder=${*:2} 5 | if [ -z "$remainder" ]; then 6 | log "🛑" "${@}" 7 | else 8 | log "${@}" 9 | fi 10 | exit 1 11 | } 12 | 13 | log() { 14 | emoji=$1 15 | remainder=${*:2} 16 | if [ -z "${NO_EMOJI}" ]; then 17 | echo "[ ${0} ] ${*}" 18 | else 19 | # if remainder is empty that means no emoji was passed 20 | if [ -z "$remainder" ]; then 21 | echo "[ ${0} ] ${*}" 22 | else # emoji was passed, but we ignore it 23 | echo "[ ${0} ] ${remainder}" 24 | fi 25 | fi 26 | } 27 | 28 | debug() { 29 | message=$1 30 | if [ -z "${DOCKBOX_DEBUG}" ]; then 31 | return 32 | fi 33 | log "🐛" "${message}" 34 | } 35 | 36 | usage() { 37 | description=$1 38 | arg_names=$2 39 | pre_hook=$3 40 | post_hook=$4 41 | echo "usage: ${0} [-h] ${arg_names}" 42 | if [ -n "${description}" ]; then 43 | echo 44 | echo "DESCRIPTION" 45 | echo " ${description}" 46 | fi 47 | if [ -n "${pre_hook}" ] || [ -n "${post_hook}" ]; then 48 | echo 49 | echo "HOOKS" 50 | if [ -n "${pre_hook}" ]; then 51 | echo " ${pre_hook} - if present, called before the main action" 52 | fi 53 | if [ -n "${post_hook}" ]; then 54 | echo " ${post_hook} - if present, called after the main action" 55 | fi 56 | fi 57 | exit 0 58 | } 59 | 60 | usage_on_help() { 61 | description=$1 62 | arg_names=$2 63 | pre_hook=$3 64 | post_hook=$4 65 | # These are the args passed to the invocation so this 66 | # function can determine if the user requested help 67 | cli_args=( "${@:5}" ) 68 | 69 | for arg in "${cli_args[@]}"; do 70 | if [ "${arg}" = "-h" ] || [ "${arg}" = "--help" ]; then 71 | usage "${description}" "${arg_names}" "${pre_hook}" "${post_hook}" 72 | fi 73 | done 74 | } 75 | 76 | # Read user input into the variable 'INPUT' 77 | # 78 | # Args: 79 | # 80 | # [1] - an emoji to use for messages 81 | # [2] - the message explaining what input is being requested 82 | # [3] - a default value to use if no value is provided 83 | # 84 | # Respects NO_EMOJI when outputing messages to the user 85 | user_input() { 86 | emoji=$1 87 | message=$2 88 | default=$3 89 | prompt=$4 90 | 91 | if [ -z "$message" ]; then 92 | echo "user_input requires a message" 93 | exit 1 94 | fi 95 | 96 | INPUT= 97 | 98 | if [ -z "${prompt}" ]; then 99 | prompt=$(log "${emoji}" "Value: ") 100 | if [ -n "${default}" ]; then 101 | prompt=$(log "${emoji}" "Value (or hit return to use '${default}'): ") 102 | fi 103 | fi 104 | 105 | while [ -z "${INPUT}" ]; do 106 | 107 | log "$emoji" "$message" 108 | read -r -p "${prompt}" INPUT 109 | if [ -z "$INPUT" ]; then 110 | INPUT=$default 111 | fi 112 | if [ -z "$INPUT" ]; then 113 | log "😶", "You must provide a value" 114 | fi 115 | done 116 | } 117 | 118 | user_confirm() { 119 | user_input "$1" "$2" "$3" "y/n> " 120 | } 121 | 122 | require_not_exist() { 123 | file=$1 124 | message=$2 125 | if [ -e "${file}" ]; then 126 | fatal "$message" 127 | fi 128 | } 129 | require_exist() { 130 | file=$1 131 | message=$2 132 | if [ ! -e "${file}" ]; then 133 | fatal "$message" 134 | fi 135 | } 136 | 137 | require_command() { 138 | command_name=$1 139 | if ! command -v "${command_name}" >/dev/null 2>&1; then 140 | fatal "Command '${command_name}' not found - it is required for this script to run" 141 | fi 142 | } 143 | 144 | # vim: ft=bash 145 | -------------------------------------------------------------------------------- /dx/show-help-in-app-container-then-wait.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | set -e 4 | 5 | # Ideally, the message below is shown after everything starts up. We can't 6 | # achieve this using healtchecks because the interval for a healtcheck is 7 | # also an initial delay, and we don't really want to do healthchecks on 8 | # our DB or Redis every 2 seconds. So, we sleep just a bit to let 9 | # the other containers start up and vomit out their output first. 10 | sleep 2 11 | # Output some helpful messaging when invoking `dx/start` (which itself is 12 | # a convenience script for `docker compose up`. 13 | # 14 | # Adding this to work around the mild inconvenience of the `app` container's 15 | # entrypoint generating no output. 16 | # 17 | cat <<-'PROMPT' 18 | 19 | 20 | 21 | 🎉 Dev Environment Initialized! 🎉 22 | 23 | ℹ️ To use this environment, open a new terminal and run 24 | 25 | dx/exec bash 26 | 27 | 🕹 Use `ctrl-c` to exit. 28 | 29 | 30 | 31 | PROMPT 32 | 33 | # Using `sleep infinity` instead of `tail -f /dev/null`. This may be a 34 | # performance improvement based on the conversation on a semi-related 35 | # StackOverflow page. 36 | # 37 | # @see https://stackoverflow.com/a/41655546 38 | sleep infinity 39 | -------------------------------------------------------------------------------- /dx/start: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | set -e 4 | 5 | SCRIPT_DIR=$( cd -- "$( dirname -- "${0}" )" > /dev/null 2>&1 && pwd ) 6 | 7 | . "${SCRIPT_DIR}/dx.sh.lib" 8 | require_command "docker" 9 | load_docker_compose_env 10 | 11 | usage_on_help "Starts all services, including a container in which to run your app" "" "" "" "${@}" 12 | 13 | log "🚀" "Starting docker-compose.dx.yml" 14 | 15 | BUILD=--build 16 | if [ "${1}" == "--no-build" ]; then 17 | BUILD= 18 | fi 19 | 20 | docker \ 21 | compose \ 22 | --file docker-compose.dx.yml \ 23 | --project-name "${PROJECT_NAME}" \ 24 | --env-file "${ENV_FILE}" \ 25 | up \ 26 | "${BUILD}" \ 27 | --timestamps \ 28 | --force-recreate 29 | 30 | # vim: ft=bash 31 | -------------------------------------------------------------------------------- /dx/stop: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | set -e 4 | 5 | SCRIPT_DIR=$( cd -- "$( dirname -- "${0}" )" > /dev/null 2>&1 && pwd ) 6 | 7 | . "${SCRIPT_DIR}/dx.sh.lib" 8 | require_command "docker" 9 | load_docker_compose_env 10 | 11 | usage_on_help "Stops all services, the container in which to run your app and removes any volumes" "" "" "" "${@}" 12 | 13 | log "🚀" "Stopping docker-compose.dx.yml" 14 | 15 | docker \ 16 | compose \ 17 | --file docker-compose.dx.yml \ 18 | --project-name "${PROJECT_NAME}" \ 19 | --env-file "${ENV_FILE}" \ 20 | down \ 21 | --volumes 22 | 23 | # vim: ft=bash 24 | -------------------------------------------------------------------------------- /lib/with_clues.rb: -------------------------------------------------------------------------------- 1 | module WithClues 2 | end 3 | require_relative "with_clues/method" 4 | -------------------------------------------------------------------------------- /lib/with_clues/browser_logs.rb: -------------------------------------------------------------------------------- 1 | module WithClues 2 | class BrowserLogs 3 | def dump(notifier, page:, context:, captured_logs: []) 4 | if !page.respond_to?(:driver) 5 | notifier.notify "Something may be wrong. page (#{page.class}) does not respond to #driver" 6 | return 7 | end 8 | if page.driver.respond_to?(:browser) 9 | logs = locate_logs(page.driver.browser, notifier: notifier) 10 | if !logs.nil? 11 | browser_logs = logs.get(:browser) 12 | notifier.notify "BROWSER LOGS {" 13 | browser_logs.each do |log| 14 | notifier.notify_raw log.message 15 | end 16 | notifier.notify "} END BROWSER LOGS" 17 | end 18 | else 19 | notifier.notify "[with_clues: #{self.class}] NO BROWSER LOGS: page.driver #{page.driver.class} does not respond to #browser" 20 | end 21 | end 22 | 23 | private 24 | 25 | def locate_logs(browser, notifier:) 26 | if browser.respond_to?(:logs) 27 | return browser.logs 28 | elsif browser.respond_to?(:manage) 29 | if browser.manage.respond_to?(:logs) 30 | return browser.manage.logs 31 | end 32 | notifier.notify "[with_clues: #{self.class}] NO BROWSER LOGS: page.driver.browser.manage #{browser.manage.class} does not respond to #logs" 33 | else 34 | notifier.notify "[with_clues: #{self.class}] NO BROWSER LOGS: page.driver.browser #{browser.class} does not respond to #manage or #logs" 35 | end 36 | nil 37 | end 38 | 39 | end 40 | end 41 | -------------------------------------------------------------------------------- /lib/with_clues/html.rb: -------------------------------------------------------------------------------- 1 | module WithClues 2 | class Html 3 | def dump(notifier, page:, context:, captured_logs: []) 4 | access_page_html = if page.respond_to?(:html) 5 | ->(page) { page.html } 6 | elsif page.respond_to?(:content) 7 | ->(page) { page.content } 8 | elsif page.respond_to?(:native) 9 | ->(page) { page.native } 10 | else 11 | notifier.notify "Something may be wrong. page (#{page.class}) does not respond to #html, #native, or #content" 12 | return 13 | end 14 | notifier.blank_line 15 | notifier.notify "HTML {" 16 | notifier.blank_line 17 | notifier.notify_raw access_page_html.(page) 18 | notifier.blank_line 19 | notifier.notify "} END HTML" 20 | if captured_logs.any? 21 | notifier.notify "LOGS {" 22 | notifier.blank_line 23 | captured_logs.each do |log| 24 | notifier.notify_raw log 25 | end 26 | notifier.blank_line 27 | notifier.notify "} END LOGS" 28 | end 29 | end 30 | end 31 | end 32 | -------------------------------------------------------------------------------- /lib/with_clues/method.rb: -------------------------------------------------------------------------------- 1 | require_relative "html" 2 | require_relative "browser_logs" 3 | require_relative "notifier" 4 | require_relative "private/custom_clue_method_analysis" 5 | 6 | module WithClues 7 | module Method 8 | @@clue_classes = { 9 | require_page: [ 10 | WithClues::BrowserLogs, 11 | WithClues::Html, 12 | ], 13 | custom: [] 14 | } 15 | # Wrap any assertion with this method to get more 16 | # useful context and diagnostics when a test is 17 | # unexpectedly failing 18 | def with_clues(context=nil, &block) 19 | notifier = WithClues::Notifier.new($stdout) 20 | captured_logs = [] 21 | if defined?(page) && page.respond_to?(:on) 22 | begin 23 | page.on("console", ->(msg) { captured_logs << msg.text }) 24 | rescue => ex 25 | raise ex 26 | notifier.notify "'page' was defined and responds to #on, however invoking it generated an exception: #{ex}" 27 | end 28 | end 29 | block.() 30 | notifier.notify "A passing test has been wrapped with `with_clues`. You should remove the call to `with_clues`" 31 | rescue Exception => ex 32 | notifier.notify context 33 | @@clue_classes[:custom].each do |klass| 34 | klass.new.dump(notifier, context: context) 35 | end 36 | if defined?(page) 37 | notifier.notify "Test failed: #{ex.message}" 38 | @@clue_classes[:require_page].each do |klass| 39 | klass.new.dump(notifier, context: context, page: page, captured_logs: captured_logs) 40 | end 41 | end 42 | raise ex 43 | end 44 | 45 | def self.use_custom_clue(klass) 46 | dump_method = klass.instance_method(:dump) 47 | analysis = WithClues::Private::CustomClueMethodAnalysis.from_method(dump_method) 48 | if analysis.standard_implementation? 49 | @@clue_classes[:custom] << klass 50 | elsif analysis.requires_page_object? 51 | @@clue_classes[:require_page] << klass 52 | else 53 | analysis.raise_exception! 54 | end 55 | end 56 | end 57 | end 58 | -------------------------------------------------------------------------------- /lib/with_clues/notifier.rb: -------------------------------------------------------------------------------- 1 | module WithClues 2 | class Notifier 3 | def initialize(io) 4 | @io = io 5 | end 6 | 7 | def blank_line 8 | self.notify_raw("") 9 | end 10 | 11 | def notify(message) 12 | @io.puts "[ with_clues ] #{message}" 13 | end 14 | 15 | def notify_raw(message) 16 | @io.puts message 17 | end 18 | end 19 | end 20 | -------------------------------------------------------------------------------- /lib/with_clues/private/custom_clue_method_analysis.rb: -------------------------------------------------------------------------------- 1 | module WithClues 2 | module Private 3 | class CustomClueMethodAnalysis 4 | 5 | def self.from_method(unbound_method) 6 | 7 | params = unbound_method.parameters.map { |param_array| Param.new(param_array) } 8 | 9 | if params.size == 2 10 | two_arg_method = TwoArgMethod.new(params) 11 | if two_arg_method.valid? 12 | return StandardImplementation.new 13 | end 14 | 15 | return BadParams.new(two_arg_method.errors) 16 | 17 | elsif params.size == 4 18 | three_arg_method = FourArgMethod.new(params) 19 | if three_arg_method.valid? 20 | return RequiresPageObject.new 21 | end 22 | return BadParams.new(three_arg_method.errors) 23 | end 24 | 25 | BadParams.new([ 26 | "dump (#{unbound_method.owner}) accepted #{params.size} arguments, not 2 or 4. Got: #{params.map(&:to_s).join(", ")}, should be one positional and either one keyword arg named 'context:' or three keyword args named 'context:', 'page:', and 'captured_logs:'" 27 | ]) 28 | end 29 | 30 | def standard_implementation? 31 | false 32 | end 33 | 34 | def requires_page_object? 35 | false 36 | end 37 | 38 | def raise_exception! 39 | raise StandardError.new("Unimplemented condition found inside #from_method") 40 | end 41 | 42 | class Param 43 | 44 | def initialize(method_param_array) 45 | @type = method_param_array[0] 46 | @name = method_param_array[1] 47 | 48 | end 49 | 50 | def required? 51 | @type == :req 52 | end 53 | def keyword_required? 54 | @type == :keyreq 55 | end 56 | 57 | def named?(*allowed_names) 58 | allowed_names.include?(@name) 59 | end 60 | def name 61 | if self.keyword_required? 62 | "#{@name}:" 63 | else 64 | @name 65 | end 66 | end 67 | def to_s 68 | "#<#{self.class} #{name}/#{@type}" 69 | end 70 | end 71 | 72 | class TwoArgMethod 73 | attr_reader :errors 74 | def initialize(params) 75 | @errors = [] 76 | if !params[0].required? 77 | @errors << "Param 1, #{params[0].name}, is not required" 78 | end 79 | require_keyword(2,params[1]) 80 | end 81 | 82 | def valid? 83 | @errors.empty? 84 | end 85 | private 86 | 87 | def require_keyword(param_number, param) 88 | if !param.keyword_required? 89 | @errors << "Param #{param_number}, #{param.name}, is not a required keyword param" 90 | end 91 | if !param.named?(*allowed_names) 92 | @errors << "Param #{param_number}, #{param.name}, should be one of #{allowed_names.join(',')}" 93 | end 94 | end 95 | 96 | def allowed_names 97 | [ :context ] 98 | end 99 | end 100 | 101 | class FourArgMethod < TwoArgMethod 102 | def initialize(params) 103 | super(params) 104 | require_keyword(3,params[2]) 105 | require_keyword(4,params[3]) 106 | end 107 | private 108 | def allowed_names 109 | [ :context, :page, :captured_logs ] 110 | end 111 | end 112 | 113 | end 114 | 115 | class GoodParams < CustomClueMethodAnalysis 116 | def raise_exception! 117 | raise StandardError.new("You should not have called .exception on a #{self.class.name}") 118 | end 119 | end 120 | 121 | class RequiresPageObject < CustomClueMethodAnalysis 122 | def requires_page_object? 123 | true 124 | end 125 | end 126 | 127 | class StandardImplementation < CustomClueMethodAnalysis 128 | def standard_implementation? 129 | true 130 | end 131 | end 132 | 133 | class BadParams < CustomClueMethodAnalysis 134 | def initialize(errors) 135 | if errors.empty? 136 | raise ArgumentError,"BadParams requires errors" 137 | else 138 | @message = errors.map(&:to_s).join(", ") 139 | end 140 | end 141 | 142 | DEFAULT_ERROR = "dump must take one required param, one keyword param named context: and an optional keyword param named page:" 143 | 144 | def raise_exception! 145 | raise NameError.new(@message) 146 | end 147 | end 148 | end 149 | end 150 | -------------------------------------------------------------------------------- /lib/with_clues/version.rb: -------------------------------------------------------------------------------- 1 | module WithClues 2 | VERSION="1.3.0" 3 | end 4 | -------------------------------------------------------------------------------- /spec/custom_clues_spec.rb: -------------------------------------------------------------------------------- 1 | require "spec_helper" 2 | require "ostruct" 3 | require "stringio" 4 | 5 | require "with_clues" 6 | 7 | RSpec.describe "custom clues" do 8 | include WithClues::Method 9 | 10 | class MyCustomClue 11 | def dump(notifier, context:) 12 | notifier.notify "OH YEAH" 13 | end 14 | end 15 | 16 | class MyCustomClueThatNeedsThePage 17 | def dump(notifier, context:, page:, captured_logs:) 18 | notifier.notify "YUP" 19 | end 20 | end 21 | 22 | WithClues::Method.use_custom_clue(MyCustomClue) 23 | WithClues::Method.use_custom_clue(MyCustomClueThatNeedsThePage) 24 | 25 | let(:fake_stdout) { StringIO.new } 26 | before do 27 | @stdout = $stdout 28 | $stdout = fake_stdout 29 | end 30 | 31 | after do 32 | $stdout = @stdout 33 | end 34 | 35 | context "clues that won't work" do 36 | context "no dump method" do 37 | it "raises an error" do 38 | clazz = Class.new 39 | expect { 40 | WithClues::Method.use_custom_clue(clazz) 41 | }.to raise_error(NameError,/undefined method.*dump/) 42 | end 43 | end 44 | context "has a dump method" do 45 | context "takes one arg" do 46 | it "raises an error" do 47 | clazz = Class.new 48 | clazz.define_method(:dump) do |x| 49 | end 50 | expect { 51 | WithClues::Method.use_custom_clue(clazz) 52 | }.to raise_error(NameError,/accepted 1 arguments, not 2 or 4/) 53 | end 54 | end 55 | context "takes 2 args" do 56 | context "second arg is context:" do 57 | it "works" do 58 | clazz = Class.new 59 | clazz.define_method(:dump) do |x, context: | 60 | end 61 | expect { 62 | WithClues::Method.use_custom_clue(clazz) 63 | }.not_to raise_error 64 | end 65 | end 66 | context "second arg is not context:" do 67 | it "raises an error" do 68 | clazz = Class.new 69 | clazz.define_method(:dump) do |x, not_context: | 70 | end 71 | expect { 72 | WithClues::Method.use_custom_clue(clazz) 73 | }.to raise_error(NameError,/not_context:/) 74 | end 75 | end 76 | end 77 | context "takes 4 args" do 78 | context "third arg is page: and fourth is captured_logs:" do 79 | it "works" do 80 | clazz = Class.new 81 | clazz.define_method(:dump) do |x, context: , page:, captured_logs:| 82 | end 83 | expect { 84 | WithClues::Method.use_custom_clue(clazz) 85 | }.not_to raise_error 86 | end 87 | end 88 | context "second arg is page:, third is context: and fourth is captured_logs:" do 89 | it "works" do 90 | clazz = Class.new 91 | clazz.define_method(:dump) do |x, page:, context:, captured_logs:| 92 | end 93 | expect { 94 | WithClues::Method.use_custom_clue(clazz) 95 | }.not_to raise_error 96 | end 97 | end 98 | context "third arg is not page:" do 99 | it "raises an error" do 100 | clazz = Class.new 101 | clazz.define_method(:dump) do |x, page:, foo:, captured_logs: | 102 | end 103 | expect { 104 | WithClues::Method.use_custom_clue(clazz) 105 | }.to raise_error(NameError,/foo:/) 106 | end 107 | end 108 | end 109 | context "takes 3 args" do 110 | it "raises an error" do 111 | clazz = Class.new 112 | clazz.define_method(:dump) do |x, page:, context:| 113 | end 114 | expect { 115 | WithClues::Method.use_custom_clue(clazz) 116 | }.to raise_error(NameError,/accepted 3 arguments, not 2 or 4/) 117 | end 118 | end 119 | end 120 | end 121 | 122 | context "not in a browser context" do 123 | it "includes only the non-browser custom clue on a test failure" do 124 | expect { 125 | with_clues do 126 | expect(true).to eq(false) 127 | end 128 | }.to raise_error(RSpec::Expectations::ExpectationNotMetError) 129 | expect(fake_stdout.string).to match(/\[ with_clues \] OH YEAH/) 130 | expect(fake_stdout.string).not_to match(/\[ with_clues \] YUP/) 131 | end 132 | end 133 | context "in a browser context" do 134 | before do 135 | Object.define_method(:page) do 136 | Object.new 137 | end 138 | end 139 | 140 | after do 141 | Object.remove_method(:page) 142 | end 143 | 144 | it "includes the browser custom clue as well on a test failure" do 145 | expect { 146 | with_clues do 147 | expect(true).to eq(false) 148 | end 149 | }.to raise_error(RSpec::Expectations::ExpectationNotMetError) 150 | expect(fake_stdout.string).to match(/\[ with_clues \] OH YEAH/) 151 | expect(fake_stdout.string).to match(/\[ with_clues \] YUP/) 152 | end 153 | end 154 | end 155 | -------------------------------------------------------------------------------- /spec/spec_helper.rb: -------------------------------------------------------------------------------- 1 | RSpec.configure do |config| 2 | config.expect_with :rspec do |expectations| 3 | expectations.include_chain_clauses_in_custom_matcher_descriptions = true 4 | end 5 | 6 | config.mock_with :rspec do |mocks| 7 | mocks.verify_partial_doubles = true 8 | end 9 | 10 | config.shared_context_metadata_behavior = :apply_to_host_groups 11 | 12 | config.disable_monkey_patching! 13 | 14 | config.warnings = true 15 | 16 | if config.files_to_run.one? 17 | config.default_formatter = "doc" 18 | end 19 | 20 | config.order = :random 21 | 22 | Kernel.srand config.seed 23 | end 24 | -------------------------------------------------------------------------------- /spec/with_clues_spec.rb: -------------------------------------------------------------------------------- 1 | require "spec_helper" 2 | require "ostruct" 3 | require "stringio" 4 | 5 | require "with_clues" 6 | 7 | RSpec.describe WithClues::Method do 8 | include WithClues::Method 9 | let(:fake_stdout) { StringIO.new } 10 | before do 11 | @stdout = $stdout 12 | $stdout = fake_stdout 13 | end 14 | 15 | after do 16 | $stdout = @stdout 17 | undef page rescue nil 18 | end 19 | 20 | class LogsFromSomeBrowser 21 | def initialize(logs) 22 | @logs = logs.map { |log| 23 | OpenStruct.new(message: log) 24 | } 25 | end 26 | 27 | def get(sym) 28 | if sym != :browser 29 | raise "Don't know how to get(#{sym})" 30 | end 31 | @logs 32 | end 33 | end 34 | 35 | describe "#with_clues" do 36 | context "when code raises an exception" do 37 | context "and page is defined" do 38 | context "page responds to html" do 39 | it "dumps the HTML, then raises" do 40 | def page 41 | OpenStruct.new(html: "some html", driver: OpenStruct.new()) 42 | end 43 | expect { 44 | with_clues do 45 | expect(true).to eq(false) 46 | end 47 | }.to raise_error(RSpec::Expectations::ExpectationNotMetError) 48 | aggregate_failures do 49 | expect(fake_stdout.string).to match(/\[ with_clues \] HTML \{/) 50 | expect(fake_stdout.string).to match(/\[ with_clues \] \} END HTML/) 51 | expect(fake_stdout.string).to match(/some html/) 52 | end 53 | end 54 | it "includes context, if given" do 55 | def page 56 | OpenStruct.new(html: "some html", driver: OpenStruct.new()) 57 | end 58 | expect { 59 | with_clues("some context") do 60 | expect(true).to eq(false) 61 | end 62 | }.to raise_error(RSpec::Expectations::ExpectationNotMetError) 63 | aggregate_failures do 64 | expect(fake_stdout.string).to match(/\[ with_clues \] HTML \{/) 65 | expect(fake_stdout.string).to match(/\[ with_clues \] \} END HTML/) 66 | expect(fake_stdout.string).to match(/some html/) 67 | expect(fake_stdout.string).to match(/some context/) 68 | end 69 | end 70 | context "and driver responds to browser" do 71 | context "and browser responds to manage" do 72 | context "and manage responds to logs" do 73 | it "dumps the logs" do 74 | def page 75 | logs = LogsFromSomeBrowser.new([ 76 | "log line 1", 77 | "log line 2", 78 | "log line 3", 79 | ]) 80 | OpenStruct.new( 81 | html: "some html", 82 | driver: OpenStruct.new( 83 | browser: OpenStruct.new( 84 | manage: OpenStruct.new( 85 | logs: logs 86 | ) 87 | ) 88 | ) 89 | ) 90 | end 91 | expect { 92 | with_clues do 93 | expect(true).to eq(false) 94 | end 95 | }.to raise_error(RSpec::Expectations::ExpectationNotMetError) 96 | aggregate_failures do 97 | expect(fake_stdout.string).to match(/\[ with_clues \] BROWSER LOGS \{/) 98 | expect(fake_stdout.string).to match(/\[ with_clues \] \} END BROWSER LOGS/) 99 | expect(fake_stdout.string).to match(/log line 1/) 100 | expect(fake_stdout.string).to match(/log line 2/) 101 | expect(fake_stdout.string).to match(/log line 3/) 102 | expect(fake_stdout.string).to match(/\[ with_clues \] HTML \{/) 103 | expect(fake_stdout.string).to match(/\[ with_clues \] \} END HTML/) 104 | expect(fake_stdout.string).to match(/some html/) 105 | end 106 | end 107 | end 108 | context "but manage does not respond to logs" do 109 | it "raises and prints a warning" do 110 | def page 111 | OpenStruct.new( 112 | html: "some html", 113 | driver: OpenStruct.new( 114 | browser: OpenStruct.new( 115 | manage: OpenStruct.new() 116 | ) 117 | ) 118 | ) 119 | end 120 | expect { 121 | with_clues do 122 | expect(true).to eq(false) 123 | end 124 | }.to raise_error(RSpec::Expectations::ExpectationNotMetError) 125 | aggregate_failures do 126 | expect(fake_stdout.string).to match(/NO BROWSER LOGS/) 127 | expect(fake_stdout.string).to match(/#{page.driver.browser.manage.class}/) 128 | expect(fake_stdout.string).to match(/does not respond to #logs/) 129 | expect(fake_stdout.string).to match(/\[ with_clues \] HTML \{/) 130 | expect(fake_stdout.string).to match(/\[ with_clues \] \} END HTML/) 131 | expect(fake_stdout.string).to match(/some html/) 132 | end 133 | end 134 | end 135 | end 136 | context "but browser does not respond to manage" do 137 | context "yet it responds to logs" do 138 | it "dumps the logs" do 139 | def page 140 | logs = LogsFromSomeBrowser.new([ 141 | "log line 1", 142 | "log line 2", 143 | "log line 3", 144 | ]) 145 | OpenStruct.new( 146 | html: "some html", 147 | driver: OpenStruct.new( 148 | browser: OpenStruct.new( 149 | logs: logs 150 | ) 151 | ) 152 | ) 153 | end 154 | expect { 155 | with_clues do 156 | expect(true).to eq(false) 157 | end 158 | }.to raise_error(RSpec::Expectations::ExpectationNotMetError) 159 | aggregate_failures do 160 | expect(fake_stdout.string).to match(/\[ with_clues \] BROWSER LOGS \{/) 161 | expect(fake_stdout.string).to match(/\[ with_clues \] \} END BROWSER LOGS/) 162 | expect(fake_stdout.string).to match(/log line 1/) 163 | expect(fake_stdout.string).to match(/log line 2/) 164 | expect(fake_stdout.string).to match(/log line 3/) 165 | expect(fake_stdout.string).to match(/\[ with_clues \] HTML \{/) 166 | expect(fake_stdout.string).to match(/\[ with_clues \] \} END HTML/) 167 | expect(fake_stdout.string).to match(/some html/) 168 | end 169 | end 170 | end 171 | context "and it does not respond to logs" do 172 | it "raises and prints a warning" do 173 | def page 174 | OpenStruct.new( 175 | html: "some html", 176 | driver: OpenStruct.new( 177 | browser: OpenStruct.new() 178 | ) 179 | ) 180 | end 181 | expect { 182 | with_clues do 183 | expect(true).to eq(false) 184 | end 185 | }.to raise_error(RSpec::Expectations::ExpectationNotMetError) 186 | aggregate_failures do 187 | expect(fake_stdout.string).to match(/NO BROWSER LOGS/) 188 | expect(fake_stdout.string).to match(/#{page.driver.browser.class}/) 189 | expect(fake_stdout.string).to match(/does not respond to #manage/) 190 | expect(fake_stdout.string).to match(/\[ with_clues \] HTML \{/) 191 | expect(fake_stdout.string).to match(/\[ with_clues \] \} END HTML/) 192 | expect(fake_stdout.string).to match(/some html/) 193 | end 194 | end 195 | end 196 | end 197 | end 198 | context "but driver does not respond to browser" do 199 | it "raises and prints a warning" do 200 | def page 201 | OpenStruct.new( 202 | html: "some html", 203 | driver: OpenStruct.new() 204 | ) 205 | end 206 | expect { 207 | with_clues do 208 | expect(true).to eq(false) 209 | end 210 | }.to raise_error(RSpec::Expectations::ExpectationNotMetError) 211 | aggregate_failures do 212 | expect(fake_stdout.string).to match(/NO BROWSER LOGS/) 213 | expect(fake_stdout.string).to match(/#{page.driver.class}/) 214 | expect(fake_stdout.string).to match(/does not respond to #browser/) 215 | expect(fake_stdout.string).to match(/\[ with_clues \] HTML \{/) 216 | expect(fake_stdout.string).to match(/\[ with_clues \] \} END HTML/) 217 | expect(fake_stdout.string).to match(/some html/) 218 | end 219 | end 220 | end 221 | context "but page does not respond to html" do 222 | it "raises and prints a warning" do 223 | def page 224 | OpenStruct.new( 225 | driver: OpenStruct.new() 226 | ) 227 | end 228 | expect { 229 | with_clues do 230 | expect(true).to eq(false) 231 | end 232 | }.to raise_error(RSpec::Expectations::ExpectationNotMetError) 233 | aggregate_failures do 234 | expect(fake_stdout.string).to match(/Something may be wrong/) 235 | expect(fake_stdout.string).to match(/#{page.class}/) 236 | expect(fake_stdout.string).to match(/does not respond to #html/) 237 | end 238 | end 239 | end 240 | context "but page does not respond to driver" do 241 | it "raises and prints a warning" do 242 | def page 243 | OpenStruct.new( 244 | html: "some html" 245 | ) 246 | end 247 | expect { 248 | with_clues do 249 | expect(true).to eq(false) 250 | end 251 | }.to raise_error(RSpec::Expectations::ExpectationNotMetError) 252 | aggregate_failures do 253 | expect(fake_stdout.string).to match(/Something may be wrong/) 254 | expect(fake_stdout.string).to match(/#{page.class}/) 255 | expect(fake_stdout.string).to match(/does not respond to #driver/) 256 | end 257 | end 258 | end 259 | end 260 | context "but page does not respond to html" do 261 | context "but it does respond to #content as in Playwright" do 262 | it "prints the HTML" do 263 | def page 264 | OpenStruct.new( 265 | content: "some html", 266 | ) 267 | end 268 | expect { 269 | with_clues do 270 | expect(true).to eq(false) 271 | end 272 | }.to raise_error(RSpec::Expectations::ExpectationNotMetError) 273 | aggregate_failures do 274 | expect(fake_stdout.string).to match(/\[ with_clues \] HTML \{/) 275 | expect(fake_stdout.string).to match(/\[ with_clues \] \} END HTML/) 276 | expect(fake_stdout.string).to match(/some html/) 277 | end 278 | end 279 | context "it also responds to on to record console logs" do 280 | it "includes those logs after the HTML" do 281 | def page 282 | @page ||= begin 283 | page = OpenStruct.new( 284 | content: "some html" 285 | ) 286 | def page.on(event,block) 287 | if event == "console" 288 | @block = block 289 | end 290 | end 291 | def page.trigger_logs 292 | @block.(OpenStruct.new(text: "first console log")) 293 | @block.(OpenStruct.new(text: "second console log")) 294 | end 295 | page 296 | end 297 | end 298 | expect { 299 | with_clues do 300 | page.trigger_logs 301 | expect(true).to eq(false) 302 | end 303 | }.to raise_error(RSpec::Expectations::ExpectationNotMetError) 304 | aggregate_failures do 305 | expect(fake_stdout.string).to match(/\[ with_clues \] HTML \{/) 306 | expect(fake_stdout.string).to match(/\[ with_clues \] \} END HTML/) 307 | expect(fake_stdout.string).to match(/some html/) 308 | expect(fake_stdout.string).to match(/\[ with_clues \] LOGS \{/) 309 | expect(fake_stdout.string).to match(/\[ with_clues \] \} END LOGS/) 310 | expect(fake_stdout.string).to match(/first console log/) 311 | expect(fake_stdout.string).to match(/second console log/) 312 | end 313 | end 314 | end 315 | end 316 | context "nor does it respond to #content" do 317 | it "raises and prints a warning" do 318 | def page 319 | OpenStruct.new( 320 | driver: OpenStruct.new() 321 | ) 322 | end 323 | expect { 324 | with_clues do 325 | expect(true).to eq(false) 326 | end 327 | }.to raise_error(RSpec::Expectations::ExpectationNotMetError) 328 | aggregate_failures do 329 | expect(fake_stdout.string).to match(/Something may be wrong/) 330 | expect(fake_stdout.string).to match(/#{page.class}/) 331 | expect(fake_stdout.string).to match(/does not respond to #html/) 332 | end 333 | end 334 | end 335 | context "page does respond to #native" do 336 | it "uses native to access HTML" do 337 | #OpenStruct.new(native: "some html", driver: OpenStruct.new()) 338 | def page 339 | OpenStruct.new( 340 | native: "some html", 341 | ) 342 | end 343 | expect { 344 | with_clues do 345 | expect(true).to eq(false) 346 | end 347 | }.to raise_error(RSpec::Expectations::ExpectationNotMetError) 348 | aggregate_failures do 349 | expect(fake_stdout.string).to match(/\[ with_clues \] HTML \{/) 350 | expect(fake_stdout.string).to match(/\[ with_clues \] \} END HTML/) 351 | expect(fake_stdout.string).to match(/some html/) 352 | end 353 | end 354 | end 355 | context "page does not respond to #native" do 356 | it "raises an error" do 357 | def page 358 | OpenStruct.new() 359 | end 360 | expect { 361 | with_clues do 362 | expect(true).to eq(false) 363 | end 364 | }.to raise_error(RSpec::Expectations::ExpectationNotMetError) 365 | aggregate_failures do 366 | expect(fake_stdout.string).to include("Something may be wrong") 367 | expect(fake_stdout.string).to include("does not respond to #html, #native, or #content") 368 | end 369 | end 370 | end 371 | end 372 | end 373 | context "but page is not defined" do 374 | it "lets the error bubble up" do 375 | expect { 376 | with_clues do 377 | expect(true).to eq(false) 378 | end 379 | }.to raise_error(RSpec::Expectations::ExpectationNotMetError) 380 | end 381 | end 382 | end 383 | context "when code does not raise an exception" do 384 | it "logs that you should remove with_clues" do 385 | expect { 386 | with_clues do 387 | expect(true).to eq(true) 388 | end 389 | }.not_to raise_error 390 | expect(fake_stdout.string).to match(/passing.*remove/) 391 | end 392 | end 393 | end 394 | end 395 | -------------------------------------------------------------------------------- /with_clues.gemspec: -------------------------------------------------------------------------------- 1 | require_relative "lib/with_clues/version" 2 | 3 | Gem::Specification.new do |spec| 4 | spec.name = "with_clues" 5 | spec.version = WithClues::VERSION 6 | spec.authors = ["Dave Copeland"] 7 | spec.email = ["davec@naildrivin5.com"] 8 | spec.summary = %q{Temporarily add context to failing tests to get more information, such as what HTML was being examined when a browser-based test fails.} 9 | spec.homepage = "https://sustainable-rails.com" 10 | spec.license = "Hippocratic" 11 | 12 | spec.required_ruby_version = Gem::Requirement.new(">= 2.5.0") 13 | 14 | spec.metadata["homepage_uri"] = spec.homepage 15 | spec.metadata["source_code_uri"] = "https://github.com/sustainable-rails/with_clues" 16 | spec.metadata["changelog_uri"] = "https://github.com/sustainable-rails/with_clues/releases" 17 | 18 | spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do 19 | `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) } 20 | end 21 | spec.bindir = "exe" 22 | spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) } 23 | spec.require_paths = ["lib"] 24 | 25 | spec.add_development_dependency("rspec") 26 | spec.add_development_dependency("minitest") 27 | spec.add_development_dependency("capybara") 28 | spec.add_development_dependency("rspec_junit_formatter") 29 | end 30 | --------------------------------------------------------------------------------