-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2306 from ptrippett/add-remote-config-file
Add remote config files from http/https urls
- Loading branch information
Showing
9 changed files
with
156 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -347,3 +347,4 @@ | |
require 'rubocop/cli' | ||
require 'rubocop/options' | ||
require 'rubocop/warning' | ||
require 'rubocop/remote_config' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
# encoding: utf-8 | ||
|
||
require 'net/http' | ||
|
||
module RuboCop | ||
# Common methods and behaviors for dealing with remote config files. | ||
class RemoteConfig | ||
CACHE_LIFETIME = 24 * 60 * 60 | ||
|
||
def initialize(url) | ||
@uri = URI.parse(url) | ||
end | ||
|
||
def file | ||
return cache_path unless cache_path_expired? | ||
|
||
http = Net::HTTP.new(@uri.hostname, @uri.port) | ||
http.use_ssl = true if @uri.instance_of? URI::HTTPS | ||
|
||
request = Net::HTTP::Get.new(@uri.request_uri) | ||
if cache_path_exists? | ||
request['If-Modified-Since'] = File.stat(cache_path).mtime.rfc2822 | ||
end | ||
response = http.request(request) | ||
|
||
cache_path.tap do |f| | ||
if response.is_a?(Net::HTTPSuccess) | ||
open f, 'w' do |io| | ||
io.write response.body | ||
end | ||
end | ||
end | ||
end | ||
|
||
private | ||
|
||
def cache_path | ||
".rubocop-#{cache_name_from_uri}" | ||
end | ||
|
||
def cache_path_exists? | ||
@cache_path_exists ||= File.exist?(cache_path) | ||
end | ||
|
||
def cache_path_expired? | ||
return true unless cache_path_exists? | ||
|
||
@cache_path_expired ||= begin | ||
file_age = (Time.now - File.stat(cache_path).mtime).to_f | ||
(file_age / CACHE_LIFETIME) > 1 | ||
end | ||
end | ||
|
||
def cache_name_from_uri | ||
uri = @uri.clone | ||
uri.query = nil | ||
uri.to_s.gsub!(/[^0-9A-Za-z]/, '-') | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# encoding: utf-8 | ||
|
||
require 'spec_helper' | ||
|
||
describe RuboCop::RemoteConfig do | ||
include FileHelper | ||
|
||
let(:remote_config_url) { 'http://example.com/rubocop.yml' } | ||
let(:cached_file_name) { '.rubocop-http---example-com-rubocop-yml' } | ||
|
||
subject(:remote_config) { described_class.new(remote_config_url).file } | ||
|
||
before do | ||
stub_request(:get, /example.com/) | ||
.to_return(status: 200, body: "Style/Encoding:\n Enabled: true") | ||
end | ||
|
||
after do | ||
File.unlink cached_file_name if File.exist? cached_file_name | ||
end | ||
|
||
describe '.file' do | ||
it 'downloads the file if the file does not exist' do | ||
expect(subject).to eq(cached_file_name) | ||
expect(File.exist? cached_file_name).to be_truthy | ||
end | ||
|
||
it 'does not download the file if cache lifetime has not been reached' do | ||
FileUtils.touch cached_file_name, mtime: Time.now - ((60 * 60) * 20) | ||
|
||
expect(subject).to eq(cached_file_name) | ||
assert_not_requested :get, remote_config_url | ||
end | ||
|
||
it 'downloads the file if cache lifetime has been reached' do | ||
FileUtils.touch cached_file_name, mtime: Time.now - ((60 * 60) * 30) | ||
|
||
expect(subject).to eq(cached_file_name) | ||
assert_requested :get, remote_config_url | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters