From c01c8a21a735c3a74270635e5bd843bac71bc624 Mon Sep 17 00:00:00 2001 From: George Roman Date: Wed, 13 Jun 2018 20:09:07 +0300 Subject: Tests: patch.lua --- spec/fixtures/invalid_patch.patch | 20 +++++++++ spec/fixtures/lao | 11 +++++ spec/fixtures/tzu | 13 ++++++ spec/fixtures/valid_patch.patch | 19 +++++++++ spec/tools_spec.lua | 88 +++++++++++++++++++++++++++++++++++++++ src/luarocks/tools/patch.lua | 41 +----------------- 6 files changed, 152 insertions(+), 40 deletions(-) create mode 100644 spec/fixtures/invalid_patch.patch create mode 100644 spec/fixtures/lao create mode 100644 spec/fixtures/tzu create mode 100644 spec/fixtures/valid_patch.patch create mode 100644 spec/tools_spec.lua diff --git a/spec/fixtures/invalid_patch.patch b/spec/fixtures/invalid_patch.patch new file mode 100644 index 00000000..75d05c58 --- /dev/null +++ b/spec/fixtures/invalid_patch.patch @@ -0,0 +1,20 @@ +--- lao 2002-02-21 23:30:39.942229878 -0800 ++++ tzu 2002-02-21 23:30:50.442260588 -0800 +@@ -1,7 +1,6 @@ +-The Way that can be told of is not the eternal Way; +-The name that can be named is not the eternal name. + The Nameless is the origin of Heaven and Earth; +-The Named is the mother of all things. ++The named is the mother of all things. ++ +? + Therefore let there always be non-being, + so we may see their subtlety, + And let there always be being, +@@ -9,3 +8,7 @@ + The two are the same, + But after they are produced, + they have different names. ++They both may be called deep and profound. ++Deeper and more profound, ++The door of all subtleties! diff --git a/spec/fixtures/lao b/spec/fixtures/lao new file mode 100644 index 00000000..635ef2c4 --- /dev/null +++ b/spec/fixtures/lao @@ -0,0 +1,11 @@ +The Way that can be told of is not the eternal Way; +The name that can be named is not the eternal name. +The Nameless is the origin of Heaven and Earth; +The Named is the mother of all things. +Therefore let there always be non-being, + so we may see their subtlety, +And let there always be being, + so we may see their outcome. +The two are the same, +But after they are produced, + they have different names. diff --git a/spec/fixtures/tzu b/spec/fixtures/tzu new file mode 100644 index 00000000..5af88a8f --- /dev/null +++ b/spec/fixtures/tzu @@ -0,0 +1,13 @@ +The Nameless is the origin of Heaven and Earth; +The named is the mother of all things. + +Therefore let there always be non-being, + so we may see their subtlety, +And let there always be being, + so we may see their outcome. +The two are the same, +But after they are produced, + they have different names. +They both may be called deep and profound. +Deeper and more profound, +The door of all subtleties! diff --git a/spec/fixtures/valid_patch.patch b/spec/fixtures/valid_patch.patch new file mode 100644 index 00000000..042c7d22 --- /dev/null +++ b/spec/fixtures/valid_patch.patch @@ -0,0 +1,19 @@ +--- lao 2002-02-21 23:30:39.942229878 -0800 ++++ tzu 2002-02-21 23:30:50.442260588 -0800 +@@ -1,7 +1,6 @@ +-The Way that can be told of is not the eternal Way; +-The name that can be named is not the eternal name. + The Nameless is the origin of Heaven and Earth; +-The Named is the mother of all things. ++The named is the mother of all things. ++ + Therefore let there always be non-being, + so we may see their subtlety, + And let there always be being, +@@ -9,3 +8,6 @@ + The two are the same, + But after they are produced, + they have different names. ++They both may be called deep and profound. ++Deeper and more profound, ++The door of all subtleties! 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 @@ +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) diff --git a/src/luarocks/tools/patch.lua b/src/luarocks/tools/patch.lua index 2e95e879..b46bd1d0 100644 --- a/src/luarocks/tools/patch.lua +++ b/src/luarocks/tools/patch.lua @@ -55,35 +55,6 @@ local function exists(filename) end local function isfile() return true end --FIX? -local function read_file(filename) - local fh, data, err, oserr - fh, err, oserr = io.open(filename, 'rb') - if not fh then return fh, err, oserr end - data, err, oserr = fh:read'*a' - fh:close() - if not data then return nil, err, oserr end - return data -end - -local function write_file(filename, data) - local fh, status, err, oserr - fh, err, oserr = io.open(filename 'wb') - if not fh then return fh, err, oserr end - status, err, oserr = fh:write(data) - fh:close() - if not status then return nil, err, oserr end - return true -end - -local function file_copy(src, dest) - local data, status, err, oserr - data, err, oserr = read_file(src) - if not data then return data, err, oserr end - status, err, oserr = write_file(dest) - if not status then return status, err, oserr end - return true -end - local function string_as_file(s) return { at = 0, @@ -720,17 +691,7 @@ local function patch_file(source, target, epoch, hunks, strip, create_delete) warning(format("failed backing up %s when patching", source)) return false end - ok = patch_hunks(backupname, source, hunks) - if not ok then - warning(format("error patching file %s", source)) - if file_copy(source, source .. ".invalid") then - warning(format("invalid version is saved to %s", - source .. ".invalid")) - -- todo: proper rejects - os.rename(backupname, source) - end - return false - end + patch_hunks(backupname, source, hunks) info(format("successfully patched %s", source)) os.remove(backupname) return true -- cgit v1.2.3-55-g6feb