From 0328b1cb3c7c5540617fd30479f11f3e5c46afff Mon Sep 17 00:00:00 2001 From: Adam Roben Date: Wed, 14 Jan 2015 08:54:49 -0500 Subject: [PATCH 1/2] Use the original FileBlob path for filesystem access FileBlob now remembers the full path that was passed to its constructor, and uses that for performing filesystem access. FileBlob#path continues to return a relative path as before. This ensures that you can call methods like #size and #mode on FileBlobs with relative paths, regardless of the current working directory. --- lib/linguist/file_blob.rb | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/lib/linguist/file_blob.rb b/lib/linguist/file_blob.rb index 55a420ef..2ca74c2d 100644 --- a/lib/linguist/file_blob.rb +++ b/lib/linguist/file_blob.rb @@ -14,6 +14,7 @@ module Linguist # # Returns a FileBlob. def initialize(path, base_path = nil) + @fullpath = path @path = base_path ? path.sub("#{base_path}/", '') : path end @@ -35,28 +36,28 @@ module Linguist # # Returns a String like '100644' def mode - File.stat(@path).mode.to_s(8) + File.stat(@fullpath).mode.to_s(8) end # Public: File name # # Returns a String def name - File.basename(path) + File.basename(@fullpath) end # Public: Read file contents. # # Returns a String. def data - File.read(@path) + File.read(@fullpath) end # Public: Get byte size # # Returns an Integer. def size - File.size(@path) + File.size(@fullpath) end # Public: Get file extension. @@ -73,7 +74,7 @@ module Linguist # # Returns an Array def extensions - basename, *segments = File.basename(path).split(".") + basename, *segments = name.split(".") segments.map.with_index do |segment, index| "." + segments[index..-1].join(".") From 96c7bc30d86d99af5f29c23dc84f3ecc0d5e7841 Mon Sep 17 00:00:00 2001 From: Adam Roben Date: Wed, 14 Jan 2015 08:57:14 -0500 Subject: [PATCH 2/2] Simplify blob tests Now that FileBlobs with relative paths can still access their files on disk, we can use relative paths for all FileBlobs in the test. This more closely matches the behavior in github.com's codebase, where all blobs use relative paths. --- test/test_blob.rb | 46 +++++++++++++++++++--------------------------- 1 file changed, 19 insertions(+), 27 deletions(-) diff --git a/test/test_blob.rb b/test/test_blob.rb index b70a60b2..4c4f7978 100644 --- a/test/test_blob.rb +++ b/test/test_blob.rb @@ -22,22 +22,14 @@ class TestBlob < Minitest::Test File.expand_path("../fixtures", __FILE__) end - def sample_blob(name, enforce_relative = false) + def sample_blob(name) name = File.join(samples_path, name) unless name =~ /^\// - if enforce_relative - FileBlob.new(name, samples_path) - else - FileBlob.new(name) - end + FileBlob.new(name, samples_path) end - def fixture_blob(name, enforce_relative = false) + def fixture_blob(name) name = File.join(fixtures_path, name) unless name =~ /^\// - if enforce_relative - FileBlob.new(name, fixtures_path) - else - FileBlob.new(name) - end + FileBlob.new(name, fixtures_path) end def script_blob(name) @@ -271,7 +263,7 @@ class TestBlob < Minitest::Test assert !sample_blob("ext/extconf.rb").vendored? # Dependencies - assert sample_blob("dependencies/windows/headers/GL/glext.h", true).vendored? + assert sample_blob("dependencies/windows/headers/GL/glext.h").vendored? # Node dependencies assert sample_blob("node_modules/coffee-script/lib/coffee-script.js").vendored? @@ -296,10 +288,10 @@ class TestBlob < Minitest::Test assert sample_blob("external/jquery.min.js").vendored? # C deps - assert sample_blob("deps/http_parser/http_parser.c", true).vendored? - assert sample_blob("deps/v8/src/v8.h", true).vendored? + assert sample_blob("deps/http_parser/http_parser.c").vendored? + assert sample_blob("deps/v8/src/v8.h").vendored? - assert sample_blob("tools/something/else.c", true).vendored? + assert sample_blob("tools/something/else.c").vendored? # Chart.js assert sample_blob("some/vendored/path/Chart.js").vendored? @@ -309,10 +301,10 @@ class TestBlob < Minitest::Test assert sample_blob("codemirror/mode/blah.js").vendored? # Debian packaging - assert sample_blob("debian/cron.d", true).vendored? + assert sample_blob("debian/cron.d").vendored? # Erlang - assert sample_blob("rebar", true).vendored? + assert sample_blob("rebar").vendored? # Minified JavaScript and CSS assert sample_blob("foo.min.js").vendored? @@ -323,7 +315,7 @@ class TestBlob < Minitest::Test assert !sample_blob("foo.min.txt").vendored? #.osx - assert sample_blob(".osx", true).vendored? + assert sample_blob(".osx").vendored? # Prototype assert !sample_blob("public/javascripts/application.js").vendored? @@ -333,7 +325,7 @@ class TestBlob < Minitest::Test assert sample_blob("public/javascripts/dragdrop.js").vendored? # Samples - assert sample_blob("Samples/Ruby/foo.rb", true).vendored? + assert sample_blob("Samples/Ruby/foo.rb").vendored? # jQuery assert sample_blob("jquery.js").vendored? @@ -398,10 +390,10 @@ class TestBlob < Minitest::Test assert sample_blob("public/javascripts/modernizr.custom.01009.js").vendored? # Fabric - assert sample_blob("fabfile.py", true).vendored? + assert sample_blob("fabfile.py").vendored? # WAF - assert sample_blob("waf", true).vendored? + assert sample_blob("waf").vendored? # Visual Studio IntelliSense assert sample_blob("Scripts/jquery-1.7-vsdoc.js").vendored? @@ -423,7 +415,7 @@ class TestBlob < Minitest::Test assert sample_blob("Scripts/jquery.unobtrusive-ajax.min.js").vendored? # NuGet Packages - assert sample_blob("packages/Modernizr.2.0.6/Content/Scripts/modernizr-2.0.6-development-only.js", true).vendored? + assert sample_blob("packages/Modernizr.2.0.6/Content/Scripts/modernizr-2.0.6-development-only.js").vendored? # Font Awesome assert sample_blob("some/asset/path/font-awesome.min.css").vendored? @@ -433,15 +425,15 @@ class TestBlob < Minitest::Test assert sample_blob("some/asset/path/normalize.css").vendored? # Cocoapods - assert sample_blob('Pods/blah', true).vendored? + assert sample_blob('Pods/blah').vendored? # Html5shiv assert sample_blob("Scripts/html5shiv.js").vendored? assert sample_blob("Scripts/html5shiv.min.js").vendored? # Test fixtures - assert sample_blob("test/fixtures/random.rkt", true).vendored? - assert sample_blob("Test/fixtures/random.rkt", true).vendored? + assert sample_blob("test/fixtures/random.rkt").vendored? + assert sample_blob("Test/fixtures/random.rkt").vendored? # Cordova/PhoneGap assert sample_blob("cordova.js").vendored? @@ -455,7 +447,7 @@ class TestBlob < Minitest::Test assert sample_blob("foundation.abide.js").vendored? # Vagrant - assert sample_blob("Vagrantfile", true).vendored? + assert sample_blob("Vagrantfile").vendored? # Gradle assert sample_blob("gradlew").vendored?