gitdocs 0.5.0 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (65) hide show
  1. checksums.yaml +6 -14
  2. data/.codeclimate.yml +26 -0
  3. data/.rubocop.yml +8 -2
  4. data/.travis.yml +8 -0
  5. data/CHANGELOG +13 -0
  6. data/Gemfile +1 -1
  7. data/README.md +7 -6
  8. data/Rakefile +31 -5
  9. data/bin/gitdocs +1 -0
  10. data/config.ru +6 -4
  11. data/gitdocs.gemspec +22 -19
  12. data/lib/gitdocs.rb +54 -16
  13. data/lib/gitdocs/browser_app.rb +34 -41
  14. data/lib/gitdocs/cli.rb +41 -32
  15. data/lib/gitdocs/configuration.rb +40 -101
  16. data/lib/gitdocs/git_notifier.rb +111 -0
  17. data/lib/gitdocs/initializer.rb +83 -0
  18. data/lib/gitdocs/manager.rb +90 -60
  19. data/lib/gitdocs/migration/004_add_index_for_path.rb +1 -1
  20. data/lib/gitdocs/notifier.rb +70 -104
  21. data/lib/gitdocs/rendering_helper.rb +3 -0
  22. data/lib/gitdocs/repository.rb +324 -307
  23. data/lib/gitdocs/repository/committer.rb +77 -0
  24. data/lib/gitdocs/repository/path.rb +157 -140
  25. data/lib/gitdocs/search.rb +40 -25
  26. data/lib/gitdocs/settings_app.rb +5 -3
  27. data/lib/gitdocs/share.rb +64 -0
  28. data/lib/gitdocs/synchronizer.rb +40 -0
  29. data/lib/gitdocs/version.rb +1 -1
  30. data/lib/gitdocs/views/_header.haml +2 -2
  31. data/lib/gitdocs/views/dir.haml +3 -3
  32. data/lib/gitdocs/views/edit.haml +1 -1
  33. data/lib/gitdocs/views/file.haml +1 -1
  34. data/lib/gitdocs/views/home.haml +3 -3
  35. data/lib/gitdocs/views/layout.haml +13 -13
  36. data/lib/gitdocs/views/revisions.haml +3 -3
  37. data/lib/gitdocs/views/search.haml +1 -1
  38. data/lib/gitdocs/views/settings.haml +6 -6
  39. data/test/integration/cli/full_sync_test.rb +83 -0
  40. data/test/integration/cli/share_management_test.rb +29 -0
  41. data/test/integration/cli/status_test.rb +14 -0
  42. data/test/integration/test_helper.rb +185 -151
  43. data/test/integration/{browse_test.rb → web/browse_test.rb} +11 -29
  44. data/test/integration/web/share_management_test.rb +46 -0
  45. data/test/support/git_factory.rb +276 -0
  46. data/test/unit/browser_app_test.rb +346 -0
  47. data/test/unit/configuration_test.rb +8 -70
  48. data/test/unit/git_notifier_test.rb +116 -0
  49. data/test/unit/gitdocs_test.rb +90 -0
  50. data/test/unit/manager_test.rb +36 -0
  51. data/test/unit/notifier_test.rb +60 -124
  52. data/test/unit/repository_committer_test.rb +111 -0
  53. data/test/unit/repository_path_test.rb +92 -76
  54. data/test/unit/repository_test.rb +243 -356
  55. data/test/unit/search_test.rb +15 -0
  56. data/test/unit/settings_app_test.rb +80 -0
  57. data/test/unit/share_test.rb +97 -0
  58. data/test/unit/test_helper.rb +17 -3
  59. metadata +114 -108
  60. data/lib/gitdocs/runner.rb +0 -108
  61. data/lib/gitdocs/server.rb +0 -62
  62. data/test/integration/full_sync_test.rb +0 -66
  63. data/test/integration/share_management_test.rb +0 -95
  64. data/test/integration/status_test.rb +0 -21
  65. data/test/unit/runner_test.rb +0 -122
