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
|
local test_env = require("spec.util.test_env")
local testing_paths = test_env.testing_paths
local get_tmp_path = test_env.get_tmp_path
local write_file = test_env.write_file
test_env.unload_luarocks()
local fs = require("luarocks.fs")
local patch = package.loaded["luarocks.tools.patch"]
describe("Luarocks patch test #unit", function()
local runner
setup(function()
runner = require("luacov.runner")
runner.init(testing_paths.testrun_dir .. "/luacov.config")
runner.tick = true
end)
teardown(function()
runner.shutdown()
end)
describe("patch.read_patch", function()
it("returns a table with the patch file info and the result of parsing the file", function()
local t, result
t, result = patch.read_patch(testing_paths.fixtures_dir .. "/valid_patch.patch")
assert.truthy(result)
t, result = patch.read_patch(testing_paths.fixtures_dir .. "/invalid_patch.patch")
assert.falsy(result)
end)
end)
describe("patch.apply_patch", function()
local tmpdir
local olddir
before_each(function()
tmpdir = get_tmp_path()
olddir = lfs.currentdir()
lfs.mkdir(tmpdir)
lfs.chdir(tmpdir)
local fd = assert(io.open(testing_paths.fixtures_dir .. "/lao"))
local laocontent = assert(fd:read("*a"))
fd:close()
write_file("lao", laocontent, finally)
fd = assert(io.open(testing_paths.fixtures_dir .. "/tzu"))
local tzucontent = assert(fd:read("*a"))
fd:close()
write_file("tzu", tzucontent, finally)
end)
after_each(function()
if olddir then
lfs.chdir(olddir)
if tmpdir then
lfs.rmdir(tmpdir)
end
end
end)
it("applies the given patch and returns true if the patch is valid", function()
local p = patch.read_patch(testing_paths.fixtures_dir .. "/valid_patch.patch")
local result = patch.apply_patch(p)
assert.truthy(result)
end)
it("returns false if the files to be patched are not valid or doesn't exist", function()
os.remove("lao")
os.remove("tzu")
local p = patch.read_patch(testing_paths.fixtures_dir .. "/invalid_patch.patch")
local result = patch.apply_patch(p)
assert.falsy(result)
end)
it("returns false if the target file is already patched", function()
local p = patch.read_patch(testing_paths.fixtures_dir .. "/valid_patch.patch")
local result = patch.apply_patch(p)
assert.truthy(result)
result = patch.apply_patch(p)
assert.falsy(result)
end)
end)
end)
|