Skip to content

Commit

Permalink
Use Pathname instance for PathSet exectuables
Browse files Browse the repository at this point in the history
`PathSet` executables should respond to `#executable?`, so that callers
can verify their existence and viability as a runnable command.
  • Loading branch information
seanpdoyle committed Jun 9, 2017
1 parent 47e9b46 commit 2707c08
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 45 deletions.
31 changes: 19 additions & 12 deletions lib/ember_cli/path_set.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,15 @@ def build_error_file

def bower
@bower ||= begin
bower_path = app_options.fetch(:bower_path) { which("bower") }

bower_path.tap do |path|
unless Pathname.new(path.to_s).executable?
path_for_executable("bower").tap do |bower_path|
if bower_path.blank? || !bower_path.executable?
fail DependencyError.new <<-MSG.strip_heredoc
Bower is required by EmberCLI
Bower is required by EmberCLI
Install it with:
Install it with:
$ npm install -g bower
$ npm install -g bower
MSG
end
end
Expand All @@ -84,12 +83,12 @@ def bower_components
end

def npm
@npm ||= app_options.fetch(:npm_path) { which("npm") }
@npm ||= path_for_executable("npm")
end

def yarn
if yarn?
@yarn ||= app_options.fetch(:yarn_path) { which("yarn") }
@yarn ||= path_for_executable("yarn")
end
end

Expand All @@ -98,17 +97,25 @@ def node_modules
end

def tee
@tee ||= app_options.fetch(:tee_path) { which("tee") }
@tee ||= path_for_executable("tee")
end

def bundler
@bundler ||= app_options.fetch(:bundler_path) { which("bundler") }
@bundler ||= path_for_executable("bundler")
end

private

attr_reader :app, :ember_cli_root, :environment, :rails_root

def path_for_executable(command)
path = app_options.fetch("#{command}_path") { which(command) }

if path.present?
Pathname.new(path)
end
end

def package_manager
if yarn?
"yarn"
Expand All @@ -126,7 +133,7 @@ def app_name
end

def app_options
app.options
app.options.with_indifferent_access
end

def which(executable)
Expand Down
84 changes: 51 additions & 33 deletions spec/lib/ember_cli/path_set_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,13 @@
describe "#ember" do
it "is an executable child of #node_modules" do
app = build_app
path_set = build_path_set(app: app)
ember_path = rails_root.join(app.name, "node_modules", "ember-cli", "bin", "ember")
create_executable(ember_path)

path_set = build_path_set(app: app)
ember = path_set.ember

expect(path_set.ember).to eq ember_path
expect(ember).to eq(ember_path).and(be_executable)
end

it "raises a DependencyError if the file isn't executable" do
Expand All @@ -99,20 +100,22 @@
describe "#bower" do
it "can be overridden" do
fake_bower = create_executable(ember_cli_root.join("bower"))
app = build_app(options: { bower_path: fake_bower })

app = build_app(options: { bower_path: fake_bower.to_s })
path_set = build_path_set(app: app)

expect(path_set.bower).to eq fake_bower
bower = path_set.bower

expect(bower).to eq(fake_bower).and(be_executable)
end

it "can be inferred from the $PATH" do
fake_bower = create_executable(ember_cli_root.join("bower"))
stub_which(bower: fake_bower)

stub_which(bower: fake_bower.to_s)
path_set = build_path_set

expect(path_set.bower).to eq fake_bower
bower = path_set.bower

expect(bower).to eq(fake_bower).and(be_executable)
end

context "when it is missing from the $PATH" do
Expand Down Expand Up @@ -140,19 +143,23 @@

describe "#npm" do
it "can be overridden" do
app = build_app(options: { npm_path: "npm-path" })

fake_npm = create_executable(ember_cli_root.join("npm"))
app = build_app(options: { npm_path: fake_npm.to_s })
path_set = build_path_set(app: app)

expect(path_set.npm).to eq "npm-path"
npm = path_set.npm

expect(npm).to eq(fake_npm).and(be_executable)
end

it "can be inferred from the $PATH" do
stub_which(npm: "npm-path")

fake_npm = create_executable(ember_cli_root.join("npm"))
stub_which(npm: fake_npm.to_s)
path_set = build_path_set

expect(path_set.npm).to eq "npm-path"
npm = path_set.npm

expect(npm).to eq(fake_npm).and(be_executable)
end
end

Expand All @@ -164,21 +171,24 @@
end

it "can be overridden" do
app = build_app(options: { yarn_path: "yarn-path" })

fake_yarn = create_executable(ember_cli_root.join("yarn"))
app = build_app(options: { yarn_path: fake_yarn.to_s })
path_set = build_path_set(app: app)

expect(path_set.yarn).to eq "yarn-path"
yarn = path_set.yarn

expect(yarn).to eq(fake_yarn).and(be_executable)
end

it "can be inferred from the $PATH" do
stub_which(yarn: "yarn-path")

fake_yarn = create_executable(ember_cli_root.join("yarn"))
stub_which(yarn: fake_yarn.to_s)
app = build_app(options: { yarn: true })

path_set = build_path_set(app: app)

expect(path_set.yarn).to eq "yarn-path"
yarn = path_set.yarn

expect(yarn).to eq(fake_yarn).and(be_executable)
end
end

Expand All @@ -194,37 +204,45 @@

describe "#tee" do
it "can be overridden" do
app = build_app(options: { tee_path: "tee-path" })

fake_tee = create_executable(rails_root.join("tee"))
app = build_app(options: { tee_path: fake_tee.to_s })
path_set = build_path_set(app: app)

expect(path_set.tee).to eq "tee-path"
tee = path_set.tee

expect(tee).to eq(fake_tee).and(be_executable)
end

it "can be inferred from the $PATH" do
stub_which(tee: "tee-path")

fake_tee = create_executable(rails_root.join("tee"))
stub_which(tee: fake_tee.to_s)
path_set = build_path_set

expect(path_set.tee).to eq "tee-path"
tee = path_set.tee

expect(tee).to eq(fake_tee).and(be_executable)
end
end

describe "#bundler" do
it "can be overridden" do
app = build_app(options: { bundler_path: "bundler-path" })

fake_bundler = create_executable(rails_root.join("bundler"))
app = build_app(options: { bundler_path: fake_bundler.to_s })
path_set = build_path_set(app: app)

expect(path_set.bundler).to eq "bundler-path"
bundler = path_set.bundler

expect(bundler).to eq(fake_bundler).and(be_executable)
end

it "can be inferred from the $PATH" do
stub_which(bundler: "bundler-path")

fake_bundler = create_executable(rails_root.join("bundler"))
stub_which(bundler: fake_bundler.to_s)
path_set = build_path_set

expect(path_set.bundler).to eq "bundler-path"
bundler = path_set.bundler

expect(bundler).to eq(fake_bundler).and(be_executable)
end
end

Expand Down

0 comments on commit 2707c08

Please sign in to comment.