@@ -0,0 +1,111 @@
1
+ # -*- encoding : utf-8 -*-
2
+ require File.expand_path('../test_helper', __FILE__)
3
+
4
+ describe Gitdocs::Repository::Committer do
5
+ before do
6
+ FileUtils.rm_rf('tmp/unit')
7
+ GitFactory.init(:local)
8
+ end
9
+
10
+ let(:root_dirname) { GitFactory.expand_path(:local) }
11
+ let(:committer) { Gitdocs::Repository::Committer.new(root_dirname) }
12
+
13
+ describe 'initialize' do
14
+ subject { committer }
15
+
16
+ describe 'when directory missing' do
17
+ let(:root_dirname) { GitFactory.expand_path(:missing) }
18
+ it { assert_raises(Gitdocs::Repository::InvalidError) { subject } }
19
+ end
20
+
21
+ describe 'when not a repository' do
22
+ let(:root_dirname) { GitFactory.expand_path(:not_a_repo) }
23
+ before { FileUtils.mkdir_p(root_dirname) }
24
+ it { assert_raises(Gitdocs::Repository::InvalidError) { subject } }
25
+ end
26
+
27
+ describe 'when valid repository' do
28
+ it { subject.must_be_kind_of Gitdocs::Repository::Committer }
29
+ end
30
+ end
31
+
32
+ describe '#commit' do
33
+ subject { committer.commit }
34
+
35
+ before { Gitdocs.stubs(:log_debug) }
36
+
37
+ # TODO: should test the paths which use the message file
38
+
39
+ describe 'no previous commits' do
40
+ describe 'nothing to commit' do
41
+ it { subject.must_equal false }
42
+ end
43
+
44
+ describe 'changes to commit' do
45
+ before do
46
+ GitFactory.write(:local, 'file1', 'foobar')
47
+ GitFactory.mkdir(:local, 'directory')
48
+ end
49
+ it { subject.must_equal true }
50
+
51
+ describe 'side effects' do
52
+ before { subject }
53
+ it { GitInspector.file_exist?(:local, 'directory/.gitignore').must_equal true }
54
+ it { GitInspector.commit_count(:local).must_equal 1 }
55
+ it { GitInspector.last_message(:local).must_equal "Auto-commit from gitdocs\n" }
56
+ it { GitInspector.clean?(:local).must_equal true }
57
+ end
58
+ end
59
+ end
60
+
61
+ describe 'previous commits' do
62
+ before do
63
+ GitFactory.commit(:local, 'file1', 'foobar')
64
+ GitFactory.commit(:local, 'file2', 'deadbeef')
65
+ end
66
+
67
+ describe 'nothing to commit' do
68
+ it { subject.must_equal false }
69
+ end
70
+
71
+ describe 'changes to commit' do
72
+ before do
73
+ GitFactory.write(:local, 'file1', 'foobar')
74
+ GitFactory.rm(:local, 'file2')
75
+ GitFactory.write(:local, 'file3', 'foobar')
76
+ GitFactory.mkdir(:local, 'directory')
77
+ end
78
+ it { subject.must_equal true }
79
+
80
+ describe 'side effects' do
81
+ before { subject }
82
+ it { GitInspector.file_exist?(:local, 'directory/.gitignore').must_equal true }
83
+ it { GitInspector.commit_count(:local).must_equal 3 }
84
+ it { GitInspector.last_message(:local).must_equal "Auto-commit from gitdocs\n" }
85
+ it { GitInspector.clean?(:local).must_equal true }
86
+ end
87
+ end
88
+ end
89
+ end
90
+
91
+ describe '#write_commit_message' do
92
+ subject { committer.write_commit_message(commit_message) }
93
+
94
+ before { subject }
95
+
96
+ describe 'with no message' do
97
+ let(:commit_message) { nil }
98
+ it { GitInspector.file_exist?(:local, '.gitmessage~').must_equal(false) }
99
+ end
100
+
101
+ describe 'with empty message' do
102
+ let(:commit_message) { '' }
103
+ it { GitInspector.file_exist?(:local, '.gitmessage~').must_equal(false) }
104
+ end
105
+
106
+ describe 'with message' do
107
+ let(:commit_message) { 'foobar' }
108
+ it { GitInspector.file_content(:local, '.gitmessage~').must_equal('foobar') }
109
+ end
110
+ end
111
+ end
@@ -2,46 +2,63 @@
2
2
  require File.expand_path('../test_helper', __FILE__)
