Skip to content

Commit 82bd084

Browse files
committed
Merge pull request #1071 from Eric-Guo/appveyor
Make testing suite running and pass in Windows
2 parents acb3679 + 30463f8 commit 82bd084

File tree

4 files changed

+57
-4
lines changed

4 files changed

+57
-4
lines changed

Gemfile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,6 @@ if version == "master"
1515
else
1616
gem "rails", "~> #{version}.0"
1717
end
18+
19+
# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
20+
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]

lib/active_model/serializer.rb

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,22 @@ class Serializer
1212
include Configuration
1313
include Associations
1414

15+
16+
# Matches
17+
# "c:/git/emberjs/ember-crm-backend/app/serializers/lead_serializer.rb:1:in `<top (required)>'"
18+
# AND
19+
# "/c/git/emberjs/ember-crm-backend/app/serializers/lead_serializer.rb:1:in `<top (required)>'"
20+
# AS
21+
# c/git/emberjs/ember-crm-backend/app/serializers/lead_serializer.rb
22+
CALLER_FILE = /
23+
\A # start of string
24+
\S+ # one or more non-spaces
25+
(?= # stop previous match when
26+
:\d+ # a colon is followed by one or more digits
27+
:in # followed by a colon followed by in
28+
)
29+
/x
30+
1531
class << self
1632
attr_accessor :_attributes
1733
attr_accessor :_attributes_keys
@@ -29,8 +45,7 @@ def self.inherited(base)
2945
base._attributes = self._attributes.try(:dup) || []
3046
base._attributes_keys = self._attributes_keys.try(:dup) || {}
3147
base._urls = []
32-
serializer_file = File.open(caller.first[/^[^:]+/])
33-
base._cache_digest = Digest::MD5.hexdigest(serializer_file.read)
48+
base._cache_digest = digest_caller_file(caller.first)
3449
super
3550
end
3651

@@ -161,6 +176,12 @@ def self.serializers_cache
161176
@serializers_cache ||= ThreadSafe::Cache.new
162177
end
163178

179+
def self.digest_caller_file(caller_line)
180+
serializer_file_path = caller_line[CALLER_FILE]
181+
serializer_file_contents = IO.read(serializer_file_path)
182+
Digest::MD5.hexdigest(serializer_file_contents)
183+
end
184+
164185
attr_reader :options
165186

166187
def self.get_serializer_for(klass)

test/generators/serializer_generator_test.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,11 @@ def test_generates_attributes_and_associations
4646
def test_with_no_attributes_does_not_add_extra_space
4747
run_generator ["account"]
4848
assert_file "app/serializers/account_serializer.rb" do |content|
49-
assert_no_match /\n\nend/, content
49+
if RUBY_PLATFORM =~ /mingw/
50+
assert_no_match /\r\n\r\nend/, content
51+
else
52+
assert_no_match /\n\nend/, content
53+
end
5054
end
5155
end
5256
end

test/serializers/cache_test.rb

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
require 'test_helper'
2+
require 'tempfile'
23
module ActiveModel
34
class Serializer
45
class CacheTest < Minitest::Test
@@ -125,10 +126,34 @@ def test_uses_file_digest_in_cache_key
125126
assert_equal(@blog_serializer.attributes, ActionController::Base.cache_store.fetch(@blog.cache_key_with_digest))
126127
end
127128

128-
def _cache_digest_definition
129+
def test_cache_digest_definition
129130
assert_equal(::Model::FILE_DIGEST, @post_serializer.class._cache_digest)
130131
end
131132

133+
def test_serializer_file_path_on_nix
134+
path = "/Users/git/emberjs/ember-crm-backend/app/serializers/lead_serializer.rb"
135+
caller_line = "#{path}:1:in `<top (required)>'"
136+
assert_equal caller_line[ActiveModel::Serializer::CALLER_FILE], path
137+
end
138+
139+
def test_serializer_file_path_on_windows
140+
path = "c:/git/emberjs/ember-crm-backend/app/serializers/lead_serializer.rb"
141+
caller_line = "#{path}:1:in `<top (required)>'"
142+
assert_equal caller_line[ActiveModel::Serializer::CALLER_FILE], path
143+
end
144+
145+
def test_digest_caller_file
146+
contents = "puts 'AMS rocks'!"
147+
file = Tempfile.new("some_ruby.rb")
148+
file.write(contents)
149+
path = file.path
150+
caller_line = "#{path}:1:in `<top (required)>'"
151+
file.close
152+
assert_equal ActiveModel::Serializer.digest_caller_file(caller_line), Digest::MD5.hexdigest(contents)
153+
ensure
154+
file.unlink
155+
end
156+
132157
private
133158
def render_object_with_cache(obj)
134159
ActiveModel::SerializableResource.new(obj).serializable_hash

0 commit comments

Comments
 (0)