Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
Igor Wiedler committed Jul 1, 2016
0 parents commit d5eb91b
Show file tree
Hide file tree
Showing 12 changed files with 314 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
--color
--require spec_helper
4 changes: 4 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
source 'https://rubygems.org'

gem 'redis', '~>3.2'
gem 'rspec', '~> 3.0'
25 changes: 25 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
GEM
remote: https://rubygems.org/
specs:
diff-lcs (1.2.5)
redis (3.3.0)
rspec (3.4.0)
rspec-core (~> 3.4.0)
rspec-expectations (~> 3.4.0)
rspec-mocks (~> 3.4.0)
rspec-core (3.4.4)
rspec-support (~> 3.4.0)
rspec-expectations (3.4.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.4.0)
rspec-mocks (3.4.1)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.4.0)
rspec-support (3.4.1)

PLATFORMS
ruby

DEPENDENCIES
redis (~> 3.2)
rspec (~> 3.0)
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2016 Travis CI GmbH

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.
7 changes: 7 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.PHONY: test
test:
bundle exec rspec

.PHONY: install
install:
bundle install
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# travis-feierabend

Unused code detection modeled after David Schnepper's talk (https://www.youtube.com/watch?v=29UXzfQWOhQ).

## usage

require 'travis/feierabend'
require 'travis/feierabend/redis_storage'

Travis::Feierabend.configure { Travis::Feierabend::RedisStorage.new(config) }
Travis::Feierabend.place('2016-07-01/old-smelly-code')

Travis::Feierabend.list_in_use('2016-07-01/old-smelly-code')

## install

$ make install

## test

$ make
50 changes: 50 additions & 0 deletions lib/travis/feierabend.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
require 'json'

module Travis
class Feierabend
attr_reader :storage

def initialize(storage)
@storage = storage
end

def place(label, meta={})
data = {
trace: Kernel.caller_locations.join("\n"),
meta: meta,
}
@storage.store(label, JSON.dump(data))
end

def list_in_use(label)
@storage.list_in_use(label).map { |r| JSON.parse(r) }
end

class << self
def configure(&block)
@storage_factory = block
@storage = nil
@instance = nil
end

def storage
if @storage_factory.nil?
raise RuntimeError.new('tried to access global Travis::Feierabend without configuring it')
end
@storage ||= @storage_factory.call
end

def instance
@instance ||= Feierabend.new(storage)
end

def place(label, meta={})
instance.place(label, meta)
end

def list_in_use(label)
instance.list_in_use(label)
end
end
end
end
18 changes: 18 additions & 0 deletions lib/travis/feierabend/memory_storage.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module Travis
class Feierabend
class MemoryStorage
def initialize
@in_use = {}
end

def store(label, data)
@in_use[label] ||= []
@in_use[label].push(data)
end

def list_in_use(label)
@in_use[label] || []
end
end
end
end
26 changes: 26 additions & 0 deletions lib/travis/feierabend/redis_storage.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
require 'redis'

module Travis
class Feierabend
class RedisStorage
attr_reader :redis

def initialize(config={})
@prefix = config[:prefix] || 'Feierabend:in_use:'
@max_length = config[:max_length] || 500
@redis = Redis.new(config[:redis] || {})
end

def store(label, trace)
@redis.multi do
@redis.rpush(@prefix+label, trace)
@redis.ltrim(@prefix+label, 0, @max_length)
end
end

def list_in_use(label)
@redis.lrange(@prefix+label, 0, @max_length).reverse
end
end
end
end
96 changes: 96 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
# This file was generated by the `rspec --init` command. Conventionally, all
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
# The generated `.rspec` file contains `--require spec_helper` which will cause
# this file to always be loaded, without a need to explicitly require it in any
# files.
#
# Given that it is always loaded, you are encouraged to keep this file as
# light-weight as possible. Requiring heavyweight dependencies from this file
# will add to the boot time of your test suite on EVERY test run, even for an
# individual file that may not need all of that loaded. Instead, consider making
# a separate helper file that requires the additional dependencies and performs
# the additional setup, and require it from the spec files that actually need
# it.
#
# The `.rspec` file also contains a few flags that are not defaults but that
# users commonly want.
#
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
RSpec.configure do |config|
# rspec-expectations config goes here. You can use an alternate
# assertion/expectation library such as wrong or the stdlib/minitest
# assertions if you prefer.
config.expect_with :rspec do |expectations|
# This option will default to `true` in RSpec 4. It makes the `description`
# and `failure_message` of custom matchers include text for helper methods
# defined using `chain`, e.g.:
# be_bigger_than(2).and_smaller_than(4).description
# # => "be bigger than 2 and smaller than 4"
# ...rather than:
# # => "be bigger than 2"
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
end

