nsync 0.4.2 → 0.4.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.4.2
1
+ 0.4.3
@@ -152,6 +152,12 @@ module Nsync
152
152
  end
153
153
 
154
154
  class Change < Struct.new(:id, :diff)
155
+ def json_data(data)
156
+ JSON.load(data)
157
+ rescue
158
+ {}
159
+ end
160
+
155
161
  def type
156
162
  if diff.deleted_file
157
163
  :deleted
@@ -162,22 +168,18 @@ module Nsync
162
168
  end
163
169
  end
164
170
 
165
- def a_data
166
- JSON.load(diff.a_blob.data)
167
- rescue
168
- {}
171
+ def a_data(parse_json=true)
172
+ val = diff.a_blob ? diff.a_blob.data : ""
173
+ parse_json ? json_data(val) : val
169
174
  end
170
175
 
171
- def b_data
172
- JSON.load(diff.b_blob.data)
173
- rescue
174
- {}
176
+ def b_data(parse_json=true)
177
+ val = diff.b_blob ? diff.b_blob.data : ""
178
+ parse_json ? json_data(val) : val
175
179
  end
176
180
 
177
181
  def data
178
- @data ||= JSON.load(diff.b_blob.data)
179
- rescue
180
- {}
182
+ @data ||= b_data
181
183
  end
182
184
  end
183
185
 
@@ -322,14 +324,14 @@ module Nsync
322
324
  end
323
325
  end
324
326
 
325
- def changeset_from_diffs(diffs)
327
+ def changeset_from_diffs(diffs, change_klass=Change)
326
328
  diffs.inject({}) do |h, diff|
327
329
  next h if diff_path(diff) =~ /\.gitignore$/
328
330
 
329
331
  classes, id = consumer_classes_and_id_from_path(diff_path(diff))
330
332
  classes.each do |klass|
331
333
  h[klass] ||= []
332
- h[klass] << Change.new(id, diff)
334
+ h[klass] << change_klass.new(id, diff)
333
335
  end
334
336
  h
335
337
  end
@@ -60,6 +60,17 @@ module Nsync
60
60
  config.log.info("[NSYNC] Removed file '#{filename}'")
61
61
  end
62
62
 
63
+ class Change < ::Nsync::Consumer::Change
64
+ def b_data(parse_json=true)
65
+ if type == :deleted
66
+ super
67
+ else
68
+ val = File.read(File.join(Nsync.config.repo_path, diff.b_path))
69
+ parse_json ? json_data(val) : val
70
+ end
71
+ end
72
+ end
73
+
63
74
  def latest_changes
64
75
  # TODO: change to using --work-tree in git
65
76
  diff = config.cd { repo.git.native('diff', {:full_index => true}) }
@@ -72,7 +83,7 @@ module Nsync
72
83
  end
73
84
 
74
85
  diffs = Grit::Diff.list_from_string(repo, diff)
75
- changeset_from_diffs(diffs)
86
+ changeset_from_diffs(diffs, Change)
76
87
  end
77
88
 
78
89
  # gets untracked files into the diff output
data/nsync.gemspec CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{nsync}
8
- s.version = "0.4.2"
8
+ s.version = "0.4.3"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Ben Hughes"]
@@ -198,6 +198,40 @@ class NsyncProducerTest < Test::Unit::TestCase
198
198
  end
199
199
  end
200
200
 
201
+ context "latest_changes" do
202
+ setup do
203
+ @repo = TestRepo.new
204
+
205
+ Nsync.config.repo_path = @repo.repo_path
206
+ @producer = Nsync::Producer.new
207
+ end
208
+
209
+ should "work the same for added/modified/deleted" do
210
+ filename = "nsync_test_foo/1.json"
211
+ full_path = File.join(@repo.repo_path, filename)
212
+ @repo.add_file(filename,
213
+ {:id => 1, :data => "new file"}, false)
214
+ changes = @producer.latest_changes
215
+ assert_equal 1, changes[NsyncTestFoo].size
216
+ assert_equal JSON.load(File.read(full_path)), changes[NsyncTestFoo][0].b_data
217
+ @producer.commit("added new file")
218
+
219
+ @repo.add_file(filename,
220
+ {:id => 1, :data => "modified file"}, false)
221
+ changes = @producer.latest_changes
222
+ assert_equal 1, changes[NsyncTestFoo].size
223
+ assert_equal JSON.load(File.read(full_path)), changes[NsyncTestFoo][0].b_data
224
+ old_value = JSON.load(File.read(full_path))
225
+ @producer.commit("modified file")
226
+
227
+ @repo.remove_file(filename)
228
+ changes = @producer.latest_changes
229
+ assert_equal 1, changes[NsyncTestFoo].size
230
+ assert_equal old_value, changes[NsyncTestFoo][0].a_data
231
+ assert changes[NsyncTestFoo][0].diff.deleted_file
232
+ end
233
+ end
234
+
201
235
  context "basic flow" do
202
236
  setup do
203
237
  @repo_path = TestRepo.repo_path
data/test/repo.rb CHANGED
@@ -38,7 +38,7 @@ class TestRepo
38
38
  "#{repo_path}_producer_push.git"
39
39
  end
40
40
 
41
- def add_file(filename, content)
41
+ def add_file(filename, content, add=true)
42
42
  cd do
43
43
  dir = File.dirname(filename)
44
44
  if ![".", "/"].include?(dir) && !File.exists?(dir)
@@ -47,7 +47,7 @@ class TestRepo
47
47
  File.open(File.join(repo_path, filename), "w") do |f|
48
48
  f.write((content.is_a?(Hash))? content.to_json : content)
49
49
  end
50
- repo.add(File.join(repo_path, filename))
50
+ repo.add(File.join(repo_path, filename)) if add
51
51
  end
52
52
  end
53
53
 
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 4
8
- - 2
9
- version: 0.4.2
8
+ - 3
9
+ version: 0.4.3
10
10
  platform: ruby
11
11
  authors:
12
12
  - Ben Hughes