From d159fac7b5360bb4ee15cfc92dd76fd3da87e6e5 Mon Sep 17 00:00:00 2001 From: Matt Date: Tue, 2 Nov 2021 10:13:09 +0000 Subject: [PATCH] Remove legacy scripts and convert `bootstrap`, `test` and `console` into the more up-to-date bin scripts --- bin/console | 15 ++++ bin/setup | 7 ++ bin/test | 7 ++ script/bootstrap | 12 --- script/console | 7 -- script/generate_certs | 48 ------------ script/proxy-server | 45 ----------- script/server | 39 ---------- script/test | 173 ------------------------------------------ 9 files changed, 29 insertions(+), 324 deletions(-) create mode 100755 bin/console create mode 100755 bin/setup create mode 100755 bin/test delete mode 100755 script/bootstrap delete mode 100755 script/console delete mode 100755 script/generate_certs delete mode 100755 script/proxy-server delete mode 100755 script/server delete mode 100755 script/test diff --git a/bin/console b/bin/console new file mode 100755 index 000000000..40446e2f1 --- /dev/null +++ b/bin/console @@ -0,0 +1,15 @@ +#!/usr/bin/env ruby +# frozen_string_literal: true + +require 'bundler/setup' +require 'faraday' + +# You can add fixtures and/or initialization code here to make experimenting +# with your gem easier. You can also use a different console, if you like. + +# (If you use this, don't forget to add pry to your Gemfile!) +# require "pry" +# Pry.start + +require 'irb' +IRB.start(__FILE__) diff --git a/bin/setup b/bin/setup new file mode 100755 index 000000000..ff8b4096c --- /dev/null +++ b/bin/setup @@ -0,0 +1,7 @@ +#!/usr/bin/env bash +set -euo pipefail +IFS=$'\n\t' +set -vx + +gem install bundler +bundle install --jobs 4 diff --git a/bin/test b/bin/test new file mode 100755 index 000000000..92920beec --- /dev/null +++ b/bin/test @@ -0,0 +1,7 @@ +#!/usr/bin/env bash +set -euo pipefail +IFS=$'\n\t' +set -vx + +bundle exec rubocop -a --format progress +bundle exec rspec diff --git a/script/bootstrap b/script/bootstrap deleted file mode 100755 index 5c2694b65..000000000 --- a/script/bootstrap +++ /dev/null @@ -1,12 +0,0 @@ -#!/bin/bash -set -e - -if ! bundle config build.eventmachine | grep -q 'cppflags='; then - if openssl_dir="$(brew --prefix openssl 2>/dev/null)"; then - bundle config --local build.eventmachine \ - --with-cppflags="-I${openssl_dir}/include" \ - --with-ldflags="-L${openssl_dir}/lib" - fi -fi - -bundle install --path vendor/bundle --jobs 4 diff --git a/script/console b/script/console deleted file mode 100755 index 4e16d58e7..000000000 --- a/script/console +++ /dev/null @@ -1,7 +0,0 @@ -#!/usr/bin/env bash -# Usage: script/console -# Starts an IRB console with this library loaded. - -gemspec="$(ls *.gemspec | head -1)" - -exec bundle exec irb -r "${gemspec%.*}" "$@" diff --git a/script/generate_certs b/script/generate_certs deleted file mode 100755 index 33633a941..000000000 --- a/script/generate_certs +++ /dev/null @@ -1,48 +0,0 @@ -#!/usr/bin/env ruby - -# frozen_string_literal: true - -# Usage: generate_certs [options] -# options: -# -s Display shell exports that link env variables to filenames -# Generate test certs for testing Faraday with SSL - -require 'openssl' -require 'fileutils' - -# Adapted from WEBrick::Utils. Skips cert extensions so it -# can be used as a CA bundle -def create_self_signed_cert(bits, cname, _comment) - rsa = OpenSSL::PKey::RSA.new(bits) - cert = OpenSSL::X509::Certificate.new - cert.version = 2 - cert.serial = 1 - name = OpenSSL::X509::Name.new(cname) - cert.subject = name - cert.issuer = name - cert.not_before = Time.now - cert.not_after = Time.now + (365 * 24 * 60 * 60) - cert.public_key = rsa.public_key - cert.sign(rsa, OpenSSL::Digest.new('SHA1')) - [cert, rsa] -end - -def write(file, contents, env_var) - FileUtils.mkdir_p(File.dirname(file)) - File.open(file, 'w') { |f| f.puts(contents) } - puts %(export #{env_var}="#{file}") if ARGV.include? '-s' -end - -# One cert / CA for ease of testing when ignoring verification -cert, key = create_self_signed_cert(1024, [%w[CN localhost]], - 'Faraday Test CA') - -write 'tmp/faraday-cert.key', key, 'SSL_KEY' -write 'tmp/faraday-cert.crt', cert, 'SSL_FILE' - -# And a second CA to prove that verification can fail -cert, key = create_self_signed_cert(1024, [%w[CN real-ca.com]], - 'A different CA') - -write 'tmp/faraday-different-ca-cert.key', key, 'SSL_KEY_ALT' -write 'tmp/faraday-different-ca-cert.crt', cert, 'SSL_FILE_ALT' diff --git a/script/proxy-server b/script/proxy-server deleted file mode 100755 index a7cf0bdb6..000000000 --- a/script/proxy-server +++ /dev/null @@ -1,45 +0,0 @@ -#!/usr/bin/env ruby -# frozen_string_literal: true - -# Usage: script/proxy-server [-p PORT] [-u USER:PASSWORD] -require 'webrick' -require 'webrick/httpproxy' - -port = 4001 - -if (found = ARGV.index('-p')) - port = ARGV[found + 1].to_i -end -if (found = ARGV.index('-u')) - username, password = ARGV[found + 1].split(':', 2) -end - -match_credentials = lambda { |credentials| - got_username, got_password = credentials.to_s.unpack1('m*').split(':', 2) - got_username == username && got_password == password -} - -log_io = $stdout -log_io.sync = true - -webrick_opts = { - Port: port, Logger: WEBrick::Log.new(log_io), - AccessLog: [[log_io, '[%{X-Faraday-Adapter}i] %m %U -> %s %b']], - ProxyAuthProc: lambda { |req, res| - if username - type, credentials = req.header['proxy-authorization'] - .first.to_s.split(/\s+/, 2) - unless type == 'Basic' && match_credentials.call(credentials) - res['proxy-authenticate'] = %(Basic realm="testing") - raise WEBrick::HTTPStatus::ProxyAuthenticationRequired - end - end - } -} - -proxy = WEBrick::HTTPProxyServer.new(webrick_opts) - -trap(:TERM) { proxy.shutdown } -trap(:INT) { proxy.shutdown } - -proxy.start diff --git a/script/server b/script/server deleted file mode 100755 index 1105c629e..000000000 --- a/script/server +++ /dev/null @@ -1,39 +0,0 @@ -#!/usr/bin/env ruby -# frozen_string_literal: true - -old_verbose = $VERBOSE -$VERBOSE = nil -begin - require File.expand_path('../test/live_server', __dir__) -ensure - $VERBOSE = old_verbose -end -require 'webrick' - -port = 4000 -if (found = ARGV.index('-p')) - port = ARGV[found + 1].to_i -end - -log_io = $stdout -log_io.sync = true - -webrick_opts = { - Port: port, Logger: WEBrick::Log.new(log_io), - AccessLog: [[log_io, '[%{X-Faraday-Adapter}i] %m %U -> %s %b']] -} - -if ENV['SSL_KEY'] - require 'openssl' - require 'webrick/https' - webrick_opts.update \ - SSLEnable: true, - SSLPrivateKey: OpenSSL::PKey::RSA.new(File.read(ENV['SSL_KEY'])), - SSLCertificate: OpenSSL::X509::Certificate.new(File.read(ENV['SSL_FILE'])), - SSLVerifyClient: OpenSSL::SSL::VERIFY_NONE -end - -Rack::Handler::WEBrick.run(Faraday::LiveServer, webrick_opts) do |server| - trap(:INT) { server.stop } - trap(:TERM) { server.stop } -end diff --git a/script/test b/script/test deleted file mode 100755 index 2a918bb76..000000000 --- a/script/test +++ /dev/null @@ -1,173 +0,0 @@ -#!/usr/bin/env bash -# Usage: script/test [file] [adapter]... -- [test/unit options] -# Runs the test suite against a local server spawned automatically in a -# thread. After tests are done, the server is shut down. -# -# If filename arguments are given, only those files are run. If arguments given -# are not filenames, they are taken as words that filter the list of files to run. -# -# Examples: -# -# $ script/test -# $ script/test test/env_test.rb -# $ script/test excon patron -# -# # Run only tests matching /ssl/ for the net_http adapter, with SSL enabled. -# $ SSL=yes script/test net_http -- -n /ssl/ -# -# # Run against multiple rbenv versions -# $ RBENV_VERSIONS="1.9.3-p194 ree-1.8.7-2012.02" script/test -set -e - -if [[ "$RUBYOPT" != *"bundler/setup"* ]]; then - export RUBYOPT="-rbundler/setup $RUBYOPT" -fi - -port=3999 -proxy_port=3998 -scheme=http - -if [ "$SSL" = "yes" ]; then - scheme=https - if [ -z "$SSL_KEY" ] || [ -z "$SSL_FILE" ]; then - eval "$(script/generate_certs -s)" - fi -fi - -find_test_files() { - find "$1" -name '*_test.rb' -} - -filter_matching() { - pattern="$1" - shift - for line in "$@"; do - [[ $line == *"$pattern"* ]] && echo "$line" - done -} - -start_server() { - mkdir -p log - script/server -p $port >log/test.log 2>&1 & - echo $! -} - -start_proxy() { - mkdir -p log - script/proxy-server -p $proxy_port -u "faraday@test.local:there is cake" >log/proxy.log 2>&1 & - echo $! -} - -server_started() { - lsof -i :${1?} >/dev/null -} - -timestamp() { - date +%s -} - -wait_for_server() { - timeout=$(( `timestamp` + $1 )) - while true; do - if server_started "$2"; then - break - elif [ `timestamp` -gt "$timeout" ]; then - echo "timed out after $1 seconds" >&2 - return 1 - fi - done -} - -filtered= -IFS=$'\n' test_files=($(find_test_files "test")) -declare -a explicit_files - -# Process filter arguments: -# - test filenames as taken as-is -# - other words are taken as pattern to match the list of known files against -# - arguments after "--" are forwarded to the ruby process -while [ $# -gt 0 ]; do - arg="$1" - shift - if [ "$arg" = "--" ]; then - break - elif [ -f "$arg" ]; then - filtered=true - explicit_files[${#explicit_files[@]}+1]="$arg" - else - filtered=true - IFS=$'\n' explicit_files=( - ${explicit_files[@]} - $(filter_matching "$arg" "${test_files[@]}" || true) - ) - fi -done - -# If there were filter args, replace test files list with the results -if [ -n "$filtered" ]; then - if [ ${#explicit_files[@]} -eq 0 ]; then - echo "Error: no test files match" >&2 - exit 1 - else - test_files=(${explicit_files[@]}) - echo running "${test_files[@]}" - fi -fi - -# If there are any adapter tests, spin up the HTTP server -if [ -n "$(filter_matching "adapters" "${test_files[@]}")" ]; then - if server_started $port; then - echo "aborted: another instance of server running on $port" >&2 - exit 1 - fi - server_pid=$(start_server) - proxy_pid=$(start_proxy) - wait_for_server 30 $port || { - cat log/test.log - kill "$server_pid" - kill "$proxy_pid" - exit 1 - } - wait_for_server 5 $proxy_port - cleanup() { - if [ $? -ne 0 ] && [ -n "$TRAVIS" ]; then - cat log/test.log - cat log/proxy.log - fi - kill "$server_pid" - kill "$proxy_pid" - } - trap cleanup INT EXIT - export LIVE="${scheme}://localhost:${port}" - export LIVE_PROXY="http://faraday%40test.local:there%20is%20cake@localhost:${proxy_port}" -fi - -warnings="${TMPDIR:-/tmp}/faraday-warnings.$$" - -run_test_files() { - # Save warnings on stderr to a separate file - RUBYOPT="$RUBYOPT -w" ruby -e 'while f=ARGV.shift and f!="--"; load f; end' "${test_files[@]}" -- "$@" \ - 2> >(tee >(grep 'warning:' >"$warnings") | grep -v 'warning:') -} - -check_warnings() { - # Display Ruby warnings from this project's source files. Abort if any were found. - num="$(grep -F "$PWD" "$warnings" | grep -v "${PWD}/vendor/bundle" | sort | uniq -c | sort -rn | tee /dev/stderr | wc -l)" - rm -f "$warnings" - if [ "$num" -gt 0 ]; then - echo "FAILED: this test suite doesn't tolerate Ruby syntax warnings!" >&2 - exit 1 - fi -} - -if [ -n "$RBENV_VERSIONS" ]; then - IFS=' ' versions=($RBENV_VERSIONS) - for version in "${versions[@]}"; do - echo "[${version}]" - RBENV_VERSION="$version" run_test_files "$@" - done -else - run_test_files "$@" -fi - -check_warnings