silent-stream-1.0.6/0000755000175000017500000000000014407572776013014 5ustar raviravisilent-stream-1.0.6/silent_stream.gemspec0000644000175000017500000000543314407572776017237 0ustar raviravi# frozen_string_literal: true lib = File.expand_path('lib', __dir__) $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) require 'silent_stream/version' Gem::Specification.new do |spec| authors = [ # Everyone who touched the files extracted from Rails: ['jeremy', 'Jeremy Daer'], ['dhh', 'David Heinemeier Hansson'], ['pixeltrix', 'Andrew White'], ['spastorino', 'Santiago Pastorino'], ['sstephenson', 'Sam Stephenson'], ['amatsuda', 'Akira Matsuda'], ['Raphomet', 'Raphael Lee'], ['rafaelfranca', 'Rafael França'], ['mariovisic', 'Mario Visic'], ['krekoten', "Мар'ян Крекотень"], ['lest', 'Sergey Nartimov'], ['joshk', 'Josh Kalderimis'], ['fxn', 'Xavier Noria'], ['deivid-rodriguez', 'David Rodríguez'], ['route', 'Dmitry Vorotilin'], ['tenderlove', 'Aaron Patterson'], ['guilleiguaran', 'Guillermo Iguaran'], ['gazay', 'Alexey Gaziev'], ['wycats', 'Yehuda Katz'], ['tommeier', 'Tom Meier'], ['lifo', 'Pratik Naik'], ['charliesome', 'Charlie Somerville'], ['atambo', 'Alex Tambellini'], ['arthurnn', 'Arthur Nogueira Neves'], ['anildigital', 'Anil Wadghule'], # Author/Maintainer of this gem: ['pboling', 'Peter Boling'] ] spec.name = 'silent_stream' spec.version = SilentStream::VERSION spec.authors = authors.map { |_gh, name| name } spec.email = ['peter.boling@gmail.com'] spec.summary = "ActiveSupport's Stream Silencing - Without ActiveSupport" spec.description = 'ActiveSupport Kernel Reporting Detritus with a few enhancements' spec.homepage = 'https://github.com/pboling/silent_stream' # Specify which files should be added to the gem when it is released. # The `git ls-files -z` loads the files in the RubyGem that have been added into git. spec.files = Dir.chdir(File.expand_path(__dir__)) do `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(tests|spec|features)/}) } end spec.bindir = 'exe' spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) } spec.require_paths = ['lib'] spec.license = 'MIT' spec.add_development_dependency 'rake' spec.add_development_dependency 'appraisal' spec.add_development_dependency 'bundler' spec.add_development_dependency 'minitest', '>= 5.10' spec.add_development_dependency 'minitest-reporters' spec.add_development_dependency 'mocha' spec.add_development_dependency 'simplecov', '>= 0.16' spec.add_development_dependency 'test-unit', '>= 3.2' spec.add_development_dependency 'wwtd' end silent-stream-1.0.6/lib/0000755000175000017500000000000014407572776013562 5ustar raviravisilent-stream-1.0.6/lib/silent_stream/0000755000175000017500000000000014407572776016433 5ustar raviravisilent-stream-1.0.6/lib/silent_stream/version.rb0000644000175000017500000000011314407572776020440 0ustar raviravi# frozen_string_literal: true module SilentStream VERSION = '1.0.6' end silent-stream-1.0.6/lib/silent_stream.rb0000644000175000017500000001055714407572776016770 0ustar raviravi# frozen_string_literal: true require 'silent_stream/version' require 'tempfile' require 'logger' module SilentStream def self.included(base) base.send(:extend, Extracted) base.send(:include, Extracted) base.send(:extend, Enhanced) base.send(:include, Enhanced) end module Enhanced # param switch is true or false # By default it is true, when means we don't want logging. # Switching it to false enables logging again. # By default ERROR log level continues to be logged. # The return value is the return value of the block, # so you can use it without changing code structure. # # This method is not thread-safe. def silence_all(switch = true, temporary_level = Logger::ERROR, logger = nil) if !switch || silent_stream_no_silence yield else begin logger ||= silent_stream_logger old_logger_level = silent_stream_reset_logger_level(logger, temporary_level) # silence STDOUT (like puts) silence_stream(STDOUT) do yield end ensure silent_stream_reset_logger_level(logger, old_logger_level) end end end private def silent_stream_no_silence ENV['NO_SILENCE'] == 'true' end def silent_stream_logger defined?(Rails) ? Rails.logger : nil end # returns previous logger's level def silent_stream_reset_logger_level(logger, temporary_level) logger && (old_logger_level = logger.level || true) && (logger.level = temporary_level) old_logger_level end end # Extracted from: # https://github.com/rails/rails/blob/4-2-stable/activesupport/lib/active_support/core_ext/kernel/reporting.rb module Extracted SILENT_STREAM_NULL_DEVICE = defined?(IO::NULL) ? IO::NULL : Gem.win_platform? ? 'NUL:' : '/dev/null' # This method is not thread-safe. def silence_stderr silence_stream(STDERR) { yield } end # Silences any stream for the duration of the block. # # silence_stream(STDOUT) do # puts 'This will never be seen' # end # # puts 'But this will' # # This method is not thread-safe. def silence_stream(stream) old_stream = stream.dup begin stream.reopen(SILENT_STREAM_NULL_DEVICE, 'a+') rescue Exception => e stream.puts "[SilentStream] Unable to silence. #{e.class}: #{e.message}" end stream.sync = true yield ensure stream.reopen(old_stream) old_stream.close end # Captures the given stream and returns it: # # stream = capture(:stdout) { puts 'notice' } # stream # => "notice\n" # # stream = capture(:stderr) { warn 'error' } # stream # => "error\n" # # even for subprocesses: # # stream = capture(:stdout) { system('echo notice') } # stream # => "notice\n" # # stream = capture(:stderr) { system('echo error 1>&2') } # stream # => "error\n" # # This method is not thread-safe. def capture(stream) stream = stream.to_s captured_stream = Tempfile.new(stream) stream_io = eval("$#{stream}") origin_stream = stream_io.dup stream_io.reopen(captured_stream) yield stream_io.rewind captured_stream.read ensure captured_stream.close captured_stream.unlink stream_io.reopen(origin_stream) end # silence is provided by the LoggerSilence concern that continues to be # shipped with Rails, so not continuing with this alias. # alias silence capture # Silences both STDOUT and STDERR, even for subprocesses. # # quietly { system 'bundle install' } # # This method is not thread-safe. def quietly silence_stream(STDOUT) do silence_stream(STDERR) do yield end end end private SILENT_STREAM_WINDOWS_REGEXP = /mswin|mingw/.freeze SILENT_STREAM_REGEXP_HAS_MATCH = Gem::Version.new(RUBY_VERSION) >= Gem::Version.new('2.4') def windows_os_test # When available, in Ruby 2.4+, we use Regexp#match? which does not update # the $~ global object and may be 3x faster than alternative match tests if SILENT_STREAM_REGEXP_HAS_MATCH SILENT_STREAM_WINDOWS_REGEXP.match?(RbConfig::CONFIG['host_os']) else SILENT_STREAM_WINDOWS_REGEXP =~ (RbConfig::CONFIG['host_os']) end end end end silent-stream-1.0.6/gemfiles/0000755000175000017500000000000014407572776014607 5ustar raviravisilent-stream-1.0.6/gemfiles/ruby_2.7.gemfile0000644000175000017500000000020114407572776017501 0ustar raviravi# This file was generated by Appraisal source "https://rubygems.org" gem "rubocop" gem "rubocop-minitest" gemspec path: "../" silent-stream-1.0.6/gemfiles/ruby_2.6.gemfile0000644000175000017500000000020114407572776017500 0ustar raviravi# This file was generated by Appraisal source "https://rubygems.org" gem "rubocop" gem "rubocop-minitest" gemspec path: "../" silent-stream-1.0.6/gemfiles/ruby_2.5.gemfile0000644000175000017500000000020114407572776017477 0ustar raviravi# This file was generated by Appraisal source "https://rubygems.org" gem "rubocop" gem "rubocop-minitest" gemspec path: "../" silent-stream-1.0.6/gemfiles/ruby_2.4.gemfile0000644000175000017500000000021314407572776017501 0ustar raviravi# This file was generated by Appraisal source "https://rubygems.org" gem "rubocop", "0.77.0" gem "rubocop-minitest" gemspec path: "../" silent-stream-1.0.6/gemfiles/ruby_2.3.gemfile0000644000175000017500000000021314407572776017500 0ustar raviravi# This file was generated by Appraisal source "https://rubygems.org" gem "rubocop", "0.77.0" gem "rubocop-minitest" gemspec path: "../" silent-stream-1.0.6/bin/0000755000175000017500000000000014407572776013564 5ustar raviravisilent-stream-1.0.6/bin/setup0000755000175000017500000000020314407572776014645 0ustar raviravi#!/usr/bin/env bash set -euo pipefail IFS=$'\n\t' set -vx bundle install # Do any other automated setup that you need to do here silent-stream-1.0.6/bin/console0000755000175000017500000000057214407572776015160 0ustar raviravi#!/usr/bin/env ruby # frozen_string_literal: true require 'bundler/setup' require 'silent_stream' # 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__) silent-stream-1.0.6/Rakefile0000644000175000017500000000031014407572776014453 0ustar raviravi# frozen_string_literal: true require 'bundler/gem_tasks' require 'rake/testtask' Rake::TestTask.new do |t| t.test_files = FileList['tests/**/test_*.rb'] end desc 'Run tests' task default: :test silent-stream-1.0.6/README.md0000644000175000017500000002003014407572776014266 0ustar raviravi# SilentStream SilentStream is an extraction of some parts of ActiveSupport's Kernel Reporting Core Extentions around silencing IO streams. Since July 2014 `silence_stream`, `silence_stderr`, `capture`, `silence`, and `quietly` have been deprecated because they are not thread safe. See that discussion in the [PR where it all went down](https://github.com/rails/rails/pull/13392). I rely on them a lot in *single threaded* code, and so I plan to keep them alive. With the exception of `silence`, which was just an alias of `capture`. This gem was taken out of Rails but it is *not* Rails dependent. The extraction was total (**even the tests**!), and this is now a pure Ruby library, which can be used in any Ruby project without encumbrances. *This gem has no runtime dependencies*. | Project | SilentStream | |------------------------ | ----------------------- | | gem name | [silent_stream](https://rubygems.org/gems/silent_stream) | | compatibility | Ruby 2.3, 2.4, 2.5, 2.6, 2.7 | | license | [![License: MIT](https://img.shields.io/badge/License-MIT-green.svg)](https://opensource.org/licenses/MIT) | | download rank | [![Downloads Today](https://img.shields.io/gem/rd/silent_stream.svg)](https://github.com/pboling/silent_stream) | | version | [![Version](https://img.shields.io/gem/v/silent_stream.svg)](https://rubygems.org/gems/silent_stream) | | continuous integration | [![Build Status](https://travis-ci.org/pboling/silent_stream.svg?branch=master)](https://travis-ci.org/pboling/silent_stream) | | test coverage | [![Test Coverage](https://api.codeclimate.com/v1/badges/ced7e39984dd9c27c528/test_coverage)](https://codeclimate.com/github/pboling/silent_stream/test_coverage) | | maintainability | [![Maintainability](https://api.codeclimate.com/v1/badges/ced7e39984dd9c27c528/maintainability)](https://codeclimate.com/github/pboling/silent_stream/maintainability) | | dependencies | [![Depfu](https://badges.depfu.com/badges/6633827ecc1ad3b5dd749b4ac822347b/count.svg)](https://depfu.com/github/pboling/silent_stream?project_id=5828) | | code triage | [![Open Source Helpers](https://www.codetriage.com/pboling/silent_stream/badges/users.svg)](https://www.codetriage.com/pboling/silent_stream) | | homepage | [on Github.com][homepage], [on Railsbling.com][blogpage] | | documentation | [on RDoc.info][documentation] | | Spread ~♡ⓛⓞⓥⓔ♡~ | [🌍 🌎 🌏](https://about.me/peter.boling), [🍚](https://www.crowdrise.com/helprefugeeswithhopefortomorrowliberia/fundraiser/peterboling), [➕](https://plus.google.com/+PeterBoling/posts), [👼](https://angel.co/peter-boling), [🐛](https://www.topcoder.com/members/pboling/), [:shipit:](http://coderwall.com/pboling), [![Tweet Peter](https://img.shields.io/twitter/follow/galtzo.svg?style=social&label=Follow)](http://twitter.com/galtzo) | ## NOTE One aspect of what this gem provides can be achieved with the Rails' built-in [`LoggerSilence`](https://github.com/rails/rails/blob/5-2-stable/activesupport/lib/active_support/logger_silence.rb), which is thread safe. You will have to decide what is right for you! ## Doing a Rails <= 4 to Rails >= 5 Upgrade? The reason for not keeping `silence` as it was in Rails 4, i.e. an alias of `capture`, is that the just mentioned `LoggerSilence` now uses this term, and it is shipping with Rails 5. I don't want to make this gem incompatible with Rails 5, so you will have to convert Rails <= 4 implementations that utilize `silence` over to `capture` when using this gem. One further point of difference is this gem does not add the methods to `Kernel` or `Object`. You can do that if you like via `include`. By default this gem does not pollute anything, so you will need to `include SilentStream` in any class using these methods. ## Installation Add this line to your application's Gemfile: ```ruby gem 'silent_stream' ``` And then execute: $ bundle Or install it yourself as: $ gem install silent_stream ## Usage Four standard methods you may be familiar with from ActiveSupport's previous implementation are provided: ``` silence_stderr silence_stream capture quietly ``` They are direct replicas, *except* not mixed into `Kernel` or `Object`, so in order to use them you must mix them into your classes or modules. ``` class Bogosity include SilentStream::Extracted # allows use at instance level extend SilentStream::Extracted # allows use at class level # or include SilentStream # access everything, and add #silence_all method, see below end ``` In addition there is a `silence_all` method that is a useful wrapper that can be easily instrumented (turned off and on) with an ENV variable switch. Including the `SilentStream` namespace fully gives access to this enhanced method, as well as the extracted methods above, and also makes everything available at the class and instance levels. ```ruby class Bogosity include SilentStream # allows use of any method at instance or class level def silent silence_all(true) do puts "play that funky music" Rails.logger.info "git jiggy with it" end end class << self def noise silence_all(false) do puts "play that funky music" Rails.logger.info "git jiggy with it" end end end end ``` And run ``` >> Bogosity.new.silent # has no output => nil >> Bogosity.noise # is noisy play that funky music => nil ``` ## Use in Specs / Tests Make the methods avaialble: ``` RSpec.configure do |conf| conf.include SilentStream end ``` Then add a test on output: ``` it 'has output' do output = capture(:stdout) { subject.request(:get, '/success') } logs = [ 'INFO -- request: GET https://api.example.com/success', 'INFO -- response: Status 200' ] expect(output).to include(*logs) end ``` See it in practice in the [specs for the oauth2 gem](https://github.com/oauth-xx/oauth2/blob/master/spec/oauth2/client_spec.rb#L193) ## Development After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment. To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org). ## Contributing Bug reports and pull requests are welcome on GitHub at https://github.com/pboling/silent_stream. ## Code of Conduct Everyone interacting in the AnonymousActiveRecord project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/pboling/silent_stream/blob/master/CODE_OF_CONDUCT.md). ## Versioning This library aims to adhere to [Semantic Versioning 2.0.0][semver]. Violations of this scheme should be reported as bugs. Specifically, if a minor or patch version is released that breaks backward compatibility, a new version should be immediately released that restores compatibility. Breaking changes to the public API will only be introduced with new major versions. As a result of this policy, you can (and should) specify a dependency on this gem using the [Pessimistic Version Constraint][pvc] with two digits of precision. For example: ```ruby spec.add_dependency 'silent_stream', '~> 1.0' ``` ## License * Copyright (c) 2018 [Peter H. Boling][peterboling] of [Rails Bling][railsbling] [![License: MIT](https://img.shields.io/badge/License-MIT-green.svg)](https://opensource.org/licenses/MIT) [license]: LICENSE [semver]: http://semver.org/ [pvc]: http://guides.rubygems.org/patterns/#pessimistic-version-constraint [railsbling]: http://www.railsbling.com [peterboling]: http://www.peterboling.com [documentation]: http://rdoc.info/github/pboling/silent_stream/frames [homepage]: https://github.com/pboling/silent_stream/ [blogpage]: http://www.railsbling.com/tags/silent_stream/ silent-stream-1.0.6/LICENSE0000644000175000017500000000205714407572776014025 0ustar raviraviCopyright 2018 Peter Boling of railsbling.com Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. silent-stream-1.0.6/Gemfile0000644000175000017500000000031014407572776014301 0ustar raviravi# frozen_string_literal: true source 'https://rubygems.org' git_source(:github) { |repo_name| "https://github.com/#{repo_name}" } # Specify your gem's dependencies in silent_stream.gemspec gemspec silent-stream-1.0.6/CODE_OF_CONDUCT.md0000644000175000017500000000623614407572776015622 0ustar raviravi# Contributor Covenant Code of Conduct ## Our Pledge In the interest of fostering an open and welcoming environment, we as contributors and maintainers pledge to making participation in our project and our community a harassment-free experience for everyone, regardless of age, body size, disability, ethnicity, gender identity and expression, level of experience, nationality, personal appearance, race, religion, or sexual identity and orientation. ## Our Standards Examples of behavior that contributes to creating a positive environment include: * Using welcoming and inclusive language * Being respectful of differing viewpoints and experiences * Gracefully accepting constructive criticism * Focusing on what is best for the community * Showing empathy towards other community members Examples of unacceptable behavior by participants include: * The use of sexualized language or imagery and unwelcome sexual attention or advances * Trolling, insulting/derogatory comments, and personal or political attacks * Public or private harassment * Publishing others' private information, such as a physical or electronic address, without explicit permission * Other conduct which could reasonably be considered inappropriate in a professional setting ## Our Responsibilities Project maintainers are responsible for clarifying the standards of acceptable behavior and are expected to take appropriate and fair corrective action in response to any instances of unacceptable behavior. Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, or to ban temporarily or permanently any contributor for other behaviors that they deem inappropriate, threatening, offensive, or harmful. ## Scope This Code of Conduct applies both within project spaces and in public spaces when an individual is representing the project or its community. Examples of representing a project or community include using an official project e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event. Representation of a project may be further defined and clarified by project maintainers. ## Enforcement Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by contacting the project team at peter.boling@gmail.com. All complaints will be reviewed and investigated and will result in a response that is deemed necessary and appropriate to the circumstances. The project team is obligated to maintain confidentiality with regard to the reporter of an incident. Further details of specific enforcement policies may be posted separately. Project maintainers who do not follow or enforce the Code of Conduct in good faith may face temporary or permanent repercussions as determined by other members of the project's leadership. ## Attribution This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, available at [http://contributor-covenant.org/version/1/4][version] [homepage]: http://contributor-covenant.org [version]: http://contributor-covenant.org/version/1/4/ silent-stream-1.0.6/Appraisals0000644000175000017500000000055414407572776015042 0ustar raviraviappraise "ruby_2.3" do gem "rubocop", "0.77.0" gem 'rubocop-minitest' end appraise "ruby_2.4" do gem "rubocop", "0.77.0" gem 'rubocop-minitest' end appraise "ruby_2.5" do gem "rubocop" gem 'rubocop-minitest' end appraise "ruby_2.6" do gem "rubocop" gem 'rubocop-minitest' end appraise "ruby_2.7" do gem "rubocop" gem 'rubocop-minitest' end silent-stream-1.0.6/.travis.yml0000644000175000017500000000170314407572776015126 0ustar raviravi--- sudo: false language: ruby cache: bundler: true env: global: - CC_TEST_REPORTER_ID=954687c8cc4840dccb8887a7bf1f1f6ea463f6c20c02f20186945ab17995e0ba before_script: - curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter - chmod +x ./cc-test-reporter - ./cc-test-reporter before-build script: - bundle exec rake after_script: - ./cc-test-reporter after-build --exit-code $TRAVIS_TEST_RESULT before_install: - gem update --system - gem install bundler -v 1.16.5 install: - bundle install --path=${BUNDLE_PATH:-vendor/bundle} --no-deployment --jobs 3 --retry 3 jobs: include: - rvm: ruby-2.3.8 gemfile: gemfiles/ruby_2.3.gemfile - rvm: ruby-2.4.9 gemfile: gemfiles/ruby_2.4.gemfile - rvm: ruby-2.5.7 gemfile: gemfiles/ruby_2.5.gemfile - rvm: ruby-2.6.5 gemfile: gemfiles/ruby_2.6.gemfile - rvm: ruby-2.7.0 gemfile: gemfiles/ruby_2.7.gemfile silent-stream-1.0.6/.ruby-version0000644000175000017500000000000614407572776015455 0ustar raviravi2.7.0 silent-stream-1.0.6/.rubocop.yml0000644000175000017500000000005614407572776015267 0ustar raviraviLayout/DotPosition: EnforcedStyle: trailing silent-stream-1.0.6/.gitignore0000644000175000017500000000030014407572776014775 0ustar raviravi/.bundle/ /.yardoc /_yardoc/ /coverage/ /doc/ /pkg/ /spec/reports/ /tmp/ # rspec failure tracking .rspec_status .byebug_history # iCloud Drive .nosync *.gem /Gemfile.lock /gemfiles/*.lock