From 0796bf445f20de84622c69dfa94371abc43ee277 Mon Sep 17 00:00:00 2001 From: George Date: Wed, 18 Jan 2017 10:23:49 +0300 Subject: [PATCH] Added OS detection for install generator system call for Windows and unit-tests for it. (#666) --- CHANGELOG.md | 2 + .../react_on_rails/install_generator.rb | 4 +- lib/react_on_rails/utils.rb | 4 ++ .../generators/install_generator_spec.rb | 64 +++++++++++++++++++ 4 files changed, 72 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c4ee209a0..ea05290e9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,8 @@ Contributors: please follow the recommendations outlined at [keepachangelog.com] ## [Unreleased] *Please add entries here for your pull requests.* +### Fixed +- Added OS detection for install generator, system call for Windows and unit-tests for it. [#666](https://github.com/shakacode/react_on_rails/pull/666) by [GeorgeGorbanev](https://github.com/GeorgeGorbanev). ## [6.4.0] - 2017-1-12 diff --git a/lib/generators/react_on_rails/install_generator.rb b/lib/generators/react_on_rails/install_generator.rb index 91be836f8..ff8afb933 100644 --- a/lib/generators/react_on_rails/install_generator.rb +++ b/lib/generators/react_on_rails/install_generator.rb @@ -64,7 +64,7 @@ def installation_prerequisites_met? end def missing_npm? - return false unless `which npm`.blank? + return false unless ReactOnRails::Utils.running_on_windows? ? `where npm`.blank? : `which npm`.blank? error = "npm is required. Please install it before continuing. " error << "https://www.npmjs.com/" GeneratorMessages.add_error(error) @@ -72,7 +72,7 @@ def missing_npm? end def missing_node? - return false unless `which node`.blank? + return false unless ReactOnRails::Utils.running_on_windows? ? `where node`.blank? : `which node`.blank? error = "** nodejs is required. Please install it before continuing. " error << "https://nodejs.org/en/" GeneratorMessages.add_error(error) diff --git a/lib/react_on_rails/utils.rb b/lib/react_on_rails/utils.rb index 08ee540c6..599c4e3fe 100644 --- a/lib/react_on_rails/utils.rb +++ b/lib/react_on_rails/utils.rb @@ -20,6 +20,10 @@ def self.default_server_bundle_js_file_path ReactOnRails.configuration.server_bundle_js_file) end + def self.running_on_windows? + (/cygwin|mswin|mingw|bccwin|wince|emx/ =~ RUBY_PLATFORM) != nil + end + module Required def required(arg_name) raise ArgumentError, "#{arg_name} is required" diff --git a/spec/react_on_rails/generators/install_generator_spec.rb b/spec/react_on_rails/generators/install_generator_spec.rb index c70b5de11..fc616fe78 100644 --- a/spec/react_on_rails/generators/install_generator_spec.rb +++ b/spec/react_on_rails/generators/install_generator_spec.rb @@ -111,4 +111,68 @@ .to include(GeneratorMessages.format_info(expected)) end end + + context "detect existing bin-files on *nix" do + before(:all) { @install_generator = InstallGenerator.new } + + specify "when node is exist" do + stub_const("RUBY_PLATFORM", "linux") + allow(@install_generator).to receive(:`).with("which node").and_return("/path/to/bin") + expect(@install_generator.send(:missing_node?)).to eq false + end + + specify "when npm is exist" do + stub_const("RUBY_PLATFORM", "linux") + allow(@install_generator).to receive(:`).with("which npm").and_return("/path/to/bin") + expect(@install_generator.send(:missing_npm?)).to eq false + end + end + + context "detect missing bin-files on *nix" do + before(:all) { @install_generator = InstallGenerator.new } + + specify "when node is missing" do + stub_const("RUBY_PLATFORM", "linux") + allow(@install_generator).to receive(:`).with("which node").and_return("") + expect(@install_generator.send(:missing_node?)).to eq true + end + + specify "when npm is missing" do + stub_const("RUBY_PLATFORM", "linux") + allow(@install_generator).to receive(:`).with("which npm").and_return("") + expect(@install_generator.send(:missing_npm?)).to eq true + end + end + + context "detect existing bin-files on windows" do + before(:all) { @install_generator = InstallGenerator.new } + + specify "when node is exist" do + stub_const("RUBY_PLATFORM", "mswin") + allow(@install_generator).to receive(:`).with("where node").and_return("/path/to/bin") + expect(@install_generator.send(:missing_node?)).to eq false + end + + specify "when npm is exist" do + stub_const("RUBY_PLATFORM", "mswin") + allow(@install_generator).to receive(:`).with("where npm").and_return("/path/to/bin") + expect(@install_generator.send(:missing_npm?)).to eq false + end + end + + context "detect missing bin-files on windows" do + before(:all) { @install_generator = InstallGenerator.new } + + specify "when node is missing" do + stub_const("RUBY_PLATFORM", "mswin") + allow(@install_generator).to receive(:`).with("where node").and_return("") + expect(@install_generator.send(:missing_node?)).to eq true + end + + specify "when npm is missing" do + stub_const("RUBY_PLATFORM", "mswin") + allow(@install_generator).to receive(:`).with("where npm").and_return("") + expect(@install_generator.send(:missing_npm?)).to eq true + end + end end