1
- require " spec"
2
- require " yaml"
3
- require " ini"
4
1
require " compiler/crystal/config"
5
2
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
6
35
7
36
private def describe_file (name , & block : String - > )
8
37
describe name do
9
38
it " has proper contents" do
10
- block.call(File .read(" tmp/ #{ name } " ))
39
+ block.call(File .read(name))
11
40
end
12
41
end
13
42
end
14
43
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 )
16
45
Crystal ::Init ::InitProject .new(
17
46
Crystal ::Init ::Config .new(skeleton_type, name, dir, author, email, github_name, true )
18
47
).run
19
48
end
20
49
21
50
module Crystal
22
51
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
35
62
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
39
66
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
45
72
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
51
78
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
62
90
end
63
- end
64
91
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
68
95
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" )
71
98
72
- readme.should contain(%{ ```yaml
99
+ readme.should contain(%{ ```yaml
73
100
dependencies:
74
101
example:
75
102
github: jsmith/example
76
103
```} )
77
104
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
84
111
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" )
87
114
88
- readme.should_not contain(%{ ```yaml
115
+ readme.should_not contain(%{ ```yaml
89
116
dependencies:
90
117
example:
91
118
github: jsmith/example
92
119
```} )
93
120
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
100
127
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
110
137
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
115
142
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)
118
145
119
- parsed[" language" ].should eq(" crystal" )
120
- end
146
+ parsed[" language" ].should eq(" crystal" )
147
+ end
121
148
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/*"
124
151
125
152
# TODO: Write documentation for `Example`
126
153
module Example
127
154
# TODO: Put your code here
128
155
end
129
156
} )
130
- end
157
+ end
131
158
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
134
161
VERSION = "0.1.0"
135
162
end
136
163
} )
137
- end
164
+ end
138
165
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"
141
168
require "../src/example"
142
169
} )
143
- end
170
+ end
144
171
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"
147
174
148
175
describe Example do
149
176
# TODO: Write tests
@@ -153,40 +180,38 @@ describe Example do
153
180
end
154
181
end
155
182
} )
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
166
184
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 " { }
168
186
169
- ` rm -rf #{ __DIR__ } /tmp `
187
+ describe_file " other-example-directory/.git/config " { }
170
188
end
189
+ end
171
190
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" )
176
197
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
178
202
end
179
203
180
204
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 "
184
208
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 )
186
210
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" )
188
212
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
190
215
end
191
216
end
192
217
end
0 commit comments