Skip to content

Commit 3e92853

Browse files
committed
Improve tests with less side effects and better assertions
1 parent f506184 commit 3e92853

14 files changed

+160
-100
lines changed

.gitignore

-2
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,3 @@ npm-debug.log
3434
/assets/node_modules/
3535

3636
/.elixir_ls/
37-
38-
/test/fixtures/legendary/config.ini

config/dev.exs

+1
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ config :phoenix, :plug_init_mode, :runtime
6969
config :heroix, legendary_config_path: Path.join([System.user_home(), ".config", "legendary"])
7070
config :heroix, heroix_config_path: Path.join([System.user_home(), ".config", "heroix"])
7171
config :heroix, legendary_bin_wrapper: Heroix.LegendaryBin
72+
config :heroix, file_manager: File
7273
config :heroix, images_cache: Heroix.ImagesCache
7374

7475
config :heroix, :phoenix_sass,

config/test.exs

+3-2
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,6 @@ config :phoenix, :plug_init_mode, :runtime
1515

1616
config :heroix, legendary_config_path: Path.join([File.cwd!(), "test", "fixtures", "legendary"])
1717
config :heroix, heroix_config_path: Path.join([File.cwd!(), "test", "fixtures", "heroix"])
18-
config :heroix, legendary_bin_wrapper: Heroix.MockedLegendaryBin
19-
config :heroix, images_cache: Heroix.MockedImagesCache
18+
config :heroix, legendary_bin_wrapper: Heroix.Mocks.LegendaryBin
19+
config :heroix, file_manager: Heroix.Mocks.TestFileManager
20+
config :heroix, images_cache: Heroix.Mocks.ImagesCache

lib/heroix/legendary.ex

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
defmodule Heroix.Legendary do
22
@binary Application.fetch_env!(:heroix, :legendary_bin_wrapper)
3+
@file_manager Application.fetch_env!(:heroix, :file_manager)
34

45
def owned_games do
56
installed_info = installed_games()
@@ -46,8 +47,8 @@ defmodule Heroix.Legendary do
4647
end
4748
end
4849

49-
def read_config do
50-
case File.read(config_ini_path()) do
50+
def read_config() do
51+
case @file_manager.read(config_ini_path()) do
5152
{:ok, body} ->
5253
lines = String.split(body, "\n")
5354
process_first_line(lines, %{})
@@ -161,7 +162,7 @@ defmodule Heroix.Legendary do
161162
|> Enum.reject(fn x -> is_nil(x) end)
162163
|> Enum.join("\n\n")
163164

164-
File.write(config_ini_path(), content <> "\n")
165+
@file_manager.write(config_ini_path(), content <> "\n")
165166
end
166167

167168
defp config_to_string(config, parent_key) do

lib/heroix/mocked_images_cache.ex

-11
This file was deleted.

lib/heroix/mocks/images_cache.ex

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
defmodule Heroix.Mocks.ImagesCache do
2+
def get(_app_name, _variant) do
3+
Path.join([images_cache_path(), "test-image.png"])
4+
end
5+
6+
defp images_cache_path do
7+
Path.join([Application.fetch_env!(:heroix, :heroix_config_path), "images_cache"])
8+
end
9+
end

lib/heroix/mocked_legendary_bin.ex lib/heroix/mocks/legendary_bin.ex

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
defmodule Heroix.MockedLegendaryBin do
1+
defmodule Heroix.Mocks.LegendaryBin do
22
use HeroixLog, "MockedBin"
33

44
def install(app_name, opts \\ []) do

lib/heroix/mocks/test_file_manager.ex

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
defmodule Heroix.Mocks.TestFileManager do
2+
def write(_path, content) do
3+
# IO.puts("New content for #{path}")
4+
# IO.inspect(content)
5+
{:ok, content}
6+
end
7+
8+
def read(path) do
9+
# IO.puts("read #{path}")
10+
File.read(path)
11+
end
12+
end

lib/heroix/settings.ex