3
3
 
4
4
  describe Gitdocs::Repository::Path do
5
- let(:path) { Gitdocs::Repository::Path.new(repository, relative_path) }
6
- let(:repository) { stub(root: local_repo_path) }
7
- let(:local_repo_path) { 'tmp/unit/local' }
8
-
5
+ let(:path) { Gitdocs::Repository::Path.new(repository, "/#{relative_path}") }
6
+ let(:repository) { stub(root: GitFactory.expand_path(:local)) }
9
7
  let(:relative_path) { 'directory/file' }
10
8
 
11
9
  before do
12
10
  FileUtils.rm_rf('tmp/unit')
11
+ GitFactory.init(:local)
12
+ end
13
+
14
+ describe '#relative_dirname' do
15
+ subject { path.relative_dirname }
16
+
17
+ describe 'root' do
18
+ let(:relative_path) { '' }
19
+ it { subject.must_equal('') }
20
+ end
21
+
22
+ describe 'root filename' do
23
+ let(:relative_path) { 'directory' }
24
+ it { subject.must_equal('') }
25
+ end
26
+
27
+ describe 'non-root filename' do
28
+ let(:relative_path) { 'directory1/directory2/file' }
29
+ it { subject.must_equal('directory1/directory2') }
30
+ end
13
31
  end
14
32
 
15
33
  describe '#join' do
16
34
  subject { path.join('new_file') }
17
35
  before { subject }
18
- it { path.relative_path.must_equal('directory/file/new_file') }
19
- it { path.absolute_path.must_equal(absolute_local_path('new_file')) }
36
+ it { path.relative_path.must_equal(File.join(relative_path, 'new_file')) }
37
+ it { path.absolute_path.must_equal(GitFactory.expand_path(:local, relative_path, 'new_file')) }
20
38
  end
21
39
 
22
40
  describe '#write' do
23
- subject { path.write('foobar', :message) }
24
- before { repository.expects(:write_commit_message).with(:message) }
41
+ subject { path.write('foobar') }
25
42
 
26
43
  describe 'directory missing' do
27
44
  before { subject }
28
- it { local_file_content.must_equal "foobar\n" }
45
+ it { GitInspector.file_content(:local, relative_path).must_equal "foobar\n" }
29
46
  end
30
47
 
31
48
  describe 'directory exists' do
32
49
  before do
33
- mkdir('directory')
50
+ mkdir(File.dirname(relative_path))
34
51
  subject
35
52
  end
36
- it { local_file_content.must_equal "foobar\n" }
53
+ it { GitInspector.file_content(:local, relative_path).must_equal "foobar\n" }
37
54
  end
38
55
 
39
56
  describe 'file exists' do
40
57
  before do
41
- write('directory/file', 'deadbeef')
58
+ write(relative_path, 'deadbeef')
42
59
  subject
43
60
  end
44
- it { local_file_content.must_equal "foobar\n" }
61
+ it { GitInspector.file_content(:local, relative_path).must_equal "foobar\n" }
45
62
  end
46
63
  end
47
64
 
@@ -50,23 +67,23 @@ describe Gitdocs::Repository::Path do
50
67
 
51
68
  describe 'when directory does not exist' do
52
69
  before { subject }
53
- it { local_file_content.must_equal '' }
70
+ it { GitInspector.file_content(:local, relative_path).must_equal '' }
54
71
  end
55
72
 
