-
-
Notifications
You must be signed in to change notification settings - Fork 631
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve EnsureAssetsCompiled logic to wait for webpack process to finish
Because there may be situations where a user is running a webpack process in watch mode in the background that will recompile the assets, it is possible that if the build time is extremely long, the user could start tests before the build completes. In this case, EnsureAssetsCompiled would start compiling the assets again even though there is already a webpack process doing that. This commit adds behavior to EnsureAssetsCompiled to check whether or not the webpack process is running in the background, and will defer the responsibility of compilation to that process, re-checking the compiled assets every second until they are up to date.
- Loading branch information
Showing
6 changed files
with
115 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,17 @@ | ||
require "english" | ||
|
||
module ReactOnRails | ||
module Utils | ||
def self.object_to_boolean(value) | ||
[true, "true", "yes", 1, "1", "t"].include?(value.class == String ? value.downcase : value) | ||
end | ||
|
||
def self.server_rendering_is_enabled? | ||
ReactOnRails.configuration.server_bundle_js_file.present? | ||
end | ||
|
||
def self.last_process_completed_successfully? | ||
$CHILD_STATUS.exitstatus == 0 | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
client-static-assets: sh -c 'rm app/assets/javascripts/generated/* || true && cd client && npm run build:dev:client' | ||
server-static-assets: sh -c 'cd client && npm run build:dev:server' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,61 @@ | ||
require_relative "simplecov_helper" | ||
require_relative "spec_helper" | ||
|
||
class WebpackAssetsCompilerDouble | ||
attr_reader :times_ran | ||
|
||
def initialize | ||
@times_ran = 0 | ||
end | ||
|
||
def compile | ||
@times_ran += 1 | ||
end | ||
end | ||
|
||
module ReactOnRails | ||
describe EnsureAssetsCompiled do | ||
let(:compiler) { WebpackAssetsCompilerDouble.new } | ||
let(:ensurer) { EnsureAssetsCompiled.new(checker, compiler) } | ||
let(:compiler) { double_assets_compiler } | ||
let(:ensurer) { EnsureAssetsCompiled.new(assets_checker, compiler, process_checker) } | ||
|
||
context "when assets are not up to date" do | ||
let(:checker) { double_webpack_assets_checker(up_to_date: false) } | ||
let(:assets_checker) { double_assets_checker(up_to_date: false) } | ||
|
||
context "and webpack process is running" do | ||
let(:process_checker) { double_process_checker(running: true) } | ||
|
||
it "sleeps until assets are up to date" do | ||
expect(compiler).not_to receive(:compile) | ||
|
||
thread = Thread.new { ensurer.call } | ||
|
||
it "compiles the webpack bundles" do | ||
expect { ensurer.call }.to change { compiler.times_ran }.from(0).to(1) | ||
sleep 1 | ||
allow(assets_checker).to receive(:up_to_date?).and_return(true) | ||
|
||
thread.join | ||
|
||
expect(ensurer.assets_have_been_compiled).to eq(true) | ||
end | ||
end | ||
|
||
context "and webpack process is NOT running" do | ||
let(:process_checker) { double_process_checker(running: false) } | ||
|
||
it "compiles the webpack assets" do | ||
expect(compiler).to receive(:compile).once | ||
ensurer.call | ||
end | ||
end | ||
end | ||
|
||
context "when assets are up to date" do | ||
let(:checker) { double_webpack_assets_checker(up_to_date: true) } | ||
let(:assets_checker) { double_assets_checker(up_to_date: true) } | ||
let(:process_checker) { double_process_checker(running: false) } | ||
|
||
it "does not compile the webpack bundles if they exist and are up to date" do | ||
expect { ensurer.call }.not_to change { compiler.times_ran } | ||
it "does nothing" do | ||
expect(compiler).not_to receive(:compile) | ||
ensurer.call | ||
end | ||
end | ||
|
||
def double_webpack_assets_checker(args = {}) | ||
def double_process_checker(args = {}) | ||
instance_double(WebpackProcessChecker, running?: args.fetch(:running)) | ||
end | ||
|
||
def double_assets_checker(args = {}) | ||
instance_double(WebpackAssetsStatusChecker, up_to_date?: args.fetch(:up_to_date)) | ||
end | ||
|
||
def double_assets_compiler | ||
instance_double(WebpackAssetsCompiler, :compile) | ||
end | ||
end | ||
end |