aboutsummaryrefslogtreecommitdiff
path: root/spec/tools_spec.lua
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--spec/tools_spec.lua88
1 files changed, 88 insertions, 0 deletions
diff --git a/spec/tools_spec.lua b/spec/tools_spec.lua
new file mode 100644
index 00000000..7717be08
--- /dev/null
+++ b/spec/tools_spec.lua
@@ -0,0 +1,88 @@
1local test_env = require("spec.util.test_env")
2local testing_paths = test_env.testing_paths
3local get_tmp_path = test_env.get_tmp_path
4local write_file = test_env.write_file
5
6test_env.unload_luarocks()
7local fs = require("luarocks.fs")
8local patch = package.loaded["luarocks.tools.patch"]
9
10describe("Luarocks patch test #unit", function()
11 local runner
12
13 setup(function()
14 runner = require("luacov.runner")
15 runner.init(testing_paths.testrun_dir .. "/luacov.config")
16 runner.tick = true
17 end)
18
19 teardown(function()
20 runner.shutdown()
21 end)
22
23 describe("patch.read_patch", function()
24 it("returns a table with the patch file info and the result of parsing the file", function()
25 local t, result
26
27 t, result = patch.read_patch(testing_paths.fixtures_dir .. "/valid_patch.patch")
28 assert.truthy(result)
29
30 t, result = patch.read_patch(testing_paths.fixtures_dir .. "/invalid_patch.patch")
31 assert.falsy(result)
32 end)
33 end)
34
35 describe("patch.apply_patch", function()
36 local tmpdir
37 local olddir
38
39 before_each(function()
40 tmpdir = get_tmp_path()
41 olddir = lfs.currentdir()
42 lfs.mkdir(tmpdir)
43 lfs.chdir(tmpdir)
44
45 local fd = assert(io.open(testing_paths.fixtures_dir .. "/lao"))
46 local laocontent = assert(fd:read("*a"))
47 fd:close()
48 write_file("lao", laocontent, finally)
49
50 fd = assert(io.open(testing_paths.fixtures_dir .. "/tzu"))
51 local tzucontent = assert(fd:read("*a"))
52 fd:close()
53 write_file("tzu", tzucontent, finally)
54 end)
55
56 after_each(function()
57 if olddir then
58 lfs.chdir(olddir)
59 if tmpdir then
60 lfs.rmdir(tmpdir)
61 end
62 end
63 end)
64
65 it("applies the given patch and returns true if the patch is valid", function()
66 local p = patch.read_patch(testing_paths.fixtures_dir .. "/valid_patch.patch")
67 local result = patch.apply_patch(p)
68 assert.truthy(result)
69 end)
70
71 it("returns false if the files to be patched are not valid or doesn't exist", function()
72 os.remove("lao")
73 os.remove("tzu")
74 local p = patch.read_patch(testing_paths.fixtures_dir .. "/invalid_patch.patch")
75 local result = patch.apply_patch(p)
76 assert.falsy(result)
77 end)
78
79 it("returns false if the target file is already patched", function()
80 local p = patch.read_patch(testing_paths.fixtures_dir .. "/valid_patch.patch")
81 local result = patch.apply_patch(p)
82 assert.truthy(result)
83
84 result = patch.apply_patch(p)
85 assert.falsy(result)
86 end)
87 end)
88end)