+19-2
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,14 @@ defmodule Heroix.Settings do
9595
new_legendary_config = Map.put(legendary_config, app_name, new_app_config)
9696
new_state = Map.put(state, :legendary, new_legendary_config)
9797

98-
if save, do: Heroix.Legendary.write_config(new_legendary_config)
98+
if save do
99+
Heroix.Legendary.write_config(new_legendary_config)
100+
101+
HeroixWeb.Endpoint.broadcast!("settings", "legendary_game_settings_stored", %{
102+
app_name: app_name,
103+
new_settings: new_legendary_config
104+
})
105+
end
99106

100107
{:reply, new_app_config, new_state}
101108
end
@@ -118,7 +125,13 @@ defmodule Heroix.Settings do
118125
new_legendary = Map.put(legendary_config, "Legendary", new_legendary_config)
119126
new_state = Map.put(state, :legendary, new_legendary)
120127

121-
if save, do: Legendary.write_config(new_legendary)
128+
if save do
129+
Legendary.write_config(new_legendary)
130+
131+
HeroixWeb.Endpoint.broadcast!("settings", "legendary_settings_stored", %{
132+
new_settings: new_legendary
133+
})
134+
end
122135

123136
{:reply, new_legendary_config, new_state}
124137
end
@@ -132,6 +145,10 @@ defmodule Heroix.Settings do
132145
def handle_cast(:save_legendary_config, state = %{legendary: legendary_config}) do
133146
Heroix.Legendary.write_config(legendary_config)
134147

148+
HeroixWeb.Endpoint.broadcast!("settings", "save_legendary_config", %{
149+
new_settings: legendary_config
150+
})
151+
135152
{:noreply, state}
136153
end
137154

priv/sass/_library.scss

+8
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,14 @@
3737
.game_image {
3838
grid-area: image;
3939
}
40+
41+
&:not(.installed) {
42+
filter: grayscale(0.9);
43+
44+
&:hover {
45+
filter: grayscale(0);
46+
}
47+
}
4048
}
4149
}
4250
}

test/fixtures/legendary/config.ini.sample test/fixtures/legendary/config.ini

-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ log_level = debug
44
max_memory = 2048
55
; maximum number of worker processes when downloading (fewer workers will be slower, but also use less system resources)
66
max_workers = 8
7-
; default install directory
8-
install_dir = /mnt/tank/games
97
; locale override, must be in RFC 1766 format (e.g. "en-US")
108
locale = en-US
119
; whether or not syncing with egl is enabled

test/heroix/legendary_test.exs

+5-22
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,11 @@ defmodule Heroix.LegendaryTest do
33

44
alias Heroix.Legendary
55