56
73
  describe 'when directory already exists' do
57
74
  before do
58
- mkdir('directory')
75
+ mkdir(File.dirname(relative_path))
59
76
  subject
60
77
  end
61
- it { local_file_content.must_equal '' }
78
+ it { GitInspector.file_content(:local, relative_path).must_equal '' }
62
79
  end
63
80
 
64
81
  describe 'when file already exists' do
65
82
  before do
66
- write('directory/file', 'test')
83
+ write(relative_path, 'test')
67
84
  subject
68
85
  end
69
- it { local_file_content.must_equal 'test' }
86
+ it { GitInspector.file_content(:local, relative_path).must_equal 'test' }
70
87
  end
71
88
  end
72
89
 
@@ -75,23 +92,35 @@ describe Gitdocs::Repository::Path do
75
92
 
76
93
  describe 'directory does not exist' do
77
94
  before { subject }
78
- it { File.directory?(File.join(local_repo_path, 'directory', 'file')) }
95
+ it { File.directory?(GitFactory.expand_path(:local, relative_path)) }
79
96
  end
80
97
 
81
98
  describe 'directory does exist' do
82
99
  before do
83
- mkdir('directory/file')
100
+ mkdir(relative_path)
84
101
  subject
85
102
  end
86
- it { File.directory?(File.join(local_repo_path, 'directory', 'file')) }
103
+ it { File.directory?(GitFactory.expand_path(:local, relative_path)) }
87
104
  end
88
105
 
89
106
  describe 'already exists as a file' do
90
- before { write('directory/file', 'foobar') }
107
+ before { write(relative_path, 'foobar') }
91
108
  it { assert_raises(Errno::EEXIST) { subject } }
92
109
  end
93
110
  end
94
111
 
112
+ describe '#mv' do
113
+ subject { path.mv(source_filename) }
114
+ let(:source_filename) { File.join(GitFactory.working_directory, 'move_me') }
115
+ before do
116
+ FileUtils.mkdir_p(File.dirname(source_filename))
117
+ File.write(source_filename, 'foobar')
118
+
119
+ subject
120
+ end
121
+ it { GitInspector.file_content(:local, relative_path).must_equal('foobar') }
122
+ end
123
+
95
124
  describe '#remove' do
96
125
  subject { path.remove }
97
126
 
@@ -100,16 +129,16 @@ describe Gitdocs::Repository::Path do
100
129
  end
101
130
 
102
131
  describe 'directory' do
103
- before { mkdir('directory/file') }
132
+ before { mkdir(relative_path) }
104
133
  it { subject.must_be_nil }
105
134
  end
106
135
 
107
136
  describe 'file' do
108
137
  before do
109
- write('directory/file', 'foobar')
138
+ write(relative_path, 'foobar')
110
139
  subject
111
140
  end
112
- it { local_file_exist?.must_equal false }
141
+ it { GitInspector.file_exist?(:local, relative_path).must_equal false }
113
142
  end
114
143
  end
115
144
 
@@ -121,22 +150,22 @@ describe Gitdocs::Repository::Path do
121
150
  end
122
151
 
123
152
  describe 'directory' do
124
- before { mkdir('directory/file') }
153
+ before { mkdir(relative_path) }
125
154
  it { subject.must_equal false }
126
155
  end
127
156
 
128
157
  describe 'not a text file' do
129
- let(:relative_path) { 'directory/file.png' }
158
+ let(:relative_path) { 'file.png' }
130
159
  it { subject.must_equal false }
131
160
  end
132
161
 
133
162
  describe 'empty file' do
134
- before { write('directory/file', '') }
163
+ before { write(relative_path, '') }
135
164
  it { subject.must_equal true }
136
165
  end
137
166
 
138
167
  describe 'text file' do
139
- before { write('directory/file', 'foobar') }
168
+ before { write(relative_path, 'foobar') }
140
169
  it { subject.must_equal true }
141
170
  end
142
171
  end
