File: test_task.rb

package info (click to toggle)
ruby-sprockets-rails 3.4.1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 436 kB
  • sloc: ruby: 2,071; makefile: 4; javascript: 4
file content (142 lines) | stat: -rw-r--r-- 3,765 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
require 'minitest/autorun'
require 'tmpdir'

require 'sprockets'
require 'sprockets/rails/task'

Minitest::Test = MiniTest::Unit::TestCase unless defined?(Minitest::Test)

class TestTask < Minitest::Test
  FIXTURES_PATH = File.expand_path("../fixtures", __FILE__)

  def setup
    @rake = Rake::Application.new
    Rake.application = @rake

    @assets = Sprockets::Environment.new
    @assets.append_path FIXTURES_PATH

    @dir = File.join(Dir::tmpdir, 'rails', 'task')

    @manifest_file = File.join(Dir::tmpdir, 'rails', 'manifest', 'custom-manifest.json')
    FileUtils.mkdir_p File.dirname(@manifest_file)
    @manifest = Sprockets::Manifest.new(@assets, @dir, @manifest_file)

    Sprockets::Rails::Task.new do |t|
      t.environment = @assets
      t.manifest    = @manifest
      t.assets      = ['foo.js', 'foo-modified.js']
      t.log_level   = :fatal
    end

    @environment_ran = false
    # Stub Rails environment task
    @rake.define_task Rake::Task, :environment do
      @environment_ran = true
    end
  end

  def teardown
    Rake.application = nil

    FileUtils.rm_rf(@dir)
    assert Dir["#{@dir}/*"].empty?

    manifest_dir = File.dirname(@manifest_file)
    FileUtils.rm_rf(manifest_dir)
    assert Dir["#{manifest_dir}/*"].empty?
  end

  def test_precompile
    assert !@environment_ran

    digest_path = @assets['foo.js'].digest_path
    assert !File.exist?("#{@dir}/#{digest_path}")

    @rake['assets:precompile'].invoke

    assert @environment_ran
    assert File.exist?(@manifest_file)
    assert File.exist?("#{@dir}/#{digest_path}")
  end

  def test_precompile_without_manifest
    Sprockets::Rails::Task.new do |t|
      t.environment = @assets
      t.manifest    = Sprockets::Manifest.new(@assets, @dir, nil)
      t.assets      = ['foo.js', 'foo-modified.js']
      t.log_level   = :fatal
    end

    assert !@environment_ran

    digest_path = @assets['foo.js'].digest_path
    assert !File.exist?("#{@dir}/#{digest_path}")

    @rake['assets:precompile'].invoke

    assert @environment_ran
    assert Dir["#{@dir}/.sprockets-manifest-*.json"].first
    assert File.exist?("#{@dir}/#{digest_path}")
  end

  def test_clobber
    assert !@environment_ran
    digest_path = @assets['foo.js'].digest_path

    @rake['assets:precompile'].invoke
    assert File.exist?("#{@dir}/#{digest_path}")

    assert @environment_ran

    @rake['assets:clobber'].invoke
    assert !File.exist?("#{@dir}/#{digest_path}")
  end

  def test_clean
    assert !@environment_ran
    digest_path = @assets['foo.js'].digest_path

    @rake['assets:precompile'].invoke
    assert File.exist?("#{@dir}/#{digest_path}")

    assert @environment_ran

    @rake['assets:clean'].invoke
    assert File.exist?("#{@dir}/#{digest_path}")
  end

  def test_clean_with_keep_specified
    assert !@environment_ran
    path     = Pathname.new(@assets['foo.js'].filename)
    new_path = path.join("../foo-modified.js")

    FileUtils.cp(path, new_path)

    assert File.exist?(new_path)
    digest_path = @assets['foo-modified.js'].digest_path

    @rake['assets:precompile'].invoke
    assert File.exist?("#{@dir}/#{digest_path}")
    assert @environment_ran

    # clean environment
    setup

    # modify file
    File.open(new_path, "a") {|f| f.write("var Bar;") }
    @rake['assets:precompile'].invoke
    old_digest_path = digest_path
    digest_path     = @assets['foo-modified.js'].digest_path

    refute_equal old_digest_path, digest_path
    assert File.exist?("#{@dir}/#{old_digest_path}")
    assert File.exist?("#{@dir}/#{digest_path}")

    @rake['assets:clean'].invoke(0)
    assert File.exist?("#{@dir}/#{digest_path}")
    # refute File.exist?("#{@dir}/#{old_digest_path}")
  ensure
    FileUtils.rm(new_path) if new_path
  end
end