From 487d7b9923b6e3a7cb4274f5db4dcd00ac256698 Mon Sep 17 00:00:00 2001 From: Benoit de Chezelles Date: Wed, 3 Jan 2018 20:46:22 +0100 Subject: [PATCH] Remove `bin/crystal` usage for init tool specs --- spec/compiler/crystal/tools/init_spec.cr | 9 +++---- src/compiler/crystal/tools/init.cr | 30 ++++++++++++------------ 2 files changed, 18 insertions(+), 21 deletions(-) diff --git a/spec/compiler/crystal/tools/init_spec.cr b/spec/compiler/crystal/tools/init_spec.cr index fa70296481f1..eb7b717d3b73 100644 --- a/spec/compiler/crystal/tools/init_spec.cr +++ b/spec/compiler/crystal/tools/init_spec.cr @@ -6,17 +6,14 @@ require "spec" require "yaml" PROJECT_ROOT_DIR = "#{__DIR__}/../../../.." -BIN_CRYSTAL = File.expand_path("#{PROJECT_ROOT_DIR}/bin/crystal") private def exec_init(project_name, project_dir = nil, type = "lib") args = ["init", type, project_name] args << project_dir if project_dir - process = Process.new(BIN_CRYSTAL, args, shell: true, error: Process::Redirect::Pipe) - stderr = process.error.gets_to_end - status = process.wait - $? = status - stderr + err_io = IO::Memory.new + Crystal::Init.run(args, stderr: err_io) + err_io.to_s end # Creates a temporary directory, cd to it and run the block inside it. diff --git a/src/compiler/crystal/tools/init.cr b/src/compiler/crystal/tools/init.cr index 151325324fa1..b58c36e4fd23 100644 --- a/src/compiler/crystal/tools/init.cr +++ b/src/compiler/crystal/tools/init.cr @@ -7,7 +7,7 @@ module Crystal module Init WHICH_GIT_COMMAND = "which git >/dev/null" - def self.run(args) + def self.run(args, *, stderr = STDERR) config = Config.new OptionParser.parse(args) do |opts| @@ -31,9 +31,9 @@ module Crystal end opts.unknown_args do |args, after_dash| - config.skeleton_type = fetch_skeleton_type(opts, args) - config.name = fetch_name(opts, args) - config.dir = fetch_directory(args, config.name) + config.skeleton_type = fetch_skeleton_type(opts, args, stderr) + config.name = fetch_name(opts, args, stderr) + config.dir = fetch_directory(args, config.name, stderr) end end @@ -67,33 +67,33 @@ module Crystal github_user || "your-github-user" end - def self.fetch_name(opts, args) - fetch_required_parameter(opts, args, "NAME") + def self.fetch_name(opts, args, stderr) + fetch_required_parameter(opts, args, "NAME", stderr) end - def self.fetch_directory(args, project_name) + def self.fetch_directory(args, project_name, stderr) directory = args.empty? ? project_name : args.shift if Dir.exists?(directory) || File.exists?(directory) - STDERR.puts "file or directory #{directory} already exists" + stderr.puts "file or directory #{directory} already exists" exit 1 end directory end - def self.fetch_skeleton_type(opts, args) - skeleton_type = fetch_required_parameter(opts, args, "TYPE") + def self.fetch_skeleton_type(opts, args, stderr) + skeleton_type = fetch_required_parameter(opts, args, "TYPE", stderr) unless {"lib", "app"}.includes?(skeleton_type) - STDERR.puts "invalid TYPE value: #{skeleton_type}" - STDERR.puts opts + stderr.puts "invalid TYPE value: #{skeleton_type}" + stderr.puts opts exit 1 end skeleton_type end - def self.fetch_required_parameter(opts, args, name) + def self.fetch_required_parameter(opts, args, name, stderr) if args.empty? - STDERR.puts "#{name} is missing" - STDERR.puts opts + stderr.puts "#{name} is missing" + stderr.puts opts exit 1 end args.shift