-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathradpath_test.exs
412 lines (349 loc) · 12 KB
/
radpath_test.exs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
Code.require_file "../test_helper.exs", __ENV__.file
defmodule RadpathTests.RadpathFacts do
# use Amrita.Sweet
use ExUnit.Case, async: true
use PatternTap
import PathHelpers
import Path, only: [basename: 1]
import Enum, only: [map: 2]
alias :file, as: F
defmodule ZippingTest do
use ExUnit.Case
test "zip" do
try do
Path.join(fixture_path(), "Testdir1.zip")
|> File.exists?
|> refute
Radpath.zip([fixture_path()], "Testdir1.zip")
"Testdir1.zip" |> File.exists?
System.cmd("zip",["-T","Testdir1.zip"])
|> (&(assert &1 = {"test of Testdir1.zip OK\n", 0})).()
System.cmd("zipinfo",["-1","Testdir1.zip"])
|> tap({result_str, _} ~> result_str)
|> (&(String.contains?(&1, "testdir1"))).()
after
File.rm_rf("Testdir1.zip")
end
end
test "zip: onepath" do
dir = Path.join(fixture_path(), "testdir2")
try do
Path.join(fixture_path(), "Testdir2.zip") |> File.exists? |> refute
Radpath.zip([dir], "Testdir2.zip")
"Testdir2.zip" |> File.exists?
System.cmd("zip",["-T","Testdir2.zip"])
|> tap({result_str, _} ~> result_str)
|> String.trim |> (&(assert &1 == "test of Testdir2.zip OK")).()
System.cmd("zipinfo",["-1","Testdir2.zip"])
|> tap({result_str2, _} ~> result_str2)
|> (&(String.contains?(&1, "testdir2"))).()
|> assert
after
File.rm_rf("Testdir2.zip")
end
end
end
test "zip: OnebitstringPath" do
dir = Path.join(fixture_path(), "testdir1")
try do
Path.join(fixture_path(), "Testdir3.zip") |> File.exists? |> refute
Radpath.zip(dir, "Testdir3.zip")
"Testdir3.zip" |> File.exists?
System.cmd("zip",["-T","Testdir3.zip"])
|> tap({result_str, _} ~> result_str)
|> String.trim
|> (&(assert &1 == "test of Testdir3.zip OK")).()
System.cmd("zipinfo",["-1","Testdir3.zip"])
|> tap({result_str, _} ~> result_str)
|> (&(String.contains?(&1, "testdir1"))).()
after
File.rm_rf("Testdir3.zip")
end
end
test "unzip: One bitstring path in CWD" do
try do
Path.join(fixture_path(), "dome.csv") |> File.exists? |> refute
Radpath.unzip(Path.join(fixture_path(), "dome.zip"), fixture_path())
Path.join(fixture_path(), "dome.csv") |> File.exists?
after
File.rm_rf(Path.join(fixture_path(), "dome.csv"))
end
end
test "unzip: One bitstring path in tmp" do
try do
Path.join(fixture_path(), "dome.csv") |> File.exists? |> refute
Radpath.unzip(Path.join(fixture_path(), "dome.zip"), "/tmp")
after
Path.join("/tmp", "dome.csv") |> File.exists?
end
end
test "zip: path that does not exist" do
try do
Path.join(fixture_path(), "Testdir-dont-exist.zip")
|> File.exists?
|> refute
Radpath.zip(["/gogo/I/don/exist"], "Testdir-dont-exist.zip")
after
"Testdir-dont-exist.zip" |> File.exists? |> refute
rescue
e in RuntimeError -> e
end
end
defmodule FilteringTest do
use ExUnit.Case
@dud_files ["testfile1.dud, file3.dud"]
@test_files ["testdir3", "testdir2", "testdir1"]
@file_list ["file1.txt", "file2.txt", "file3.log"]
@file_ext ["txt", "log"]
@long_file_list @file_list ++ ["testfile1.txt",
"testfile2.txt",
"testfile3.txt"]
test "File Filtering" do
files = Radpath.files(fixture_path(), "txt") |> Enum.map(&Path.basename(&1))
Enum.map(@dud_files, fn(x) -> refute Enum.member?(files, x) end)
Enum.all?(@dud_files -- ["file3.log"], fn(x) -> files |> Enum.member? x end)
end
test "File Filtering returns empty if path does not exist" do
assert Radpath.files("/this/path/does/not/exist") == []
end
test "Directory filtering" do
dirs = Radpath.dirs(fixture_path()) |> Enum.map(&Path.basename(&1))
map(["testfile1.dud, file3.dud"], fn(x) -> refute Enum.member?(dirs, x) end)
Enum.all?(["testdir3", "testdir2", "testdir1"], fn(x) -> Enum.member?(dirs, x) end)
end
test "Directory Filter: List" do
expected = @test_files ++ ["fixtures"] |> Enum.sort
actual = Radpath.dirs(["test", "lib"]) |> map(&basename(&1))
assert actual == expected ++ ["Radpath"]
end
test "Directory Filtering: Regex" do
actual = Radpath.dirs("test", "fixtures") |> map(&basename(&1))
assert actual == ["fixtures"]
end
test "Filtering Files" do
Radpath.files(fixture_path(), "log")
|> map(&basename(&1))
|> (&(assert &1 == ["file3.log"])).()
end
test "Filtering Files with Lists" do
Radpath.files(["test", "lib"], @file_ext)
|> map(&basename(&1))
|> Enum.sort
|> (&(assert &1 == @long_file_list)).()
end
test "Filtering with Expanded Paths" do
Radpath.files("test/fixtures", "log")
|> map(&basename(&1))
|> (&(assert &1 == ["file3.log"])).()
end
test "Filtering Multiple filter for Files" do
Radpath.files(fixture_path(), @file_ext) |> map(&basename(&1))
|> Enum.sort
|> (&(Enum.all?(@file_list, fn(x) -> Enum.member?(&1, x) end))).()
end
test "Filtering long file list" do
Radpath.files(fixture_path(), @file_ext)
|> Enum.map(&Path.basename(&1))
|> Enum.sort
|> (&(Enum.all?(@file_list, fn(x) -> Enum.member?(&1, x) end))).()
end
test "Filtering Multiple Filter for Files" do
files = Radpath.files(['lib'], @file_ext) |> Enum.map(&Path.basename(&1))
Enum.all?(@file_list, fn(x) -> Enum.member?(files, x) end)
end
end
defmodule Testsymlink do
use ExUnit.Case
import Radpath, only: [symlink: 2, islink?: 1]
@src Path.join(fixture_path(), "testdir3")
@dest Path.join(Path.expand("."), "testdir3")
test "Symlink Normal Usage" do
try do
refute File.exists? @dest
@dest |> File.exists? |> refute
symlink(@src, @dest)
{result, link} = F.read_link(@dest)
assert {result, basename(link)} == {:ok, "testdir3"}
assert islink?(@dest)
after
File.rm_rf @dest
end
end
test "Symlink for Non Existent" do
src = Path.join(fixture_path(), "testdir3xx")
try do
Path.join(Path.expand("."), "testdir3") |> File.exists? |> refute
symlink(src, @dest)
assert {:error, :enoent} == F.read_link(@dest)
Path.join(Path.expand("."), "testdir3") |> File.exists? |> refute
after
File.rm_rf Path.join(Path.expand("."), "testdir3")
end
end
test "Test symlink: islink? Return true if path is symlink" do
test_dest = Path.join(fixture_path(), "test_symlink")
try do
test_dest |> File.exists? |> refute
symlink(fixture_path(), test_dest)
assert islink?(test_dest)
{_, _} = F.read_link(test_dest)
after
File.rm_rf test_dest
end
end
test "Test symlink: islink? Return false if path is not symlink or if does not exist" do
islink?(fixture_path()) |> refute
islink?("/I/wiLL/neveR/exist") |> refute
end
end
defmodule TestTempfilefs do
use ExUnit.Case
test "Test Tempfilefs: Without Argument" do
tmpdirpath1 = Radpath.mktempdir
try do
Radpath.mktempdir |> File.exists?
after
File.rm_rf tmpdirpath1
end
end
test "Test mktempdir: With argument" do
src = Path.join(fixture_path(), "testdir3")
tmpdirpath = Radpath.mktempdir(src)
try do
assert File.exists?(tmpdirpath)
assert File.stat!(tmpdirpath).type == :directory
after
File.rm_rf tmpdirpath
end
end
test "Test mktempdir: Nonexistant parent path, error returned" do
tmpdirpath = Radpath.mktempdir("/gogo/gaga/gigi")
try do
assert tmpdirpath == :enoent
rescue
e in RuntimeError -> e
end
end
test "mktempfile: No argument" do
{_, fd, filepath} = Radpath.mktempfile
IO.write fd, "hulahoop"
try do
assert File.exists? filepath
filepath |> File.read! |> (&(String.contains?(&1, "hulahoop"))).()
after
File.close filepath
File.rm_rf filepath
end
end
test "mktempfile: With arguments" do
{_, fd, filepath} = Radpath.mktempfile(".log", "/tmp")
IO.write fd, "hulahoop with args"
try do
assert Path.extname(filepath) == ".log"
filepath |> File.read! |> (&(String.contains?(&1, "hulahoop with args"))).()
after
File.close filepath
File.rm_rf filepath
end
end
end
defmodule TestRenameandMv do
use ExUnit.Case
@source_file "/tmp/hoho.txt"
@dest_file "/tmp/hehe.txt"
test "Test rename: Normal Usage" do
File.write(@source_file, "test rename")
try do
@source_file |> File.exists?
md5sum_source = Radpath.md5sum(@source_file)
Radpath.rename(@source_file, @dest_file)
@source_file |> File.exists? |> refute
@dest_file |> File.exists?
assert Radpath.md5sum(@dest_file) == md5sum_source
after
File.rm_rf @dest_file
end
end
test "Test mv: Normal Usage" do
File.write("/tmp/hoho.txt", "test mv")
try do
@source_file |> File.exists?
source_md5 = Radpath.md5sum("/tmp/hoho.txt")
Radpath.mv(@source_file, @dest_file)
refute @source_file |> File.exists?
@dest_file |> File.exists?
assert Radpath.md5sum("/tmp/hehe.txt") == source_md5
after
File.rm_rf @dest_file
end
end
test "Test rename: Source file does not exist" do
refute @source_file |> File.exists?
Radpath.rename(@source_file, @dest_file)
refute @source_file |> File.exists?
end
test "Test mv: Source file does not exist" do
refute @source_file |> File.exists?
Radpath.mv(@source_file, @dest_file)
refute @dest_file |> File.exists?
end
test "Test mv: Destination folder does not exist" do
source_file = "mix.exs"
dest_file = "/tmp/xxxyyyzzz/mix.exs"
assert Radpath.mv(source_file, dest_file) == {:error, :enoent}
end
end
defmodule TestRelativePaths do
use ExUnit.Case
import Radpath, only: [relative_path: 2]
test "Test relative_path: Normal Usage" do
assert relative_path("/tmp/base", "/tmp/base/hoho.txt") == "hoho.txt"
end
test "Test relative_path: If same path is given empty string will result" do
assert relative_path("/tmp/base/", "/tmp/base/") == ""
end
end
defmodule TestEnsure do
use ExUnit.Case
test "Test ensure: File and Directory" do
test_files = [Path.join(fixture_path(), "gogo/gaga"), Path.join(fixture_path(), "gogo/gaga.txt")]
try do
Enum.map(test_files, fn(x) -> x |> File.exists? |> refute end)
Enum.map(test_files, fn(x) -> Radpath.ensure(x) end)
Enum.map(test_files, fn(x) -> x |> File.exists? end)
after
Enum.map(test_files, fn(x) -> x |> Radpath.parent_path |> Radpath.erusne end)
end
end
end
defmodule TestOtherfunctions do
use ExUnit.Case
import Radpath, only: [md5sum: 1, sha1sum: 1, parent_path: 1]
test "Test md5sum: md5sum function" do
:os.cmd('md5sum LICENSE')
|> to_string
|> String.split
|> List.first
|> (&(assert &1 == md5sum("LICENSE"))).()
end
test "Test sha1sum: sha1sum function" do
:os.cmd('sha1sum LICENSE')
|> to_string
|> String.split
|> List.first
|> (&(assert &1 == sha1sum("LICENSE"))).()
end
test "Test sha1sum: sha1sum on directory" do
assert sha1sum("/tmp") == :error
end
test "Test md5sum: md5sum on directory" do
assert md5sum("/tmp") == :error
end
test "Test md5sum: md5sum on non existent directory" do
assert md5sum("xxx") == :error
end
test "Test ParentPath: Return parent path of string" do
assert parent_path("/I/am/long/dir") == "/I/am/long/"
end
end
end