oneshot_cov 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/oneshot_cov/logger.rb +33 -0
- data/lib/oneshot_cov/middleware.rb +15 -0
- data/lib/oneshot_cov/railtie.rb +11 -0
- data/lib/oneshot_cov/reporter.rb +62 -0
- data/lib/oneshot_cov/version.rb +1 -1
- data/lib/oneshot_cov.rb +36 -4
- metadata +6 -2
    
        checksums.yaml
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            ---
         | 
| 2 2 | 
             
            SHA256:
         | 
| 3 | 
            -
              metadata.gz:  | 
| 4 | 
            -
              data.tar.gz:  | 
| 3 | 
            +
              metadata.gz: 8fe2713a83d9027e875fe9e4c4e522bea18f99d37f45ede1c9fb002b20faf683
         | 
| 4 | 
            +
              data.tar.gz: 222bccfbeae0c5183f139c6c37b8afabd9d2a3efc23fc63ca99e739aa5e4b413
         | 
| 5 5 | 
             
            SHA512:
         | 
| 6 | 
            -
              metadata.gz:  | 
| 7 | 
            -
              data.tar.gz:  | 
| 6 | 
            +
              metadata.gz: f8d065c6479b0b53c7b1a420a4caa15d9469b2f0178043477178778e2334af98e10b20fefbb846913f22efaaac597422382ff69d224a382f0495b83febfe48b9
         | 
| 7 | 
            +
              data.tar.gz: bd78399d64fad7df637404c6aaf90e1d4f55ad3c790aa42fa4b9fee2121c9b907635e9ebe0b1d23c297780c980c982b49dd4ef7a21429fd2ed2b91c20bbeaf46
         | 
| @@ -0,0 +1,33 @@ | |
| 1 | 
            +
            require 'json'
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            module OneshotCov
         | 
| 4 | 
            +
              class Logger
         | 
| 5 | 
            +
                def initialize(log_path)
         | 
| 6 | 
            +
                  @log_path = log_path
         | 
| 7 | 
            +
                end
         | 
| 8 | 
            +
             | 
| 9 | 
            +
                def post(new_logs)
         | 
| 10 | 
            +
                  current_coverage = fetch
         | 
| 11 | 
            +
             | 
| 12 | 
            +
                  new_logs.each do |new_log|
         | 
| 13 | 
            +
                    key = "#{new_log.path}-#{new_log.md5_hash}"
         | 
| 14 | 
            +
             | 
| 15 | 
            +
                    logged_lines = current_coverage.fetch(key, [])
         | 
| 16 | 
            +
                    current_coverage[key] = logged_lines | new_log.lines
         | 
| 17 | 
            +
                  end
         | 
| 18 | 
            +
                  save(current_coverage)
         | 
| 19 | 
            +
                end
         | 
| 20 | 
            +
             | 
| 21 | 
            +
                private
         | 
| 22 | 
            +
             | 
| 23 | 
            +
                def fetch
         | 
| 24 | 
            +
                  JSON.load(File.read(@log_path))
         | 
| 25 | 
            +
                rescue Errno::ENOENT
         | 
| 26 | 
            +
                  {}
         | 
| 27 | 
            +
                end
         | 
| 28 | 
            +
             | 
| 29 | 
            +
                def save(data)
         | 
| 30 | 
            +
                  File.write(@log_path, JSON.dump(data))
         | 
| 31 | 
            +
                end
         | 
| 32 | 
            +
              end
         | 
| 33 | 
            +
            end
         | 
| @@ -0,0 +1,11 @@ | |
| 1 | 
            +
            unless defined? Rails::Railtie
         | 
| 2 | 
            +
              raise "You need to install and require Rails to use this integration"
         | 
| 3 | 
            +
            end
         | 
| 4 | 
            +
             | 
| 5 | 
            +
            module OneshotCov
         | 
| 6 | 
            +
              class Railtie < Rails::Railtie
         | 
| 7 | 
            +
                initializer 'oneshot_cov.configure' do |app|
         | 
| 8 | 
            +
                  app.middleware.use OneshotCov::Middleware
         | 
| 9 | 
            +
                end
         | 
| 10 | 
            +
              end
         | 
