dassets 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (42) hide show
  1. data/README.md +15 -5
  2. data/lib/dassets.rb +59 -12
  3. data/lib/dassets/asset_file.rb +15 -14
  4. data/lib/dassets/cmds/cache_cmd.rb +33 -0
  5. data/lib/dassets/cmds/digest_cmd.rb +53 -0
  6. data/lib/dassets/{digests_file.rb → digests.rb} +14 -23
  7. data/lib/dassets/engine.rb +33 -0
  8. data/lib/dassets/runner.rb +10 -4
  9. data/lib/dassets/server/response.rb +1 -1
  10. data/lib/dassets/source_file.rb +71 -0
  11. data/lib/dassets/version.rb +1 -1
  12. data/test/support/app/assets/.digests +1 -0
  13. data/test/support/app/assets/file1.txt +1 -0
  14. data/test/support/app/assets/file2.txt +1 -0
  15. data/test/support/app/assets/grumpy_cat.jpg +0 -0
  16. data/test/support/app/assets/nested/a-thing.txt.useless.dumb +1 -0
  17. data/test/support/app/assets/nested/file3.txt +0 -0
  18. data/test/support/app/assets/public/nested/a-thing.txt.no-use +4 -0
  19. data/test/support/config/assets.rb +12 -1
  20. data/test/support/example.digests +3 -3
  21. data/test/support/public/nested/a-thing.txt-7413d18f2eba9c695a880aff67fde135.no-use +4 -0
  22. data/test/support/source_files/_ignored.txt +0 -0
  23. data/test/support/source_files/nested/_nested_ignored.txt +0 -0
  24. data/test/support/source_files/nested/test2.txt +0 -0
  25. data/test/support/source_files/test1.txt +0 -0
  26. data/test/system/cache_cmd_run_tests.rb +27 -0
  27. data/test/system/digest_cmd_run_tests.rb +70 -0
  28. data/test/unit/asset_file_tests.rb +11 -11
  29. data/test/unit/cmds/cache_cmd_tests.rb +33 -0
  30. data/test/unit/cmds/digest_cmd_tests.rb +23 -0
  31. data/test/unit/config_tests.rb +52 -7
  32. data/test/unit/dassets_tests.rb +59 -5
  33. data/test/unit/digests_tests.rb +79 -0
  34. data/test/unit/engine_tests.rb +59 -0
  35. data/test/unit/server/response_tests.rb +5 -5
  36. data/test/unit/source_file_tests.rb +82 -0
  37. metadata +45 -13
  38. data/lib/dassets/runner/cache_command.rb +0 -46
  39. data/lib/dassets/runner/digest_command.rb +0 -65
  40. data/test/unit/digests_file_tests.rb +0 -90
  41. data/test/unit/runner/cache_command_tests.rb +0 -62
  42. data/test/unit/runner/digest_command_tests.rb +0 -83
@@ -0,0 +1,59 @@
1
+ require 'assert'
2
+ require 'dassets/engine'
3
+
4
+ class Dassets::Engine
5
+
6
+ class BaseTests < Assert::Context
7
+ desc "the base Dassets::Engine"
8
+ setup do
9
+ @engine = Dassets::Engine.new
10
+ end
11
+ subject{ @engine }
12
+
13
+ should have_reader :opts
14
+ should have_imeths :ext, :compile
15
+
16
+ should "default the opts if none given" do
17
+ exp_opts = {}
18
+ assert_equal exp_opts, subject.opts
19
+ end
20
+
21
+ should "raise NotImplementedError on `ext` and `compile`" do
22
+ assert_raises NotImplementedError do
23
+ subject.ext('foo')
24
+ end
25
+
26
+ assert_raises NotImplementedError do
27
+ subject.compile('some content')
28
+ end
29
+ end
30
+
31
+ end
32
+
33
+ class NullEngineTests < Assert::Context
34
+ desc "the NullEngine"
35
+ setup do
36
+ @engine = Dassets::NullEngine.new('some' => 'opts')
37
+ end
38
+ subject{ @engine }
39
+
40
+ should "be a Engine" do
41
+ assert_kind_of Dassets::Engine, subject
42
+ end
43
+
44
+ should "know its opts" do
45
+ exp_opts = {'some' => 'opts'}
46
+ assert_equal exp_opts, subject.opts
47
+ end
48
+
49
+ should "return the given extension on `ext`" do
50
+ assert_equal 'foo', subject.ext('foo')
51
+ end
52
+
53
+ should "return the given input on `compile" do
54
+ assert_equal 'some content', subject.compile('some content')
55
+ end
56
+
57
+ end
58
+
59
+ end
@@ -16,7 +16,7 @@ class Dassets::Server::Response
16
16
 
