-
-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP - Rewrite tests with RSpec (#218)
* Rename test directory to spec * Install rspec gem * Rename test files to spec files * Rewrite Command specs * Rewrite Compiler specs * Rewrite CompilerStrategy specs * Rewrite Configuration specs * Rewrite DevServerRunner specs * Rewrite DevServer specs * Rewrite DigestStrategy specs * Rewrite EngineRakeTasks specs * Rewrite Env specs * Rewrite Webpacker::Helper specs * Rewrite Webpacker::Manifest specs * Rewrite Webpacker::MtimeStrategy specs * Rewrite RakeTasks specs * Rewrite VersionChecker specs * Rewrite WebpackRunner specs * Rewrite Webpacker specs * Use mock for dev_server * Refactor Configuration specs * Improve doubles * Improve doubles * Improve spec title * Improve code organization Improve tests * Use verifying double * Reorganize the code * Remove redundant spec This spec is already tested more robustly in the following dedicated files: - env_spec.erb - configuration_spec.rb * Use each instead of example in before hook
- Loading branch information
Showing
114 changed files
with
2,157 additions
and
1,888 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 |
---|---|---|
@@ -0,0 +1 @@ | ||
--require spec_helper |
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,11 +1,10 @@ | ||
# frozen_string_literal: true | ||
require "bundler/gem_tasks" | ||
require "rake/testtask" | ||
|
||
Rake::TestTask.new do |t| | ||
t.libs << "test" | ||
t.test_files = FileList["test/**/*_test.rb"] | ||
t.verbose = true | ||
begin | ||
require "rspec/core/rake_task" | ||
RSpec::Core::RakeTask.new(:test) | ||
rescue LoadError | ||
end | ||
|
||
task default: :test |
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
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
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,114 @@ | ||
describe "Command" do | ||
before do | ||
allow(Webpacker.logger).to receive(:info) | ||
end | ||
|
||
describe "#compile" do | ||
it "returns success status when stale" do | ||
expect(Webpacker.compiler).to receive(:stale?).and_return(true) | ||
expect(Webpacker.compiler).to receive(:run_webpack).and_return(true) | ||
|
||
expect(Webpacker.commands.compile).to be true | ||
end | ||
|
||
it "returns success status when fresh" do | ||
expect(Webpacker.compiler).to receive(:stale?).and_return(false) | ||
|
||
expect(Webpacker.commands.compile).to be true | ||
end | ||
|
||
it "returns failure status when stale" do | ||
expect(Webpacker.compiler).to receive(:stale?).and_return(true) | ||
expect(Webpacker.compiler).to receive(:run_webpack).and_return(false) | ||
|
||
expect(Webpacker.commands.compile).to be false | ||
end | ||
end | ||
|
||
describe "#clean" do | ||
let(:now) { Time.parse("2021-01-01 12:34:56 UTC") } | ||
let(:prev_files) do | ||
# Test assets to be kept and deleted, path and mtime | ||
{ | ||
# recent versions to be kept with Webpacker.commands.clean(count = 2) | ||
"js/application-deadbeef.js" => now - 4000, | ||
"js/common-deadbeee.js" => now - 4002, | ||
"css/common-deadbeed.css" => now - 4004, | ||
"media/images/logo-deadbeeb.css" => now - 4006, | ||
"js/application-1eadbeef.js" => now - 8000, | ||
"js/common-1eadbeee.js" => now - 8002, | ||
"css/common-1eadbeed.css" => now - 8004, | ||
"media/images/logo-1eadbeeb.css" => now - 8006, | ||
# new files to be kept with Webpacker.commands.clean(age = 3600) | ||
"js/brandnew-0001.js" => now, | ||
"js/brandnew-0002.js" => now - 10, | ||
"js/brandnew-0003.js" => now - 20, | ||
"js/brandnew-0004.js" => now - 40, | ||
}.transform_keys { |path| "#{Webpacker.config.public_output_path}/#{path}" } | ||
end | ||
|
||
let(:expired_files) do | ||
{ | ||
# old files that are outside count = 2 or age = 3600 and to be deleted | ||
"js/application-0eadbeef.js" => now - 9000, | ||
"js/common-0eadbeee.js" => now - 9002, | ||
"css/common-0eadbeed.css" => now - 9004, | ||
"js/brandnew-0005.js" => now - 3640, | ||
}.transform_keys { |path| "#{Webpacker.config.public_output_path}/#{path}" } | ||
end | ||
|
||
let(:all_files) { prev_files.merge(expired_files) } | ||
|
||
let(:file_delete_mock) { double("File Delete") } | ||
let(:file_mtime_stub) { Proc.new { |longpath| all_files[longpath] } } | ||
let(:file_delete_stub) { Proc.new { |longpath| file_delete_mock.delete(longpath) } } | ||
|
||
before :context do | ||
@dir_glob_stub = Proc.new { |arg| | ||
case arg | ||
when "#{Webpacker.config.public_output_path}/**/*" | ||
all_files.keys | ||
else | ||
[] | ||
end | ||
} | ||
end | ||
|
||
it "works with nested hashes and without any compiled files" do | ||
allow(File).to receive(:delete).and_return(true) | ||
expect(Webpacker.commands.clean).to be true | ||
end | ||
|
||
it "deletes only and only expired versioned files if no parameter passed" do | ||
all_files.keys.each do |longpath| | ||
allow(file_delete_mock).to receive(:delete).with(longpath) | ||
end | ||
|
||
with_time_dir_and_files_stub do | ||
expect(Webpacker.commands.clean).to be true | ||
|
||
# Verify that only and only expired files are deleted | ||
all_files.keys.each do |longpath| | ||
if expired_files.has_key? longpath | ||
expect(file_delete_mock).to have_received(:delete).with(longpath) | ||
else | ||
expect(file_delete_mock).to_not have_received(:delete).with(longpath) | ||
end | ||
end | ||
end | ||
end | ||
|
||
private | ||
|
||
def with_time_dir_and_files_stub(&proc) | ||
allow(Time).to receive(:now).and_return(now) | ||
allow(Dir).to receive(:glob) { |arg| @dir_glob_stub.call(arg) } | ||
allow(File).to receive(:directory?).and_return(false) | ||
allow(File).to receive(:file?).and_return(true) | ||
allow(File).to receive(:mtime) { |arg| file_mtime_stub.call(arg) } | ||
allow(File).to receive(:delete) { |arg| file_delete_stub.call(arg) } | ||
|
||
yield proc | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
describe "Webpacker::Compiler" do | ||
it "accepts custom environment variables" do | ||
expect(Webpacker.compiler.send(:webpack_env)["FOO"]).to be nil | ||
|
||
Webpacker.compiler.env["FOO"] = "BAR" | ||
expect(Webpacker.compiler.send(:webpack_env)["FOO"]).to eq "BAR" | ||
ensure | ||
Webpacker.compiler.env = {} | ||
end | ||
|
||
it "returns true when fresh" do | ||
mocked_strategy = double("Strategy") | ||
expect(mocked_strategy).to receive(:stale?).and_return(false) | ||
|
||
expect(Webpacker.compiler).to receive(:strategy).and_return(mocked_strategy) | ||
|
||
expect(Webpacker.compiler.compile).to be true | ||
end | ||
|
||
it "returns true and calls after_compile_hook on successful compile" do | ||
mocked_strategy = spy("Strategy") | ||
expect(mocked_strategy).to receive(:stale?).and_return(true) | ||
|
||
allow(Webpacker.compiler).to receive(:strategy).and_return(mocked_strategy) | ||
|
||
status = OpenStruct.new(success?: true) | ||
allow(Open3).to receive(:capture3).and_return([:sterr, :stdout, status]) | ||
|
||
expect(Webpacker.compiler.compile).to be true | ||
expect(mocked_strategy).to have_received(:after_compile_hook) | ||
end | ||
|
||
it "returns false and calls after_compile_hook on failed compile" do | ||
mocked_strategy = spy("Strategy") | ||
allow(mocked_strategy).to receive(:stale?).and_return(true) | ||
allow(mocked_strategy).to receive(:after_compile_hook) | ||
|
||
allow(Webpacker.compiler).to receive(:strategy).and_return(mocked_strategy) | ||
|
||
status = OpenStruct.new(success?: false) | ||
allow(Open3).to receive(:capture3).and_return([:sterr, :stdout, status]) | ||
|
||
expect(Webpacker.compiler.compile).to be false | ||
expect(mocked_strategy).to have_received(:after_compile_hook) | ||
end | ||
|
||
it "accepts external env variables" do | ||
expect(Webpacker.compiler.send(:webpack_env)["WEBPACKER_ASSET_HOST"]).to be nil | ||
expect(Webpacker.compiler.send(:webpack_env)["WEBPACKER_RELATIVE_URL_ROOT"]).to be nil | ||
|
||
ENV["WEBPACKER_ASSET_HOST"] = "foo.bar" | ||
ENV["WEBPACKER_RELATIVE_URL_ROOT"] = "/baz" | ||
|
||
expect(Webpacker.compiler.send(:webpack_env)["WEBPACKER_ASSET_HOST"]).to eq "foo.bar" | ||
expect(Webpacker.compiler.send(:webpack_env)["WEBPACKER_RELATIVE_URL_ROOT"]).to eq "/baz" | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
describe "Webpacker::CompilerStrategy" do | ||
describe "#from_config" do | ||
it "returns and instance of MtimeStrategy when compiler_strategy is set to mtime" do | ||
allow(Webpacker.config).to receive(:compiler_strategy).and_return("mtime") | ||
expect(Webpacker::CompilerStrategy.from_config).to be_an_instance_of(Webpacker::MtimeStrategy) | ||
end | ||
|
||
it "returns and instance of DigestStrategy when compiler_strategy is set to digest" do | ||
allow(Webpacker.config).to receive(:compiler_strategy).and_return("digest") | ||
expect(Webpacker::CompilerStrategy.from_config).to be_an_instance_of(Webpacker::DigestStrategy) | ||
end | ||
|
||
it "raise exception for unknown compiler_strategy in the config file" do | ||
expected_error_message = "Unknown strategy 'other'. Available options are 'mtime' and 'digest'." | ||
allow(Webpacker.config).to receive(:compiler_strategy).and_return("other") | ||
|
||
expect { Webpacker::CompilerStrategy.from_config }.to raise_error(expected_error_message) | ||
end | ||
end | ||
end |
Oops, something went wrong.