| 11 | 
            +
            end
         | 
| @@ -0,0 +1,62 @@ | |
| 1 | 
            +
            module OneshotCov
         | 
| 2 | 
            +
             | 
| 3 | 
            +
              OneshotLog = Struct.new(:path, :lines)
         | 
| 4 | 
            +
             | 
| 5 | 
            +
              class Reporter
         | 
| 6 | 
            +
                def initialize(target_path:, logger:, emit_term: nil)
         | 
| 7 | 
            +
                  @target_path = target_path
         | 
| 8 | 
            +
                  @logger = logger
         | 
| 9 | 
            +
                  @emit_term = emit_term
         | 
| 10 | 
            +
                  if @emit_term
         | 
| 11 | 
            +
                    @next_emit_time = Time.now.to_i + rand(@emit_term)
         | 
| 12 | 
            +
                  end
         | 
| 13 | 
            +
             | 
| 14 | 
            +
                  if defined?(Bundler)
         | 
| 15 | 
            +
                    @bundler_path = Bundler.bundle_path.to_s
         | 
| 16 | 
            +
                  end
         | 
| 17 | 
            +
                end
         | 
| 18 | 
            +
             | 
| 19 | 
            +
                def emit(force_emit)
         | 
| 20 | 
            +
                  if !force_emit
         | 
| 21 | 
            +
                    if !time_to_emit?
         | 
| 22 | 
            +
                      return
         | 
| 23 | 
            +
                    end
         | 
| 24 | 
            +
                  end
         | 
| 25 | 
            +
             | 
| 26 | 
            +
                  logs =
         | 
| 27 | 
            +
                    Coverage.result(clear: true, stop: false).
         | 
| 28 | 
            +
                    select { |k, v| is_target?(k, v) }.
         | 
| 29 | 
            +
                    map do |filepath, v|
         | 
| 30 | 
            +
                      OneshotLog.new(relative_path(filepath), v)
         | 
| 31 | 
            +
                    end
         | 
| 32 | 
            +
             | 
| 33 | 
            +
                  if logs.size > 0
         | 
| 34 | 
            +
                    @logger.post(logs)
         | 
| 35 | 
            +
                  end
         | 
| 36 | 
            +
                end
         | 
| 37 | 
            +
             | 
| 38 | 
            +
                private
         | 
| 39 | 
            +
             | 
| 40 | 
            +
                def time_to_emit?
         | 
| 41 | 
            +
                  if @emit_term
         | 
| 42 | 
            +
                    if @next_emit_time > Time.now.to_i
         | 
| 43 | 
            +
                      return false # Do not emit until next_emit_time
         | 
| 44 | 
            +
                    else
         | 
| 45 | 
            +
                      @next_emit_time += @emit_term
         | 
| 46 | 
            +
                    end
         | 
| 47 | 
            +
                  end
         | 
| 48 | 
            +
                  true
         | 
| 49 | 
            +
                end
         | 
| 50 | 
            +
             | 
| 51 | 
            +
                def is_target?(filepath, value)
         | 
| 52 | 
            +
                  return false if value.empty?
         | 
| 53 | 
            +
                  return false if !filepath.start_with?(@target_path)
         | 
| 54 | 
            +
                  return false if @bundler_path && filepath.start_with?(@bundler_path)
         | 
| 55 | 
            +
                  true
         | 
| 56 | 
            +
                end
         | 
| 57 | 
            +
             | 
| 58 | 
            +
                def relative_path(filepath)
         | 
| 59 | 
            +
                  filepath[@target_path.size..-1]
         | 
| 60 | 
            +
                end
         | 
| 61 | 
            +
              end
         | 
| 62 | 
            +
            end
         | 
    
        data/lib/oneshot_cov/version.rb
    CHANGED
    
    
    
        data/lib/oneshot_cov.rb
    CHANGED
    
    | @@ -1,6 +1,38 @@ | |
| 1 | 
            -
            require " | 
| 1 | 
            +
            require "coverage"
         | 
| 2 | 
            +
            require "digest/md5"
         | 
| 3 | 
            +
             | 
| 4 | 
            +
            require "oneshot_cov/logger"
         | 
