Skip to content

Commit

Permalink
Update init <name> to better validate <name> (#250)
Browse files Browse the repository at this point in the history
  • Loading branch information
robacarp authored and paulcsmith committed Sep 6, 2018
1 parent eba5013 commit 2b3d174
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 2 deletions.
24 changes: 24 additions & 0 deletions spec/integration/init_web_spec.cr
Expand Up @@ -68,6 +68,30 @@ describe "Initializing a new web project" do
message = "Folder named test-project already exists, please use a different name"
output.to_s.strip.should contain(message)
end

it "does not create project if project name is not a valid project name" do
output = IO::Memory.new
Process.run(
"crystal src/lucky.cr init 'test project'",
output: output,
shell: true
)
message = "Project name should only contain letters, numbers, underscores, and dashes."
output.to_s.strip.should contain(message)
end

it "translates dashes to underscores in the project name" do
output = IO::Memory.new
Process.run(
"crystal src/lucky.cr init 'test-project'",
output: output,
shell: true
)

shard_yml_name = File.read_lines("test-project/shard.yml").select { |line| line =~ /^name:/ }.first
shard_yml_name.should eq("name: test_project")
File.exists?("test-project/src/test_project.cr").should be_true
end
end

private def compile_and_run_specs_on_test_project
Expand Down
37 changes: 37 additions & 0 deletions spec/lucky_cli/generators/validators_spec.cr
@@ -0,0 +1,37 @@
require "../../spec_helper"

private def validate(string)
LuckyCli::Generators::Validators::ProjectName.valid? string
end

private def sanitize(string)
LuckyCli::Generators::Validators::ProjectName.sanitize string
end

describe LuckyCli::Generators::Validators::ProjectName do
hyphenate = "sector-seven"
special_characters = "this string is emphatic"
title = "From the Earth to the Moon"

it "doesn't allow special characters" do
validate(special_characters).should be_false
end

it "corrects special characters" do
expected = "this_string_is_emphatic"
sanitize(special_characters).should eq expected
end

it "allows hyphens" do
validate(hyphenate).should be_true
end

it "doesn't allow uppercase characters" do
validate(title).should be_false
end

it "corrects uppercase characters" do
expected = "from_the_earth_to_the_moon"
sanitize(title).should eq expected
end
end
11 changes: 11 additions & 0 deletions src/generators/validators.cr
@@ -0,0 +1,11 @@
module LuckyCli::Generators::Validators::ProjectName
extend self

def sanitize(name)
name.downcase.gsub(/[^a-z0-9_-]/, "_").strip('_')
end

def valid?(name)
sanitize(name) == name
end
end
20 changes: 18 additions & 2 deletions src/generators/web.cr
Expand Up @@ -10,8 +10,11 @@ class LuckyCli::Generators::Web
project_name : String,
@options : Options
)
@project_dir = project_name.gsub(" ", "_")
@project_name = @project_dir.gsub("-", "_")
@project_dir = project_name
@project_name = project_name.gsub('-', '_')

validate_project_name @project_name

@template_dir = File.join(__DIR__, "templates")
end

Expand Down Expand Up @@ -181,6 +184,19 @@ class LuckyCli::Generators::Web
end
end

private def validate_project_name(name)
unless Validators::ProjectName.valid?(name)
message = <<-TEXT
Project name should only contain letters, numbers, underscores, and dashes.
How about: lucky init '#{Validators::ProjectName.sanitize(name)}'?
TEXT

puts message.colorize(:red)
exit
end
end

class Options
getter? api_only, generate_auth

Expand Down

0 comments on commit 2b3d174

Please sign in to comment.