forewarn 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: aabd513081492a7b1da1723b00b2815b184a1a4f
4
+ data.tar.gz: 839cc7260ff5aed0312e8369109bed058053461e
5
+ SHA512:
6
+ metadata.gz: 9c71014c1163c1f5dbb789af78ec060c71b8ecc43536b4c6780bf9d5a8d4b6703ee84eca08342aae04746c0f4354c434544fc9bdb53dc3d980361ae7e1803481
7
+ data.tar.gz: b641a3c64d217cd8961927e8072f88cf20256d8199c52c8cb5e8fe0554c772ee8cc02f67bf818e750314d0e5701485c9d1f2d7a353a7a00fa13fe1232046809d
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.6
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in forewarn.gemspec
4
+ gemspec
5
+
6
+ gem 'gimme'
7
+ gem 'pry'
@@ -0,0 +1,39 @@
1
+ # Forewarn
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/forewarn`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'forewarn'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install forewarn
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ 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` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ 1. Fork it ( https://github.com/[my-github-username]/forewarn/fork )
36
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
37
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
38
+ 4. Push to the branch (`git push origin my-new-feature`)
39
+ 5. Create a new Pull Request
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ require 'rake/testtask'
4
+
5
+ Rake::TestTask.new do |t|
6
+ t.libs << 'test'
7
+ t.pattern = "test/**/test_*.rb"
8
+ end
9
+
10
+ task :default => [:test]
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "forewarn"
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
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'forewarn/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "forewarn"
8
+ spec.version = Forewarn::VERSION
9
+ spec.authors = ["Justin Searls"]
10
+ spec.email = ["searls@gmail.com"]
11
+
12
+ spec.summary = %q{log deprecated or unsafe method invocations}
13
+ spec.description = %q{allows users to write and configure their own method blacklists to warn of dangerous invocations (e.g. deprecated methods, unsafe methods after an impending upgrade, etc.)}
14
+ spec.homepage = "https://github.com/testdouble/forewarn"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features|docs)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.9"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ end
@@ -0,0 +1,12 @@
1
+ require "forewarn/version"
2
+
3
+ require "forewarn/config"
4
+ require "forewarn/reporters"
5
+ require "forewarn/values"
6
+ require "forewarn/sets_up_warnings"
7
+
8
+ module Forewarn
9
+ def self.start!
10
+ SetsUpWarnings.new.set_up!
11
+ end
12
+ end
@@ -0,0 +1,11 @@
1
+ require "forewarn/values/method"
2
+
3
+ module Forewarn
4
+ class BuildsMethodValues
5
+ def build(warner)
6
+ warner.methods.map do |method_object|
7
+ Values::Method.new(warner, method_object)
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,20 @@
1
+ require "forewarn/reporters/json"
2
+ require "forewarn/warners/string_mutation"
3
+
4
+ module Forewarn
5
+ DEFAULT_CONFIG = {
6
+ :enabled => true,
7
+ :logger => Kernel.method(:warn),
8
+ # TODO: implement a JSON reporter
9
+ # :reporter => Forewarn::Reporters::Json,
10
+ # :report_destination => "log/usage_warning.json",
11
+ :warners => [Forewarn::Warners::StringMutation]
12
+ }.freeze
13
+
14
+ @__config = DEFAULT_CONFIG.dup
15
+
16
+ def self.config(overrides = {})
17
+ @__config.merge!(overrides)
18
+ end
19
+ end
20
+
@@ -0,0 +1,16 @@
1
+ require "forewarn/triggers_warning"
2
+
3
+ module Forewarn
4
+ class OverridesMethods
5
+ def override!(methods)
6
+ triggers_warning = TriggersWarning.new
7
+ methods.each do |method|
8
+ real_method = method.method
9
+ real_method.owner.send(:define_method, real_method.name) do |*args, &blk|
10
+ triggers_warning.trigger!(method, caller_locations(1,1)[0].to_s)
11
+ method.bind(self).call(*args, &blk)
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,8 @@
1
+ module Forewarn
2
+ class RemembersWrappedMethods
3
+ def remember!(methods)
4
+ # TODO: remember which methods were wrapped so we can implement stop!
5
+ end
6
+ end
7
+ end
8
+
@@ -0,0 +1 @@
1
+ require "forewarn/reporters/json"
@@ -0,0 +1,6 @@
1
+ module Forewarn
2
+ module Reporters
3
+ class Json
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,21 @@
1
+ require "forewarn/builds_method_values"
2
+ require "forewarn/remembers_wrapped_methods"
3
+ require "forewarn/overrides_methods"
4
+
5
+ module Forewarn
6
+ class SetsUpWarnings
7
+ def initialize
8
+ @builds_method_values = BuildsMethodValues.new
9
+ @remembers_wrapped_methods = RemembersWrappedMethods.new
10
+ @overrides_methods = OverridesMethods.new
11
+ end
12
+
13
+ def set_up!
14
+ methods = Forewarn.config[:warners].map(&:new).map {|w|
15
+ @builds_method_values.build(w)
16
+ }.flatten
17
+ @overrides_methods.override!(methods)
18
+ @remembers_wrapped_methods.remember!(methods)
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,22 @@
1
+ module Forewarn
2
+ class TriggersWarning
3
+ def trigger!(method, kaller)
4
+ return if backtrace_includes_ourself?
5
+ Forewarn.config[:logger].call(build_warning(method, kaller))
6
+ end
7
+
8
+ private
9
+
10
+ # Need to bail out to prevent infinite recursion… Woo!
11
+ def backtrace_includes_ourself?
12
+ caller_locations.select { |location|
13
+ location.to_s.include?(File.join(%w{forewarn lib forewarn triggers_warning}))
14
+ }.size > 1
15
+ end
16
+
17
+ def build_warning(method, kaller)
18
+ "WARN: #{method.warner.message} '#{method.name}' was invoked! (Called from: \"#{kaller}\")"
19
+ end
20
+ end
21
+ end
22
+
@@ -0,0 +1 @@
1
+ require "forewarn/values/method"
@@ -0,0 +1,30 @@
1
+ module Forewarn
2
+ module Values
3
+ class Method
4
+ attr_reader :warner, :method
5
+
6
+ def initialize(warner = nil, method = nil)
7
+ @warner = warner
8
+ @method = method
9
+ end
10
+
11
+ def instance_method?
12
+ !@method.owner.singleton_class?
13
+ end
14
+
15
+ def name
16
+ @method.to_s[/:\s*(.*)>/, 1]
17
+ end
18
+
19
+ def bind(new_owner)
20
+ if @method.respond_to?(:unbind)
21
+ @method.unbind.bind(new_owner)
22
+ else
23
+ @method.bind(new_owner)
24
+ end
25
+ end
26
+
27
+ end
28
+ end
29
+ end
30
+
@@ -0,0 +1,3 @@
1
+ module Forewarn
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1 @@
1
+ require "forewarn/warners/string_mutation"
@@ -0,0 +1,27 @@
1
+ module Forewarn
2
+ module Warners
3
+ class StringMutation
4
+ def message
5
+ "String mutation method"
6
+ end
7
+
8
+ def methods
9
+ method_names.map { |m| String.instance_method(m) }
10
+ end
11
+
12
+ private
13
+
14
+ def method_names
15
+ String.instance_methods.select { |method_name|
16
+ method_name.to_s.end_with?('!')
17
+ } + [
18
+ :"[]=", :"<<", :clear, :concat, :extend, :insert,
19
+ :instance_variable_set, :prepend, :replace, :setbyte, :taint
20
+ ] - [
21
+ :"!" # <-- lol BasicObject#! is A-okay.
22
+ ]
23
+
24
+ end
25
+ end
26
+ end
27
+ end
metadata ADDED
@@ -0,0 +1,96 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: forewarn
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Justin Searls
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-08-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.9'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.9'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ description: allows users to write and configure their own method blacklists to warn
42
+ of dangerous invocations (e.g. deprecated methods, unsafe methods after an impending
43
+ upgrade, etc.)
44
+ email:
45
+ - searls@gmail.com
46
+ executables: []
47
+ extensions: []
48
+ extra_rdoc_files: []
49
+ files:
50
+ - ".gitignore"
51
+ - ".travis.yml"
52
+ - Gemfile
53
+ - README.md
54
+ - Rakefile
55
+ - bin/console
56
+ - bin/setup
57
+ - forewarn.gemspec
58
+ - lib/forewarn.rb
59
+ - lib/forewarn/builds_method_values.rb
60
+ - lib/forewarn/config.rb
61
+ - lib/forewarn/overrides_methods.rb
62
+ - lib/forewarn/remembers_wrapped_methods.rb
63
+ - lib/forewarn/reporters.rb
64
+ - lib/forewarn/reporters/json.rb
65
+ - lib/forewarn/sets_up_warnings.rb
66
+ - lib/forewarn/triggers_warning.rb
67
+ - lib/forewarn/values.rb
68
+ - lib/forewarn/values/method.rb
69
+ - lib/forewarn/version.rb
70
+ - lib/forewarn/warners.rb
71
+ - lib/forewarn/warners/string_mutation.rb
72
+ homepage: https://github.com/testdouble/forewarn
73
+ licenses: []
74
+ metadata: {}
75
+ post_install_message:
76
+ rdoc_options: []
77
+ require_paths:
78
+ - lib
79
+ required_ruby_version: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ required_rubygems_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ requirements: []
90
+ rubyforge_project:
91
+ rubygems_version: 2.2.3
92
+ signing_key:
93
+ specification_version: 4
94
+ summary: log deprecated or unsafe method invocations
95
+ test_files: []
96
+ has_rdoc: