mirror of
https://github.com/KevinMidboe/linguist.git
synced 2026-01-21 00:35:34 +00:00
Merge pull request #2162 from github/instrumentation
Add instrumentation to detection and classification
This commit is contained in:
@@ -6,3 +6,15 @@ require 'linguist/repository'
|
|||||||
require 'linguist/samples'
|
require 'linguist/samples'
|
||||||
require 'linguist/shebang'
|
require 'linguist/shebang'
|
||||||
require 'linguist/version'
|
require 'linguist/version'
|
||||||
|
|
||||||
|
class << Linguist
|
||||||
|
attr_accessor :instrumenter
|
||||||
|
|
||||||
|
def instrument(*args, &bk)
|
||||||
|
if instrumenter
|
||||||
|
instrumenter.instrument(*args, &bk)
|
||||||
|
else
|
||||||
|
yield if block_given?
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|||||||
@@ -105,19 +105,31 @@ module Linguist
|
|||||||
# Bail early if the blob is binary or empty.
|
# Bail early if the blob is binary or empty.
|
||||||
return nil if blob.likely_binary? || blob.binary? || blob.empty?
|
return nil if blob.likely_binary? || blob.binary? || blob.empty?
|
||||||
|
|
||||||
# Call each strategy until one candidate is returned.
|
Linguist.instrument("linguist.detection", :blob => blob) do
|
||||||
STRATEGIES.reduce([]) do |languages, strategy|
|
# Call each strategy until one candidate is returned.
|
||||||
candidates = strategy.call(blob, languages)
|
languages = []
|
||||||
if candidates.size == 1
|
returning_strategy = nil
|
||||||
return candidates.first
|
|
||||||
elsif candidates.size > 1
|
STRATEGIES.each do |strategy|
|
||||||
# More than one candidate was found, pass them to the next strategy.
|
returning_strategy = strategy
|
||||||
candidates
|
candidates = Linguist.instrument("linguist.strategy", :blob => blob, :strategy => strategy, :candidates => languages) do
|
||||||
else
|
strategy.call(blob, languages)
|
||||||
# No candiates were found, pass on languages from the previous strategy.
|
end
|
||||||
languages
|
if candidates.size == 1
|
||||||
|
languages = candidates
|
||||||
|
break
|
||||||
|
elsif candidates.size > 1
|
||||||
|
# More than one candidate was found, pass them to the next strategy.
|
||||||
|
languages = candidates
|
||||||
|
else
|
||||||
|
# No candidates, try the next strategy
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end.first
|
|
||||||
|
Linguist.instrument("linguist.detected", :blob => blob, :strategy => returning_strategy, :language => languages.first)
|
||||||
|
|
||||||
|
languages.first
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# Public: Get all Languages
|
# Public: Get all Languages
|
||||||
|
|||||||
50
test/test_instrumentation.rb
Normal file
50
test/test_instrumentation.rb
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
require_relative "./helper"
|
||||||
|
|
||||||
|
class TestInstrumentation < Minitest::Test
|
||||||
|
include Linguist
|
||||||
|
|
||||||
|
class LocalInstrumenter
|
||||||
|
Event = Struct.new(:name, :args)
|
||||||
|
|
||||||
|
attr_reader :events
|
||||||
|
|
||||||
|
def initialize
|
||||||
|
@events = []
|
||||||
|
end
|
||||||
|
|
||||||
|
def instrument(name, *args)
|
||||||
|
@events << Event.new(name, args)
|
||||||
|
yield if block_given?
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def setup
|
||||||
|
Linguist.instrumenter = LocalInstrumenter.new
|
||||||
|
end
|
||||||
|
|
||||||
|
def teardown
|
||||||
|
Linguist.instrumenter = nil
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_detection_instrumentation_with_binary_blob
|
||||||
|
binary_blob = fixture_blob("Binary/octocat.ai")
|
||||||
|
Language.detect(binary_blob)
|
||||||
|
|
||||||
|
# Shouldn't instrument this (as it's binary)
|
||||||
|
assert_equal 0, Linguist.instrumenter.events.size
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_modeline_instrumentation
|
||||||
|
blob = fixture_blob("Data/Modelines/ruby")
|
||||||
|
Language.detect(blob)
|
||||||
|
|
||||||
|
detect_event = Linguist.instrumenter.events.last
|
||||||
|
detect_event_payload = detect_event[:args].first
|
||||||
|
|
||||||
|
assert_equal 3, Linguist.instrumenter.events.size
|
||||||
|
assert_equal "linguist.detected", detect_event.name
|
||||||
|
assert_equal Language['Ruby'], detect_event_payload[:language]
|
||||||
|
assert_equal blob, detect_event_payload[:blob]
|
||||||
|
assert_equal Linguist::Strategy::Modeline, detect_event_payload[:strategy]
|
||||||
|
end
|
||||||
|
end
|
||||||
Reference in New Issue
Block a user