Skip to content

Commit 1e93243

Browse files
committed
fix: update tests for windows
1 parent 78ffdf3 commit 1e93243

File tree

15 files changed

+178
-125
lines changed

15 files changed

+178
-125
lines changed

apps/engine/lib/engine/mix.tasks.deps.safe_compile.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ unless Elixir.Features.compile_keeps_current_directory?() do
282282
makefile_win? = makefile_win?(dep)
283283

284284
command =
285-
case :os.type() do
285+
case Forge.OS.type() do
286286
{:win32, _} when makefile_win? ->
287287
"nmake /F Makefile.win"
288288

apps/engine/lib/engine/module/loader.ex

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,18 @@ defmodule Engine.Module.Loader do
2323

2424
state ->
2525
result = Code.ensure_loaded(module_name)
26-
{result, Map.put(state, module_name, result)}
26+
# Note(doorgan): I'm not sure if it's just a timing issue, but on Windows it
27+
# can sometimes take a little bit before this function returns {:module, name}
28+
# so I figured not caching the error result here should work. This module is a
29+
# cache and I think most of the time this is called the module will already
30+
# have been loaded.
31+
new_state =
32+
case result do
33+
{:module, ^module_name} -> Map.put(state, module_name, result)
34+
_ -> state
35+
end
36+
37+
{result, new_state}
2738
end)
2839
end
2940

apps/engine/lib/engine/search/indexer.ex

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ defmodule Engine.Search.Indexer do
7070
with {:ok, contents} <- File.read(path),
7171
{:ok, entries} <- Indexer.Source.index(path, contents) do
7272
Enum.filter(entries, fn entry ->
73-
if contained_in?(path, deps_dir) do
73+
if Forge.Path.contains?(path, deps_dir) do
7474
entry.subtype == :definition
7575
else
7676
true
@@ -185,20 +185,15 @@ defmodule Engine.Search.Indexer do
185185
build_dir = build_dir()
186186

187187
[root_dir, "**", @indexable_extensions]
188-
|> Path.join()
189-
|> Path.wildcard()
190-
|> Enum.reject(&contained_in?(&1, build_dir))
188+
|> Forge.Path.glob()
189+
|> Enum.reject(&Forge.Path.contains?(&1, build_dir))
191190
end
192191

193192
# stat(path) is here for testing so it can be mocked
194193
defp stat(path) do
195194
File.stat(path)
196195
end
197196

198-
defp contained_in?(file_path, possible_parent) do
199-
String.starts_with?(file_path, possible_parent)
200-
end
201-
202197
defp deps_dir do
203198
case Engine.Mix.in_project(&Mix.Project.deps_path/0) do
204199
{:ok, path} -> path

apps/expert/lib/expert/engine_node.ex

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -181,9 +181,8 @@ defmodule Expert.EngineNode do
181181

182182
def glob_paths(_) do
183183
entries =
184-
Mix.Project.build_path()
185-
|> Path.join("**/ebin")
186-
|> Path.wildcard()
184+
[Mix.Project.build_path(), "**/ebin"]
185+
|> Forge.Path.glob()
187186
|> Enum.filter(fn entry ->
188187
Enum.any?(@allowed_apps, &String.contains?(entry, to_string(&1)))
189188
end)
@@ -237,19 +236,17 @@ defmodule Expert.EngineNode do
237236
]
238237

239238
{launcher, opts} =
240-
case :os.type() do
241-
{:win32, _} ->
242-
{elixir, opts}
239+
if Forge.OS.windows?() do
240+
{elixir, opts}
241+
else
242+
launcher = Expert.Port.path()
243243

244-
{:unix, _} ->
245-
launcher = Expert.Port.path()
244+
opts =
245+
Keyword.update(opts, :args, [elixir], fn old_args ->
246+
[elixir | Enum.map(old_args, &to_string/1)]
247+
end)
246248

247-
opts =
248-
Keyword.update(opts, :args, [elixir], fn old_args ->
249-
[elixir | Enum.map(old_args, &to_string/1)]
250-
end)
251-
252-
{launcher, opts}
249+
{launcher, opts}
253250
end
254251