@@ -148,27 +177,27 @@ describe Gitdocs::Repository::Path do
148
177
  end
149
178
 
150
179
  describe 'when missing' do
151
- let(:commit) { nil }
180
+ let(:commit) { nil }
152
181
  it { assert_raises(RuntimeError) { subject } }
153
182
  end
154
183
 
155
- describe 'on a 'do
184
+ describe 'on a ' do
156
185
  let(:commit) { stub(author: { name: :name, time: :time }) }
157
186
  before do
158
- write('directory0/file0', '')
159
- write('directory/file1', 'foo')
160
- write('directory/file2', 'bar')
187
+ write(File.join(%w(directory0 file0)), '')
188
+ write(File.join(%w(directory file1)), 'foo')
189
+ write(File.join(%w(directory file2)), 'bar')
161
190
  end
162
191
 
163
192
  describe 'file size 0' do
164
- let(:relative_path) { 'directory0/file0' }
193
+ let(:relative_path) { File.join(%w(directory0 file0)) }
165
194
  it { subject[:author].must_equal :name }
166
195
  it { subject[:size].must_equal(-1) }
167
196
  it { subject[:modified].must_equal :time }
168
197
  end
169
198
 
170
199
  describe 'file non-zero size' do
171
- let(:relative_path) { 'directory/file1' }
200
+ let(:relative_path) { File.join(%w(directory file1)) }
172
201
  it { subject[:author].must_equal :name }
173
202
  it { subject[:size].must_equal(3) }
174
203
  it { subject[:modified].must_equal :time }
@@ -198,12 +227,12 @@ describe Gitdocs::Repository::Path do
198
227
  end
199
228
 
200
229
  describe 'directory' do
201
- before { mkdir('directory/file') }
230
+ before { mkdir(relative_path) }
202
231
  it { subject.must_equal true }
203
232
  end
204
233
 
205
234
  describe 'file' do
206
- before { write('directory/file', 'foobar') }
235
+ before { write(relative_path, 'foobar') }
207
236
  it { subject.must_equal true }
208
237
  end
209
238
  end
@@ -216,12 +245,12 @@ describe Gitdocs::Repository::Path do
216
245
  end
217
246
 
218
247
  describe 'directory' do
219
- before { mkdir('directory/file') }
248
+ before { mkdir(relative_path) }
220
249
  it { subject.must_equal true }
221
250
  end
222
251
 
223
252
  describe 'file' do
224
- before { write('directory/file', 'foobar') }
253
+ before { write(relative_path, 'foobar') }
225
254
  it { subject.must_equal false }
226
255
  end
227
256
  end
@@ -231,7 +260,7 @@ describe Gitdocs::Repository::Path do
231
260
 
232
261
  describe 'no revision' do
233
262
  let(:ref) { nil }
234
- it { subject.must_equal absolute_local_path }
263
+ it { subject.must_equal GitFactory.expand_path(:local, relative_path) }
235
264
  end
236
265
 
237
266
  describe 'with revision' do
@@ -258,13 +287,13 @@ describe Gitdocs::Repository::Path do
258
287
  end
259
288
 
260
289
  describe 'no README' do
261
- before { mkdir('directory/file') }
290
+ before { mkdir(relative_path) }
262
291
  it { subject.must_be_nil }
263
292
  end
264
293
 
265
294
  describe 'with README.md' do
266
- before { write('directory/file/README.md', 'foobar') }
267
- it { subject.must_equal absolute_local_path('README.md') }
295
+ before { write(File.join(relative_path, 'README.md'), 'foobar') }
296
+ it { subject.must_equal GitFactory.expand_path(:local, relative_path, 'README.md') }
268
297
  end
269
298
  end
270
299
 
@@ -276,21 +305,20 @@ describe Gitdocs::Repository::Path do
276
305
  end
277
306
 
278
307
  describe 'file' do
279
- before { write('directory/file', 'foobar') }
308
+ before { write(relative_path, 'foobar') }
280
309
  it { subject.must_be_nil }