17
17
  should "handle not modified files" do
18
18
  af = Dassets['file1.txt']
19
- mtime = File.mtime(af.files_path).httpdate.to_s
19
+ mtime = File.mtime(af.output_path).httpdate.to_s
20
20
  resp = file_response(af, 'HTTP_IF_MODIFIED_SINCE' => mtime)
21
21
 
22
22
  assert_equal 304, resp.status
@@ -30,8 +30,8 @@ class Dassets::Server::Response
30
30
  resp = file_response(af)
31
31
  exp_headers = {
32
32
  'Content-Type' => 'text/plain',
33
- 'Content-Length' => File.size?(af.files_path).to_s,
34
- 'Last-Modified' => File.mtime(af.files_path).httpdate.to_s
33
+ 'Content-Length' => File.size?(af.output_path).to_s,
34
+ 'Last-Modified' => File.mtime(af.output_path).httpdate.to_s
35
35
  }
36
36
 
37
37
  assert_equal 200, resp.status
@@ -53,9 +53,9 @@ class Dassets::Server::Response
53
53
  resp = file_response(af)
54
54
 
55
55
  assert_equal 404, resp.status
56
- assert_equal [], resp.body
56
+ assert_equal ['Not Found'], resp.body
57
57
  assert_equal Rack::Utils::HeaderHash.new, resp.headers
58
- assert_equal [ 404, {}, [] ], resp.to_rack
58
+ assert_equal [ 404, {}, ['Not Found'] ], resp.to_rack
59
59
  end
60
60
 
61
61
  protected
@@ -0,0 +1,82 @@
1
+ require 'assert'
2
+ require 'dassets/engine'
3
+ require 'dassets/source_file'
4
+
5
+ class Dassets::SourceFile
6
+
7
+ class BaseTests < Assert::Context
8
+ desc "Dassets::SourceFile"
9
+ setup do
10
+ @file_path = File.join(Dassets.config.source_path, 'file1.txt')
11
+ @source_file = Dassets::SourceFile.new(@file_path)
12
+ end
13
+ subject{ @source_file }
14
+
15
+ should have_readers :file_path
16
+ should have_imeths :exists?, :digest_path, :compiled, :fingerprint
17
+
18
+ should "know its file path" do
19
+ assert_equal @file_path, subject.file_path
20
+ end
21
+
22
+ should "know if it exists" do
23
+ assert subject.exists?
24
+ end
25
+
26
+ should "know its digest path" do
27
+ assert_equal 'file1.txt', subject.digest_path
28
+ end
29
+
30
+ should "know its compiled content fingerprint" do
31
+ assert_equal 'daa05c683a4913b268653f7a7e36a5b4', subject.fingerprint
32
+ end
33
+
34
+ end
35
+
36
+ class EngineTests < BaseTests
37
+ desc "compiled against engines"
38
+ setup do
39
+ @file_path = File.join(Dassets.config.source_path, 'nested/a-thing.txt.useless.dumb')
40
+ @source_file = Dassets::SourceFile.new(@file_path)
41
+ end
42
+
43
+ should "build the digest path appropriately" do
44
+ assert_equal 'nested/a-thing.txt.no-use', subject.digest_path
45
+ end
46
+
47
+ should "compile the source content appropriately" do
48
+ file_content = File.read(@file_path)
49
+ exp_compiled_content = [ file_content, 'DUMB', 'USELESS' ].join("\n")
50
+ assert_equal exp_compiled_content, subject.compiled
51
+ end
52
+
53
+ end
54
+
55
+ class DigestTests < EngineTests
56
+ desc "being digested"
57
+ setup do
58
+ Dassets.init
59
+ @digested_asset_file = @source_file.digest
60
+ end
61
+ teardown do
62
+ Dassets.reset
63
+ end
64
+
65
+ should "return the digested asset file" do
66
+ assert_not_nil @digested_asset_file
67
+ assert_kind_of Dassets::AssetFile, @digested_asset_file
68
+ end
69
+
70
+ should "compile and write an asset file to the output path" do
71
+ assert_file_exists @digested_asset_file.output_path
72
+ assert_equal subject.compiled, File.read(@digested_asset_file.output_path)
73
+ end
74
+
75
+ should "add a digests entry for the asset file with its fingerprint" do
76
+ digests_on_disk = Dassets::Digests.new(Dassets.config.digests_path)
77
+ assert_equal subject.fingerprint, digests_on_disk[subject.digest_path]
78
+ end
79
+
80
+ end
81
+
82
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dassets
3
3
  version: !ruby/object:Gem::Version