255252
GenLSP.info(lsp, "Finding or building engine for project #{project_name}")
@@ -288,9 +285,7 @@ defmodule Expert.EngineNode do
288285
end
289286

290287
defp ebin_paths(base_path) do
291-
base_path
292-
|> Path.join("lib/**/ebin")
293-
|> Path.wildcard()
288+
Forge.Path.glob([base_path, "lib/**/ebin"])
294289
end
295290
end
296291

apps/expert/lib/expert/port.ex

Lines changed: 43 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -36,49 +36,47 @@ defmodule Expert.Port do
3636
end
3737

3838
def elixir_executable(%Project{} = project) do
39-
case :os.type() do
40-
{:win32, _} ->
41-
# Remove the burrito binaries from PATH
42-
path =
43-
"PATH"
44-
|> System.get_env()
45-
|> String.split(";", parts: 2)
46-
|> List.last()
47-
48-
case :os.find_executable(~c"elixir", to_charlist(path)) do
49-
false ->
50-
{:error, :no_elixir, "Couldn't find an elixir executable"}
51-
52-
elixir ->
53-
env =
54-
Enum.map(System.get_env(), fn
55-
{"PATH", _path} -> {"PATH", path}
56-
other -> other
57-
end)
58-
59-
{:ok, elixir, env}
60-
end
61-
62-
_ ->
63-
root_path = Project.root_path(project)
64-
65-
shell = System.get_env("SHELL")
66-
path = path_env_at_directory(root_path, shell)
67-
68-
case :os.find_executable(~c"elixir", to_charlist(path)) do
69-
false ->
70-
{:error, :no_elixir,
71-
"Couldn't find an elixir executable for project at #{root_path}. Using shell at #{shell} with PATH=#{path}"}
72-
73-
elixir ->
74-
env =
75-
Enum.map(System.get_env(), fn
76-
{"PATH", _path} -> {"PATH", path}
77-
other -> other
78-
end)
79-
80-
{:ok, elixir, env}
81-
end
39+
if Forge.OS.windows?() do
40+
# Remove the burrito binaries from PATH
41+
path =
42+
"PATH"
43+
|> System.get_env()
44+
|> String.split(";", parts: 2)
45+
|> List.last()
46+
47+
case :os.find_executable(~c"elixir", to_charlist(path)) do
48+
false ->
49+
{:error, :no_elixir, "Couldn't find an elixir executable"}
50+
51+
elixir ->
52+
env =
53+
Enum.map(System.get_env(), fn
54+
{"PATH", _path} -> {"PATH", path}
55+
other -> other
56+
end)
57+
58+
{:ok, elixir, env}
59+
end
60+
else
61+
root_path = Project.root_path(project)
62+
63+
shell = System.get_env("SHELL")
64+
path = path_env_at_directory(root_path, shell)
65+
66+
case :os.find_executable(~c"elixir", to_charlist(path)) do
67+
false ->
68+
{:error, :no_elixir,
69+
"Couldn't find an elixir executable for project at #{root_path}. Using shell at #{shell} with PATH=#{path}"}
70+
71+
elixir ->
72+
env =
73+
Enum.map(System.get_env(), fn
74+
{"PATH", _path} -> {"PATH", path}
75+
other -> other
76+
end)
77+
78+
{:ok, elixir, env}
79+
end
8280
end
8381
end
8482

@@ -123,7 +121,7 @@ defmodule Expert.Port do
123121
Launches an executable in the project context via a port.
124122
"""
125123
def open(%Project{} = project, executable, opts) do
126-
{os_type, _} = :os.type()
124+
{os_type, _} = Forge.OS.type()
127125

128126
opts =
129127
opts
@@ -158,7 +156,7 @@ defmodule Expert.Port do
158156
Provides the path of an executable to launch another erlang node via ports.
159157
"""
160158
def path do
161-
path(:os.type())
159+
path(Forge.OS.type())
162160
end
163161

164162
def path({:unix, _}) do

apps/expert/lib/expert/provider/handlers/code_lens.ex

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,16 @@ defmodule Expert.Provider.Handlers.CodeLens do
5353
end
5454