# rspec-mocks config goes here. You can use an alternate test double
# library (such as bogus or mocha) by changing the `mock_with` option here.
config.mock_with :rspec do |mocks|
# Prevents you from mocking or stubbing a method that does not exist on
# a real object. This is generally recommended, and will default to
# `true` in RSpec 4.
mocks.verify_partial_doubles = true
end

# The settings below are suggested to provide a good initial experience
# with RSpec, but feel free to customize to your heart's content.
=begin
# These two settings work together to allow you to limit a spec run
# to individual examples or groups you care about by tagging them with
# `:focus` metadata. When nothing is tagged with `:focus`, all examples
# get run.
config.filter_run :focus
config.run_all_when_everything_filtered = true
# Allows RSpec to persist some state between runs in order to support
# the `--only-failures` and `--next-failure` CLI options. We recommend
# you configure your source control system to ignore this file.
config.example_status_persistence_file_path = "spec/examples.txt"
# Limits the available syntax to the non-monkey patched syntax that is
# recommended. For more details, see:
# - http://rspec.info/blog/2012/06/rspecs-new-expectation-syntax/
# - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
# - http://rspec.info/blog/2014/05/notable-changes-in-rspec-3/#zero-monkey-patching-mode
config.disable_monkey_patching!
# This setting enables warnings. It's recommended, but in some cases may
# be too noisy due to issues in dependencies.
config.warnings = true
# Many RSpec users commonly either run the entire suite or an individual
# file, and it's useful to allow more verbose output when running an
# individual spec file.
if config.files_to_run.one?
# Use the documentation formatter for detailed output,
# unless a formatter has already been configured
# (e.g. via a command-line flag).
config.default_formatter = 'doc'
end
# Print the 10 slowest examples and example groups at the
# end of the spec run, to help surface which specs are running
# particularly slow.
config.profile_examples = 10
# Run specs in random order to surface order dependencies. If you find an
# order dependency and want to debug it, you can fix the order by providing
# the seed, which is printed after each run.
# --seed 1234
config.order = :random
# Seed global randomization in this process using the `--seed` CLI option.
# Setting this allows you to use `--seed` to deterministically reproduce
# test failures related to randomization by passing the same `--seed` value
# as the one that triggered the failure.
Kernel.srand config.seed
=end
end
32 changes: 32 additions & 0 deletions spec/tombstone_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
require 'travis/feierabend'
require 'travis/feierabend/memory_storage'
require 'travis/feierabend/redis_storage'

RSpec.describe Travis::Feierabend, "#place" do
context "memory storage" do
before {
Travis::Feierabend.configure { Travis::Feierabend::MemoryStorage.new }
}

it "stores a refutation" do
Travis::Feierabend.place('2016-07-01/test-case')
expect(Travis::Feierabend.get_refutations('2016-07-01/test-case').count).to be(1)
end
end

context "redis storage" do
before {
Travis::Feierabend.configure { Travis::Feierabend::RedisStorage.new }

redis = Travis::Feierabend.storage.redis
redis.keys('feierabend:in_use:*').each do |key|
redis.del(key)
end
}

it "stores a refutation" do
Travis::Feierabend.place('2016-07-01/test-case')
expect(Travis::Feierabend.get_refutations('2016-07-01/test-case').count).to be(1)
end
end
end
12 changes: 12 additions & 0 deletions travis-feierabend.gemspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
Gem::Specification.new do |s|
s.name = 'travis-feierabend'
s.version = '1.0.0'
s.date = '2016-06-01'
s.summary = "Feierabend"
s.description = "Unused code detection modeled after David Schnepper's talk (https://www.youtube.com/watch?v=29UXzfQWOhQ)"
s.authors = ["Igor Wiedler"]
s.email = '[email protected]'
s.files = ["lib/travis/feierabend.rb"]
s.homepage = 'http://rubygems.org/gems/travis-Feierabend'
s.license = 'MIT'
end

0 comments on commit d5eb91b

Please sign in to comment.