Skip to content

Commit c689833

Browse files
committed
refactor specs
1 parent abf32e3 commit c689833

7 files changed

+48
-33
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/.idea
12
/_build
23
/cover
34
/deps

lib/test_helpers.ex

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
defmodule Loki.TestHelpers do
2+
import ExUnit.CaptureIO
3+
import Loki.Directory
4+
import Loki.File
5+
6+
@tests_directory "temp"
7+
8+
def prepare_tests() do
9+
if exists_directory?(@tests_directory) do
10+
capture_io(fn -> remove_directory(@tests_directory) end)
11+
end
12+
13+
create_directory_silently(@tests_directory)
14+
end
15+
16+
def create_directory_silently(dir_name) do
17+
capture_io(fn -> create_directory("#{@tests_directory}/#{dir_name}") end)
18+
end
19+
20+
def create_file_silently(file_name) do
21+
capture_io(fn -> create_file("#{@tests_directory}/#{file_name}") end)
22+
end
23+
24+
def create_file_silently(file_name, content) do
25+
capture_io(fn -> create_file("#{@tests_directory}/#{file_name}", content) end)
26+
end
27+
end

test/loki/cmd_test.exs

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ defmodule Loki.CmdTest do
2020

2121
test "#format_output" do
2222
{test_user, _} = System.cmd("whoami", [])
23+
2324
assert capture_io(fn ->
2425
format_output(System.cmd("whoami", []))
2526
end) == "\n\e[33m#{test_user}\e[0m\n"

test/loki/directory_test.exs

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ defmodule Loki.DirectoryTest do
22
use ExUnit.Case, async: true
33

44
import Loki.Directory
5+
import Loki.TestHelpers
56
import ExUnit.CaptureIO
67

78

@@ -17,15 +18,15 @@ defmodule Loki.DirectoryTest do
1718
end
1819

1920
test "#copy_directory" do
20-
capture_io(fn -> create_directory("temp/copy_dir") end)
21+
create_directory_silently("copy_dir")
2122

2223
assert capture_io(fn ->
2324
copy_directory("temp/copy_dir", "temp/copied_dir")
2425
end) == "\e[32m * copy \e[0mtemp/copy_dir\e[32m to \e[0mtemp/copied_dir\e[0m\n"
2526
end
2627

2728
test "#remove_directory" do
28-
capture_io(fn -> create_directory("temp/remove") end)
29+
create_directory_silently("remove")
2930

