Skip to content

Commit

Permalink
WIP - Rewrite tests with RSpec (#218)
Browse files Browse the repository at this point in the history
* 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
ahangarha authored Jan 10, 2023
1 parent f1da0c9 commit 6c3ef9c
Show file tree
Hide file tree
Showing 114 changed files with 2,157 additions and 1,888 deletions.
6 changes: 3 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/.bundle
Gemfile.lock
/pkg
/test/mounted_app/test/dummy/log
/test/test_app/log
/spec/mounted_app/test/dummy/log
/spec/test_app/log
node_modules
.byebug_history
/test/test_app/tmp
/spec/test_app/tmp
yarn-debug.log*
yarn-error.log*
.yarn-integrity
Expand Down
1 change: 1 addition & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--require spec_helper
4 changes: 3 additions & 1 deletion Gemfile.development_dependencies
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ gem "rack-proxy", ">= 0.7.2"
gem "nokogiri", ">= 1.13.6"
gem "rails-html-sanitizer", ">= 1.4.3"

group :test, :development do
gem "rspec-rails", "~> 5.0.0"
end

group :test do
gem "minitest", "~> 5.0"
gem "byebug"
end
9 changes: 4 additions & 5 deletions Rakefile
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
2 changes: 1 addition & 1 deletion gemfiles/Gemfile-rails-edge
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ gem "rails", github: "rails/rails", branch: "main"
gem "arel", github: "rails/arel"
gem "rake", ">= 11.1"
gem "rack-proxy", require: false
gem "minitest", "~> 5.0"
gem "rspec-rails", "~> 6.0.0"
gem "byebug"
2 changes: 1 addition & 1 deletion gemfiles/Gemfile-rails.5.2.x
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ gemspec path: "../"
gem "rails", "~> 5.2.0"
gem "rake", ">= 11.1"
gem "rack-proxy", require: false
gem "minitest", "~> 5.0"
gem "rspec-rails", "~> 5.0.0"
gem "byebug"
2 changes: 1 addition & 1 deletion gemfiles/Gemfile-rails.6.0.x
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ gemspec path: "../"
gem "rails", "~> 6.0.0.rc2"
gem "rake", ">= 11.1"
gem "rack-proxy", require: false
gem "minitest", "~> 5.0"
gem "rspec-rails", "~> 5.0.0"
gem "byebug"
2 changes: 1 addition & 1 deletion gemfiles/Gemfile-rails.6.1.x
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ gem "rails", '~>6.1.0'
gem "arel", github: "rails/arel"
gem "rake", ">= 11.1"
gem "rack-proxy", require: false
gem "minitest", "~> 5.0"
gem "rspec-rails", "~> 6.0.0"
gem "byebug"
2 changes: 1 addition & 1 deletion gemfiles/Gemfile-rails.7.0.x
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ gem "rails", '~>7.0.0'
gem "arel", github: "rails/arel"
gem "rake", ">= 11.1"
gem "rack-proxy", require: false
gem "minitest", "~> 5.0"
gem "rspec-rails", "~> 6.0.0"
gem "byebug"
2 changes: 1 addition & 1 deletion package/utils/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const isArray = (value) => Array.isArray(value)
const isBoolean = (str) => /^true/.test(str) || /^false/.test(str)
const chdirTestApp = () => {
try {
return process.chdir('test/test_app')
return process.chdir('spec/test_app')
} catch (e) {
return null
}
Expand Down
114 changes: 114 additions & 0 deletions spec/command_spec.rb
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
57 changes: 57 additions & 0 deletions spec/compiler_spec.rb
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
20 changes: 20 additions & 0 deletions spec/compiler_strategy_spec.rb
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
Loading

0 comments on commit 6c3ef9c

Please sign in to comment.