1
1
defmodule ElixirLS.Experimental.ProjectTest do
2
2
alias ElixirLS.LanguageServer.Experimental.Project
3
3
alias ElixirLS.LanguageServer.SourceFile
4
+ alias ElixirLS.LanguageServer.Test.Paths
4
5
5
6
use ExUnit.Case , async: false
6
7
use Patch
@@ -30,11 +31,13 @@ defmodule ElixirLS.Experimental.ProjectTest do
30
31
end
31
32
32
33
def root_uri do
33
- "file:///tmp/my_project"
34
+ System . tmp_dir! ( )
35
+ |> Path . join ( "my_project" )
36
+ |> SourceFile.Path . to_uri ( )
34
37
end
35
38
36
39
def with_a_root_uri ( _ ) do
37
- { :ok , root_uri: "file:///tmp/my_project" }
40
+ { :ok , root_uri: root_uri ( ) }
38
41
end
39
42
40
43
setup do
@@ -240,35 +243,40 @@ defmodule ElixirLS.Experimental.ProjectTest do
240
243
String . ends_with? ( path , "mix.exs" )
241
244
end )
242
245
243
- assert { :ok , % Project { } = project } =
244
- Project . change_project_directory ( ctx . project , "sub_dir/new/dir" )
246
+ sub_dir = Path . join ( ~w( sub_dir new dir) )
247
+ assert { :ok , % Project { } = project } = Project . change_project_directory ( ctx . project , sub_dir )
248
+
249
+ assert Project . project_path ( project ) ==
250
+ [ File . cwd! ( ) , "sub_dir" , "new" , "dir" ]
251
+ |> Path . join ( )
252
+ |> Paths . to_native_separators ( )
245
253
246
- assert Project . project_path ( project ) == "#{ File . cwd! ( ) } /sub_dir/new/dir"
247
254
assert project . mix_project?
248
255
end
249
256
250
257
test "only sets the project directory if the root uri is set" do
251
258
project = Project . new ( nil )
259
+ sub_dir = Path . join ( ~w( sub_dir new dir) )
252
260
253
- assert { :ok , project } = Project . change_project_directory ( project , " sub_dir/new/dir" )
261
+ assert { :ok , project } = Project . change_project_directory ( project , sub_dir )
254
262
assert project . root_uri == nil
255
263
assert Project . project_path ( project ) == nil
256
264
end
257
265
258
266
test "defaults to the root uri's directory" , ctx do
259
267
assert { :ok , project } = Project . change_project_directory ( ctx . project , nil )
260
268
root_path = SourceFile.Path . absolute_from_uri ( project . root_uri )
261
- assert Project . project_path ( project ) == root_path
269
+ assert Project . project_path ( project ) == Paths . to_native_separators ( root_path )
262
270
end
263
271
264
272
test "defaults to the root uri's directory if the project directory is empty" , ctx do
265
273
assert { :ok , project } = Project . change_project_directory ( ctx . project , "" )
266
274
root_path = SourceFile.Path . absolute_from_uri ( project . root_uri )
267
- assert Project . project_path ( project ) == root_path
275
+ assert Project . project_path ( project ) == Paths . to_native_separators ( root_path )
268
276
end
269
277
270
278
test "normalizes the project directory" , ctx do
271
- subdirectory = " sub_dir/../ sub_dir/ new/../ new/ dir"
279
+ subdirectory = Path . join ( ~w ( sub_dir .. sub_dir new .. new dir) )
272
280
273
281
patch ( File , :exists? , fn path , _ ->
274
282
String . ends_with? ( path , "mix.exs" )
@@ -277,29 +285,41 @@ defmodule ElixirLS.Experimental.ProjectTest do
277
285
assert { :ok , % Project { } = project } =
278
286
Project . change_project_directory ( ctx . project , subdirectory )
279
287
280
- assert Project . project_path ( project ) == "#{ File . cwd! ( ) } /sub_dir/new/dir"
288
+ assert sub_dir = Path . join ( [ File . cwd! ( ) , "sub_dir" , "new" , "dir" ] )
289
+ assert Project . project_path ( project ) == Paths . to_native_separators ( sub_dir )
281
290
assert project . mix_project?
282
- assert Project . mix_exs_path ( project ) == "#{ File . cwd! ( ) } /sub_dir/new/dir/mix.exs"
291
+
292
+ assert Project . mix_exs_path ( project ) ==
293
+ sub_dir
294
+ |> Path . join ( "mix.exs" )
295
+ |> Paths . to_native_separators ( )
283
296
end
284
297
285
298
test "sets mix project to false if the mix.exs doesn't exist" , ctx do
286
299
patch ( File , :exists? , fn file_name ->
287
300
! String . ends_with? ( file_name , "mix.exs" )
288
301
end )
289
302
290
- assert { :ok , % Project { } = project } =
291
- Project . change_project_directory ( ctx . project , "sub_dir/new/dir" )
303
+ sub_dir = Path . join ( ~w( sub_dir new dir) )
304
+ assert { :ok , % Project { } = project } = Project . change_project_directory ( ctx . project , sub_dir )
305
+
306
+ assert Project . project_path ( project ) ==
307
+ File . cwd! ( )
308
+ |> Path . join ( sub_dir )
309
+ |> Paths . to_native_separators ( )
292
310
293
- assert Project . project_path ( project ) == "#{ File . cwd! ( ) } /sub_dir/new/dir"
294
311
refute project . mix_project?
295
312
end
296
313
297
314
test "asks for a restart if the project directory was set and the new one isn't the same" ,
298
315
ctx do
299
- { :ok , project } = Project . change_project_directory ( ctx . project , "sub_dir/foo" )
316
+ foo_sub_dir = Path . join ( ~w( sub_dir foo) )
317
+ { :ok , project } = Project . change_project_directory ( ctx . project , foo_sub_dir )
318
+
319
+ new_dir_sub_dir = Path . join ( ~w( sub_dir new dir) )
300
320
301
321
assert { :restart , :warning , message } =
302
- Project . change_project_directory ( project , "sub_dir/new/dir" )
322
+ Project . change_project_directory ( project , new_dir_sub_dir )
303
323
304
324
assert message =~ "Project directory change detected"
305
325
end
@@ -308,7 +328,8 @@ defmodule ElixirLS.Experimental.ProjectTest do
308
328
{ :ok , project } = Project . change_project_directory ( ctx . project , "sub_dir/foo" )
309
329
310
330
patch ( File , :dir? , false )
311
- new_directory = "sub_dir/new/dir"
331
+
332
+ new_directory = Path . join ( ~w( sub_dir new dir) )
312
333
expected_message_directory = Path . join ( File . cwd! ( ) , new_directory )
313
334
314
335
assert { :error , message } = Project . change_project_directory ( project , new_directory )
@@ -317,8 +338,9 @@ defmodule ElixirLS.Experimental.ProjectTest do
317
338
318
339
test "rejects a change if the project directory isn't a subdirectory of the project root" ,
319
340
ctx do
320
- assert { :error , message } =
321
- Project . change_project_directory ( ctx . project , "../../../../not-a-subdir" )
341
+ not_in_project = Path . join ( ~w( .. .. .. .. not-a-subdir) )
342
+
343
+ assert { :error , message } = Project . change_project_directory ( ctx . project , not_in_project )
322
344
323
345
assert message =~ "is not a subdirectory of"
324
346
end
0 commit comments