Skip to content

Commit

Permalink
Remove bin/crystal usage for init tool specs
Browse files Browse the repository at this point in the history
  • Loading branch information
bew committed Jan 3, 2018
1 parent 288af4b commit 487d7b9
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 21 deletions.
9 changes: 3 additions & 6 deletions spec/compiler/crystal/tools/init_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
30 changes: 15 additions & 15 deletions src/compiler/crystal/tools/init.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand All @@ -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

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 487d7b9

Please sign in to comment.