This repository has been archived by the owner on Jun 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 99
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
second part - cache spec file analysis
- Loading branch information
Showing
29 changed files
with
284 additions
and
190 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
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,77 @@ | ||
module Jasmine::Headless | ||
class CacheableAction | ||
class << self | ||
def enabled=(bool) | ||
@enabled = bool | ||
end | ||
|
||
def enabled? | ||
@enabled = true if @enabled == nil | ||
@enabled | ||
end | ||
|
||
def cache_type | ||
raise ArgumentError.new("No cache type defined for #{self.name}") if @cache_type == nil | ||
@cache_type | ||
end | ||
|
||
def cache_type=(type) | ||
@cache_type = type | ||
end | ||
|
||
def cache_dir=(dir) | ||
@cache_dir = dir | ||
end | ||
|
||
def cache_dir | ||
@cache_dir ||= '.jhw-cache' | ||
end | ||
|
||
def for(file) | ||
new(file).handle | ||
end | ||
end | ||
|
||
attr_reader :file | ||
|
||
def initialize(file) | ||
@file = file | ||
end | ||
|
||
def handle | ||
if CacheableAction.enabled? | ||
if fresh? | ||
unserialize(File.read(cache_file)) | ||
else | ||
result = action | ||
FileUtils.mkdir_p File.split(cache_file).first | ||
File.open(cache_file, 'wb') { |fh| fh.print serialize(result) } | ||
result | ||
end | ||
else | ||
action | ||
end | ||
end | ||
|
||
def cache_file | ||
@cache_file ||= File.join(self.class.cache_dir, self.class.cache_type, Digest::SHA1.hexdigest(file)) | ||
end | ||
|
||
def fresh? | ||
File.exist?(cache_file) && (File.mtime(file) < File.mtime(cache_file)) | ||
end | ||
|
||
def action | ||
raise StandardError.new("Override action") | ||
end | ||
|
||
def serialize(data) | ||
data | ||
end | ||
|
||
def unserialize(data) | ||
data | ||
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
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,37 @@ | ||
require 'iconv' | ||
require 'multi_json' | ||
|
||
module Jasmine::Headless | ||
class SpecFileAnalyzer < CacheableAction | ||
class << self | ||
def cache_type | ||
"spec_file_analysis" | ||
end | ||
end | ||
|
||
def action | ||
line_numbers = {} | ||
|
||
ic = Iconv.new('UTF-8//IGNORE', 'US-ASCII') | ||
data = ic.iconv(File.read(file) + ' ')[0..-2] | ||
data.force_encoding('US-ASCII') if data.respond_to?(:force_encoding) | ||
|
||
data.lines.each_with_index.each { |line, index| | ||
if description = line[%r{(describe|context|it)[( ]*(["'])(.*)\2}, 3] | ||
(line_numbers[description] ||= []) << (index + 1) | ||
end | ||
} | ||
|
||
line_numbers | ||
end | ||
|
||
def serialize(data) | ||
MultiJson.encode(data) | ||
end | ||
|
||
def unserialize(data) | ||
MultiJson.decode(data) | ||
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
Oops, something went wrong.