Skip to content

Commit

Permalink
Fix specs on 2.4 with truthy_presence
Browse files Browse the repository at this point in the history
* Created truthy_presence so that we use that for possible pathname
  objects.
  • Loading branch information
justin808 committed Jun 4, 2017
1 parent eee3762 commit c3e4314
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 51 deletions.
19 changes: 11 additions & 8 deletions lib/react_on_rails/assets_precompile.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

module ReactOnRails
class AssetsPrecompile
class SymlinkTargetDoesNotExistException < StandardError; end
class SymlinkTargetDoesNotExistException < StandardError;
end

# Used by the rake task
def default_asset_path
Expand All @@ -11,13 +12,15 @@ def default_asset_path
Pathname.new(dir)
end

# assets_path should be a Pathname object
def initialize(assets_path: nil,
symlink_non_digested_assets_regex: nil,
generated_assets_dir: nil)
@assets_path = assets_path.presence || default_asset_path
@symlink_non_digested_assets_regex = symlink_non_digested_assets_regex.presence ||
ReactOnRails.configuration.symlink_non_digested_assets_regex
@generated_assets_dir = generated_assets_dir.presence || ReactOnRails.configuration.generated_assets_dir
@assets_path = ReactOnRails::Utils.truthy_presence(assets_path) || default_asset_path
@symlink_non_digested_assets_regex =
ReactOnRails::Utils.truthy_presence(symlink_non_digested_assets_regex) ||
ReactOnRails.configuration.symlink_non_digested_assets_regex
@generated_assets_dir = ReactOnRails::Utils.truthy_presence(generated_assets_dir) || ReactOnRails.configuration.generated_assets_dir
end

# target and symlink are relative to the assets directory
Expand Down Expand Up @@ -61,8 +64,8 @@ def symlink_non_digested_assets
# file back to the original digested name, and make a similar symlink for the gz version.
return unless @symlink_non_digested_assets_regex
manifest_glob = Dir.glob(@assets_path.join(".sprockets-manifest-*.json")) +
Dir.glob(@assets_path.join("manifest-*.json")) +
Dir.glob(@assets_path.join("manifest.yml"))
Dir.glob(@assets_path.join("manifest-*.json")) +
Dir.glob(@assets_path.join("manifest.yml"))
if manifest_glob.empty?
puts "Warning: React On Rails: expected to find .sprockets-manifest-*.json, manifest-*.json "\
"or manifest.yml at #{@assets_path}, but found none. Canceling symlinking tasks."
Expand Down Expand Up @@ -100,7 +103,7 @@ def delete_broken_symlinks
target = File.readlink(filename)
rescue
puts "React on Rails: Warning: your platform doesn't support File::readlink method." /
"Skipping broken link check."
"Skipping broken link check."
break
end
path = Pathname.new(File.dirname(filename))
Expand Down
3 changes: 2 additions & 1 deletion lib/react_on_rails/locales_to_js.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ def locale_files
if i18n_yml_dir.present?
Dir["#{i18n_yml_dir}/**/*.yml"]
else
(Rails.application && Rails.application.config.i18n.load_path).presence
ReactOnRails::Utils.truthy_presence(
Rails.application && Rails.application.config.i18n.load_path).presence
end
end
end
Expand Down
15 changes: 13 additions & 2 deletions lib/react_on_rails/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,17 @@

module ReactOnRails
module Utils

# https://forum.shakacode.com/t/yak-of-the-week-ruby-2-4-pathname-empty-changed-to-look-at-file-size/901
# return object if truthy, else return nil
def self.truthy_presence(obj)
if obj.nil? || obj == false
nil
else
obj
end
end

def self.object_to_boolean(value)
[true, "true", "yes", 1, "1", "t"].include?(value.class == String ? value.downcase : value)
end
Expand All @@ -20,14 +31,14 @@ def self.invoke_and_exit_if_failed(cmd, failure_message)
stdout, stderr, status = Open3.capture3(cmd)
unless status.success?
msg = <<-MSG.strip_heredoc
#{'Z' * 80}
#{'Z' * 80}
React on Rails FATAL ERROR!
#{failure_message}
cmd: #{cmd}"
stdout: #{stdout.strip}
stderr: #{stderr.strip}
exitstatus: #{status.exitstatus}
#{'Z' * 80}
#{'Z' * 80}
MSG
puts Rainbow(msg).red
exit(1)
Expand Down
5 changes: 4 additions & 1 deletion spec/react_on_rails/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,11 @@
### Fix Net::ReadTimeout error on first test
# Show retry status in spec process
config.verbose_retry = true
# Try twice (retry once)

# ENV value RSPEC_RETRY_RETRY_COUNT should be set to 1 if you don't want to retry (defined in
# rspec/retry)
config.default_retry_count = 3

# Only retry when Selenium raises Net::ReadTimeout
# config.exceptions_to_retry = [Net::ReadTimeout]
end
Expand Down
2 changes: 1 addition & 1 deletion spec/react_on_rails/support/generator_spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def simulate_existing_file(file, data = "some existing text\n")
path = Pathname.new(File.join(destination_root, file))
mkdir_p(path.dirname)
File.open(path, "w+") do |f|
f.puts(data) if data.presence
f.puts(data) if data.present?
end
end

