git-approvals 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -20,6 +20,9 @@ Gem::Specification.new do |spec|
20
20
 
21
21
  spec.add_dependency 'awesome_print', '1.1.0'
22
22
 
23
+ # soft dependencies
24
+ spec.add_development_dependency 'uglifier', '~> 2.1.1'
25
+
23
26
  spec.add_development_dependency 'bundler', '~> 1.3'
24
27
  spec.add_development_dependency 'rspec', '~> 2.13.0'
25
28
  spec.add_development_dependency 'guard-rspec', '~> 2.5.2'
@@ -2,18 +2,91 @@ require 'open3'
2
2
 
3
3
  module Git
4
4
  module Approvals
5
+ ##
6
+ # The base class for approval errors.
7
+ class ApprovalError < StandardError; end
8
+
9
+ ##
10
+ # Raised when an unregistered formatter is requested.
11
+ class UnknownFormat < ApprovalError
12
+ def initialize( name )
13
+ super "There is no registered formatter named '#{name}'."
14
+ end
15
+ end
16
+
17
+ ##
18
+ # Raised when a formatter's soft dependencies are missing.
19
+ class MissingSoftDependency < ApprovalError
20
+ def initialize( name, dependency )
21
+ super <<-EOS
22
+ The format '#{name}' requires #{dependency}.
23
+ To use this formatter, make sure to load the dependency:
24
+
25
+ require '#{dependency}'
26
+ EOS
27
+ end
28
+ end
29
+
30
+ ##
31
+ #
5
32
  class Approval
6
33
 
7
- FORMATTERS = {
8
- :txt => lambda { |object|
9
- require 'awesome_print'
10
- object.awesome_inspect :plain => true, :indent => -2
11
- },
12
- :json => lambda { |string|
13
- require 'json'
14
- JSON.pretty_generate JSON.parse( string )
15
- }
16
- }
34
+ class << self
35
+
36
+ ##
37
+ # Registers a new formatter block by name. The block
38
+ # is expected to return a deterministic string
39
+ # representation of an object.
40
+ def register_formatter( name, &block )
41
+ formatters[ name.to_sym ] = block
42
+ end
43
+
44
+ ##
45
+ # Looks up the formatter named `name` and attempts to
46
+ # format `object`. Raises a helpful error message if
47
+ # a formatter's soft dependency cannot be loaded.
48
+ def format( name, object )
49
+ formatters[ name ][ object ]
50
+ rescue NoMethodError => e
51
+ raise UnknownFormat, name
52
+ rescue LoadError => e
53
+ raise MissingSoftDependency.new name,
54
+ e.message[ /^cannot load such file -- (.*)$/, 1 ]
55
+ end
56
+
57
+ protected
58
+
59
+ ##
60
+ # The hash of registered formatters by name.
61
+ def formatters
62
+ @formatters ||= { }
63
+ end
64
+ end
65
+
66
+ ##
67
+ # The `txt` format requires the `awesome_print` gem.
68
+ # It is suitable for formatting most native ruby types.
69
+ register_formatter :txt do |object|
70
+ require 'awesome_print'
71
+ object.awesome_inspect :plain => true, :indent => -2
72
+ end
73
+
74
+ ##
75
+ # The `json` format requires the `json` library.
76
+ # It is suitable for formatting JSON strings.
77
+ register_formatter :json do |object|
78
+ require 'json'
79
+ JSON.pretty_generate JSON.parse( object )
80
+ end
81
+
82
+ ##
83
+ # The `js` format requires the `uglifier` gem.
84
+ # It is suitable for formatting javascript.
85
+ register_formatter :js do |object|
86
+ require 'uglifier'
87
+ Uglifier.compile object,
88
+ :output => { :beautify => true, :indent_level => 2 }
89
+ end
17
90
 
18
91
  def initialize( path, options={} ) # :nodoc:
19
92
  @path, @options = path, options
@@ -25,14 +98,17 @@ module Git
25
98
  attr_reader :path, :options
26
99
 
27
100
  ##
28
- #
101
+ # Diffs the given string with this approval file. If the
102
+ # file has not been checked in, this method will raise an
103
+ # exception. Otherwise, the supplied block will only be
104
+ # called if the diff fails, meaning there are differences.
29
105
  def diff( string, &block )
30
106
  # Make sure the directory of the file exists.
31
107
  FileUtils.mkdir_p File.dirname( path )
32
108
 
33
109
  # Write the new string to the file.
34
110
  File.open path, 'w' do |f|
35
- f << FORMATTERS[ format ][ string ]
111
+ f << self.class.format( format, string )
36
112
  end
37
113
 
38
114
  # If the file hasn't been checked in, raise an error.
@@ -1,5 +1,5 @@
1
1
  module Git
2
2
  module Approvals
3
- VERSION = '0.2.0'
3
+ VERSION = '0.2.1'
4
4
  end
5
5
  end
@@ -0,0 +1,3 @@
1
+ (function() {
2
+ return "IT WERKS";
3
+ })();
@@ -69,5 +69,9 @@ describe Git::Approvals::Approval do
69
69
  approval = described_class.new './spec/fixtures/hash.json', :format => :json
70
70
  approval.diff( '{"foo":"bar","baz":"quux"}' ){ |diff| fail diff }
71
71
  end
72
+ it 'formats javascript' do
73
+ approval = described_class.new './spec/fixtures/asset.js', :format => :js
74
+ approval.diff( '(function(){return "IT WERKS";})();' ){ |diff| fail diff }
75
+ end
72
76
  end
73
77
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: git-approvals
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-04-19 00:00:00.000000000 Z
12
+ date: 2013-06-11 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: awesome_print
@@ -27,6 +27,22 @@ dependencies:
27
27
  - - '='
28
28
  - !ruby/object:Gem::Version
29
29
  version: 1.1.0
30
+ - !ruby/object:Gem::Dependency
31
+ name: uglifier
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: 2.1.1
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: 2.1.1
30
46
  - !ruby/object:Gem::Dependency
31
47
  name: bundler
32
48
  requirement: !ruby/object:Gem::Requirement
@@ -148,6 +164,7 @@ files:
148
164
  - lib/git/approvals/version.rb
149
165
  - lib/rspec/approvals.rb
150
166
  - spec/fixtures/array.txt
167
+ - spec/fixtures/asset.js
151
168
  - spec/fixtures/hash.json
152
169
  - spec/fixtures/hash.txt
153
170
  - spec/fixtures/string.txt
@@ -184,6 +201,7 @@ specification_version: 3
184
201
  summary: Simple git-powered approval tests.
185
202
  test_files:
186
203
  - spec/fixtures/array.txt
204
+ - spec/fixtures/asset.js
187
205
  - spec/fixtures/hash.json
188
206
  - spec/fixtures/hash.txt
189
207
  - spec/fixtures/string.txt