Skip to content

Commit 288af4b

Browse files
bewRX14
authored andcommittedJan 3, 2018
Refactor init spec (#4915)
1 parent 2d7c774 commit 288af4b

File tree

1 file changed

+138
-113
lines changed

1 file changed

+138
-113
lines changed
 

Diff for: ‎spec/compiler/crystal/tools/init_spec.cr

+138-113
Original file line numberDiff line numberDiff line change
@@ -1,149 +1,176 @@
1-
require "spec"
2-
require "yaml"
3-
require "ini"
41
require "compiler/crystal/config"
52
require "compiler/crystal/tools/init"
3+
require "file_utils"
4+
require "ini"
5+
require "spec"
6+
require "yaml"
7+
8+
PROJECT_ROOT_DIR = "#{__DIR__}/../../../.."
9+
BIN_CRYSTAL = File.expand_path("#{PROJECT_ROOT_DIR}/bin/crystal")
10+
11+
private def exec_init(project_name, project_dir = nil, type = "lib")
12+
args = ["init", type, project_name]
13+
args << project_dir if project_dir
14+
15+
process = Process.new(BIN_CRYSTAL, args, shell: true, error: Process::Redirect::Pipe)
16+
stderr = process.error.gets_to_end
17+
status = process.wait
18+
$? = status
19+
stderr
20+
end
21+
22+
# Creates a temporary directory, cd to it and run the block inside it.
23+
# The directory and its content is deleted when the block return.
24+
private def within_temporary_directory
25+
tmp_path = "#{PROJECT_ROOT_DIR}/tmp/init_spec_tmp_dir-#{Process.pid}"
26+
Dir.mkdir_p(tmp_path)
27+
begin
28+
Dir.cd(tmp_path) do
29+
yield
30+
end
31+
ensure
32+
FileUtils.rm_rf(tmp_path)
33+
end
34+
end
635

736
private def describe_file(name, &block : String ->)
837
describe name do
938
it "has proper contents" do
10-
block.call(File.read("tmp/#{name}"))
39+
block.call(File.read(name))
1140
end
1241
end
1342
end
1443

15-
private def run_init_project(skeleton_type, name, dir, author, email, github_name)
44+
private def run_init_project(skeleton_type, name, author, email, github_name, dir = name)
1645
Crystal::Init::InitProject.new(
1746
Crystal::Init::Config.new(skeleton_type, name, dir, author, email, github_name, true)
1847
).run
1948
end
2049

2150
module Crystal
2251
describe Init::InitProject do
23-
`[ -d tmp/example ] && rm -r tmp/example`
24-
`[ -d tmp/example_app ] && rm -r tmp/example_app`
25-
26-
run_init_project("lib", "example", "tmp/example", "John Smith", "john@smith.com", "jsmith")
27-
run_init_project("app", "example_app", "tmp/example_app", "John Smith", "john@smith.com", "jsmith")
28-
run_init_project("lib", "example-lib", "tmp/example-lib", "John Smith", "john@smith.com", "jsmith")
29-
run_init_project("lib", "camel_example-camel_lib", "tmp/camel_example-camel_lib", "John Smith", "john@smith.com", "jsmith")
30-
run_init_project("lib", "example", "tmp/other-example-directory", "John Smith", "john@smith.com", "jsmith")
31-
32-
describe_file "example-lib/src/example-lib.cr" do |file|
33-
file.should contain("Example::Lib")
34-
end
52+
within_temporary_directory do
53+
run_init_project("lib", "example", "John Smith", "john@smith.com", "jsmith")
54+
run_init_project("app", "example_app", "John Smith", "john@smith.com", "jsmith")
55+
run_init_project("lib", "example-lib", "John Smith", "john@smith.com", "jsmith")
56+
run_init_project("lib", "camel_example-camel_lib", "John Smith", "john@smith.com", "jsmith")
57+
run_init_project("lib", "example", "John Smith", "john@smith.com", "jsmith", dir: "other-example-directory")
58+
59+
describe_file "example-lib/src/example-lib.cr" do |file|
60+
file.should contain("Example::Lib")
61+
end
3562

36-
describe_file "camel_example-camel_lib/src/camel_example-camel_lib.cr" do |file|
37-
file.should contain("CamelExample::CamelLib")
38-
end
63+
describe_file "camel_example-camel_lib/src/camel_example-camel_lib.cr" do |file|
64+
file.should contain("CamelExample::CamelLib")
65+
end
3966

40-
describe_file "example/.gitignore" do |gitignore|
41-
gitignore.should contain("/.shards/")
42-
gitignore.should contain("/shard.lock")
43-
gitignore.should contain("/lib/")
44-
end
67+
describe_file "example/.gitignore" do |gitignore|
68+
gitignore.should contain("/.shards/")
69+
gitignore.should contain("/shard.lock")
70+
gitignore.should contain("/lib/")
71+
end
4572

46-
describe_file "example_app/.gitignore" do |gitignore|
47-
gitignore.should contain("/.shards/")
48-
gitignore.should_not contain("/shard.lock")
49-
gitignore.should contain("/lib/")
50-
end
73+
describe_file "example_app/.gitignore" do |gitignore|
74+
gitignore.should contain("/.shards/")
75+
gitignore.should_not contain("/shard.lock")
76+
gitignore.should contain("/lib/")
77+
end
5178

52-
["example", "example_app", "example-lib", "camel_example-camel_lib"].each do |name|
53-
describe_file "#{name}/.editorconfig" do |editorconfig|
54-
parsed = INI.parse(editorconfig)
55-
cr_ext = parsed["*.cr"]
56-
cr_ext["charset"].should eq("utf-8")
57-
cr_ext["end_of_line"].should eq("lf")
58-
cr_ext["insert_final_newline"].should eq("true")
59-
cr_ext["indent_style"].should eq("space")
60-
cr_ext["indent_size"].should eq("2")
61-
cr_ext["trim_trailing_whitespace"].should eq("true")
79+
{"example", "example_app", "example-lib", "camel_example-camel_lib"}.each do |name|
80+
describe_file "#{name}/.editorconfig" do |editorconfig|
81+
parsed = INI.parse(editorconfig)
82+
cr_ext = parsed["*.cr"]
83+
cr_ext["charset"].should eq("utf-8")
84+
cr_ext["end_of_line"].should eq("lf")
85+
cr_ext["insert_final_newline"].should eq("true")
86+
cr_ext["indent_style"].should eq("space")
87+
cr_ext["indent_size"].should eq("2")
88+
cr_ext["trim_trailing_whitespace"].should eq("true")
89+
end
6290
end
63-
end
6491

65-
describe_file "example/LICENSE" do |license|
66-
license.should match %r{Copyright \(c\) \d+ John Smith}
67-
end
92+
describe_file "example/LICENSE" do |license|
93+
license.should match %r{Copyright \(c\) \d+ John Smith}
94+
end
6895

69-
describe_file "example/README.md" do |readme|
70-
readme.should contain("# example")
96+
describe_file "example/README.md" do |readme|
97+
readme.should contain("# example")
7198

72-
readme.should contain(%{```yaml
99+
readme.should contain(%{```yaml
73100
dependencies:
74101
example:
75102
github: jsmith/example
76103
```})
77104

78-
readme.should contain(%{TODO: Write a description here})
79-
readme.should_not contain(%{TODO: Write installation instructions here})
80-
readme.should contain(%{require "example"})
81-
readme.should contain(%{1. Fork it ( https://github.com/jsmith/example/fork )})
82-
readme.should contain(%{[jsmith](https://github.com/jsmith) John Smith - creator, maintainer})
83-
end
105+
readme.should contain(%{TODO: Write a description here})
106+
readme.should_not contain(%{TODO: Write installation instructions here})
107+
readme.should contain(%{require "example"})
108+
readme.should contain(%{1. Fork it ( https://github.com/jsmith/example/fork )})
109+
readme.should contain(%{[jsmith](https://github.com/jsmith) John Smith - creator, maintainer})
110+
end
84111

85-
describe_file "example_app/README.md" do |readme|
86-
readme.should contain("# example")
112+
describe_file "example_app/README.md" do |readme|
113+
readme.should contain("# example")
87114

88-
readme.should_not contain(%{```yaml
115+
readme.should_not contain(%{```yaml
89116
dependencies:
90117
example:
91118
github: jsmith/example
92119
```})
93120

94-
readme.should contain(%{TODO: Write a description here})
95-
readme.should contain(%{TODO: Write installation instructions here})
96-
readme.should_not contain(%{require "example"})
97-
readme.should contain(%{1. Fork it ( https://github.com/jsmith/example_app/fork )})
98-
readme.should contain(%{[jsmith](https://github.com/jsmith) John Smith - creator, maintainer})
99-
end
121+
readme.should contain(%{TODO: Write a description here})
122+
readme.should contain(%{TODO: Write installation instructions here})
123+
readme.should_not contain(%{require "example"})
124+
readme.should contain(%{1. Fork it ( https://github.com/jsmith/example_app/fork )})
125+
readme.should contain(%{[jsmith](https://github.com/jsmith) John Smith - creator, maintainer})
126+
end
100127

101-
describe_file "example/shard.yml" do |shard_yml|
102-
parsed = YAML.parse(shard_yml)
103-
parsed["name"].should eq("example")
104-
parsed["version"].should eq("0.1.0")
105-
parsed["authors"].should eq(["John Smith <john@smith.com>"])
106-
parsed["license"].should eq("MIT")
107-
parsed["crystal"].should eq(Crystal::Config.version)
108-
parsed["targets"]?.should be_nil
109-
end
128+
describe_file "example/shard.yml" do |shard_yml|
129+
parsed = YAML.parse(shard_yml)
130+
parsed["name"].should eq("example")
131+
parsed["version"].should eq("0.1.0")
132+
parsed["authors"].should eq(["John Smith <john@smith.com>"])
133+
parsed["license"].should eq("MIT")
134+
parsed["crystal"].should eq(Crystal::Config.version)
135+
parsed["targets"]?.should be_nil
136+
end
110137

111-
describe_file "example_app/shard.yml" do |shard_yml|
112-
parsed = YAML.parse(shard_yml)
113-
parsed["targets"].should eq({"example_app" => {"main" => "src/example_app.cr"}})
114-
end
138+
describe_file "example_app/shard.yml" do |shard_yml|
139+
parsed = YAML.parse(shard_yml)
140+
parsed["targets"].should eq({"example_app" => {"main" => "src/example_app.cr"}})
141+
end
115142

116-
describe_file "example/.travis.yml" do |travis|
117-
parsed = YAML.parse(travis)
143+
describe_file "example/.travis.yml" do |travis|
144+
parsed = YAML.parse(travis)
118145

119-
parsed["language"].should eq("crystal")
120-
end
146+
parsed["language"].should eq("crystal")
147+
end
121148

122-
describe_file "example/src/example.cr" do |example|
123-
example.should eq(%{require "./example/*"
149+
describe_file "example/src/example.cr" do |example|
150+
example.should eq(%{require "./example/*"
124151
125152
# TODO: Write documentation for `Example`
126153
module Example
127154
# TODO: Put your code here
128155
end
129156
})
130-
end
157+
end
131158

132-
describe_file "example/src/example/version.cr" do |version|
133-
version.should eq(%{module Example
159+
describe_file "example/src/example/version.cr" do |version|
160+
version.should eq(%{module Example
134161
VERSION = "0.1.0"
135162
end
136163
})
137-
end
164+
end
138165

139-
describe_file "example/spec/spec_helper.cr" do |example|
140-
example.should eq(%{require "spec"
166+
describe_file "example/spec/spec_helper.cr" do |example|
167+
example.should eq(%{require "spec"
141168
require "../src/example"
142169
})
143-
end
170+
end
144171

145-
describe_file "example/spec/example_spec.cr" do |example|
146-
example.should eq(%{require "./spec_helper"
172+
describe_file "example/spec/example_spec.cr" do |example|
173+
example.should eq(%{require "./spec_helper"
147174
148175
describe Example do
149176
# TODO: Write tests
@@ -153,40 +180,38 @@ describe Example do
153180
end
154181
end
155182
})
156-
end
157-
158-
describe_file "example/.git/config" { }
159-
160-
describe_file "other-example-directory/.git/config" { }
161-
end
162-
163-
describe Init do
164-
it "prints error if a directory already present" do
165-
Dir.mkdir_p("#{__DIR__}/tmp")
183+
end
166184

167-
`bin/crystal init lib "#{__DIR__}/tmp" 2>&1 >/dev/null`.should contain("file or directory #{__DIR__}/tmp already exists")
185+
describe_file "example/.git/config" { }
168186

169-
`rm -rf #{__DIR__}/tmp`
187+
describe_file "other-example-directory/.git/config" { }
170188
end
189+
end
171190

172-
it "prints error if a file already present" do
173-
File.open("#{__DIR__}/tmp", "w")
174-
175-
`bin/crystal init lib "#{__DIR__}/tmp" 2>&1 >/dev/null`.should contain("file or directory #{__DIR__}/tmp already exists")
191+
describe "Init invocation" do
192+
it "prints error if a directory or a file is already present" do
193+
within_temporary_directory do
194+
existing_dir = "existing-dir"
195+
Dir.mkdir(existing_dir)
196+
exec_init(existing_dir).should contain("file or directory #{existing_dir} already exists")
176197

177-
File.delete("#{__DIR__}/tmp")
198+
existing_file = "existing-file"
199+
File.touch(existing_file)
200+
exec_init(existing_file).should contain("file or directory #{existing_file} already exists")
201+
end
178202
end
179203

180204
it "honors the custom set directory name" do
181-
Dir.mkdir_p("tmp")
182-
183-
`bin/crystal init lib tmp 2>&1 >/dev/null`.should contain("file or directory tmp already exists")
205+
within_temporary_directory do
206+
project_name = "my_project"
207+
project_dir = "project_dir"
184208

185-
`bin/crystal init lib tmp "#{__DIR__}/fresh-new-tmp" 2>&1 >/dev/null`.should_not contain("file or directory tmp already exists")
209+
Dir.mkdir(project_name)
186210

187-
`bin/crystal init lib tmp "#{__DIR__}/fresh-new-tmp" 2>&1 >/dev/null`.should contain("file or directory #{__DIR__}/fresh-new-tmp already exists")
211+
exec_init(project_name, project_dir).should_not contain("file or directory #{project_name} already exists")
188212

189-
`rm -rf tmp #{__DIR__}/fresh-new-tmp`
213+
exec_init(project_name, project_dir).should contain("file or directory #{project_dir} already exists")
214+
end
190215
end
191216
end
192217
end

0 commit comments

Comments
 (0)
Please sign in to comment.