4
- hash: 23
4
+ hash: 19
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 2
8
+ - 3
9
9
  - 0
10
- version: 0.2.0
10
+ version: 0.3.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Kelly Redding
@@ -16,7 +16,7 @@ autorequire:
16
16
  bindir: bin
17
17
  cert_chain: []
18
18
 
19
- date: 2013-04-18 00:00:00 Z
19
+ date: 2013-04-25 00:00:00 Z
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
22
22
  name: assert
@@ -114,22 +114,30 @@ files:
114
114
  - lib/dassets.rb
115
115
  - lib/dassets/asset_file.rb
116
116
  - lib/dassets/cli.rb
117
- - lib/dassets/digests_file.rb
117
+ - lib/dassets/cmds/cache_cmd.rb
118
+ - lib/dassets/cmds/digest_cmd.rb
119
+ - lib/dassets/digests.rb
120
+ - lib/dassets/engine.rb
118
121
  - lib/dassets/root_path.rb
119
122
  - lib/dassets/runner.rb
120
- - lib/dassets/runner/cache_command.rb
121
- - lib/dassets/runner/digest_command.rb
122
123
  - lib/dassets/server.rb
123
124
  - lib/dassets/server/request.rb
124
125
  - lib/dassets/server/response.rb
126
+ - lib/dassets/source_file.rb
125
127
  - lib/dassets/version.rb
126
128
  - log/.gitkeep
127
129
  - test/helper.rb
128
130
  - test/support/app.rb
129
131
  - test/support/app/assets/.digests
132
+ - test/support/app/assets/file1.txt
133
+ - test/support/app/assets/file2.txt
134
+ - test/support/app/assets/grumpy_cat.jpg
135
+ - test/support/app/assets/nested/a-thing.txt.useless.dumb
136
+ - test/support/app/assets/nested/file3.txt
130
137
  - test/support/app/assets/public/file1.txt
131
138
  - test/support/app/assets/public/file2.txt
132
139
  - test/support/app/assets/public/grumpy_cat.jpg
140
+ - test/support/app/assets/public/nested/a-thing.txt.no-use
133
141
  - test/support/app/assets/public/nested/file3.txt
134
142
  - test/support/app_public/.gitkeep
135
143
  - test/support/config/assets.rb
@@ -137,18 +145,27 @@ files:
137
145
  - test/support/public/file1-daa05c683a4913b268653f7a7e36a5b4.txt
