Add BlobHelper#include_in_language_stats?

This just extracts some logic from Repository#compute_stats and makes it
testable.
This commit is contained in:
Adam Roben
2015-02-13 14:26:36 -05:00
parent a817d95d6c
commit 6a86e8ea97
3 changed files with 37 additions and 8 deletions

View File

@@ -332,5 +332,15 @@ module Linguist
def tm_scope def tm_scope
language && language.tm_scope language && language.tm_scope
end end
DETECTABLE_TYPES = [:programming, :markup].freeze
# Internal: Should this blob be included in repository language statistics?
def include_in_language_stats?
!vendored? &&
!documentation? &&
!generated? &&
language && DETECTABLE_TYPES.include?(language.type)
end
end end
end end

View File

@@ -8,8 +8,6 @@ module Linguist
# Its primary purpose is for gathering language statistics across # Its primary purpose is for gathering language statistics across
# the entire project. # the entire project.
class Repository class Repository
DETECTABLE_TYPES = [:programming, :markup].freeze
attr_reader :repository attr_reader :repository
# Public: Create a new Repository based on the stats of # Public: Create a new Repository based on the stats of
@@ -158,12 +156,8 @@ module Linguist
blob = Linguist::LazyBlob.new(repository, delta.new_file[:oid], new, mode.to_s(8)) blob = Linguist::LazyBlob.new(repository, delta.new_file[:oid], new, mode.to_s(8))
# Skip vendored or generated blobs next unless blob.include_in_language_stats?
next if blob.vendored? || blob.documentation? || blob.generated? || blob.language.nil? file_map[new] = [blob.language.group.name, blob.size]
if DETECTABLE_TYPES.include?(blob.language.type)
file_map[new] = [blob.language.group.name, blob.size]
end
end end
end end

View File

@@ -517,4 +517,29 @@ class TestBlob < Minitest::Test
refute blob.new(" ").empty? refute blob.new(" ").empty?
refute blob.new("nope").empty? refute blob.new("nope").empty?
end end
def test_include_in_language_stats
vendored = sample_blob("bower_components/custom/custom.js")
assert_predicate vendored, :vendored?
refute_predicate vendored, :include_in_language_stats?
documentation = fixture_blob("README")
assert_predicate documentation, :documentation?
refute_predicate documentation, :include_in_language_stats?
generated = sample_blob("CSS/bootstrap.min.css")
assert_predicate generated, :generated?
refute_predicate generated, :include_in_language_stats?
data = sample_blob("Ant Build System/filenames/ant.xml")
assert_equal :data, data.language.type
refute_predicate data, :include_in_language_stats?
prose = sample_blob("Markdown/tender.md")
assert_equal :prose, prose.language.type
refute_predicate prose, :include_in_language_stats?
included = sample_blob("HTML/pages.html")
assert_predicate included, :include_in_language_stats?
end
end end