| 5 | 
            +
            require "oneshot_cov/middleware"
         | 
| 6 | 
            +
            require "oneshot_cov/reporter"
         | 
| 7 | 
            +
            require "oneshot_cov/railtie" if defined?(Rails)
         | 
| 2 8 |  | 
| 3 9 | 
             
            module OneshotCov
         | 
| 4 | 
            -
               | 
| 5 | 
            -
             | 
| 6 | 
            -
             | 
| 10 | 
            +
              module_function
         | 
| 11 | 
            +
             | 
| 12 | 
            +
              def start
         | 
| 13 | 
            +
                Coverage.start(oneshot_lines: true)
         | 
| 14 | 
            +
             | 
| 15 | 
            +
                # To handle execution with exit immediatly
         | 
| 16 | 
            +
                at_exit do
         | 
| 17 | 
            +
                  OneshotCov.emit(force_emit: true)
         | 
| 18 | 
            +
                end
         | 
| 19 | 
            +
              end
         | 
| 20 | 
            +
             | 
| 21 | 
            +
              def emit(force_emit: false)
         | 
| 22 | 
            +
                @reporter&.emit(force_emit)
         | 
| 23 | 
            +
              end
         | 
| 24 | 
            +
             | 
| 25 | 
            +
              def configure(target_path: Rails.root, logger: OneshotCov::Logger.new, emit_term: nil)
         | 
| 26 | 
            +
                target_path_by_pathname =
         | 
| 27 | 
            +
                  if target_path.is_a? Pathname
         | 
| 28 | 
            +
                    target_path
         | 
| 29 | 
            +
                  else
         | 
| 30 | 
            +
                    Pathname.new(target_path)
         | 
| 31 | 
            +
                  end
         | 
| 32 | 
            +
                @reporter = OneshotCov::Reporter.new(
         | 
| 33 | 
            +
                  target_path: target_path_by_pathname.cleanpath.to_s + "/",
         | 
| 34 | 
            +
                  logger: logger,
         | 
| 35 | 
            +
                  emit_term: emit_term,
         | 
| 36 | 
            +
                )
         | 
| 37 | 
            +
              end
         | 
| 38 | 
            +
            end
         | 
    
        metadata
    CHANGED
    
    | @@ -1,14 +1,14 @@ | |
| 1 1 | 
             
            --- !ruby/object:Gem::Specification
         | 
| 2 2 | 
             
            name: oneshot_cov
         | 
| 3 3 | 
             
            version: !ruby/object:Gem::Version
         | 
| 4 | 
            -
              version: 0. | 
| 4 | 
            +
              version: 0.2.0
         | 
| 5 5 | 
             
            platform: ruby
         | 
| 6 6 | 
             
            authors:
         | 
| 7 7 | 
             
            - soartec-lab
         | 
| 8 8 | 
             
            autorequire: 
         | 
| 9 9 | 
             
            bindir: exe
         | 
| 10 10 | 
             
            cert_chain: []
         | 
| 11 | 
            -
            date: 2019-09- | 
| 11 | 
            +
            date: 2019-09-29 00:00:00.000000000 Z
         | 
| 12 12 | 
             
            dependencies:
         | 
| 13 13 | 
             
            - !ruby/object:Gem::Dependency
         | 
| 14 14 | 
             
              name: bundler
         | 
| @@ -70,6 +70,10 @@ files: | |
| 70 70 | 
             
            - bin/console
         | 
| 71 71 | 
             
            - bin/setup
         | 
| 72 72 | 
             
            - lib/oneshot_cov.rb
         | 
| 73 | 
            +
            - lib/oneshot_cov/logger.rb
         | 
| 74 | 
            +
            - lib/oneshot_cov/middleware.rb
         | 
| 75 | 
            +
            - lib/oneshot_cov/railtie.rb
         | 
| 76 | 
            +
            - lib/oneshot_cov/reporter.rb
         | 
| 73 77 | 
             
            - lib/oneshot_cov/version.rb
         | 
| 74 78 | 
             
            - oneshot_cov.gemspec
         | 
| 75 79 | 
             
            homepage: https://github.com/soartec-lab/oneshot_cov
         |