From 6a86e8ea97a25c90f2a994efd1eab45b3edc9330 Mon Sep 17 00:00:00 2001 From: Adam Roben Date: Fri, 13 Feb 2015 14:26:36 -0500 Subject: [PATCH] Add BlobHelper#include_in_language_stats? This just extracts some logic from Repository#compute_stats and makes it testable. --- lib/linguist/blob_helper.rb | 10 ++++++++++ lib/linguist/repository.rb | 10 ++-------- test/test_blob.rb | 25 +++++++++++++++++++++++++ 3 files changed, 37 insertions(+), 8 deletions(-) diff --git a/lib/linguist/blob_helper.rb b/lib/linguist/blob_helper.rb index ff11aefd..56c15f02 100644 --- a/lib/linguist/blob_helper.rb +++ b/lib/linguist/blob_helper.rb @@ -332,5 +332,15 @@ module Linguist def tm_scope language && language.tm_scope 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 diff --git a/lib/linguist/repository.rb b/lib/linguist/repository.rb index 3837977f..895a3754 100644 --- a/lib/linguist/repository.rb +++ b/lib/linguist/repository.rb @@ -8,8 +8,6 @@ module Linguist # Its primary purpose is for gathering language statistics across # the entire project. class Repository - DETECTABLE_TYPES = [:programming, :markup].freeze - attr_reader :repository # 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)) - # Skip vendored or generated blobs - next if blob.vendored? || blob.documentation? || blob.generated? || blob.language.nil? - - if DETECTABLE_TYPES.include?(blob.language.type) - file_map[new] = [blob.language.group.name, blob.size] - end + next unless blob.include_in_language_stats? + file_map[new] = [blob.language.group.name, blob.size] end end diff --git a/test/test_blob.rb b/test/test_blob.rb index fabd5a74..24eaff83 100644 --- a/test/test_blob.rb +++ b/test/test_blob.rb @@ -517,4 +517,29 @@ class TestBlob < Minitest::Test refute blob.new(" ").empty? refute blob.new("nope").empty? 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