mirror of
				https://github.com/KevinMidboe/linguist.git
				synced 2025-10-29 17:50:22 +00:00 
			
		
		
		
	Allow classifier to run on symlinks as usual (#3948)
* Fixups for symlink detection, incl. test * assert the heuristics return none for symlink
This commit is contained in:
		| @@ -18,8 +18,6 @@ module Linguist | ||||
|     # | ||||
|     # Returns an Array of Language objects, most probable first. | ||||
|     def self.call(blob, possible_languages) | ||||
|       return [] if blob.symlink? | ||||
|  | ||||
|       language_names = possible_languages.map(&:name) | ||||
|       classify(Samples.cache, blob.data[0...CLASSIFIER_CONSIDER_BYTES], language_names).map do |name, _| | ||||
|         Language[name] # Return the actual Language objects | ||||
|   | ||||
| @@ -27,7 +27,7 @@ module Linguist | ||||
|     end | ||||
|  | ||||
|     def symlink? | ||||
|       return @symlink if !@symlink.nil? | ||||
|       return @symlink if defined? @symlink | ||||
|       @symlink = (File.symlink?(@fullpath) rescue false) | ||||
|     end | ||||
|  | ||||
|   | ||||
							
								
								
									
										1
									
								
								samples/Markdown/symlink.md
									
									
									
									
									
										Symbolic link
									
								
							
							
						
						
									
										1
									
								
								samples/Markdown/symlink.md
									
									
									
									
									
										Symbolic link
									
								
							| @@ -0,0 +1 @@ | ||||
| README.mdown | ||||
| @@ -13,8 +13,9 @@ class TestHeuristics < Minitest::Test | ||||
|   end | ||||
|  | ||||
|   def all_fixtures(language_name, file="*") | ||||
|     Dir.glob("#{samples_path}/#{language_name}/#{file}") - | ||||
|     fixs = Dir.glob("#{samples_path}/#{language_name}/#{file}") - | ||||
|              ["#{samples_path}/#{language_name}/filenames"] | ||||
|     fixs.reject { |f| File.symlink?(f) } | ||||
|   end | ||||
|  | ||||
|   def test_no_match | ||||
| @@ -23,6 +24,10 @@ class TestHeuristics < Minitest::Test | ||||
|     assert_equal [], results | ||||
|   end | ||||
|  | ||||
|   def test_symlink_empty | ||||
|     assert_equal [], Heuristics.call(file_blob("Markdown/symlink.md"), [Language["Markdown"]]) | ||||
|   end | ||||
|  | ||||
|   def assert_heuristics(hash) | ||||
|     candidates = hash.keys.map { |l| Language[l] } | ||||
|  | ||||
|   | ||||
| @@ -473,4 +473,10 @@ class TestLanguage < Minitest::Test | ||||
|     assert_nil Language.find_by_name(',') | ||||
|     assert_nil Language.find_by_alias(',') | ||||
|   end | ||||
|  | ||||
|   def test_detect_prefers_markdown_for_md | ||||
|     blob = Linguist::FileBlob.new(File.join(samples_path, "Markdown/symlink.md")) | ||||
|     match = Linguist.detect(blob) | ||||
|     assert_equal Language["Markdown"], match | ||||
|   end | ||||
| end | ||||
|   | ||||
		Reference in New Issue
	
	Block a user