Giter VIP home page Giter VIP logo

type_fusion's Introduction

TypeFusion

Installation

Install the gem and add to the application's Gemfile by executing:

bundle add type_fusion

If bundler is not being used to manage dependencies, install the gem by executing:

gem install type_fusion

Running TypeFusion in the test environment

Setup TypeFusion if you only want to run TypeFusion as part of your test suite.

Minitest

You can require type_fusion/minitest in your test_helper.rb:

# test/test_helper.rb

ENV["RAILS_ENV"] ||= "test"
require_relative "../config/environment"
require "rails/test_help"

+require "type_fusion/minitest"

class ActiveSupport::TestCase
  # ...
end

RSpec

You can require type_fusion/rspec in your spec_helper.rb:

# spec/spec_helper.rb

+require "type_fusion/rspec"

RSpec.configure do |config|
  # ...
end

Running TypeFusion in development or production

Rack

require "type_fusion/rack/middleware"

use TypeFusion::Middleware

Rails

Adding the gem to your applications Gemfile will automatically setup type_fusion.

Configuration

Setup TypeFusion in an initializer

# config/initializers/type_fusion.rb

require "type_fusion"

TypeFusion.config do |config|

  # === application_name
  #
  # Set application_name to a string which is used to know where the samples
  # came from. Set application_name to an empty string if you wish to not
  # send the application name alongside the samples.
  #
  # Default: "TypeFusion"
  # Default when using Rails: Rails.application.class.module_parent_name
  #
  # config.application_name = "YourApplication"


  # === endpoint
  #
  # Set endpoint to an URL where TypeFusion should send the samples to.
  #
  # Default: "https://gem.sh/api/v1/types/samples"
  #
  # config.endpoint = "https://your-domain.com/api/v1/types/samples"


  # === type_sample_request
  #
  # Set type_sample_request to a lambda which resolves to true/false
  # to set if type sampling should be enabled for the whole rack request.
  #
  # Default: ->(rack_env) { [true, false, false, false].sample }
  #
  # config.type_sample_request = ->(rack_env) { [true, false, false, false].sample }


  # === type_sample_tracepoint_path
  #
  # Set type_sample_tracepoint_path to a lambda which resolves
  # to true/false to check if a tracepoint_path should be sampled
  # or not.
  #
  # This can be useful when you want to only sample method calls for
  # certain gems or want to exclude a gem from being sampled.
  #
  # Example:
  # config.type_sample_tracepoint_path = ->(tracepoint_path) {
  #   return false if tracepoint_path.include?("activerecord")
  #   return false if tracepoint_path.include?("sprockets")
  #   return false if tracepoint_path.include?("some-private-gem")
  #
  #   true
  # }
  #
  # Default: ->(tracepoint_path) { true }
  #
  # config.type_sample_tracepoint_path = ->(tracepoint_path) { true }


  # === type_sample_call_rate
  #
  # Set type_sample_call_rate to 1.0 to capture 100% of method calls
  # within a rack request.
  #
  # Default: 0.001
  #
  # config.type_sample_call_rate = 0.001
end

Usage

Type sample inside a block

TypeFusion.with_sampling do
  # run code you want to type sample here
end

Type sample globally

TypeFusion.start

# run code you want to type sample here

TypeFusion.stop

Retrieve the samples

TypeFusion::Sampler.instance.samples
# => [...]
TypeFusion::Sampler.instance.samples.first

# => #<struct TypeFusion::SampleCall
#      gem_name="nokogiri"
#      gem_version="1.15.4-x86_64-darwin",
#      receiver="Nokogiri",
#      method_name=:parse,
#      application_name="TypeFusion",
#      location="/Users/marcoroth/.anyenv/envs/rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/nokogiri-1.15.4-x86_64-darwin/lib/nokogiri.rb:43",
#      type_fusion_version="0.0.4",
#      parameters=[
#        [:string, :req, String],
#        [:url, :opt, NilClass],
#        [:encoding, :opt, NilClass],
#        [:options, :opt, NilClass]
#      ],
#      return_value="Nokogiri::XML::Document"
#     >

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake test 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 the created tag, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/marcoroth/type_fusion. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the code of conduct.

Code of Conduct

Everyone interacting in the TypeFusion project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.

type_fusion's People

Contributors

dependabot[bot] avatar marcoroth avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

type_fusion's Issues

Sampling a gem via its test suite

Currently when adding type_fusion to a test suite of a gem, no samples are captured for the gem under test, as the sampler only selects any files that are in the gem_path https://github.com/marcoroth/type_fusion/blob/main/lib/type_fusion/sampler.rb#L137.

I guess this is so that only gems that are released to rubygems are sampled, but running the test suite of a gem itself seems like a nice way to collect type info, rather that needing an application or script as a sort of harness to exercise the gem in.

I managed to sample a gem via running its test suite, by simply modifying the test helper to require the released and installed gem, rather than including the local lib/ directory.

However while messing around trying to get the sampling to work I seem to have also sent samples for the test classes themselves, sorry about that. See https://gem.sh/gems/typed_operation/v1.0.0.pre3/types .. would it be possible to reject any samples for code that is defined in files that are not in the actual build/released gem?

This gem is a very neat idea!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.