3031
assert capture_io(fn ->
3132
remove_directory("temp/remove")

test/loki/file_manipulation_test.exs

+9-15
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,70 @@
11
defmodule Loki.FileManipulationTest do
22
use ExUnit.Case, async: true
33

4-
import Loki.File
5-
import Loki.Directory
64
import Loki.FileManipulation
5+
import Loki.TestHelpers
76
import ExUnit.CaptureIO
87

98

10-
setup_all do
11-
create_directory("temp")
12-
:ok
13-
end
14-
159
describe "FileManipulation" do
1610
test "#append_to_file" do
17-
capture_io(fn -> create_file("temp/append") end)
11+
create_file_silently("append")
1812

1913
assert capture_io(fn ->
2014
append_to_file("temp/append", "appended")
2115
end) == "\e[32m * append \e[0mtemp/append\e[0m\n"
2216
end
2317

2418
test "#prepend_to_file" do
25-
capture_io(fn -> create_file("temp/prepend") end)
19+
create_file_silently("prepend")
2620

2721
assert capture_io(fn ->
2822
prepend_to_file("temp/prepend", "prepended")
2923
end) == "\e[32m * prepend \e[0mtemp/prepend\e[0m\n"
3024
end
3125

3226
test "#remove_from_file" do
33-
capture_io(fn -> create_file("temp/remove", "remove") end)
27+
create_file_silently("remove", "remove")
3428

3529
assert capture_io(fn ->
3630
remove_from_file("temp/remove", "remove")
3731
end) == "\e[32m * remove \e[0mtemp/remove\e[0m\n"
3832
end
3933

4034
test "#inject_into_file" do
41-
capture_io(fn -> create_file("temp/inject", "line") end)
35+
create_file_silently("inject", "line")
4236

4337
assert capture_io(fn ->
4438
inject_into_file("temp/inject", "injected", :after, "line")
4539
end) == "\e[32m * inject \e[0mtemp/inject\e[0m\n"
4640
end
4741

4842
test "#replace_in_file" do
49-
capture_io(fn -> create_file("temp/replace", "replace") end)
43+
create_file_silently("replace", "replace")
5044

5145
assert capture_io(fn ->
5246
replace_in_file("temp/replace", "replaced", "replace")
5347
end) == "\e[32m * replace \e[0mtemp/replace\e[0m\n"
5448
end
5549

5650
test "#comment_in_file" do
57-
capture_io(fn -> create_file("temp/comment", "comment") end)
51+
create_file_silently("comment", "comment")
5852

5953
assert capture_io(fn ->
6054
comment_in_file("temp/comment", "comment")
6155
end) == "\e[32m * comment \e[0mtemp/comment\e[0m\n"
6256
end
6357

6458
test "#uncomment_in_file" do
65-
capture_io(fn -> create_file("temp/uncomment", "# uncomment") end)
59+
create_file_silently("uncomment", "# uncomment")
6660

6761
assert capture_io(fn ->
6862
comment_in_file("temp/uncomment", "# uncomment")
6963
end) == "\e[32m * comment \e[0mtemp/uncomment\e[0m\n"
7064
end
7165

7266
test "#remove_comments_in_file" do
73-
capture_io(fn -> create_file("temp/remove_all_comments", "# comment\n # comment") end)
67+
create_file_silently("remove_all_comments", "# comment\n # comment")
7468

7569
assert capture_io(fn ->
7670
remove_comments_in_file("temp/remove_all_comments")

test/loki/file_test.exs

+6-10
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,9 @@ defmodule Loki.FileTest do
22
use ExUnit.Case, async: true
33

44
import Loki.File
5-
import Loki.Directory
5+
import Loki.TestHelpers
66
import ExUnit.CaptureIO
77

8-
setup_all do
9-
create_directory("temp")
10-
:ok
11-
end
128

139
describe "File" do
1410
test "#create_file" do
@@ -18,39 +14,39 @@ defmodule Loki.FileTest do
1814
end
1915

2016
test "#create_file_force" do
21-
capture_io(fn -> create_file("temp/force") end)
17+
create_file_silently("force")
2218

2319
assert capture_io(fn ->
2420
create_file_force("temp/force")
2521
end) == "\e[33m * force \e[0mtemp/force\e[0m\n"
2622
end
2723

2824
test "#copy_file" do
29-
capture_io(fn -> create_file("temp/copy") end)
25+
create_file_silently("copy")
3026

3127
assert capture_io(fn ->
3228
copy_file("temp/copy", "temp/copied")
3329
end) == "\e[32m * copy \e[0mtemp/copy\e[32m to \e[0mtemp/copied\e[0m\n"
3430
end
3531

3632
test "#create_link" do
37-
capture_io(fn -> create_file("temp/link") end)
33+
create_file_silently("link")
3834

3935
assert capture_io(fn ->
4036
create_link("temp/link", "temp/linked")
4137
end) == "\e[32m * link \e[0mtemp/link\e[32m to \e[0mtemp/linked\e[0m\n"
4238
end
4339

4440
test "#remove_file" do
45-
capture_io(fn -> create_file("temp/remove") end)
41+
create_file_silently("remove")
4642

4743
assert capture_io(fn ->
4844
remove_file("temp/remove")
4945
end) == "\e[32m * remove \e[0mtemp/remove\e[0m\n"
5046
end
5147

5248
test "#rename_file" do
53-
capture_io(fn -> create_file("temp/rename") end)
49+
create_file_silently("rename")
5450

5551
assert capture_io(fn ->
5652
rename("temp/rename", "temp/renamed")

test/test_helper.exs

+1-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,2 @@
1-
import Loki.Directory
2-
3-
temp = "temp"
4-
5-
if exists_directory?(temp), do: remove_directory(temp)
6-
1+
Loki.TestHelpers.prepare_tests()
72
ExUnit.start()

0 commit comments

Comments
 (0)