6-
@config_path Application.fetch_env!(:heroix, :legendary_config_path)
7-
@config_ini Path.join([@config_path, "config.ini"])
8-
@config_ini_sample Path.join([@config_path, "config.ini.sample"])
9-
106
@config_as_map %{
117
"Legendary" => %{
128
"log_level" => "debug",
139
"max_memory" => "2048",
1410
"max_workers" => "8",
15-
"install_dir" => "/mnt/tank/games",
1611
"locale" => "en-US",
1712
"egl_sync" => "false",
1813
"egl_programdata" => "/home/user/Games/epic-games-store/drive_c/...",
@@ -98,11 +93,7 @@ defmodule Heroix.LegendaryTest do
9893
end
9994

10095
test "reads config.ini correctly" do
101-
File.copy(@config_ini_sample, @config_ini)
102-
10396
assert @config_as_map == Legendary.read_config()
104-
105-
File.rm(@config_ini)
10697
end
10798

10899
test "writes config.ini properly" do
@@ -116,7 +107,6 @@ defmodule Heroix.LegendaryTest do
116107
disable_update_notice = false
117108
egl_programdata = /home/user/Games/epic-games-store/drive_c/...
118109
egl_sync = false
119-
install_dir = /mnt/tank/games
120110
install_platform_fallback = true
121111
locale = en-US
122112
log_level = debug
@@ -163,13 +153,9 @@ defmodule Heroix.LegendaryTest do
163153
pre_launch_wait = false
164154
"""
165155

166-
Legendary.write_config(@config_as_map)
167-
168-
{:ok, body} = File.read(@config_ini)
156+
{:ok, new_content} = Legendary.write_config(@config_as_map)
169157

170-
assert body == config_content
171-
172-
File.rm(@config_ini)
158+
assert new_content == config_content
173159
end
174160

175161
test "ignores empty configurations" do
@@ -178,12 +164,9 @@ defmodule Heroix.LegendaryTest do
178164
wrapper = gamemode
179165
"""
180166

181-
Legendary.write_config(%{"default" => %{"wrapper" => "gamemode", "wine" => ""}})
182-
183-
{:ok, body} = File.read(@config_ini)
184-
185-
assert body == config_content
167+
{:ok, new_content} =
168+
Legendary.write_config(%{"default" => %{"wrapper" => "gamemode", "wine" => ""}})
186169

187-
File.rm(@config_ini)
170+
assert new_content == config_content
188171
end
189172
end
+98
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
defmodule HeroixWeb.GameViewTest do
2+
use HeroixWeb.ConnCase
3+
4+
import Phoenix.ConnTest
5+
import Phoenix.LiveViewTest
6+
alias Heroix.Settings
7+
@endpoint HeroixWeb.Endpoint
8+
9+
setup do
10+
Heroix.GameInstaller.reset()
11+
Heroix.GameUninstaller.reset()
12+
13+
%{}
14+
end
15+
16+
describe "install button" do
17+
test "starts installing a game", %{conn: conn} do
18+
{:ok, view, _html} = live(conn, "/library/0afb9d54dd3743a582b48b506466d3f8")
19+
20+
view |> element("button", "Add Game") |> render_click()
21+
view |> element("button", "Install") |> render_click()
22+
23+
assert Heroix.GameInstaller.installing() == "0afb9d54dd3743a582b48b506466d3f8"
24+
end
25+
26+
test "adds a game to the install queue", %{conn: conn} do
27+
Heroix.GameInstaller.install_game("0a697c1235fb4706a635cfa33f0306ec", %{})
28+
29+
assert Heroix.GameInstaller.installing() == "0a697c1235fb4706a635cfa33f0306ec"
30+
31+
{:ok, view, _html} = live(conn, "/library/0afb9d54dd3743a582b48b506466d3f8")
32+
33+
view |> element("button", "Add Game") |> render_click()
34+
view |> element("button", "Install") |> render_click()
35+
36+
assert Heroix.GameInstaller.installing() == "0a697c1235fb4706a635cfa33f0306ec"
37+
38+
assert Heroix.GameInstaller.queue() == [
39+
%{app_name: "0afb9d54dd3743a582b48b506466d3f8", opts: %{"install_path" => nil}}
40+
]
41+
end
42+
end
43+
44+
describe "stop installation button" do
45+
test "cancels the installation", %{conn: conn} do
46+
Heroix.GameInstaller.install_game("0a697c1235fb4706a635cfa33f0306ec", %{})
47+
48+
assert Heroix.GameInstaller.installing() == "0a697c1235fb4706a635cfa33f0306ec"
49+
assert Heroix.GameInstaller.queue() == []
50+
51+
{:ok, view, html} = live(conn, "/library/0a697c1235fb4706a635cfa33f0306ec")
52+
53+
assert html =~ "Starting installation..."
54+
55+
view |> element("button", "Stop Installation") |> render_click()
56+
57+
assert :sys.get_state(GameInstaller).stopping() == "0a697c1235fb4706a635cfa33f0306ec"
58+
end
59+
end
60+
61+
describe "uninstall button" do
62+
test "triggers the game uninstallation", %{conn: conn} do
63+
{:ok, view, _html} = live(conn, "/library/Godwit")
64+
65+
view |> element("button", "Uninstall") |> render_click()
66+
67+
assert :sys.get_state(GameUninstaller).uninstalling == "Godwit"
68+
end
69+
end
70+
71+
describe "settings" do
72+
test "shows games settings", %{conn: conn} do
73+
HeroixWeb.Endpoint.subscribe("settings")
74+
75+
{:ok, view, _html} = live(conn, "/library/Condor")
76+
77+
refute view |> element("#game.show-config") |> has_element?()
78+
79+
view |> element("button", "Config") |> render_click()
80+
81+
assert view |> element("#game.show-config") |> has_element?()
82+
83+
assert Settings.legendary_game_config("Condor")["language"] == nil
84+
85+
view
86+
|> element("#game-config form")
87+
|> render_change(%{"_target" => ["language"], "language" => "en"})
88+
89+
assert Settings.legendary_game_config("Condor")["language"] == "en"
90+
91+
view
92+
|> element("#game-config input[name=\"language\"]")
93+
|> render_blur()
94+
95+
assert_received %Phoenix.Socket.Broadcast{event: "save_legendary_config"}
96+
end
97+
end
98+
end

test/heroix_web/live/library_view_test.exs

-55
Original file line numberDiff line numberDiff line change
@@ -114,59 +114,4 @@ defmodule HeroixWeb.LibraryViewTest do
114114
assert "Sherlock Holmes Crimes and Punishments" == game["app_title"]
115115
end
116116
end
117-
118-
describe "install button" do
119-
test "starts installing a game", %{conn: conn} do
120-
{:ok, view, _html} = live(conn, "/library/0afb9d54dd3743a582b48b506466d3f8")
121-
122-
view |> element("button", "Add Game") |> render_click()
123-
view |> element("button", "Install") |> render_click()
124-
125-
assert Heroix.GameInstaller.installing() == "0afb9d54dd3743a582b48b506466d3f8"
126-
end
127-
128-
test "adds a game to the install queue", %{conn: conn} do
129-
Heroix.GameInstaller.install_game("0a697c1235fb4706a635cfa33f0306ec", %{})
130-
131-
assert Heroix.GameInstaller.installing() == "0a697c1235fb4706a635cfa33f0306ec"
132-
133-
{:ok, view, _html} = live(conn, "/library/0afb9d54dd3743a582b48b506466d3f8")
134-
135-
view |> element("button", "Add Game") |> render_click()
136-
view |> element("button", "Install") |> render_click()
137-
138-
assert Heroix.GameInstaller.installing() == "0a697c1235fb4706a635cfa33f0306ec"
139-
140-
assert Heroix.GameInstaller.queue() == [
141-
%{app_name: "0afb9d54dd3743a582b48b506466d3f8", opts: %{"install_path" => nil}}
142-
]
143-
end
144-
end
145-
146-
describe "stop installation button" do
147-
test "cancels the installation", %{conn: conn} do
148-
Heroix.GameInstaller.install_game("0a697c1235fb4706a635cfa33f0306ec", %{})
149-
150-
assert Heroix.GameInstaller.installing() == "0a697c1235fb4706a635cfa33f0306ec"
151-
assert Heroix.GameInstaller.queue() == []
152-
153-
{:ok, view, html} = live(conn, "/library/0a697c1235fb4706a635cfa33f0306ec")
154-
155-
assert html =~ "Starting installation..."
156-
157-
view |> element("button", "Stop Installation") |> render_click()
158-
159-
assert :sys.get_state(GameInstaller).stopping() == "0a697c1235fb4706a635cfa33f0306ec"
160-
end
161-
end
162-
163-
describe "uninstall button" do
164-
test "triggers the game uninstallation", %{conn: conn} do
165-
{:ok, view, _html} = live(conn, "/library/Godwit")
166-
167-
view |> element("button", "Uninstall") |> render_click()
168-
169-
assert :sys.get_state(GameUninstaller).uninstalling == "Godwit"
170-
end
171-
end
172117
end

0 commit comments

Comments
 (0)