Skip to content

Commit

Permalink
Add online_emit mode
Browse files Browse the repository at this point in the history
  • Loading branch information
riseshia committed Sep 10, 2024
1 parent d4a042d commit 7312d68
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 5 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@
/tmp/

Gemfile.lock
coverage.json
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,26 @@ run App
Boot up application, do something, and access `/akainaa`.
It will show Web UI what and how many executed.

### Enable online emit mode

Akainaa can emit coverage data which recorded in interval to the file.
This feature is intended to be used with vscode-akainaa extension.

```ruby
Akainaa.start(
project_dir: File.expand_path(__dir__),
ignore_glob_patterns: %w[
config/application.rb
config/initializers/*_initializer.rb
],
online_emit: {
mode: :file,
interval: 1, # seconds
output_path: '/tmp/akainaa.json',
},
)
```

## Development

After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
Expand Down
62 changes: 60 additions & 2 deletions lib/akainaa.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

require 'coverage'
require 'fileutils'

require_relative 'akainaa/version'
require_relative 'akainaa/call_node_visitor'
Expand All @@ -15,7 +16,8 @@ class << self
def start(
project_dir:,
ignore_glob_patterns: [],
hide_not_executed_files: false
hide_not_executed_files: false,
online_emit: nil
)
@project_dir = project_dir
@project_dir += '/' unless @project_dir.end_with?('/')
Expand All @@ -24,8 +26,15 @@ def start(
end
@ignore_files = Set.new(ignore_files)
@hide_not_executed_files = hide_not_executed_files
@monitor = Monitor.new

Coverage.start(lines: true)

if online_emit.is_a?(Hash)
option = default_online_emit.merge(online_emit)
FileUtils.mkdir_p(File.dirname(option[:path]))
start_multipart_emit(option)
end
end

def peek_result
Expand All @@ -37,7 +46,56 @@ def peek_result
end

def reset
Coverage.result(stop: false, clear: true)
@monitor.synchronize do
Coverage.result(stop: false, clear: true)
@previous_result = {}
end
end

private def default_online_emit
{
mode: :file,
interval: 1,
path: 'tmp/coverage.json',
}
end

private def start_multipart_emit(option)
Thread.new do
@monitor.synchronize do
@previous_result = {}
end

loop do
sleep option[:interval]
current_result = peek_result

diff = {}
current_result.each do |path, path_coverage|
previous_path_coverage = @previous_result[path]

if previous_path_coverage.nil?
diff[path] = path_coverage
elsif previous_path_coverage[:lines].size != path_coverage[:lines].size
diff[path] = path_coverage
else
diff[path] = { lines: [] }

path_coverage[:lines].each_with_index do |count, index|
val = count ? count - previous_path_coverage[:lines][index] : nil

diff[path][:lines] << val
end
end
end

@monitor.synchronize do
@previous_result = current_result
end
File.write(option[:path], diff.to_json)
puts 'Akainaa: Coverage result emitted'
end
end
end
end

Expand Down
1 change: 0 additions & 1 deletion sample/.ruby-version

This file was deleted.

2 changes: 1 addition & 1 deletion sample/Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: ..
specs:
akainaa (0.1.1)
akainaa (0.1.3)

GEM
remote: https://rubygems.org/
Expand Down
8 changes: 7 additions & 1 deletion sample/config.ru
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@

require 'akainaa'

Akainaa.start(project_dir: File.expand_path(__dir__))
Akainaa.start(
project_dir: File.expand_path(__dir__),
online_emit: {
mode: :file,
interval: 10,
},
)

require_relative 'app'

Expand Down

0 comments on commit 7312d68

Please sign in to comment.