Expand Down
125 changes: 87 additions & 38 deletions spec/react_on_rails/utils_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,69 +4,118 @@

module ReactOnRails
RSpec.describe Utils do
subject { Utils.rails_version_less_than("4") }
describe ".truthy_presence" do
context "With non-empty string" do
subject { "foobar" }
it "returns subject (same value as presence) for a non-empty string" do
expect(Utils.truthy_presence(subject)).to eq(subject.presence)

# Blank strings are nil for presence
expect(Utils.truthy_presence(subject)).to eq(subject)
end
end

describe ".rails_version_less_than" do
before(:each) { Utils.instance_variable_set :@rails_version_less_than, nil }
context "With empty string" do
subject { "" }
it "returns \"\" for an empty string" do
expect(Utils.truthy_presence(subject)).to eq(subject)
end
end

context "with Rails 3" do
before { allow(Rails).to receive(:version).and_return("3") }
context "With nil object" do
subject { nil }
it "returns nil (same value as presence)" do
expect(Utils.truthy_presence(subject)).to eq(subject.presence)

it { expect(subject).to eq(true) }
# Blank strings are nil for presence
expect(Utils.truthy_presence(subject)).to eq(nil)
end
end

context "with Rails 3.2" do
before { allow(Rails).to receive(:version).and_return("3.2") }
context "With pathname pointing to empty dir (obj.empty? is true)" do
subject(:empty_dir) { Pathname.new(Dir.mktmpdir) }
it "returns Pathname object" do
# Blank strings are nil for presence
expect(Utils.truthy_presence(empty_dir)).to eq(empty_dir)
end
end

it { expect(subject).to eq(true) }
context "With pathname pointing to empty file" do
let(:empty_dir) { Pathname.new(Dir.mktmpdir) }
subject(:empty_file) { File.basename(Tempfile.new("tempfile",
empty_dir)) }
it "returns Pathname object" do
expect(Utils.truthy_presence(empty_file)).to eq(empty_file)
end
end
end

context "with Rails 4" do
before { allow(Rails).to receive(:version).and_return("4") }
describe ".rails_version_less_than" do
subject { Utils.rails_version_less_than("4") }

it { expect(subject).to eq(false) }
end

context "with Rails 4.2" do
before { allow(Rails).to receive(:version).and_return("4.2") }
describe ".rails_version_less_than" do
before(:each) { Utils.instance_variable_set :@rails_version_less_than, nil }

it { expect(subject).to eq(false) }
end
context "with Rails 3" do
before { allow(Rails).to receive(:version).and_return("3") }

context "with Rails 10.0" do
before { allow(Rails).to receive(:version).and_return("10.0") }
it { expect(subject).to eq(true) }
end

it { expect(subject).to eq(false) }
end
context "with Rails 3.2" do
before { allow(Rails).to receive(:version).and_return("3.2") }

context "called twice" do
before do
allow(Rails).to receive(:version).and_return("4.2")
it { expect(subject).to eq(true) }
end

it "should memoize the result" do
2.times { Utils.rails_version_less_than("4") }
context "with Rails 4" do
before { allow(Rails).to receive(:version).and_return("4") }

expect(Rails).to have_received(:version).once
it { expect(subject).to eq(false) }
end
end
end

describe ".rails_version_less_than_4_1_1" do
subject { Utils.rails_version_less_than_4_1_1 }
context "with Rails 4.2" do
before { allow(Rails).to receive(:version).and_return("4.2") }

it { expect(subject).to eq(false) }
end

context "with Rails 10.0" do
before { allow(Rails).to receive(:version).and_return("10.0") }

it { expect(subject).to eq(false) }
end

before(:each) { Utils.instance_variable_set :@rails_version_less_than, nil }
context "called twice" do
before do
allow(Rails).to receive(:version).and_return("4.2")
end

context "with Rails 4.1.0" do
before { allow(Rails).to receive(:version).and_return("4.1.0") }
it "should memoize the result" do
2.times { Utils.rails_version_less_than("4") }

it { expect(subject).to eq(true) }
expect(Rails).to have_received(:version).once
end
end
end

context "with Rails 4.1.1" do
before { allow(Rails).to receive(:version).and_return("4.1.1") }
describe ".rails_version_less_than_4_1_1" do
subject { Utils.rails_version_less_than_4_1_1 }

before(:each) { Utils.instance_variable_set :@rails_version_less_than, nil }

it { expect(subject).to eq(false) }
context "with Rails 4.1.0" do
before { allow(Rails).to receive(:version).and_return("4.1.0") }

it { expect(subject).to eq(true) }
end

context "with Rails 4.1.1" do
before { allow(Rails).to receive(:version).and_return("4.1.1") }

it { expect(subject).to eq(false) }
end
end
end
end
Expand Down

0 comments on commit c3e4314

Please sign in to comment.