138
146
  - test/support/public/file2-9bbe1047cffbb590f59e0e5aeff46ae4.txt
139
147
  - test/support/public/grumpy_cat-b0d1f399a916f7a25c4c0f693c619013.jpg
148
+ - test/support/public/nested/a-thing.txt-7413d18f2eba9c695a880aff67fde135.no-use
140
149
  - test/support/public/nested/file3-d41d8cd98f00b204e9800998ecf8427e.txt
150
+ - test/support/source_files/_ignored.txt
151
+ - test/support/source_files/nested/_nested_ignored.txt
152
+ - test/support/source_files/nested/test2.txt
153
+ - test/support/source_files/test1.txt
154
+ - test/system/cache_cmd_run_tests.rb
155
+ - test/system/digest_cmd_run_tests.rb
141
156
  - test/system/rack_tests.rb
142
157
  - test/unit/asset_file_tests.rb
158
+ - test/unit/cmds/cache_cmd_tests.rb
159
+ - test/unit/cmds/digest_cmd_tests.rb
143
160
  - test/unit/config_tests.rb
144
161
  - test/unit/dassets_tests.rb
145
- - test/unit/digests_file_tests.rb
146
- - test/unit/runner/cache_command_tests.rb
147
- - test/unit/runner/digest_command_tests.rb
162
+ - test/unit/digests_tests.rb
163
+ - test/unit/engine_tests.rb
148
164
  - test/unit/runner_tests.rb
149
165
  - test/unit/server/request_tests.rb
150
166
  - test/unit/server/response_tests.rb
151
167
  - test/unit/server_tests.rb
168
+ - test/unit/source_file_tests.rb
152
169
  - tmp/.gitkeep
153
170
  homepage: http://github.com/redding/dassets
154
171
  licenses: []
@@ -187,9 +204,15 @@ test_files:
187
204
  - test/helper.rb
188
205
  - test/support/app.rb
189
206
  - test/support/app/assets/.digests
207
+ - test/support/app/assets/file1.txt
208
+ - test/support/app/assets/file2.txt
209
+ - test/support/app/assets/grumpy_cat.jpg
210
+ - test/support/app/assets/nested/a-thing.txt.useless.dumb
211
+ - test/support/app/assets/nested/file3.txt
190
212
  - test/support/app/assets/public/file1.txt
191
213
  - test/support/app/assets/public/file2.txt
192
214
  - test/support/app/assets/public/grumpy_cat.jpg
215
+ - test/support/app/assets/public/nested/a-thing.txt.no-use
193
216
  - test/support/app/assets/public/nested/file3.txt
194
217
  - test/support/app_public/.gitkeep
195
218
  - test/support/config/assets.rb
@@ -197,15 +220,24 @@ test_files:
197
220
  - test/support/public/file1-daa05c683a4913b268653f7a7e36a5b4.txt
198
221
  - test/support/public/file2-9bbe1047cffbb590f59e0e5aeff46ae4.txt
199
222
  - test/support/public/grumpy_cat-b0d1f399a916f7a25c4c0f693c619013.jpg
223
+ - test/support/public/nested/a-thing.txt-7413d18f2eba9c695a880aff67fde135.no-use
200
224
  - test/support/public/nested/file3-d41d8cd98f00b204e9800998ecf8427e.txt
225
+ - test/support/source_files/_ignored.txt
226
+ - test/support/source_files/nested/_nested_ignored.txt
227
+ - test/support/source_files/nested/test2.txt
228
+ - test/support/source_files/test1.txt
229
+ - test/system/cache_cmd_run_tests.rb
230
+ - test/system/digest_cmd_run_tests.rb
201
231
  - test/system/rack_tests.rb
202
232
  - test/unit/asset_file_tests.rb
233
+ - test/unit/cmds/cache_cmd_tests.rb
234
+ - test/unit/cmds/digest_cmd_tests.rb
203
235
  - test/unit/config_tests.rb
