|
|
|
@ -2,16 +2,23 @@ require 'test_helper'
|
|
|
|
|
require 'docs'
|
|
|
|
|
|
|
|
|
|
class FileScraperTest < MiniTest::Spec
|
|
|
|
|
ROOT_PATH = File.expand_path('../../../../../../', __FILE__)
|
|
|
|
|
|
|
|
|
|
class Scraper < Docs::FileScraper
|
|
|
|
|
self.dir = '/'
|
|
|
|
|
self.html_filters = Docs::FilterStack.new
|
|
|
|
|
self.text_filters = Docs::FilterStack.new
|
|
|
|
|
|
|
|
|
|
version 'version' do; end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
let :scraper do
|
|
|
|
|
Scraper.new
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
let :versioned_scraper do
|
|
|
|
|
Scraper.versions.first.new
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
let :response do
|
|
|
|
|
OpenStruct.new body: 'body', url: Docs::URL.parse(Scraper.base_url)
|
|
|
|
|
end
|
|
|
|
@ -22,9 +29,16 @@ class FileScraperTest < MiniTest::Spec
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
describe "#source_directory" do
|
|
|
|
|
it "returns the directory at docs/[slug]" do
|
|
|
|
|
assert_equal File.join(ROOT_PATH, 'docs', 'scraper'), scraper.source_directory
|
|
|
|
|
assert_equal File.join(ROOT_PATH, 'docs', 'scraper~version'), versioned_scraper.source_directory
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
describe "#request_one" do
|
|
|
|
|
let :path do
|
|
|
|
|
File.join(Scraper.dir, 'path')
|
|
|
|
|
'path'
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
let :result do
|
|
|
|
@ -35,6 +49,19 @@ class FileScraperTest < MiniTest::Spec
|
|
|
|
|
stub(scraper).read_file
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context "when the source directory doesn't exist" do
|
|
|
|
|
it "raises an error" do
|
|
|
|
|
assert_raises Docs::SetupError do
|
|
|
|
|
result
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context "when the source directory exists" do
|
|
|
|
|
before do
|
|
|
|
|
stub(scraper).assert_source_directory_exists
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it "reads a file" do
|
|
|
|
|
mock(scraper).read_file(path)
|
|
|
|
|
result
|
|
|
|
@ -52,12 +79,26 @@ class FileScraperTest < MiniTest::Spec
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
describe "#request_all" do
|
|
|
|
|
let :urls do
|
|
|
|
|
%w(one two)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context "when the source directory doesn't exist" do
|
|
|
|
|
it "raises an error" do
|
|
|
|
|
assert_raises Docs::SetupError do
|
|
|
|
|
scraper.send(:request_all, urls) {}
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context "when the source directory exists" do
|
|
|
|
|
before do
|
|
|
|
|
stub(scraper).assert_source_directory_exists
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it "requests the given url" do
|
|
|
|
|
mock(scraper).request_one('url')
|
|
|
|
|
scraper.send(:request_all, 'url') {}
|
|
|
|
@ -104,6 +145,7 @@ class FileScraperTest < MiniTest::Spec
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
describe "#process_response?" do
|
|
|
|
|
let :result do
|
|
|
|
@ -126,13 +168,13 @@ class FileScraperTest < MiniTest::Spec
|
|
|
|
|
scraper.send :read_file, 'file'
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it "returns the file's content when the file exists" do
|
|
|
|
|
stub(File).read('file') { 'content' }
|
|
|
|
|
it "returns the file's content when the file exists in the source directory" do
|
|
|
|
|
stub(File).read(File.join(ROOT_PATH, 'docs', 'scraper', 'file')) { 'content' }
|
|
|
|
|
assert_equal 'content', result
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it "returns nil when the file doesn't exist" do
|
|
|
|
|
stub(File).read('file') { raise }
|
|
|
|
|
stub(File).read(File.join(ROOT_PATH, 'docs', 'scraper', 'file')) { raise }
|
|
|
|
|
assert_nil result
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|