281
310
  end
282
311
 
283
312
  describe 'directory' do
284
313
  before do
285
- write('directory/file/.hidden', 'beef')
286
- mkdir('directory/file/dir1')
287
- write('directory/file/file1', 'foo')
288
- write('directory/file/file2', 'bar')
314
+ write(File.join(relative_path, '.hidden'), 'beef')
315
+ mkdir(File.join(relative_path, 'dir1'))
316
+ write(File.join(relative_path, 'file1'), 'foo')
317
+ write(File.join(relative_path, 'file2'), 'bar')
289
318
 
290
319
  # Paths which should not be included
291
- write('directory/file/.git', 'test')
292
- write('directory/file/.gitignore', 'test')
293
- write('directory/file/.gitmessage~', 'test')
320
+ write(File.join(relative_path, '.gitignore'), 'test')
321
+ write(File.join(relative_path, '.gitmessage~'), 'test')
294
322
  end
295
323
 
296
324
  it { subject.size.must_equal 4 }
@@ -307,12 +335,12 @@ describe Gitdocs::Repository::Path do
307
335
  end
308
336
 
309
337
  describe 'directory' do
310
- before { mkdir('directory/file') }
338
+ before { mkdir(relative_path) }
311
339
  it { subject.must_be_nil }
312
340
  end
313
341
 
314
342
  describe 'file' do
315
- before { write('directory/file', 'foobar') }
343
+ before { write(relative_path, 'foobar') }
316
344
  it { subject.must_equal 'foobar' }
317
345
  end
318
346
  end
@@ -321,10 +349,12 @@ describe Gitdocs::Repository::Path do
321
349
  subject { path.revisions }
322
350
 
323
351
  before do
324
- repository.stubs(:commits_for).returns([
325
- stub(oid: '1234567890', message: "short1\nlong", author: { name: :name1, time: :time1 }),
326
- stub(oid: '0987654321', message: "short2\nlong", author: { name: :name2, time: :time2 })
327
- ])
352
+ repository.stubs(:commits_for).returns(
353
+ [
354
+ stub(oid: '1234567890', message: "short1\nlong", author: { name: :name1, time: :time1 }),
355
+ stub(oid: '0987654321', message: "short2\nlong", author: { name: :name2, time: :time2 })
356
+ ]
357
+ )
328
358
  end
329
359
  it { subject.size.must_equal(2) }
330
360
  it { subject[0].must_equal(commit: '1234567', subject: 'short1', author: :name1, date: :time1) }
@@ -342,7 +372,7 @@ describe Gitdocs::Repository::Path do
342
372
 
343
373
  describe 'blob present' do
344
374
  let(:blob) { stub(text: 'deadbeef') }
345
- before { path.expects(:write).with('deadbeef', "Reverting '#{relative_path}' to ref") }
375
+ before { path.expects(:write).with('deadbeef') }
346
376
  it { subject }
347
377
  end
348
378
  end
@@ -352,24 +382,10 @@ describe Gitdocs::Repository::Path do
352
382
  private
353
383
 
354
384
  def write(filename, content)
355
- mkdir(File.dirname(filename))
356
- File.write(File.join(local_repo_path, filename), content)
357
- end
358
-
359
- def mkdir(*path)
360
- FileUtils.mkdir_p(File.join(local_repo_path, *path))
361
- end
362
-
363
- def local_file_exist?
364
- File.exist?(File.join(local_repo_path, relative_path))
365
- end
366
-
367
- def local_file_content
368
- return nil unless local_file_exist?
369
- File.read(File.join(local_repo_path, relative_path))
385
+ GitFactory.write(:local, filename, content)
370
386
  end
371
387
 
372
- def absolute_local_path(*path_elements)
373
- File.join(File.absolute_path(local_repo_path), relative_path, *path_elements)
388
+ def mkdir(path)
389
+ GitFactory.mkdir(:local, path)
374
390
  end
375
391
  end