From b227816e917ae7c3156925a109183b5939c381b0 Mon Sep 17 00:00:00 2001 From: Arfon Smith Date: Thu, 29 Jan 2015 08:24:12 -0600 Subject: [PATCH] Fail without heuristics --- lib/linguist/heuristics.rb | 9 +++++++++ test/test_heuristics.rb | 38 +++++++++++++++++++++++++++----------- 2 files changed, 36 insertions(+), 11 deletions(-) diff --git a/lib/linguist/heuristics.rb b/lib/linguist/heuristics.rb index c512a38e85..c0f45f448d 100644 --- a/lib/linguist/heuristics.rb +++ b/lib/linguist/heuristics.rb @@ -23,6 +23,15 @@ def self.call(blob, languages) [] # No heuristics matched end + # Public: Do we have a heuristic for these candidate languages? + # + # languages - Array of Language objects + # + # Returns true if there are matching heuristics or nil + def self.defined_for?(languages) + @heuristics.select { |h| h.matches?(languages) }.any? + end + # Internal: Define a new heuristic. # # languages - String names of languages to disambiguate. diff --git a/test/test_heuristics.rb b/test/test_heuristics.rb index b88d0fdaeb..d99afad7a5 100644 --- a/test/test_heuristics.rb +++ b/test/test_heuristics.rb @@ -26,6 +26,17 @@ def test_no_match assert_equal [], results end + def assert_heuristics(hash) + candidates = hash.keys.map { |l| Language[l] } + + hash.each do |language, blobs| + Array(blobs).each do |blob| + result = Heuristics.call(file_blob(blob), candidates) + assert_equal [Language[language]], result, "Failed for #{blob}" + end + end + end + # Candidate languages = ["C++", "Objective-C"] def test_obj_c_by_heuristics # Only calling out '.h' filenames as these are the ones causing issues @@ -147,17 +158,6 @@ def test_cs_by_heuristics }) end - def assert_heuristics(hash) - candidates = hash.keys.map { |l| Language[l] } - - hash.each do |language, blobs| - Array(blobs).each do |blob| - result = Heuristics.call(file_blob(blob), candidates) - assert_equal [Language[language]], result, "Failed for #{blob}" - end - end - end - def test_ls_by_heuristics assert_heuristics({ "LiveScript" => "LiveScript/hello.ls", @@ -171,4 +171,20 @@ def test_ts_by_heuristics "XML" => all_fixtures("XML", "*.ts") }) end + + def output_languages(languages) + languages.map { |l| l.name }.join(", ") + end + + # If a language extension isn't globally unique then check if we have a heuristic + Linguist::Language.all.each do |language| + define_method "test_#{language.name}_has_adequate_heuristics" do + language.extensions.each do |extension| + languages = Language.find_by_extension(extension) + next if extension == ".script!" || languages.size == 1 + + assert Heuristics.defined_for?(languages), "No heuristic for #{extension} and #{output_languages(languages)}" + end + end + end end