1local test_env = require("spec.util.test_env") 2local get_tmp_path = test_env.get_tmp_path 3local testing_paths = test_env.testing_paths 4local write_file = test_env.write_file 5 6test_env.unload_luarocks() 7local fs = require("luarocks.fs") 8local cfg = require("luarocks.core.cfg") 9local patch = require("luarocks.tools.patch") 10 11local lao = 12[[The Nameless is the origin of Heaven and Earth; 13The named is the mother of all things. 14 15Therefore let there always be non-being, 16 so we may see their subtlety, 17And let there always be being, 18 so we may see their outcome. 19The two are the same, 20But after they are produced, 21 they have different names. 22They both may be called deep and profound. 23Deeper and more profound, 24The door of all subtleties!]] 25 26local tzu = 27[[The Way that can be told of is not the eternal Way; 28The name that can be named is not the eternal name. 29The Nameless is the origin of Heaven and Earth; 30The Named is the mother of all things. 31Therefore let there always be non-being, 32 so we may see their subtlety, 33And let there always be being, 34 so we may see their outcome. 35The two are the same, 36But after they are produced, 37 they have different names.]] 38 39local valid_patch1 = 40[[--- lao 2002-02-21 23:30:39.942229878 -0800 41+++ tzu 2002-02-21 23:30:50.442260588 -0800 42@@ -1,7 +1,6 @@ 43-The Way that can be told of is not the eternal Way; 44-The name that can be named is not the eternal name. 45 The Nameless is the origin of Heaven and Earth; 46-The Named is the mother of all things. 47+The named is the mother of all things. 48+ 49 Therefore let there always be non-being, 50 so we may see their subtlety, 51 And let there always be being, 52@@ -9,3 +8,6 @@ 53 The two are the same, 54 But after they are produced, 55 they have different names. 56+They both may be called deep and profound. 57+Deeper and more profound, 58+The door of all subtleties!]] 59 60local valid_patch2 = 61[[--- /dev/null 1969-02-21 23:30:39.942229878 -0800 62+++ tzu 2002-02-21 23:30:50.442260588 -0800 63@@ -1,7 +1,6 @@ 64-The Way that can be told of is not the eternal Way; 65-The name that can be named is not the eternal name. 66 The Nameless is the origin of Heaven and Earth; 67-The Named is the mother of all things. 68+The named is the mother of all things. 69+ 70 Therefore let there always be non-being, 71 so we may see their subtlety, 72 And let there always be being, 73@@ -9,3 +8,6 @@ 74 The two are the same, 75 But after they are produced, 76 they have different names. 77+They both may be called deep and profound. 78+Deeper and more profound, 79+The door of all subtleties!]] 80 81local invalid_patch1 = 82[[--- lao 2002-02-21 23:30:39.942229878 -0800 83+++ tzu 2002-02-21 23:30:50.442260588 -0800 84@@ -1,7 +1,6 @@ 85-The Way that can be told of is not the eternal Way; 86-The name that can be named is not the eternal name. 87 The Nameless is the origin of Heaven and Earth; 88-The Named is the mother of all things. 89--- Extra 90+The named is the mother of all things. 91+ 92 Therefore let there always be non-being, 93 so we may see their subtlety, 94 And let there always be being, 95--- Extra 96@@ -9,3 +8,7 @@ 97 The two are the same, 98 But after they are produced, 99 they have different names. 100+They both may be called deep and profound. 101+Deeper and more profound, 102+The door of all subtleties!]] 103 104local invalid_patch2 = 105[[--- lao 2002-02-21 23:30:39.942229878 -0800 106+++ tzu 2002-02-21 23:30:50.442260588 -0800 107@@ -1,7 +1,6 @@ 108-The Way that can be told of is not the eternal Way; 109-The name that can be named is not the eternal name. 110 The Nameless is the origin of Heaven and Earth; 111-The Named is the mother of all things. 112+The named is the mother of all things. 113+ 114 Therefore let there always be non-being, 115 so we may see their subtlety, 116 And let there always be being, 117@@ -9,3 +8,6 @@ 118 The two are the same, 119 But after they are produced, 120 they have different names. 121+They both may be called deep and profound. 122+Deeper and more profound, 123? ... 124+The door of all subtleties!]] 125 126local invalid_patch3 = 127[[--- lao 2002-02-21 23:30:39.942229878 -0800 128+++ tzu 2002-02-21 23:30:50.442260588 -0800 129@@ -1,7 +1,6 @@ 130-The Way that can be told of is not the eternal Way; 131-The name that can be named is not the eternal name. 132 The Nameless is the origin of Heaven and Earth; 133-The Named is the mother of all things. 134+The named is the mother of all things. 135+ 136 Therefore let there always be non-being, 137 so we may see their subtlety, 138 And let there always be being, 139@@ -9,3 +8,6 @@ 140 The two are the same, 141 But after they are produced, 142 they have different names. 143+They both may be called deep and profound. 144+Deeper and more profound, 145? ... 146+The door of all subtleties!]] 147 148describe("Luarocks patch test #unit", function() 149 local runner 150 151 setup(function() 152 cfg.init() 153 fs.init() 154 runner = require("luacov.runner") 155 runner.init(testing_paths.testrun_dir .. "/luacov.config") 156 runner.tick = true 157 end) 158 159 teardown(function() 160 runner.shutdown() 161 end) 162 163 describe("patch.read_patch", function() 164 it("returns a table with the patch file info and the result of parsing the file", function() 165 local t, result 166 167 write_file("test.patch", valid_patch1, finally) 168 t, result = patch.read_patch("test.patch") 169 assert.truthy(result) 170 assert.truthy(t) 171 172 write_file("test.patch", invalid_patch1, finally) 173 t, result = patch.read_patch("test.patch") 174 assert.falsy(result) 175 assert.truthy(t) 176 177 write_file("test.patch", invalid_patch2, finally) 178 t, result = patch.read_patch("test.patch") 179 assert.falsy(result) 180 assert.truthy(t) 181 182 write_file("test.patch", invalid_patch3, finally) 183 t, result = patch.read_patch("test.patch") 184 assert.falsy(result) 185 assert.truthy(t) 186 end) 187 end) 188 189 describe("patch.apply_patch", function() 190 local tmpdir 191 local olddir 192 193 before_each(function() 194 tmpdir = get_tmp_path() 195 olddir = lfs.currentdir() 196 lfs.mkdir(tmpdir) 197 lfs.chdir(tmpdir) 198 199 write_file("lao", tzu, finally) 200 write_file("tzu", lao, finally) 201 end) 202 203 after_each(function() 204 if olddir then 205 lfs.chdir(olddir) 206 if tmpdir then 207 lfs.rmdir(tmpdir) 208 end 209 end 210 end) 211 212 it("applies the given patch and returns the result of patching", function() 213 write_file("test.patch", valid_patch1, finally) 214 local p = patch.read_patch("test.patch") 215 local result = patch.apply_patch(p) 216 assert.truthy(result) 217 end) 218 219 it("applies the given patch with custom arguments and returns the result of patching", function() 220 write_file("test.patch", valid_patch2, finally) 221 local p = patch.read_patch("test.patch") 222 local result = patch.apply_patch(p, nil, true) 223 assert.truthy(result) 224 end) 225 226 it("fails if the patch file is invalid", function() 227 write_file("test.patch", invalid_patch1, finally) 228 local p = patch.read_patch("test.patch") 229 local result = pcall(patch.apply_patch, p) 230 assert.falsy(result) 231 end) 232 233 it("returns false if the files from the patch doesn't exist", function() 234 os.remove("lao") 235 os.remove("tzu") 236 237 write_file("test.patch", valid_patch1, finally) 238 local p = patch.read_patch("test.patch") 239 local result = patch.apply_patch(p) 240 assert.falsy(result) 241 end) 242 243 it("returns false if the target file was already patched", function() 244 write_file("test.patch", valid_patch1, finally) 245 local p = patch.read_patch("test.patch") 246 local result = patch.apply_patch(p) 247 assert.truthy(result) 248 249 result = patch.apply_patch(p) 250 assert.falsy(result) 251 end) 252 end) 253end) 254