204
236
  - test/unit/dassets_tests.rb
205
- - test/unit/digests_file_tests.rb
206
- - test/unit/runner/cache_command_tests.rb
207
- - test/unit/runner/digest_command_tests.rb
237
+ - test/unit/digests_tests.rb
238
+ - test/unit/engine_tests.rb
208
239
  - test/unit/runner_tests.rb
209
240
  - test/unit/server/request_tests.rb
210
241
  - test/unit/server/response_tests.rb
211
242
  - test/unit/server_tests.rb
243
+ - test/unit/source_file_tests.rb
@@ -1,46 +0,0 @@
1
- require 'pathname'
2
- require 'fileutils'
3
- require 'dassets/asset_file'
4
- require 'dassets/digests_file'
5
-
6
- module Dassets; end
7
- class Dassets::Runner; end
8
- class Dassets::Runner::CacheCommand
9
-
10
- attr_reader :files_root_path, :cache_root_path, :digests_file, :asset_files
11
-
12
- def initialize(cache_root_path)
13
- unless cache_root_path && File.directory?(cache_root_path)
14
- raise Dassets::Runner::CmdError, "specify an existing cache directory"
15
- end
16
-
17
- @files_root_path = Pathname.new(Dassets.config.files_path)
18
- @cache_root_path = Pathname.new(cache_root_path)
19
- @digests_file = Dassets::DigestsFile.new(Dassets.config.digests_file_path)
20
- @asset_files = @digests_file.asset_files
21
- end
22
-
23
- def run(write_files=true)
24
- begin
25
- @asset_files.each do |file|
26
- files_path = @files_root_path.join(file.path).to_s
27
- cache_path = @cache_root_path.join(file.cache_path).to_s
28
-
29
- if write_files
30
- FileUtils.mkdir_p File.dirname(cache_path)
31
- FileUtils.cp(files_path, cache_path)
32
- end
33
- end
34
- unless ENV['DASSETS_TEST_MODE']
35
- $stdout.puts "#{@asset_files.size} files written to #{@cache_root_path}"
36
- end
37
- return write_files
38
- rescue Exception => e
39
- unless ENV['DASSETS_TEST_MODE']
40
- $stderr.puts e, *e.backtrace; $stderr.puts ""
41
- end
42
- raise Dassets::Runner::CmdFail
43
- end
44
- end
45
-
46
- end
@@ -1,65 +0,0 @@
1
- require 'set'
2
- require 'dassets/asset_file'
3
- require 'dassets/digests_file'
4
-
5
- module Dassets; end
6
- class Dassets::Runner; end
7
- class Dassets::Runner::DigestCommand
8
-
9
- attr_reader :asset_files, :digests_file
10
-
11
- def initialize(file_paths)
12
- @pwd = ENV['PWD']
13
- @asset_files = if (file_paths || []).empty?
14
- get_asset_files([*Dassets.config.files_path])
15
- else
16
- get_asset_files(file_paths)
17
- end
18
- @digests_file = Dassets::DigestsFile.new(Dassets.config.digests_file_path)
19
- end
20
-
21
- def run(save=true)
22
- begin
23
- digest_paths = @digests_file.keys
24
- asset_paths = @asset_files.map{ |f| f.path }
25
-
26
- (digest_paths - asset_paths).each{ |file| @digests_file.delete(file) }
27
- @asset_files.each{ |f| @digests_file[f.path] = f.md5 }
28
-
29
- @digests_file.save! if save
30
- unless ENV['DASSETS_TEST_MODE']
31
- $stdout.puts "digested #{@asset_files.size} assets, saved to #{@digests_file.path}"
32
- end
33
- return save
34
- rescue Exception => e
35
- unless ENV['DASSETS_TEST_MODE']
36
- $stderr.puts e, *e.backtrace; $stderr.puts ""
37
- end
38
- raise Dassets::Runner::CmdFail
39
- end
40
- end
41
-
42
- private
43
-
44
- # Get all file paths fuzzy-matching the given paths. Each path must be a
45
- # file that exists and is in the `config.files_path` tree. Return them
46
- # as sorted AssetFile objects.
47
- def get_asset_files(paths)
48
- fuzzy_paths(paths).
49
- select{ |p| is_asset_file?(p) }.
50
- sort.
51
- map{ |p| Dassets::AssetFile.from_abs_path(p) }
52
- end
53
-
54
- def fuzzy_paths(paths)
55
- paths.inject(Set.new) do |paths, path|
56
- p = File.expand_path(path, @pwd)
57
- paths += Dir.glob("#{p}*") + Dir.glob("#{p}*/**/*")
58
- end
59
- end
60
-
61
- def is_asset_file?(path)
62
- File.file?(path) && path.include?("#{Dassets.config.files_path}/")
63
- end
64
-
65
- end
@@ -1,90 +0,0 @@
1
- require 'assert'
2
- require 'fileutils'
3
- require 'dassets/digests_file'
4
- require 'dassets/asset_file'
5
-
6
- class Dassets::DigestsFile
7
-
8
- class BaseTests < Assert::Context
9
- desc "Dassets::DigestsFile"
10
- setup do
11
- @file_path = File.join(Dassets.config.root_path, 'example.digests')
12
- @digests = Dassets::DigestsFile.new(@file_path)
13
- end
14
- subject{ @digests }
15
-
16
- should have_reader :path
17
- should have_imeths :asset_files, :asset_file, :to_hash, :save!
18
- should have_imeths :[], :[]=, :delete, :each, :keys, :values, :empty?
19
-
20
- should "know its path" do
21
- assert_equal @file_path, subject.path
22
- end
23
-
24
- should "know its asset files" do
25
- assert_equal subject.keys.size, subject.asset_files.size
26
- assert_kind_of Dassets::AssetFile, subject.asset_files.first
27
- end
28
-
29
- should "get a specific asset file from its data" do
30
- file = subject.asset_file('/path/to/file1')
31
-
32
- assert_kind_of Dassets::AssetFile, file
33
- assert_equal '/path/to/file1', file.path
34
- assert_equal subject['/path/to/file1'], file.md5
35
- end
36
-
37
- should "know whether it is empty or not" do
38
- assert_not_empty subject
39
- end
40
-
41
- should "read values with the index operator" do
42
- assert_equal 'abc123', subject['/path/to/file1']
43
- end
44
-
45
- should "write values with the index operator" do
46
- subject['/path/to/test'] = 'testytest'
47
- assert_equal 'testytest', subject['/path/to/test']
48
- end
49
-
50
- should "remove values with the delete method" do
51
- assert_includes '/path/to/file1', subject.keys
52
-
53
- subject.delete '/path/to/file1'
54
- assert_not_includes '/path/to/file1', subject.keys
55
- end
56
-
57
- should "know its hash representation" do
58
- exp_hash = {
59
- "/path/to/file1" => "abc123",
60
- "/path/to/file2" => "123abc",
61
- "/path/to/file3" => "a1b2c3"
62
- }
63
- subject_to_hash = subject.to_hash
64
- subject_internal_hash = subject.instance_variable_get("@hash")
65
-
66
- assert_equal exp_hash, subject_to_hash
67
- assert_not_equal subject_internal_hash.object_id, subject_to_hash.object_id
68
- end
69
-
70
- end
71
-
72
- class SaveTests < BaseTests
73
- desc "on save"
74
- setup do
75
- FileUtils.mv(@file_path, "#{@file_path}.bak")
76
- end
77
- teardown do
78
- FileUtils.mv("#{@file_path}.bak", @file_path)
79
- end
80
-
81
- should "write out the digests to the path" do
82
- assert_not_file_exists subject.path
83
- subject.save!
84
-
85
- assert_file_exists subject.path
86
- end
87
-
88
- end
89
-
90
- end