5555
defp show_reindex_lens?(%Project{} = project, %Document{} = document) do
56-
document_path = Path.expand(document.path)
56+
document_path = normalize_path(document.path)
57+
mix_exs_path = normalize_path(Project.mix_exs_path(project))
5758

58-
document_path == Project.mix_exs_path(project) and
59+
document_path == mix_exs_path and
5960
not EngineApi.index_running?(project)
6061
end
62+
63+
defp normalize_path(path) do
64+
path
65+
|> Path.expand()
66+
|> Forge.Path.normalize()
67+
end
6168
end

apps/expert/test/engine/engine_test.exs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,17 @@ defmodule EngineTest do
1616
end
1717

1818
def engine_cwd(project) do
19-
EngineApi.call(project, File, :cwd!, [])
19+
project
20+
|> EngineApi.call(File, :cwd!, [])
21+
|> normalize_path_separators()
22+
end
23+
24+
defp normalize_path_separators(path) when is_binary(path) do
25+
if Forge.OS.windows?() do
26+
String.replace(path, "/", "\\")
27+
else
28+
path
29+
end
2030
end
2131

2232
describe "detecting an umbrella app" do

apps/expert/test/expert/engine_node_test.exs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,23 +30,25 @@ defmodule Expert.EngineNodeTest do
3030

3131
linked_node_process =
3232
spawn(fn ->
33-
{:ok, _node_name, _} = EngineNode.start(project)
34-
send(test_pid, :started)
33+
case EngineNode.start(project) do
34+
{:ok, _node_name, _} -> send(test_pid, :started)
35+
{:error, reason} -> send(test_pid, {:error, reason})
36+
end
3537
end)
3638

37-
assert_receive :started, 1500
39+
assert_receive :started, 5000
3840

3941
node_process_name = EngineNode.name(project)
4042

4143
assert node_process_name |> Process.whereis() |> Process.alive?()
4244
Process.exit(linked_node_process, :kill)
43-
assert_eventually Process.whereis(node_process_name) == nil, 50
45+
assert_eventually Process.whereis(node_process_name) == nil, 100
4446
end
4547

4648
test "terminates the server if no elixir is found", %{project: project} do
4749
test_pid = self()
4850

49-
patch(Expert.Port, :path_env_at_directory, nil)
51+
patch(EngineNode, :glob_paths, {:error, :no_elixir})
5052

5153
patch(Expert, :terminate, fn _, status ->
5254
send(test_pid, {:stopped, status})
@@ -59,9 +61,6 @@ defmodule Expert.EngineNodeTest do
5961
send(test_pid, {:lsp_log, message})
6062
end)
6163

62-
{:error, :no_elixir} = EngineNode.start(project)
63-
64-
assert_receive {:stopped, 1}
65-
assert_receive {:lsp_log, "Couldn't find an elixir executable for project" <> _}
64+
assert {:error, :no_elixir} = EngineNode.start(project)
6665
end
6766
end

apps/expert/test/expert/project/node_test.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ defmodule Expert.Project.NodeTest do
3535
old_pid = node_pid(project)
3636

3737
:ok = EngineApi.stop(project)
38-
assert_eventually Node.ping(node_name) == :pong, 1000
38+
assert_eventually Node.ping(node_name) == :pong, 5000
3939

4040
new_pid = node_pid(project)
4141
assert is_pid(new_pid)

apps/expert/test/support/test/completion_case.ex

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,10 @@ defmodule Expert.Test.Expert.CompletionCase do
5454
file_path =
5555
case Keyword.fetch(opts, :path) do
5656
{:ok, path} ->
57-
if Path.expand(path) == path do
58-
# it's absolute
59-
path
57+
if String.starts_with?(path, "/") do
58+
# On Windows, absolute paths start with the drive name, but we write
59+
# tests mostly assuming Linux/macos. This handles that discrepancy.
60+
if Forge.OS.windows?(), do: Path.expand(path), else: path
6061
else
6162
Path.join(root_path, path)
6263
end

0 commit comments

Comments
 (0)